diff --git a/src/pages/find/index.tsx b/src/pages/find/index.tsx index 788feec..e59ccf6 100644 --- a/src/pages/find/index.tsx +++ b/src/pages/find/index.tsx @@ -11,6 +11,13 @@ import React, { useMemo, useState } from 'react' import AssetGrid from "@/components/shared/AssetGrid"; import FloatingBar from "@/components/shared/FloatingBar"; import AssetsBulkDeleteButton from "@/components/shared/AssetsBulkDeleteButton"; +// Context Imports +import PhotoSelectionContext, { IPhotoSelectionContext } from '@/contexts/PhotoSelectionContext'; +// Album Imports +import AlbumSelectorDialog from '@/components/albums/AlbumSelectorDialog'; +import { addAssetToAlbum, createAlbum } from '@/handlers/api/album.handler'; +import { IAlbum, IAlbumCreate } from '@/types/album'; +import { useToast } from '@/components/ui/use-toast'; // Import useToast interface IFindFilters { [key: string]: string; @@ -28,22 +35,42 @@ const FILTER_KEY_MAP = { } export default function FindPage() { - const [selectedIds, setSelectedIds] = useState([]); + // Remove local selectedIds and assets state + // const [selectedIds, setSelectedIds] = useState([]); + // const [assets, setAssets] = useState([]); + const { toast } = useToast(); // Initialize toast const { geminiEnabled, exImmichUrl } = useConfig(); const [query, setQuery] = useState(''); - const [assets, setAssets] = useState([]); const [loading, setLoading] = useState(false); const [filters, setFilters] = useState({}); const [error, setError] = useState(null); + // Initialize context state + const [contextState, setContextState] = useState({ + selectedIds: [], + assets: [], + config: {}, + updateContext: (newConfig: Partial) => { + setContextState(prevState => ({ + ...prevState, + ...newConfig, + // No deep merge needed for config here as it's simple + config: newConfig.config ? { ...prevState.config, ...newConfig.config } : prevState.config + })); + } + }); + + const updateContext = contextState.updateContext; + + // Derive slides from contextState.assets const slides = useMemo( () => - assets.map((asset) => ({ + contextState.assets.map((asset) => ({ src: ASSET_PREVIEW_PATH(asset.id), width: 1000, height: 1000, })), - [assets] + [contextState.assets] ); const appliedFilters: { @@ -67,29 +94,74 @@ export default function FindPage() { const handleSearch = (query: string) => { setQuery(query); setLoading(true); + setError(null); // Clear previous errors findAssets(query) .then(({ assets, filters: _filters }) => { - setAssets(assets); + // Update context state with fetched assets + updateContext({ assets: assets, selectedIds: [] }); setFilters(_filters); }) .catch((error: any) => { setError(error.message || error.error || "Failed to fetch assets"); + updateContext({ assets: [], selectedIds: [] }); // Clear assets on error }) .finally(() => { setLoading(false); }); } + // Update handleSelectionChange to use context const handleSelectionChange = (ids: string[]) => { - setSelectedIds(ids); + updateContext({ selectedIds: ids }); } + // Update handleDelete to use context const handleDelete = (ids: string[]) => { - setSelectedIds([]); - setAssets(assets.filter((asset) => !ids.includes(asset.id))); - + const newAssets = contextState.assets.filter((asset) => !ids.includes(asset.id)); + updateContext({ selectedIds: [], assets: newAssets }); } + // --- Album Handling Logic (Adapted from potential-albums.tsx) --- + const handleSelectAlbum = (album: IAlbum) => { + return addAssetToAlbum(album.id, contextState.selectedIds) + .then(() => { + toast({ + title: `Assets added to ${album.albumName}`, + description: `${contextState.selectedIds.length} assets added to album`, + }); + // Optionally clear selection or remove assets from view if needed + // updateContext({ selectedIds: [] }); + }) + .catch(() => { + toast({ + title: "Error", + description: "Failed to add assets album", + variant: "destructive", + }); + }); + }; + + const handleCreateAlbum = (formData: IAlbumCreate) => { + return createAlbum({ + ...formData, + assetIds: contextState.selectedIds, + }).then((newAlbum) => { // Assuming createAlbum returns the new album + toast({ + title: "Album created", + description: `Album "${newAlbum.albumName}" created successfully with ${contextState.selectedIds.length} assets.`, // Provide more detail + }); + // Optionally clear selection or remove assets from view + // updateContext({ selectedIds: [] }); + }).catch(() => { + toast({ + title: "Error creating album", + description: "Failed to create album", + variant: "destructive", + }); + }); + } + // --- End Album Handling Logic --- + const renderFilters = () => { if (appliedFilters.length === 0) return null; return ( @@ -112,7 +184,6 @@ export default function FindPage() { } else if (error) { return ( -

Oops, something went wrong

@@ -127,44 +198,60 @@ export default function FindPage() {

Search for photos in natural language

-

- Example: Sunset photos from last week. Use @ to search for photos of a specific person. +

{/* Centered and max-width */} + Example: Sunset photos from last week. Use @ to search for photos of a specific person.

-

+

{/* Added margin top */} Power tools uses Google Gemini only for parsing your query. None of your data is sent to Gemini.

) } - else if (assets.length === 0) { + // Use contextState.assets for the check + else if (contextState.assets.length === 0) { return

No results found for the below filters

-

- {renderFilters()} -

+ {appliedFilters.length > 0 && // Show filters only if they exist +
{/* Added margin top */} + {renderFilters()} +
+ }
} + // Wrap grid and floating bar with Context Provider return ( - <> - {renderFilters()} -
- - {selectedIds.length > 0 && ( - -

- {selectedIds.length} Selected -

- - -
- )} + +
{/* Added gap */} + {renderFilters()} +
+ {/* Pass context assets and selection handler */} + + {/* Use contextState.selectedIds for FloatingBar condition and props */} + {contextState.selectedIds.length > 0 && ( + +

+ {contextState.selectedIds.length} Selected +

+
{/* Container for buttons */} + {/* Add AlbumSelectorDialog */} + +
{/* Separator */} + +
+
+ )} +
- +
) } @@ -176,6 +263,7 @@ export default function FindPage() {
+ {/* renderContent now includes the Provider */} {renderContent()} ) : (