π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
refractor(photos):create-shared-util-function
Aswin Asok17048ecf271a
diff --git a/web/apps/photos/src/components/Collections/PickCoverPhotoDialog.tsx b/web/apps/photos/src/components/Collections/PickCoverPhotoDialog.tsxindex a70f7131a6..dd02d64651 100644--- a/web/apps/photos/src/components/Collections/PickCoverPhotoDialog.tsx+++ b/web/apps/photos/src/components/Collections/PickCoverPhotoDialog.tsx@@ -1,4 +1,4 @@-import type { SelectedState } from "@/utils/file";+import { fileTimelineDateString, type SelectedState } from "@/utils/file";import CloseIcon from "@mui/icons-material/Close";import {Box,@@ -12,12 +12,9 @@ import {import type { LocalUser } from "ente-accounts/services/user";import { LoadingButton } from "ente-base/components/mui/LoadingButton";import { useIsSmallWidth } from "ente-base/components/utils/hooks";-import { isSameDay } from "ente-base/date";import { ut } from "ente-base/i18n";-import { formattedDate } from "ente-base/i18n-date";import type { Collection } from "ente-media/collection";import type { EnteFile } from "ente-media/file";-import { fileCreationPhotoDate } from "ente-media/file-metadata";import { FileType } from "ente-media/file-type";import { t } from "i18next";import React, { useCallback, useEffect, useId, useMemo, useState } from "react";@@ -307,12 +304,3 @@ const createSingleSelection = (collectionID,context: { mode: "albums", collectionID },});--const fileTimelineDateString = (file: EnteFile) => {- const date = fileCreationPhotoDate(file);- return isSameDay(date, new Date())- ? t("today")- : isSameDay(date, new Date(Date.now() - 24 * 60 * 60 * 1000))- ? t("yesterday")- : formattedDate(date);-};diff --git a/web/apps/photos/src/components/FileList.tsx b/web/apps/photos/src/components/FileList.tsxindex dc8dddab0f..6c037de2a3 100644--- a/web/apps/photos/src/components/FileList.tsx+++ b/web/apps/photos/src/components/FileList.tsx@@ -5,7 +5,10 @@ import {} from "@/components/FileContextMenu";import type { GalleryBarMode } from "@/components/gallery/reducer";import { StarIcon } from "@/components/icons/StarIcon";-import type { SelectedState } from "@/utils/file";+import {+ selectedFavoriteCount as countSelectedFavorites,+ type SelectedState,+} from "@/utils/file";import {getAvailableFileActions,type FileContextAction,@@ -469,18 +472,10 @@ export const FileList: React.FC<FileListProps> = ({if (selected.count == 0) setRangeStartIndex(undefined);}, [selected]);- const selectedFavoriteCount = useMemo(() => {- if (!favoriteFileIDs || selected.count == 0) return 0;- let count = 0;- for (const [key, value] of Object.entries(selected)) {- if (typeof value === "boolean" && value) {- if (favoriteFileIDs.has(Number(key))) {- count += 1;- }- }- }- return count;- }, [favoriteFileIDs, selected]);+ const selectedFavoriteCount = useMemo(+ () => countSelectedFavorites(selected, favoriteFileIDs),+ [favoriteFileIDs, selected],+ );const contextMenuActions = useMemo(() => {if (!onContextMenuAction || !contextMenu) return [];diff --git a/web/apps/photos/src/components/FileListWithViewer.tsx b/web/apps/photos/src/components/FileListWithViewer.tsxindex b5aea5183a..80629973dc 100644--- a/web/apps/photos/src/components/FileListWithViewer.tsx+++ b/web/apps/photos/src/components/FileListWithViewer.tsx@@ -2,12 +2,11 @@ import { CollectionMapDialog } from "@/components/Collections/CollectionMapDialoimport type { RemotePullOpts } from "@/components/gallery";import { downloadAndSaveFiles } from "@/services/save";import { uploadManager } from "@/services/upload-manager";+import { fileTimelineDateString } from "@/utils/file";import { IconButton, Tooltip, styled } from "@mui/material";import { useColorScheme, useTheme } from "@mui/material/styles";import { useModalVisibility } from "ente-base/components/utils/modal";import { useBaseContext } from "ente-base/context";-import { isSameDay } from "ente-base/date";-import { formattedDate } from "ente-base/i18n-date";import type { AddSaveGroup } from "ente-gallery/components/utils/save-groups";import {FileViewer,@@ -16,7 +15,7 @@ import {} from "ente-gallery/components/viewer/FileViewer";import type { Collection } from "ente-media/collection";import type { EnteFile } from "ente-media/file";-import { fileCreationPhotoDate, fileFileName } from "ente-media/file-metadata";+import { fileFileName } from "ente-media/file-metadata";import { useSettingsSnapshot } from "ente-new/photos/components/utils/use-snapshot";import { moveToTrash } from "ente-new/photos/services/collection";import type { CollectionSummary } from "ente-new/photos/services/collection-summary";@@ -434,12 +433,3 @@ const MapIcon = styled("img")<{ $isDarkMode: boolean }>($isDarkMode || theme.palette.mode === "dark" ? "invert(1)" : "none",}),);--const fileTimelineDateString = (file: EnteFile) => {- const date = fileCreationPhotoDate(file);- return isSameDay(date, new Date())- ? t("today")- : isSameDay(date, new Date(Date.now() - 24 * 60 * 60 * 1000))- ? t("yesterday")- : formattedDate(date);-};diff --git a/web/apps/photos/src/components/gallery/reducer.ts b/web/apps/photos/src/components/gallery/reducer.tsindex 24952a8c08..be6417d6c5 100644--- a/web/apps/photos/src/components/gallery/reducer.ts+++ b/web/apps/photos/src/components/gallery/reducer.ts@@ -970,6 +970,22 @@ const deriveArchivedFileIDs = (.map((f) => f.id),);+const favoriteUpdateFileIDs = (+ user: LocalUser,+ collectionFiles: GalleryState["collectionFiles"],+ update: UnsyncedFavoriteUpdate,+) =>+ update.fileHashAndTypeKey+ ? collectionFiles+ .filter(+ (file) =>+ file.ownerID != user.id &&+ favoriteFileHashAndTypeKey(file) ==+ update.fileHashAndTypeKey,+ )+ .map((file) => file.id)+ : [update.fileID];+const deriveFavoriteFileIDs = (user: LocalUser,collections: GalleryState["collections"],@@ -1005,16 +1021,11 @@ const deriveFavoriteFileIDs = (}for (const update of unsyncedFavoriteUpdates.values()) {- const updatedFileIDs = update.fileHashAndTypeKey- ? collectionFiles- .filter(- (file) =>- file.ownerID != user.id &&- favoriteFileHashAndTypeKey(file) ==- update.fileHashAndTypeKey,- )- .map((file) => file.id)- : [update.fileID];+ const updatedFileIDs = favoriteUpdateFileIDs(+ user,+ collectionFiles,+ update,+ );for (const fileID of updatedFileIDs) {if (update.isFavorite) favoriteFileIDs.add(fileID);else favoriteFileIDs.delete(fileID);@@ -1032,16 +1043,11 @@ const preserveUnreflectedFavoriteUpdates = (// Keep an overlay until a pull reports the same state for every copy.for (const [key, update] of unsyncedFavoriteUpdates.entries()) {- const updatedFileIDs = update.fileHashAndTypeKey- ? collectionFiles- .filter(- (file) =>- file.ownerID != user.id &&- favoriteFileHashAndTypeKey(file) ==- update.fileHashAndTypeKey,- )- .map((file) => file.id)- : [update.fileID];+ const updatedFileIDs = favoriteUpdateFileIDs(+ user,+ collectionFiles,+ update,+ );if (updatedFileIDs.some(diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex ea64083dda..d62b4b30f0 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -131,6 +131,7 @@ import { postPullFiles, prePullFiles, pullFiles } from "@/services/pull";import { uploadManager } from "@/services/upload-manager";import watcher from "@/services/watch";import {+ selectedFavoriteCount as countSelectedFavorites,getSelectedFiles,performFileOp,type SelectedState,@@ -1459,18 +1460,10 @@ const Page: React.FC = () => {const selectedCount = selected.count;const selectedOwnCount = selected.ownCount;- const selectedFavoriteCount = useMemo(() => {- if (selected.count == 0) return 0;- let count = 0;- for (const [key, value] of Object.entries(selected)) {- if (typeof value === "boolean" && value) {- if (favoriteFileIDs.has(Number(key))) {- count += 1;- }- }- }- return count;- }, [favoriteFileIDs, selected]);+ const selectedFavoriteCount = useMemo(+ () => countSelectedFavorites(selected, favoriteFileIDs),+ [favoriteFileIDs, selected],+ );const handleUpdateCollectionCover = useCallback(async (coverID: number) => {diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.tsindex 857335b6ca..81e5b09490 100644--- a/web/apps/photos/src/utils/file/index.ts+++ b/web/apps/photos/src/utils/file/index.ts@@ -1,9 +1,15 @@import type { SelectionContext } from "@/components/gallery";import type { FileOp } from "@/components/SelectedFileOptions";import { downloadAndSaveFiles } from "@/services/save";+import { isSameDay } from "ente-base/date";+import { formattedDate } from "ente-base/i18n-date";import type { AddSaveGroup } from "ente-gallery/components/utils/save-groups";import type { EnteFile } from "ente-media/file";-import { fileFileName, ItemVisibility } from "ente-media/file-metadata";+import {+ fileCreationPhotoDate,+ fileFileName,+ ItemVisibility,+} from "ente-media/file-metadata";import { FileType } from "ente-media/file-type";import {addToFavoritesCollection,@@ -45,6 +51,31 @@ export function getSelectedFiles(return files.filter((file) => selectedFilesIDs.has(file.id));}+export const fileTimelineDateString = (file: EnteFile) => {+ const date = fileCreationPhotoDate(file);+ return isSameDay(date, new Date())+ ? t("today")+ : isSameDay(date, new Date(Date.now() - 24 * 60 * 60 * 1000))+ ? t("yesterday")+ : formattedDate(date);+};++export const selectedFavoriteCount = (+ selected: SelectedState,+ favoriteFileIDs: Set<number> | undefined,+) => {+ if (!favoriteFileIDs || selected.count == 0) return 0;+ let count = 0;+ for (const [key, value] of Object.entries(selected)) {+ if (typeof value === "boolean" && value) {+ if (favoriteFileIDs.has(Number(key))) {+ count += 1;+ }+ }+ }+ return count;+};+export const performFileOp = async (op: FileOp,files: EnteFile[],