Fix the problem of resuming downloads when the webpage is closed or the network is disconnected. Further testing is needed.

This commit is contained in:
david_bai
2025-07-19 00:24:44 +08:00
parent 522f567fb4
commit 9ce63992b7
7 changed files with 121 additions and 76 deletions
+1 -28
View File
@@ -1,19 +1,9 @@
import { useState, useCallback, useEffect } from "react";
import { useState, useCallback } from "react";
import { CustomFile, FileMeta, fileMetadata } from "@/types/webrtc";
import { Messages } from "@/types/messages";
import JSZip from "jszip";
import { downloadAs } from "@/lib/fileUtils";
// Helper functions for beforeunload (can be kept local to this hook if not used elsewhere)
const handleWindowBeforeUnload = (event: BeforeUnloadEvent) => {
event.preventDefault();
event.returnValue = ""; // Required for Chrome
};
const allowWindowUnload = () => {
window.removeEventListener("beforeunload", handleWindowBeforeUnload);
};
interface UseFileTransferHandlerProps {
messages: Messages | null;
putMessageInMs: (
@@ -33,23 +23,6 @@ export function useFileTransferHandler({
const [retrievedFiles, setRetrievedFiles] = useState<CustomFile[]>([]);
const [retrievedFileMetas, setRetrievedFileMetas] = useState<FileMeta[]>([]);
// Manage beforeunload event based on content
useEffect(() => {
if (
sendFiles.length === 0 &&
shareContent === "" &&
retrievedFiles.length === 0 &&
retrievedContent === ""
) {
allowWindowUnload();
} else {
window.addEventListener("beforeunload", handleWindowBeforeUnload);
}
return () => {
allowWindowUnload(); // Clean up listener when hook unmounts or dependencies change if any
};
}, [sendFiles, shareContent, retrievedFiles, retrievedContent]);
const updateShareContent = useCallback((content: string) => {
setShareContent(content);
}, []);