diff --git a/web/apps/photos/src/components/WatchFolder.tsx b/web/apps/photos/src/components/WatchFolder.tsx index c0e7ab38a..cc5a09223 100644 --- a/web/apps/photos/src/components/WatchFolder.tsx +++ b/web/apps/photos/src/components/WatchFolder.tsx @@ -1,3 +1,4 @@ +import { ensureElectron } from "@/next/electron"; import { FlexWrapper, HorizontalFlex, @@ -37,17 +38,21 @@ interface WatchFolderProps { onClose: () => void; } +/** + * View the state of and manage folder watches. + * + * This is the screen that controls that "watch folder" feature in the app. + */ export const WatchFolder: React.FC = ({ open, onClose }) => { - const [mappings, setMappings] = useState([]); - const [inputFolderPath, setInputFolderPath] = useState(""); + const [watches, setWatches] = useState(); + const [inputFolderPath, setInputFolderPath] = useState< + string | undefined + >(); const [choiceModalOpen, setChoiceModalOpen] = useState(false); const appContext = useContext(AppContext); - const electron = globalThis.electron; - useEffect(() => { - if (!electron) return; - watcher.getWatchMappings().then((m) => setMappings(m)); + watcher.getWatchMappings().then((ws) => setWatches(ws)); }, []); useEffect(() => { @@ -71,10 +76,8 @@ export const WatchFolder: React.FC = ({ open, onClose }) => { }; const addFolderForWatching = async (path: string) => { - if (!electron) return; - setInputFolderPath(path); - const files = await electron.getDirFiles(path); + const files = await ensureElectron().getDirFiles(path); const analysisResult = getImportSuggestion( PICKED_UPLOAD_TYPE.FOLDERS, files, @@ -86,17 +89,6 @@ export const WatchFolder: React.FC = ({ open, onClose }) => { } }; - const handleAddFolderClick = async () => { - await handleFolderSelection(); - }; - - const handleFolderSelection = async () => { - const folderPath = await watcher.selectFolder(); - if (folderPath) { - await addFolderForWatching(folderPath); - } - }; - const handleAddWatchMapping = async ( uploadStrategy: UPLOAD_STRATEGY, folderPath?: string, @@ -108,12 +100,19 @@ export const WatchFolder: React.FC = ({ open, onClose }) => { uploadStrategy, ); setInputFolderPath(""); - setMappings(await watcher.getWatchMappings()); + setWatches(await watcher.getWatchMappings()); }; - const stopWatching = async (watch: FolderWatch) => { + const addWatch = async () => { + const folderPath = await watcher.selectFolder(); + if (folderPath) { + await addFolderForWatching(folderPath); + } + }; + + const removeWatch = async (watch: FolderWatch) => { await watcher.removeWatchForFolderPath(watch.folderPath); - setMappings(await watcher.getWatchMappings()); + setWatches(await watcher.getWatchMappings()); }; const closeChoiceModal = () => setChoiceModalOpen(false); @@ -143,15 +142,8 @@ export const WatchFolder: React.FC = ({ open, onClose }) => { - -