spacedrive/interface/hooks/useQuickRescan.ts
jake 6e61849536
[ENG-1321] Webview reload keybind change and make MacOS reload explorer menu item functional (#2093)
* feat: cmd+r to quick rescan the current location, and change reload webview keybind

* chore: lockfile update

* fix: provide a location id to quick rescan in keybind event handler

* fix: add cooldown to quick rescan to prevent double-rescans

* refactor: use `explorerStore` instead
2024-03-09 06:11:14 +00:00

47 lines
1.4 KiB
TypeScript

import { useEffect, useRef } from 'react';
import { useRspcLibraryContext } from '@sd/client';
import { toast } from '@sd/ui';
import { explorerStore } from '~/app/$libraryId/Explorer/store';
import { useExplorerContext } from '../app/$libraryId/Explorer/Context';
import { useExplorerSearchParams } from '../app/$libraryId/Explorer/util';
export const useQuickRescan = () => {
// subscription so that we can cancel it if in progress
const quickRescanSubscription = useRef<() => void | undefined>();
// gotta clean up any rescan subscriptions if the exist
useEffect(() => () => quickRescanSubscription.current?.(), []);
const { client } = useRspcLibraryContext();
const explorer = useExplorerContext({ suspense: false });
const [{ path }] = useExplorerSearchParams();
const rescan = (id?: number) => {
const locationId =
id ?? (explorer?.parent?.type === 'Location' ? explorer.parent.location.id : undefined);
if (locationId === undefined) return;
if (Date.now() - explorerStore.quickRescanLastRun < 200) return;
explorerStore.quickRescanLastRun = Date.now();
quickRescanSubscription.current?.();
quickRescanSubscription.current = client.addSubscription(
[
'locations.quickRescan',
{
location_id: locationId,
sub_path: path ?? ''
}
],
{ onData() {} }
);
toast.success({
title: `Quick rescan started`
});
};
return rescan;
};