spacedrive/interface/hooks/useQuickRescan.ts
nikec b8d13a8cfc
[ENG-1418] Explorer settings (#1764)
* preferences

* useShortcut and move location id to function

* void

* rescan
2023-11-10 10:23:51 +00:00

43 lines
1.2 KiB
TypeScript

import { useEffect, useRef } from 'react';
import { useRspcLibraryContext } from '@sd/client';
import { toast } from '@sd/ui';
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;
quickRescanSubscription.current?.();
quickRescanSubscription.current = client.addSubscription(
[
'locations.quickRescan',
{
location_id: locationId,
sub_path: path ?? ''
}
],
{ onData() {} }
);
toast.success({
title: `Quick rescan started`
});
};
return rescan;
};