From 682dfc3a021245f1d06b0ad49281dde36bd19d3b Mon Sep 17 00:00:00 2001 From: lethemanh Date: Wed, 8 Jan 2025 10:02:41 +0700 Subject: [PATCH] Fix bug infinite switching preview files when using shortcut keys --- .../features/drive/hooks/use-drive-preview.ts | 2 +- .../app/views/client/viewer/drive-preview.tsx | 43 ++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/tdrive/frontend/src/app/features/drive/hooks/use-drive-preview.ts b/tdrive/frontend/src/app/features/drive/hooks/use-drive-preview.ts index a9f08e2c2..7fbb7d312 100644 --- a/tdrive/frontend/src/app/features/drive/hooks/use-drive-preview.ts +++ b/tdrive/frontend/src/app/features/drive/hooks/use-drive-preview.ts @@ -61,7 +61,7 @@ export const useDrivePreview = () => { }); } }, - [status.item?.id], + [status.item?.id, modal.isOpen], ); return { diff --git a/tdrive/frontend/src/app/views/client/viewer/drive-preview.tsx b/tdrive/frontend/src/app/views/client/viewer/drive-preview.tsx index cc7ae0b87..2a98281a4 100644 --- a/tdrive/frontend/src/app/views/client/viewer/drive-preview.tsx +++ b/tdrive/frontend/src/app/views/client/viewer/drive-preview.tsx @@ -25,6 +25,8 @@ import Controls from './controls'; interface DrivePreviewProps { items: DriveItem[]; } +let currentItemIndex: any; + export const DrivePreview: React.FC = ({ items }) => { const history = useHistory(); const company = useRouterCompany(); @@ -37,21 +39,33 @@ export const DrivePreview: React.FC = ({ items }) => { const { type = '' } = useDrivePreviewDisplayData(); const name = status.details?.item.name; + useEffect(() => { + if (!isOpen) { + currentItemIndex = undefined; + return; + } + + if (currentItemIndex === undefined && items && status.item?.id) { + currentItemIndex = items.findIndex(x => x.id === status.item?.id); + } + }, [status.item?.id, items, isOpen]); const handleSwitchRight = () => { - const currentItem = status.item; - if (currentItem) { - const currentItemPos = items.findIndex(x => x.id === currentItem.id); - switchPreview(items[(currentItemPos + 1) % items.length]); + if (currentItemIndex < 0) { + return; } + + currentItemIndex = (currentItemIndex + 1) % items.length; + switchPreview(items[currentItemIndex]); }; const handleSwitchLeft = () => { - const currentItem = status.item; - if (currentItem) { - const currentItemPos = items.findIndex(x => x.id === currentItem.id); - switchPreview(items[(currentItemPos - 1 + items.length) % items.length]); + if (currentItemIndex < 0) { + return; } + + currentItemIndex = (currentItemIndex - 1 + items.length) % items.length; + switchPreview(items[currentItemIndex]); }; useEffect(() => { @@ -67,16 +81,14 @@ export const DrivePreview: React.FC = ({ items }) => { // eslint-disable-next-line @typescript-eslint/no-empty-function return () => {}; - if (!status.loading) { - addShortcut({ shortcut: 'Right', handler: handleSwitchRight }); - addShortcut({ shortcut: 'Left', handler: handleSwitchLeft }); - } + addShortcut({ shortcut: 'Right', handler: handleSwitchRight }); + addShortcut({ shortcut: 'Left', handler: handleSwitchLeft }); return () => { - removeShortcut({ shortcut: 'Right', handler: handleSwitchRight }); - removeShortcut({ shortcut: 'Left', handler: handleSwitchLeft }); + removeShortcut({ shortcut: 'Right' }); + removeShortcut({ shortcut: 'Left' }); }; - }, [status]); + }, [JSON.stringify([...items])]); useEffect(() => { clearTimeout(animationTimeout); @@ -89,7 +101,6 @@ export const DrivePreview: React.FC = ({ items }) => { }, [loading]); const switchPreview = async (item: DriveItem) => { - close(); //TODO[ASH] fix state management for this component //right now changing the routing leads to a lot of components rerender //and galery become unusable