mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-02-18 14:47:35 +00:00
refactor(useFileMentions): implement abort controller for fetch requests
This commit is contained in:
@@ -56,15 +56,19 @@ export function useFileMentions({ selectedProject, input, setInput, textareaRef
|
|||||||
const [atSymbolPosition, setAtSymbolPosition] = useState(-1);
|
const [atSymbolPosition, setAtSymbolPosition] = useState(-1);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const abortController = new AbortController();
|
||||||
|
|
||||||
const fetchProjectFiles = async () => {
|
const fetchProjectFiles = async () => {
|
||||||
if (!selectedProject) {
|
const projectName = selectedProject?.name;
|
||||||
setFileList([]);
|
setFileList([]);
|
||||||
setFilteredFiles([]);
|
setFilteredFiles([]);
|
||||||
|
if (!projectName) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await api.getFiles(selectedProject.name);
|
const response = await api.getFiles(projectName, { signal: abortController.signal });
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -72,12 +76,19 @@ export function useFileMentions({ selectedProject, input, setInput, textareaRef
|
|||||||
const files = (await response.json()) as ProjectFileNode[];
|
const files = (await response.json()) as ProjectFileNode[];
|
||||||
setFileList(flattenFileTree(files));
|
setFileList(flattenFileTree(files));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
// Ignore aborts from rapid project switches; we only care about the latest request.
|
||||||
|
if ((error as { name?: string })?.name === 'AbortError') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
console.error('Error fetching files:', error);
|
console.error('Error fetching files:', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchProjectFiles();
|
fetchProjectFiles();
|
||||||
}, [selectedProject]);
|
return () => {
|
||||||
|
abortController.abort();
|
||||||
|
};
|
||||||
|
}, [selectedProject?.name]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const textBeforeCursor = input.slice(0, cursorPosition);
|
const textBeforeCursor = input.slice(0, cursorPosition);
|
||||||
|
|||||||
@@ -101,8 +101,8 @@ export const api = {
|
|||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: JSON.stringify({ filePath, content }),
|
body: JSON.stringify({ filePath, content }),
|
||||||
}),
|
}),
|
||||||
getFiles: (projectName) =>
|
getFiles: (projectName, options = {}) =>
|
||||||
authenticatedFetch(`/api/projects/${projectName}/files`),
|
authenticatedFetch(`/api/projects/${projectName}/files`, options),
|
||||||
transcribe: (formData) =>
|
transcribe: (formData) =>
|
||||||
authenticatedFetch('/api/transcribe', {
|
authenticatedFetch('/api/transcribe', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|||||||
Reference in New Issue
Block a user