fix(files): ignore stale text loads (#1169)
* fix(files): ignore stale text loads * fix(files): clear loading on directory read error --------- Co-authored-by: Isaac Sanchez <isanchez-hawkins@arize.com>
This commit is contained in:
committed by
GitHub
co-authored by
Isaac Sanchez
parent
d7f8045e82
commit
26caa33319
@@ -665,6 +665,7 @@ export const FilesView: React.FC<FilesViewProps> = ({ mode = 'full' }) => {
|
|||||||
const [isSaving, setIsSaving] = React.useState(false);
|
const [isSaving, setIsSaving] = React.useState(false);
|
||||||
const autoSaveTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
|
const autoSaveTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
const lastLoadedFileStatRef = React.useRef<FileStatSnapshot | null>(null);
|
const lastLoadedFileStatRef = React.useRef<FileStatSnapshot | null>(null);
|
||||||
|
const activeFileLoadIdRef = React.useRef(0);
|
||||||
const [autoSaveStatus, setAutoSaveStatus] = React.useState<'idle' | 'saved'>('idle');
|
const [autoSaveStatus, setAutoSaveStatus] = React.useState<'idle' | 'saved'>('idle');
|
||||||
const [autoSaveEnabled, setAutoSaveEnabled] = React.useState(getInitialAutoSaveEnabled);
|
const [autoSaveEnabled, setAutoSaveEnabled] = React.useState(getInitialAutoSaveEnabled);
|
||||||
|
|
||||||
@@ -1452,6 +1453,15 @@ export const FilesView: React.FC<FilesViewProps> = ({ mode = 'full' }) => {
|
|||||||
}, [isSaving, saveDraft]);
|
}, [isSaving, saveDraft]);
|
||||||
|
|
||||||
const loadSelectedFile = React.useCallback(async (node: FileNode) => {
|
const loadSelectedFile = React.useCallback(async (node: FileNode) => {
|
||||||
|
const loadId = activeFileLoadIdRef.current + 1;
|
||||||
|
activeFileLoadIdRef.current = loadId;
|
||||||
|
const isCurrentLoad = () => {
|
||||||
|
if (!root) return false;
|
||||||
|
const rootState = useFilesViewTabsStore.getState().byRoot[root];
|
||||||
|
const currentPath = rootState?.selectedPath ?? rootState?.openPaths[0] ?? null;
|
||||||
|
return activeFileLoadIdRef.current === loadId && currentPath === node.path;
|
||||||
|
};
|
||||||
|
|
||||||
setFileError(null);
|
setFileError(null);
|
||||||
setDesktopImageSrc('');
|
setDesktopImageSrc('');
|
||||||
setLoadedFilePath(null);
|
setLoadedFilePath(null);
|
||||||
@@ -1486,6 +1496,9 @@ export const FilesView: React.FC<FilesViewProps> = ({ mode = 'full' }) => {
|
|||||||
|
|
||||||
await readFile(node.path, readOptions)
|
await readFile(node.path, readOptions)
|
||||||
.then((content) => {
|
.then((content) => {
|
||||||
|
if (!isCurrentLoad()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
setFileContent(content);
|
setFileContent(content);
|
||||||
setDraftContent(content.length > MAX_VIEW_CHARS
|
setDraftContent(content.length > MAX_VIEW_CHARS
|
||||||
? `${content.slice(0, MAX_VIEW_CHARS)}\n\n… truncated …`
|
? `${content.slice(0, MAX_VIEW_CHARS)}\n\n… truncated …`
|
||||||
@@ -1493,14 +1506,18 @@ export const FilesView: React.FC<FilesViewProps> = ({ mode = 'full' }) => {
|
|||||||
setLoadedFilePath(node.path);
|
setLoadedFilePath(node.path);
|
||||||
void readFileStat(node.path, readOptions)
|
void readFileStat(node.path, readOptions)
|
||||||
.then((stat) => {
|
.then((stat) => {
|
||||||
if (stat) {
|
if (stat && isCurrentLoad()) {
|
||||||
lastLoadedFileStatRef.current = stat;
|
lastLoadedFileStatRef.current = stat;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {});
|
.catch(() => {});
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
if (!isCurrentLoad()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (isDirectoryReadError(error)) {
|
if (isDirectoryReadError(error)) {
|
||||||
|
setFileLoading(false);
|
||||||
if (root) {
|
if (root) {
|
||||||
setSelectedPath(root, null);
|
setSelectedPath(root, null);
|
||||||
}
|
}
|
||||||
@@ -1535,7 +1552,9 @@ export const FilesView: React.FC<FilesViewProps> = ({ mode = 'full' }) => {
|
|||||||
lastLoadedFileStatRef.current = null;
|
lastLoadedFileStatRef.current = null;
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
setFileLoading(false);
|
if (isCurrentLoad()) {
|
||||||
|
setFileLoading(false);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}, [expandPaths, isMobile, loadDirectory, mode, readFile, readFileStat, root, runtime.isDesktop, searchQuery, setSelectedPath, t]);
|
}, [expandPaths, isMobile, loadDirectory, mode, readFile, readFileStat, root, runtime.isDesktop, searchQuery, setSelectedPath, t]);
|
||||||
|
|
||||||
@@ -1601,6 +1620,8 @@ export const FilesView: React.FC<FilesViewProps> = ({ mode = 'full' }) => {
|
|||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!selectedFile) {
|
if (!selectedFile) {
|
||||||
|
activeFileLoadIdRef.current += 1;
|
||||||
|
setFileLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user