fix: prevent duplicate tabs when clicking file paths

Modify selectFile function to check for existing tabs before creating new ones.
When clicking file paths in AIChat, it will now switch to the existing tab
instead of creating duplicates.
This commit is contained in:
Akhileshrangani4 2025-01-01 08:33:13 -05:00
parent 37ebe41526
commit 0117e197bb

View File

@ -793,31 +793,32 @@ export default function CodeEditor({
setGenerate((prev) => ({ ...prev, show: false })) setGenerate((prev) => ({ ...prev, show: false }))
// Check if the tab already exists in the list of open tabs // Check if the tab already exists in the list of open tabs
const exists = tabs.find((t) => t.id === tab.id) const existingTab = tabs.find((t) => t.id === tab.id)
setTabs((prev) => {
if (exists) {
// If the tab exists, make it the active tab
setActiveFileId(exists.id)
return prev
}
// If the tab doesn't exist, add it to the list of tabs and make it active
return [...prev, tab]
})
// If the file's content is already cached, set it as the active content if (existingTab) {
if (fileContents[tab.id]) { // If the tab exists, just make it active
setActiveFileContent(fileContents[tab.id]) setActiveFileId(existingTab.id)
if (fileContents[existingTab.id]) {
setActiveFileContent(fileContents[existingTab.id])
}
} else { } else {
// Otherwise, fetch the content of the file and cache it // If the tab doesn't exist, add it to the list and make it active
setTabs((prev) => [...prev, tab])
// Fetch content if not cached
if (!fileContents[tab.id]) {
debouncedGetFile(tab.id, (response: string) => { debouncedGetFile(tab.id, (response: string) => {
setFileContents((prev) => ({ ...prev, [tab.id]: response })) setFileContents((prev) => ({ ...prev, [tab.id]: response }))
setActiveFileContent(response) setActiveFileContent(response)
}) })
} else {
setActiveFileContent(fileContents[tab.id])
}
} }
// Set the editor language based on the file type // Set the editor language based on the file type
setEditorLanguage(processFileType(tab.name)) setEditorLanguage(processFileType(tab.name))
// Set the active file ID to the new tab // Set the active file ID
setActiveFileId(tab.id) setActiveFileId(tab.id)
} }