add watch modal

This commit is contained in:
Rushikesh Tote 2022-06-02 20:08:54 +05:30
parent 440c5bf4f9
commit df5ecdf046
3 changed files with 39 additions and 22 deletions

View file

@ -0,0 +1,8 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React from 'react';
function WatchModal({ watchModalView, setWatchModalView }) {
return <div>WatchModal</div>;
}
export default WatchModal;

View file

@ -122,6 +122,7 @@ export default function Upload(props: Props) {
isPendingDesktopUpload.current = true; isPendingDesktopUpload.current = true;
pendingDesktopUploadCollectionName.current = collectionName; pendingDesktopUploadCollectionName.current = collectionName;
}; };
watchService.syncWithRemote = props.syncWithRemote;
watchService.init(); watchService.init();
} }
}, []); }, []);

View file

@ -27,6 +27,7 @@ class WatchService {
pathToIDMap = new Map<string, number>(); pathToIDMap = new Map<string, number>();
setElectronFiles: (files: ElectronFile[]) => void; setElectronFiles: (files: ElectronFile[]) => void;
setCollectionName: (collectionName: string) => void; setCollectionName: (collectionName: string) => void;
syncWithRemote: () => void;
constructor() { constructor() {
this.ElectronAPIs = runningInBrowser() && window['ElectronAPIs']; this.ElectronAPIs = runningInBrowser() && window['ElectronAPIs'];
@ -89,6 +90,17 @@ class WatchService {
this.ElectronAPIs.setWatchMappings(mappings); this.ElectronAPIs.setWatchMappings(mappings);
this.setWatchFunctions(); 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); console.log('new mappings', mappings);
await this.ElectronAPIs.setWatchMappings(mappings); this.ElectronAPIs.setWatchMappings(mappings);
this.syncWithRemote();
console.log( console.log(
'now mappings', '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) { async trashByIDs(toTrashFiles: Mapping['files'], collectionName: string) {
if (this.allElectronAPIsExist) { if (this.allElectronAPIsExist) {
const collections = await syncCollections(); const collections = await syncCollections();
@ -273,20 +276,23 @@ class WatchService {
} }
} }
async function diskFileAddedCallback(w: WatchService, filePath: string) { async function diskFileAddedCallback(instance: WatchService, filePath: string) {
console.log('diskFileAddedCallback', w, filePath); console.log('diskFileAddedCallback', instance, filePath);
const collectionName = await w.getCollectionName(filePath); const collectionName = await instance.getCollectionName(filePath);
const event: UploadQueueType = { const event: UploadQueueType = {
collectionName, collectionName,
paths: [filePath], paths: [filePath],
}; };
w.uploadQueue.push(event); instance.uploadQueue.push(event);
w.runNextUpload(); instance.runNextUpload();
} }
async function diskFileRemovedCallback(w: WatchService, filePath: string) { async function diskFileRemovedCallback(
const collectionName = await w.getCollectionName(filePath); instance: WatchService,
filePath: string
) {
const collectionName = await instance.getCollectionName(filePath);
console.log('collection', collectionName); console.log('collection', collectionName);
@ -294,7 +300,7 @@ async function diskFileRemovedCallback(w: WatchService, filePath: string) {
return; return;
} }
const mappings: Mapping[] = await w.ElectronAPIs.getWatchMappings(); let mappings: Mapping[] = await instance.ElectronAPIs.getWatchMappings();
const mapping = mappings.find( const mapping = mappings.find(
(mapping) => mapping.collectionName === collectionName (mapping) => mapping.collectionName === collectionName
@ -308,12 +314,14 @@ async function diskFileRemovedCallback(w: WatchService, filePath: string) {
return; 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); 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(); export default new WatchService();