mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-22 07:15:47 +00:00
7a7a9f5e6b
111 of 122 top-level src/utils/ files move into 16 topic folders (audio, cache, cover, share, server, playback, playlist, deviceSync, waveform, mix, format, export, changelog, ui, perf, componentHelpers). True singletons with no cluster stay at the utils/ root. Pure file-move: a path-aware codemod rewrote 539 relative-import specifiers across 275 files; no logic touched. The hot-path coverage gate list (.github/frontend-hot-path-files.txt) is updated to the new paths for the 11 gated utils files — a mechanical consequence of the move, not a CI change. tsc is green.
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
import { invoke } from '@tauri-apps/api/core';
|
|
import type { RemovableDrive } from '../utils/deviceSync/deviceSyncHelpers';
|
|
|
|
export interface DeviceSyncDrivesResult {
|
|
drives: RemovableDrive[];
|
|
drivesLoading: boolean;
|
|
activeDrive: RemovableDrive | null;
|
|
driveDetected: boolean;
|
|
refreshDrives: () => Promise<void>;
|
|
}
|
|
|
|
export function useDeviceSyncDrives(targetDir: string | null): DeviceSyncDrivesResult {
|
|
const [drives, setDrives] = useState<RemovableDrive[]>([]);
|
|
const [drivesLoading, setDrivesLoading] = useState(false);
|
|
|
|
const refreshDrives = useCallback(async () => {
|
|
setDrivesLoading(true);
|
|
try {
|
|
const result = await invoke<RemovableDrive[]>('get_removable_drives');
|
|
setDrives(result);
|
|
} catch {
|
|
setDrives([]);
|
|
} finally {
|
|
setDrivesLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
// Fetch drives on mount, then poll every 5 seconds
|
|
useEffect(() => {
|
|
refreshDrives();
|
|
const interval = setInterval(refreshDrives, 5000);
|
|
return () => clearInterval(interval);
|
|
}, [refreshDrives]);
|
|
|
|
// Detect if the current targetDir is on a detected removable drive
|
|
const activeDrive = useMemo(() => {
|
|
if (!targetDir) return null;
|
|
return drives.find(d => targetDir.startsWith(d.mount_point)) ?? null;
|
|
}, [targetDir, drives]);
|
|
|
|
const driveDetected = activeDrive !== null;
|
|
|
|
return { drives, drivesLoading, activeDrive, driveDetected, refreshDrives };
|
|
}
|