Skip to content

Commit 9c81a2d

Browse files
author
NellowTCS
committed
hooks
1 parent 918be77 commit 9c81a2d

10 files changed

Lines changed: 405 additions & 461 deletions

File tree

Build/src/hooks/useDialogVisibility.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

Build/src/hooks/useFileHandler.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { useState, useEffect, useRef } from "react";
2+
import { toast } from "sonner";
3+
import {
4+
setupFileHandler,
5+
type FileHandlerResult,
6+
} from "../platform/storage/fileHandler";
7+
import type { Track } from "../core/engine/types";
8+
9+
export function useFileHandler(
10+
addSong: (song: Track) => Promise<void>,
11+
t: any,
12+
importFiles: (
13+
files: File[],
14+
addSong: (song: Track) => Promise<void>,
15+
t: any,
16+
) => Promise<void>,
17+
isInitialized?: boolean,
18+
) {
19+
const [isSupported] = useState(
20+
() =>
21+
"launchQueue" in window &&
22+
typeof window.launchQueue?.setConsumer === "function",
23+
);
24+
const pendingFilesRef = useRef<File[]>([]);
25+
const hasProcessedRef = useRef(false);
26+
27+
useEffect(() => {
28+
if (!isSupported) return;
29+
30+
const cleanup = setupFileHandler(async (result: FileHandlerResult) => {
31+
if (hasProcessedRef.current) return;
32+
33+
if (result.files.length > 0) {
34+
if (isInitialized === false) {
35+
pendingFilesRef.current.push(...result.files);
36+
return;
37+
}
38+
39+
hasProcessedRef.current = true;
40+
toast.success(
41+
t("fileHandler.filesReceived", { count: result.successCount }),
42+
);
43+
await importFiles(result.files, addSong, t);
44+
}
45+
if (result.errorCount > 0) {
46+
toast.error(
47+
t("fileHandler.filesSkipped", { count: result.errorCount }),
48+
);
49+
}
50+
});
51+
52+
return cleanup;
53+
}, [addSong, t, importFiles, isInitialized, isSupported]);
54+
55+
useEffect(() => {
56+
if (isInitialized && pendingFilesRef.current.length > 0) {
57+
const filesToProcess = [...pendingFilesRef.current];
58+
pendingFilesRef.current = [];
59+
60+
importFiles(filesToProcess, addSong, t)
61+
.then(() => {
62+
hasProcessedRef.current = true;
63+
toast.success(
64+
t("fileHandler.filesReceived", { count: filesToProcess.length }),
65+
);
66+
})
67+
.catch((error) => {
68+
console.error("Failed to process queued files:", error);
69+
toast.error(
70+
t("filePicker.failedImport", { count: filesToProcess.length }),
71+
);
72+
});
73+
}
74+
}, [isInitialized, addSong, t, importFiles]);
75+
76+
return { isSupported };
77+
}

0 commit comments

Comments
 (0)