diff --git a/src/workspaces/officerdev/src/apps/FileBrowser/FileBrowserApp/useFileBrowserApp.ts b/src/workspaces/officerdev/src/apps/FileBrowser/FileBrowserApp/useFileBrowserApp.ts index 71b5f192..2b0360ab 100644 --- a/src/workspaces/officerdev/src/apps/FileBrowser/FileBrowserApp/useFileBrowserApp.ts +++ b/src/workspaces/officerdev/src/apps/FileBrowser/FileBrowserApp/useFileBrowserApp.ts @@ -342,15 +342,31 @@ export const useFileBrowserApp = ( `visibilitychange` as well as `focus`, because a background tab brought forward by switching tabs fires only the second one in some browsers. */ + /* + ...and the FOLDER as well as the clipboard. + + Cutting in one window and pasting in another moves the file on disk, and the window it left went on + listing it until somebody pressed refresh. The clipboard was the obvious half of "these two windows + disagree"; the listing is the half that matters more, because a file that is not there any more is + a lie rather than a stale label. + + Held in a ref because `refreshRef.current` is rebuilt on every render — putting it in the + dependency array would tear down and re-add both listeners several times a second. + */ + const refreshRef = useRef(refresh); + refreshRef.current = refresh; + useEffect(() => { - const refresh = () => { - if (document.visibilityState === 'visible') void queryClient.invalidateQueries({ queryKey: ['USER_STATE'] }); + const onForeground = () => { + if (document.visibilityState !== 'visible') return; + void queryClient.invalidateQueries({ queryKey: ['USER_STATE'] }); + void refreshRef.current(); }; - window.addEventListener('focus', refresh); - document.addEventListener('visibilitychange', refresh); + window.addEventListener('focus', onForeground); + document.addEventListener('visibilitychange', onForeground); return () => { - window.removeEventListener('focus', refresh); - document.removeEventListener('visibilitychange', refresh); + window.removeEventListener('focus', onForeground); + document.removeEventListener('visibilitychange', onForeground); }; }, [queryClient]);