diff --git a/src/components/assets/duplicate-assets/AlbumTransferDialog.tsx b/src/components/assets/duplicate-assets/AlbumTransferDialog.tsx new file mode 100644 index 0000000..61a87d1 --- /dev/null +++ b/src/components/assets/duplicate-assets/AlbumTransferDialog.tsx @@ -0,0 +1,86 @@ +import React, { useState } from 'react' +import { + Dialog, + DialogContent, + DialogHeader, + DialogTitle, + DialogDescription, + DialogFooter, +} from '@/components/ui/dialog' +import { Button } from '@/components/ui/button' +import { Checkbox } from '@/components/ui/checkbox' +import { FolderOpen } from 'lucide-react' +import { IAssetAlbumInfo } from '@/handlers/api/asset.handler' + +interface AlbumTransferDialogProps { + open: boolean + onClose: () => void + albums: IAssetAlbumInfo[] + onSkip: () => void + onTransfer: (albumIds: string[]) => void +} + +export default function AlbumTransferDialog({ + open, + onClose, + albums, + onSkip, + onTransfer, +}: AlbumTransferDialogProps) { + const [selectedAlbumIds, setSelectedAlbumIds] = useState>( + () => new Set(albums.map((a) => a.albumId)) + ) + + const handleToggle = (albumId: string) => { + setSelectedAlbumIds((prev) => { + const next = new Set(prev) + if (next.has(albumId)) { + next.delete(albumId) + } else { + next.add(albumId) + } + return next + }) + } + + return ( + { if (!isOpen) onClose() }}> + + + Move Album Memberships + + Select which albums to transfer to the kept asset before deleting. + + + +
+ {albums.map((album) => ( + + ))} +
+ + + + + +
+
+ ) +} diff --git a/src/components/assets/duplicate-assets/DuplicateAssetRecord.tsx b/src/components/assets/duplicate-assets/DuplicateAssetRecord.tsx index 758350f..8fa71e4 100644 --- a/src/components/assets/duplicate-assets/DuplicateAssetRecord.tsx +++ b/src/components/assets/duplicate-assets/DuplicateAssetRecord.tsx @@ -5,9 +5,11 @@ import LazyImage from '@/components/ui/lazy-image' import { Checkbox } from '@/components/ui/checkbox' import { Button } from '@/components/ui/button' import { AlertDialog } from '@/components/ui/alert-dialog' +import { Badge } from '@/components/ui/badge' import { humanizeBytes, humanizeNumber } from '@/helpers/string.helper' import { formatDate } from '@/helpers/date.helper' -import { Camera, Calendar, HardDrive, MapPin, Trash2, Check, X, Shield } from 'lucide-react' +import { Camera, Calendar, FolderOpen, HardDrive, MapPin, Trash2, Check, X, Shield } from 'lucide-react' +import { IAssetAlbumInfo } from '@/handlers/api/asset.handler' interface DuplicateAssetRecordProps { record: IDuplicateAssetRecord @@ -17,6 +19,7 @@ interface DuplicateAssetRecordProps { onKeepSelected: (record: IDuplicateAssetRecord, selectedIds: string[], unselectedIds: string[]) => void onKeepAllInRecord: (record: IDuplicateAssetRecord) => void selectionMode: 'keep' | 'discard' + assetAlbums: Record } interface DuplicateAssetItemProps { @@ -24,9 +27,10 @@ interface DuplicateAssetItemProps { isSelected: boolean onSelect: (assetId: string, isShiftClick?: boolean) => void selectionMode: 'keep' | 'discard' + albums: IAssetAlbumInfo[] } -function DuplicateAssetItem({ asset, isSelected, onSelect, selectionMode }: DuplicateAssetItemProps) { +function DuplicateAssetItem({ asset, isSelected, onSelect, selectionMode, albums }: DuplicateAssetItemProps) { const handleCheckboxChange = (event: React.MouseEvent) => { const isShiftClick = event.shiftKey onSelect(asset.id, isShiftClick) @@ -124,6 +128,21 @@ function DuplicateAssetItem({ asset, isSelected, onSelect, selectionMode }: Dupl {asset.exifInfo.make} {asset.exifInfo.model} )} + + {albums.length > 0 && ( +
+ {albums.map((album) => ( + + + {album.albumName} + + ))} +
+ )} @@ -137,7 +156,8 @@ export default function DuplicateAssetRecord({ onDeleteRecord, onKeepSelected, onKeepAllInRecord, - selectionMode + selectionMode, + assetAlbums }: DuplicateAssetRecordProps) { const recordAssetIds = record.assets.map(asset => asset.id) const selectedInRecord = recordAssetIds.filter(id => selectedAssets.has(id)).length @@ -302,6 +322,7 @@ export default function DuplicateAssetRecord({ isSelected={selectedAssets.has(asset.id)} onSelect={onAssetSelect} selectionMode={selectionMode} + albums={assetAlbums[asset.id] || []} /> ))} diff --git a/src/components/assets/duplicate-assets/VirtualizedDuplicateList.tsx b/src/components/assets/duplicate-assets/VirtualizedDuplicateList.tsx index 85b9a15..14e8be4 100644 --- a/src/components/assets/duplicate-assets/VirtualizedDuplicateList.tsx +++ b/src/components/assets/duplicate-assets/VirtualizedDuplicateList.tsx @@ -1,6 +1,7 @@ import React, { useMemo } from 'react' import { VariableSizeList as List } from 'react-window' import { IDuplicateAssetRecord } from '@/types/asset' +import { IAssetAlbumInfo } from '@/handlers/api/asset.handler' import DuplicateAssetRecord from './DuplicateAssetRecord' interface VirtualizedDuplicateListProps { @@ -12,6 +13,7 @@ interface VirtualizedDuplicateListProps { onKeepAllInRecord: (record: IDuplicateAssetRecord) => void height: number selectionMode: 'keep' | 'discard' + assetAlbums: Record } interface ListItemProps { @@ -25,11 +27,12 @@ interface ListItemProps { onKeepSelected: (record: IDuplicateAssetRecord, selectedIds: string[], unselectedIds: string[]) => void onKeepAllInRecord: (record: IDuplicateAssetRecord) => void selectionMode: 'keep' | 'discard' + assetAlbums: Record } } const ListItem: React.FC = ({ index, style, data }) => { - const { duplicates, selectedAssets, onAssetSelect, onDeleteRecord, onKeepSelected, onKeepAllInRecord, selectionMode } = data + const { duplicates, selectedAssets, onAssetSelect, onDeleteRecord, onKeepSelected, onKeepAllInRecord, selectionMode, assetAlbums } = data const record = duplicates[index] return ( @@ -43,6 +46,7 @@ const ListItem: React.FC = ({ index, style, data }) => { onKeepSelected={onKeepSelected} onKeepAllInRecord={onKeepAllInRecord} selectionMode={selectionMode} + assetAlbums={assetAlbums} /> @@ -72,7 +76,8 @@ export default function VirtualizedDuplicateList({ onKeepSelected, onKeepAllInRecord, height, - selectionMode + selectionMode, + assetAlbums }: VirtualizedDuplicateListProps) { // Custom item size getter for VariableSizeList const getItemHeight = (index: number) => { @@ -88,7 +93,8 @@ export default function VirtualizedDuplicateList({ onDeleteRecord, onKeepSelected, onKeepAllInRecord, - selectionMode + selectionMode, + assetAlbums } // For better performance with many items, we'll use a custom implementation diff --git a/src/config/routes.ts b/src/config/routes.ts index bf04144..5011619 100644 --- a/src/config/routes.ts +++ b/src/config/routes.ts @@ -34,6 +34,7 @@ export const DELETE_ALBUMS_PATH = BASE_API_ENDPOINT + "/albums/delete"; export const ADD_ASSETS_TO_ALBUM_PATH = (id: string) => BASE_PROXY_ENDPOINT + "/albums/" + id + "/assets"; export const MERGE_ALBUMS_PATH = BASE_API_ENDPOINT + "/albums/merge"; // Assets +export const ASSET_ALBUMS_BY_ASSETS_PATH = BASE_API_ENDPOINT + "/assets/albums-by-assets"; export const LIST_MISSING_LOCATION_DATES_PATH = BASE_API_ENDPOINT + "/assets/missing-location-dates"; export const LIST_MISSING_LOCATION_ALBUMS_PATH = BASE_API_ENDPOINT + "/assets/missing-location-albums"; export const LIST_MISSING_LOCATION_ASSETS_PATH = BASE_API_ENDPOINT + "/assets/missing-location-assets"; diff --git a/src/handlers/api/asset.handler.ts b/src/handlers/api/asset.handler.ts index 82822c5..4549f06 100644 --- a/src/handlers/api/asset.handler.ts +++ b/src/handlers/api/asset.handler.ts @@ -1,4 +1,5 @@ import { + ASSET_ALBUMS_BY_ASSETS_PATH, ASSET_GEO_HEATMAP_PATH, LIST_EMPTY_VIDEOS_PATH, FIND_ASSETS, @@ -90,4 +91,13 @@ export const listEmptyVideos = async (filters: IEmptyVideosParams) => { export const listDuplicates = async () => { return API.get(LIST_DUPLICATES_PATH); -} \ No newline at end of file +} + +export interface IAssetAlbumInfo { + albumId: string; + albumName: string; +} + +export const getAlbumsByAssetIds = async (assetIds: string[]): Promise> => { + return API.post(ASSET_ALBUMS_BY_ASSETS_PATH, { assetIds }); +} diff --git a/src/pages/api/assets/albums-by-assets.ts b/src/pages/api/assets/albums-by-assets.ts new file mode 100644 index 0000000..3573a9a --- /dev/null +++ b/src/pages/api/assets/albums-by-assets.ts @@ -0,0 +1,50 @@ +import { NextApiRequest, NextApiResponse } from "next"; +import { db } from "@/config/db"; +import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; +import { albums } from "@/schema/albums.schema"; +import { albumsAssetsAssets } from "@/schema/albumAssetsAssets.schema"; +import { eq, inArray, and } from "drizzle-orm"; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + if (req.method !== "POST") { + return res.status(405).json({ error: "Method not allowed" }); + } + + const currentUser = await getCurrentUser(req); + if (!currentUser) { + return res.status(401).json({ error: "Unauthorized" }); + } + + const { assetIds } = req.body as { assetIds: string[] }; + if (!assetIds || assetIds.length === 0) { + return res.status(200).json({}); + } + + const rows = await db + .select({ + assetId: albumsAssetsAssets.assetId, + albumId: albums.id, + albumName: albums.albumName, + }) + .from(albumsAssetsAssets) + .innerJoin(albums, eq(albumsAssetsAssets.albumId, albums.id)) + .where( + and( + inArray(albumsAssetsAssets.assetId, assetIds), + eq(albums.ownerId, currentUser.id) + ) + ); + + const result: Record> = {}; + for (const row of rows) { + if (!result[row.assetId]) { + result[row.assetId] = []; + } + result[row.assetId].push({ albumId: row.albumId, albumName: row.albumName }); + } + + return res.status(200).json(result); +} diff --git a/src/pages/assets/bulk-duplicate-finder.tsx b/src/pages/assets/bulk-duplicate-finder.tsx index f496903..df4db1e 100644 --- a/src/pages/assets/bulk-duplicate-finder.tsx +++ b/src/pages/assets/bulk-duplicate-finder.tsx @@ -1,12 +1,14 @@ -import { listDuplicates, deleteAssets, updateAssets } from "@/handlers/api/asset.handler"; +import { listDuplicates, deleteAssets, updateAssets, getAlbumsByAssetIds, IAssetAlbumInfo } from "@/handlers/api/asset.handler"; +import { addAssetToAlbum } from "@/handlers/api/album.handler"; import { IDuplicateAssetRecord } from "@/types/asset"; import React, { useCallback, useEffect, useMemo, useState, useRef } from 'react' import PageLayout from '@/components/layouts/PageLayout' import Header from '@/components/shared/Header' import VirtualizedDuplicateList from '@/components/assets/duplicate-assets/VirtualizedDuplicateList' +import AlbumTransferDialog from '@/components/assets/duplicate-assets/AlbumTransferDialog' import Loader from '@/components/ui/loader' import { Button } from '@/components/ui/button' -import { RefreshCw, Search, Shield, Trash2, Zap } from 'lucide-react' +import { FolderSync, RefreshCw, Search, Shield, Trash2, Zap } from 'lucide-react' import FloatingBar from '@/components/shared/FloatingBar' import { AlertDialog } from '@/components/ui/alert-dialog' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' @@ -14,6 +16,14 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@ import { toast } from '@/components/ui/use-toast' import { humanizeBytes } from '@/helpers/string.helper' +type AlbumTransferMode = 'always' | 'never' | 'ask'; + +interface PendingDedup { + keptIds: string[]; + discardedIds: string[]; + albumsToTransfer: IAssetAlbumInfo[]; +} + export default function BulkDuplicatePage() { const [duplicates, setDuplicates] = useState([]); const [loading, setLoading] = useState(false); @@ -23,8 +33,24 @@ export default function BulkDuplicatePage() { const [lastSelectedIndex, setLastSelectedIndex] = useState(-1); const [containerHeight, setContainerHeight] = useState(600); const [selectionMode, setSelectionMode] = useState<'keep' | 'discard'>('keep'); + const [assetAlbums, setAssetAlbums] = useState>({}); + const [albumTransferMode, setAlbumTransferMode] = useState('always'); + const [pendingDedup, setPendingDedup] = useState(null); const containerRef = useRef(null); + // Load album move mode from localStorage + useEffect(() => { + const saved = localStorage.getItem('duplicateFinderAlbumTransfer'); + if (saved === 'always' || saved === 'never' || saved === 'ask') { + setAlbumTransferMode(saved); + } + }, []); + + const handleAlbumTransferModeChange = (mode: AlbumTransferMode) => { + setAlbumTransferMode(mode); + localStorage.setItem('duplicateFinderAlbumTransfer', mode); + }; + const fetchDuplicates = async () => { setLoading(true); setError(null); @@ -32,6 +58,15 @@ export default function BulkDuplicatePage() { const duplicates = await listDuplicates(); setDuplicates(duplicates); setSelectedAssets(new Set()); // Clear selection when refetching + + // Fetch album data for all assets + const allIds = duplicates.flatMap((r: IDuplicateAssetRecord) => r.assets.map(a => a.id)); + if (allIds.length > 0) { + const albums = await getAlbumsByAssetIds(allIds); + setAssetAlbums(albums); + } else { + setAssetAlbums({}); + } } catch (error: any) { setError(error.message || 'Failed to load duplicate assets'); toast({ @@ -259,13 +294,39 @@ export default function BulkDuplicatePage() { }); }; - // Core dedup execution: mark kept assets as non-duplicate, delete discarded assets + // Compute albums that are only on discarded assets (not already on kept assets) + const getAlbumsToTransfer = useCallback((keptIds: string[], discardedIds: string[]): IAssetAlbumInfo[] => { + const keptAlbumIds = new Set(); + keptIds.forEach(id => { + (assetAlbums[id] || []).forEach(a => keptAlbumIds.add(a.albumId)); + }); + + const seen = new Set(); + const result: IAssetAlbumInfo[] = []; + discardedIds.forEach(id => { + (assetAlbums[id] || []).forEach(a => { + if (!keptAlbumIds.has(a.albumId) && !seen.has(a.albumId)) { + seen.add(a.albumId); + result.push(a); + } + }); + }); + return result; + }, [assetAlbums]); + + // Core dedup execution: move albums then delete const executeDedup = async ( keptIds: string[], - discardedIds: string[] + discardedIds: string[], + albumIdsToTransfer: string[] ) => { setIsDeleting(true); try { + // Move albums to kept assets + await Promise.all( + albumIdsToTransfer.map(albumId => addAssetToAlbum(albumId, keptIds)) + ); + // Mark kept assets as non-duplicate if (keptIds.length > 0) { await updateAssets({ ids: keptIds, duplicateId: null }); @@ -297,9 +358,13 @@ export default function BulkDuplicatePage() { }); setLastSelectedIndex(-1); + const transferMsg = albumIdsToTransfer.length > 0 + ? ` Moved ${albumIdsToTransfer.length} album(s).` + : ''; + toast({ title: "Success", - description: `Kept ${keptIds.length} asset(s), deleted ${discardedIds.length}.${discardedSize > 0 ? ` Saved ${humanizeBytes(discardedSize)}.` : ''}`, + description: `Kept ${keptIds.length} asset(s), deleted ${discardedIds.length}.${transferMsg}${discardedSize > 0 ? ` Saved ${humanizeBytes(discardedSize)}.` : ''}`, }); } catch (error: any) { toast({ @@ -312,6 +377,26 @@ export default function BulkDuplicatePage() { } }; + // Core dedup execution wrapped in useCallback to avoid stale closures + const executeDedupCb = useCallback(executeDedup, []); + + // Initiate dedup with album move logic + const initiateDedup = useCallback(( + keptIds: string[], + discardedIds: string[] + ) => { + const albumsToTransfer = getAlbumsToTransfer(keptIds, discardedIds); + + if (albumsToTransfer.length === 0 || albumTransferMode === 'never') { + executeDedupCb(keptIds, discardedIds, []); + } else if (albumTransferMode === 'always') { + executeDedupCb(keptIds, discardedIds, albumsToTransfer.map(a => a.albumId)); + } else { + // 'ask' mode + setPendingDedup({ keptIds, discardedIds, albumsToTransfer }); + } + }, [albumTransferMode, getAlbumsToTransfer, executeDedupCb]); + const handleDeleteAllSelected = async () => { if (selectedAssets.size === 0 || selectionMode !== 'discard') return; @@ -327,11 +412,11 @@ export default function BulkDuplicatePage() { }); }); - executeDedup(keptIds, selectedAssetIds); + initiateDedup(keptIds, selectedAssetIds); }; const handleKeepSelected = async (_record: IDuplicateAssetRecord, keptIds: string[], discardedIds: string[]) => { - executeDedup(keptIds, discardedIds); + initiateDedup(keptIds, discardedIds); }; @@ -377,7 +462,41 @@ export default function BulkDuplicatePage() { - + + {/* Album Move Mode Button Group */} +
+ + + Move Albums: + +
+ + + +
+
+ {/* Refresh Button */}