diff --git a/src/renderer/src/app.tsx b/src/renderer/src/app.tsx index b1867279..8184b15d 100644 --- a/src/renderer/src/app.tsx +++ b/src/renderer/src/app.tsx @@ -31,7 +31,6 @@ import { HydraCloudModal } from "./pages/shared-modals/hydra-cloud/hydra-cloud-m import { injectCustomCss } from "./helpers"; import "./app.scss"; -import { DownloadSource } from "@types"; export interface AppProps { children: React.ReactNode; @@ -137,71 +136,16 @@ export function App() { }); }, [fetchUserDetails, updateUserDetails, dispatch]); - const syncDownloadSources = useCallback(async () => { - const downloadSources = await window.electron.getDownloadSources(); - - const existingDownloadSources: DownloadSource[] = - await downloadSourcesTable.toArray(); - - window.electron.createDownloadSources( - existingDownloadSources.map((source) => source.url) - ); - - await Promise.allSettled( - downloadSources.map(async (source) => { - return new Promise((resolve) => { - const existingDownloadSource = existingDownloadSources.find( - (downloadSource) => downloadSource.url === source.url - ); - - if (!existingDownloadSource) { - const channel = new BroadcastChannel( - `download_sources:import:${source.url}` - ); - - downloadSourcesWorker.postMessage([ - "IMPORT_DOWNLOAD_SOURCE", - source.url, - ]); - - channel.onmessage = () => { - resolve(true); - channel.close(); - }; - } else { - resolve(true); - } - }); - }) - ); - - updateRepacks(); - - const id = crypto.randomUUID(); - const channel = new BroadcastChannel(`download_sources:sync:${id}`); - - channel.onmessage = async (event: MessageEvent) => { - const newRepacksCount = event.data; - window.electron.publishNewRepacksNotification(newRepacksCount); - updateRepacks(); - - const downloadSources = await downloadSourcesTable.toArray(); - - downloadSources - .filter((source) => !source.fingerprint) - .forEach(async (downloadSource) => { - const { fingerprint } = await window.electron.putDownloadSource( - downloadSource.objectIds - ); - - downloadSourcesTable.update(downloadSource.id, { fingerprint }); - }); - }; - - downloadSourcesWorker.postMessage(["SYNC_DOWNLOAD_SOURCES", id]); - }, [updateRepacks]); - const onSignIn = useCallback(() => { + window.electron.getDownloadSources().then((sources) => { + sources.forEach((source) => { + downloadSourcesWorker.postMessage([ + "IMPORT_DOWNLOAD_SOURCE", + source.url, + ]); + }); + }); + fetchUserDetails().then((response) => { if (response) { updateUserDetails(response); @@ -209,15 +153,7 @@ export function App() { showSuccessToast(t("successfully_signed_in")); } }); - - syncDownloadSources(); - }, [ - fetchUserDetails, - t, - showSuccessToast, - updateUserDetails, - syncDownloadSources, - ]); + }, [fetchUserDetails, t, showSuccessToast, updateUserDetails]); useEffect(() => { const unsubscribe = window.electron.onSyncFriendRequests((result) => { @@ -285,8 +221,41 @@ export function App() { }, [dispatch, draggingDisabled]); useEffect(() => { - syncDownloadSources(); - }, [syncDownloadSources]); + updateRepacks(); + + const id = crypto.randomUUID(); + const channel = new BroadcastChannel(`download_sources:sync:${id}`); + + channel.onmessage = async (event: MessageEvent) => { + const newRepacksCount = event.data; + window.electron.publishNewRepacksNotification(newRepacksCount); + updateRepacks(); + + const downloadSources = await downloadSourcesTable.toArray(); + + await Promise.all( + downloadSources + .filter((source) => !source.fingerprint) + .map(async (downloadSource) => { + const { fingerprint } = await window.electron.putDownloadSource( + downloadSource.objectIds + ); + + return downloadSourcesTable.update(downloadSource.id, { + fingerprint, + }); + }) + ); + + channel.close(); + }; + + downloadSourcesWorker.postMessage(["SYNC_DOWNLOAD_SOURCES", id]); + + return () => { + channel.close(); + }; + }, [updateRepacks]); useEffect(() => { const loadAndApplyTheme = async () => {