TagSpaces is an offline, open source, document manager with tagging support
Enable update on focus for grid and list (#2341)
Stanimirbe3e60474b20
diff --git a/src/renderer/perspectives/common/useReloadOnFocus.ts b/src/renderer/perspectives/common/useReloadOnFocus.tsnew file mode 100644index 000000000..bf37b8f9a--- /dev/null+++ b/src/renderer/perspectives/common/useReloadOnFocus.ts@@ -0,0 +1,48 @@+import { useEffect, useRef } from 'react';++/**+ * Custom hook to reload data when window/tab gains focus or becomes visible.+ * @param {boolean} reloadOnFocus - Flag to enable/disable reload behavior.+ * @param {() => void} reloadFn - Callback to invoke when reload should occur.+ */+export function useReloadOnFocus(+ reloadOnFocus: boolean,+ reloadFn: () => void,+): void {+ const lastLoadRef = useRef<number>(Date.now());+ const reloadTimeout = 15000;++ function throttledLoad() {+ const now = Date.now();+ if (now - lastLoadRef.current > reloadTimeout) {+ lastLoadRef.current = now;+ reloadFn();+ } else {+ console.log('Skipping reload; too soon since last one');+ }+ }++ useEffect(() => {+ if (!reloadOnFocus) return;++ const handleVisibilityChange = () => {+ if (document.visibilityState === 'visible') {+ console.log('Window is visible - triggering reload');+ throttledLoad();+ }+ };++ const handleWindowFocus = () => {+ console.log('Window focus - triggering reload');+ throttledLoad();+ };++ document.addEventListener('visibilitychange', handleVisibilityChange);+ window.addEventListener('focus', handleWindowFocus);++ return () => {+ document.removeEventListener('visibilitychange', handleVisibilityChange);+ window.removeEventListener('focus', handleWindowFocus);+ };+ }, [reloadOnFocus, reloadFn]);+}diff --git a/src/renderer/perspectives/grid/components/MainContainer.tsx b/src/renderer/perspectives/grid/components/MainContainer.tsxindex 0424375c9..560980462 100644--- a/src/renderer/perspectives/grid/components/MainContainer.tsx+++ b/src/renderer/perspectives/grid/components/MainContainer.tsx@@ -44,10 +44,11 @@ import { openURLExternally } from '-/services/utils-io';import { TS } from '-/tagspaces.namespace';import useFirstRender from '-/utils/useFirstRender';import Links from 'assets/links';-import React, { useEffect, useRef, useState } from 'react';+import React, { useCallback, useEffect, useRef, useState } from 'react';import { GlobalHotKeys } from 'react-hotkeys';import { useSelector } from 'react-redux';import GridCell from './GridCell';+import { useReloadOnFocus } from '-/perspectives/common/useReloadOnFocus';interface Props {}@@ -60,7 +61,7 @@ function GridPerspective(props: Props) {const { openEntry, openPrevFile, openNextFile } = useOpenedEntryContext();const { actions } = usePerspectiveActionsContext();const { showDirectories } = usePerspectiveSettingsContext();- const { findLocation } = useCurrentLocationContext();+ const { currentLocation } = useCurrentLocationContext();const { openDirectory, currentDirectoryPath } = useDirectoryContentContext();const { openFileNatively, duplicateFile } = useIOActionsContext();const { openDeleteMultipleEntriesDialog } =@@ -97,6 +98,10 @@ function GridPerspective(props: Props) {useState<boolean>(false);const firstRender = useFirstRender();+ useReloadOnFocus(currentLocation.reloadOnFocus, () =>+ openDirectory(currentDirectoryPath),+ );+useEffect(() => {if (!firstRender && actions && actions.length > 0) {for (const action of actions) {diff --git a/src/renderer/perspectives/list/components/MainContainer.tsx b/src/renderer/perspectives/list/components/MainContainer.tsxindex 628959302..1750404bc 100644--- a/src/renderer/perspectives/list/components/MainContainer.tsx+++ b/src/renderer/perspectives/list/components/MainContainer.tsx@@ -44,10 +44,11 @@ import { openURLExternally } from '-/services/utils-io';import { TS } from '-/tagspaces.namespace';import useFirstRender from '-/utils/useFirstRender';import Links from 'assets/links';-import React, { useEffect, useRef, useState } from 'react';+import React, { useCallback, useEffect, useRef, useState } from 'react';import { GlobalHotKeys } from 'react-hotkeys';import { useSelector } from 'react-redux';import { ListCellsStyleContextProvider } from '../hooks/ListCellsStyleProvider';+import { useReloadOnFocus } from '-/perspectives/common/useReloadOnFocus';interface Props {}@@ -60,7 +61,7 @@ function ListPerspective(props: Props) {const { openEntry, openPrevFile, openNextFile } = useOpenedEntryContext();const { actions } = usePerspectiveActionsContext();const { showDirectories } = usePerspectiveSettingsContext();- const { findLocation } = useCurrentLocationContext();+ const { currentLocation } = useCurrentLocationContext();const { openDirectory, currentDirectoryPath } = useDirectoryContentContext();const { openFileNatively, duplicateFile } = useIOActionsContext();const { openDeleteMultipleEntriesDialog } =@@ -97,6 +98,10 @@ function ListPerspective(props: Props) {useState<boolean>(false);const firstRender = useFirstRender();+ useReloadOnFocus(currentLocation.reloadOnFocus, () =>+ openDirectory(currentDirectoryPath),+ );+useEffect(() => {if (!firstRender && actions && actions.length > 0) {for (const action of actions) {