Skip to content

Commit

Permalink
Fix bug infinity switching preview files when using shortcut keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lethemanh committed Jan 8, 2025
1 parent f04e5f0 commit 265ef55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const useDrivePreview = () => {
});
}
},
[status.item?.id],
[status.item?.id, modal.isOpen],
);

return {
Expand Down
43 changes: 27 additions & 16 deletions tdrive/frontend/src/app/views/client/viewer/drive-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import Controls from './controls';
interface DrivePreviewProps {
items: DriveItem[];
}
let currentItemIndex: any;

export const DrivePreview: React.FC<DrivePreviewProps> = ({ items }) => {
const history = useHistory();
const company = useRouterCompany();
Expand All @@ -37,21 +39,33 @@ export const DrivePreview: React.FC<DrivePreviewProps> = ({ 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(() => {
Expand All @@ -67,16 +81,14 @@ export const DrivePreview: React.FC<DrivePreviewProps> = ({ 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);
Expand All @@ -89,7 +101,6 @@ export const DrivePreview: React.FC<DrivePreviewProps> = ({ 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
Expand Down

0 comments on commit 265ef55

Please sign in to comment.