From df5ecdf04665dc5e9a7c27f4c8e46a3c14da8972 Mon Sep 17 00:00:00 2001 From: Rushikesh Tote Date: Thu, 2 Jun 2022 20:08:54 +0530 Subject: [PATCH] add watch modal --- src/components/WatchModal.tsx | 8 ++++ src/components/pages/gallery/Upload.tsx | 1 + src/services/watchService.ts | 52 ++++++++++++++----------- 3 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 src/components/WatchModal.tsx diff --git a/src/components/WatchModal.tsx b/src/components/WatchModal.tsx new file mode 100644 index 000000000..1926da746 --- /dev/null +++ b/src/components/WatchModal.tsx @@ -0,0 +1,8 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +import React from 'react'; + +function WatchModal({ watchModalView, setWatchModalView }) { + return
WatchModal
; +} + +export default WatchModal; diff --git a/src/components/pages/gallery/Upload.tsx b/src/components/pages/gallery/Upload.tsx index 20841eac4..12c635d6e 100644 --- a/src/components/pages/gallery/Upload.tsx +++ b/src/components/pages/gallery/Upload.tsx @@ -122,6 +122,7 @@ export default function Upload(props: Props) { isPendingDesktopUpload.current = true; pendingDesktopUploadCollectionName.current = collectionName; }; + watchService.syncWithRemote = props.syncWithRemote; watchService.init(); } }, []); diff --git a/src/services/watchService.ts b/src/services/watchService.ts index d9fe8b20b..fda2ea429 100644 --- a/src/services/watchService.ts +++ b/src/services/watchService.ts @@ -27,6 +27,7 @@ class WatchService { pathToIDMap = new Map(); setElectronFiles: (files: ElectronFile[]) => void; setCollectionName: (collectionName: string) => void; + syncWithRemote: () => void; constructor() { this.ElectronAPIs = runningInBrowser() && window['ElectronAPIs']; @@ -89,6 +90,17 @@ class WatchService { this.ElectronAPIs.setWatchMappings(mappings); this.setWatchFunctions(); + this.syncWithRemote(); + } + } + + setWatchFunctions() { + if (this.allElectronAPIsExist) { + this.ElectronAPIs.registerWatcherFunctions( + this, + diskFileAddedCallback, + diskFileRemovedCallback + ); } } @@ -208,7 +220,8 @@ class WatchService { console.log('new mappings', mappings); - await this.ElectronAPIs.setWatchMappings(mappings); + this.ElectronAPIs.setWatchMappings(mappings); + this.syncWithRemote(); console.log( 'now mappings', @@ -222,16 +235,6 @@ class WatchService { } } - setWatchFunctions() { - if (this.allElectronAPIsExist) { - this.ElectronAPIs.registerWatcherFunctions( - this, - diskFileAddedCallback, - diskFileRemovedCallback - ); - } - } - async trashByIDs(toTrashFiles: Mapping['files'], collectionName: string) { if (this.allElectronAPIsExist) { const collections = await syncCollections(); @@ -273,20 +276,23 @@ class WatchService { } } -async function diskFileAddedCallback(w: WatchService, filePath: string) { - console.log('diskFileAddedCallback', w, filePath); - const collectionName = await w.getCollectionName(filePath); +async function diskFileAddedCallback(instance: WatchService, filePath: string) { + console.log('diskFileAddedCallback', instance, filePath); + const collectionName = await instance.getCollectionName(filePath); const event: UploadQueueType = { collectionName, paths: [filePath], }; - w.uploadQueue.push(event); - w.runNextUpload(); + instance.uploadQueue.push(event); + instance.runNextUpload(); } -async function diskFileRemovedCallback(w: WatchService, filePath: string) { - const collectionName = await w.getCollectionName(filePath); +async function diskFileRemovedCallback( + instance: WatchService, + filePath: string +) { + const collectionName = await instance.getCollectionName(filePath); console.log('collection', collectionName); @@ -294,7 +300,7 @@ async function diskFileRemovedCallback(w: WatchService, filePath: string) { return; } - const mappings: Mapping[] = await w.ElectronAPIs.getWatchMappings(); + let mappings: Mapping[] = await instance.ElectronAPIs.getWatchMappings(); const mapping = mappings.find( (mapping) => mapping.collectionName === collectionName @@ -308,12 +314,14 @@ async function diskFileRemovedCallback(w: WatchService, filePath: string) { return; } - await w.trashByIDs([file], collectionName); + await instance.trashByIDs([file], collectionName); + mappings = await instance.ElectronAPIs.getWatchMappings(); mapping.files = mapping.files.filter((file) => file.path !== filePath); - await w.ElectronAPIs.setWatchMappings(mappings); + instance.ElectronAPIs.setWatchMappings(mappings); + instance.syncWithRemote(); - console.log('after trash', w.ElectronAPIs.getWatchMappings()); + console.log('after trash', instance.ElectronAPIs.getWatchMappings()); } export default new WatchService();