diff --git a/web/apps/photos/src/components/WatchFolder.tsx b/web/apps/photos/src/components/WatchFolder.tsx index a12bb2e49..5715dfb45 100644 --- a/web/apps/photos/src/components/WatchFolder.tsx +++ b/web/apps/photos/src/components/WatchFolder.tsx @@ -27,12 +27,11 @@ import { } from "@mui/material"; import { styled } from "@mui/material/styles"; import { CollectionMappingChoiceModal } from "components/Upload/CollectionMappingChoiceModal"; -import { PICKED_UPLOAD_TYPE } from "constants/upload"; import { t } from "i18next"; import { AppContext } from "pages/_app"; import React, { useContext, useEffect, useState } from "react"; import watcher from "services/watch"; -import { getImportSuggestion } from "utils/upload"; +import { areAllInSameDirectory } from "utils/upload"; interface WatchFolderProps { open: boolean; @@ -84,15 +83,11 @@ export const WatchFolder: React.FC = ({ open, onClose }) => { const selectCollectionMappingAndAddWatch = async (path: string) => { const filePaths = await ensureElectron().watch.findFiles(path); - const { hasNestedFolders } = getImportSuggestion( - PICKED_UPLOAD_TYPE.FOLDERS, - filePaths, - ); - if (hasNestedFolders) { + if (areAllInSameDirectory(filePaths)) { + addWatch(path, "root"); + } else { setSavedFolderPath(path); setChoiceModalOpen(true); - } else { - addWatch(path, "root"); } }; diff --git a/web/apps/photos/src/utils/upload/index.ts b/web/apps/photos/src/utils/upload/index.ts index 935400352..0f3ceb684 100644 --- a/web/apps/photos/src/utils/upload/index.ts +++ b/web/apps/photos/src/utils/upload/index.ts @@ -1,3 +1,4 @@ +import { directoryNameFromPOSIXPath } from "@/next/file"; import { FILE_TYPE } from "constants/file"; import { A_SEC_IN_MICROSECONDS, @@ -113,46 +114,11 @@ export function areFileWithCollectionsSame( /** * Return true if all the paths in the given list are items that belong to the * same (arbitrary) directory. + * + * Empty list of paths is considered to be in the same directory. */ -export const areInSameDirectory = ( - uploadType: PICKED_UPLOAD_TYPE, - toUploadFiles: File[] | ElectronFile[], -): ImportSuggestion { - if (isElectron() && uploadType === PICKED_UPLOAD_TYPE.FILES) { - return DEFAULT_IMPORT_SUGGESTION; - } - - const paths: string[] = toUploadFiles.map((file) => file["path"]); - const getCharCount = (str: string) => (str.match(/\//g) ?? []).length; - paths.sort((path1, path2) => getCharCount(path1) - getCharCount(path2)); - const firstPath = paths[0]; - const lastPath = paths[paths.length - 1]; - - const L = firstPath.length; - let i = 0; - const firstFileFolder = firstPath.substring(0, firstPath.lastIndexOf("/")); - const lastFileFolder = lastPath.substring(0, lastPath.lastIndexOf("/")); - - while (i < L && firstPath.charAt(i) === lastPath.charAt(i)) i++; - let commonPathPrefix = firstPath.substring(0, i); - - if (commonPathPrefix) { - commonPathPrefix = commonPathPrefix.substring( - 0, - commonPathPrefix.lastIndexOf("/"), - ); - if (commonPathPrefix) { - commonPathPrefix = commonPathPrefix.substring( - commonPathPrefix.lastIndexOf("/") + 1, - ); - } - } - return { - rootFolderName: commonPathPrefix || null, - hasNestedFolders: firstFileFolder !== lastFileFolder, - hasRootLevelFileWithFolder: firstFileFolder === "", - }; -} +export const areAllInSameDirectory = (paths: string[]) => + new Set(paths.map(directoryNameFromPOSIXPath)).size == 1; export function getImportSuggestion( uploadType: PICKED_UPLOAD_TYPE,