refactor: enhance FindInput and AssetGrid components for better media handling

- Updated FindInput to accept value and onChange props for improved state management.
- Modified AssetGrid to support video assets by integrating Video plugin in Lightbox and adding isVideo property to asset objects.
- Enhanced LazyGridImage to display video duration and a play icon for video assets.
- Introduced humanizeDuration helper function to format video duration for better user experience.
- Updated search API to include download URL for video assets, ensuring consistent asset handling.
This commit is contained in:
Varun Raj committed 2025-04-27 13:56:35 +05:30
1 parent a583c8250c
commit 094a2a6e57
7 files changed
+91 -15

No files matched your search

+6 -6
View File
@@ -6,11 +6,11 @@ import mentionStyle from './findMentionStyle';
interface FindInputProps {
onSearch: (query: string) => void;
value: string;
onChange: (value: string) => void;
}
export default function FindInput({ onSearch }: FindInputProps) {
const [query, setQuery] = useState("");
export default function FindInput({ onSearch, value, onChange }: FindInputProps) {
const handleSearchPeople = async (e: any, callback: any) => {
if (!e.length) return [];
@@ -22,16 +22,16 @@ export default function FindInput({ onSearch }: FindInputProps) {
return (
<MentionsInput
value={query}
value={value}
singleLine={true}
placeholder='Search for photos, use @ to search for people'
style={inputStyle}
onKeyDown={(e) => {
if (e.key === 'Enter') {
onSearch(query);
onSearch(value);
}
}}
onChange={(e) => setQuery(e.target.value)}>
onChange={(e) => onChange(e.target.value)}>
<Mention
trigger="@"
style={mentionStyle}
+3 -1
View File
@@ -6,6 +6,7 @@ import Lightbox from 'yet-another-react-lightbox';
import { Gallery } from "react-grid-gallery";
import LazyGridImage from "../ui/lazy-grid-image";
import Download from "yet-another-react-lightbox/plugins/download";
import Video from "yet-another-react-lightbox/plugins/video";
interface AssetGridProps {
@@ -86,6 +87,7 @@ export default function AssetGrid({ assets, isInternal = true, selectable = fals
height: p.exifImageHeight / 10 as number,
orientation: 1,
isSelected: selectedIds.includes(p.id),
isVideo: p.type === "VIDEO",
}));
}, [assets, selectedIds]);
@@ -106,7 +108,7 @@ export default function AssetGrid({ assets, isInternal = true, selectable = fals
<div>
<Lightbox
slides={slides}
plugins={[Download]}
plugins={[Download, Video]}
open={index >= 0}
index={index}
close={() => setIndex(-1)}
+11 -2
View File
@@ -1,8 +1,11 @@
/* eslint-disable @next/next/no-img-element */
import { humanizeDuration } from '@/helpers/string.helper'
import { PlayIcon } from '@radix-ui/react-icons'
import React, { useEffect } from 'react'
import { Image, ImageExtended, ThumbnailImageProps } from 'react-grid-gallery'
interface LazyImageProps extends ThumbnailImageProps<ImageExtended<Image>> {}
interface LazyImageProps extends ThumbnailImageProps<ImageExtended<Image & { isVideo?: boolean, duration?: string }>> {}
export default function LazyGridImage(
props: LazyImageProps
@@ -39,6 +42,12 @@ export default function LazyGridImage(
)
return (
<img {...props.imageProps} alt={props.imageProps.alt || ""} title="" />
<div className="relative">
<img {...props.imageProps} alt={props.imageProps.alt || ""} title="" />
{props.item.isVideo && <div className="absolute bottom-2 right-2 bg-black/50 p-1 rounded-full flex items-center gap-1">
<PlayIcon className="w-3 h-3 text-white" />
{!!props.item.duration && <span className="text-xs text-white">{humanizeDuration(props.item.duration)}</span>}
</div>}
</div>
)
}
+17 -2
View File
@@ -14,7 +14,14 @@ interface FindQuery {
model?: string;
createdAfter?: string;
state?: string;
type?: string;
}
const allowedTypes = [
'IMAGE',
'VIDEO',
'AUDIO'
]
const responseSchema = {
type: SchemaType.OBJECT,
properties: {
@@ -50,6 +57,10 @@ const responseSchema = {
type: SchemaType.STRING,
description: "Name of the device that the query is about"
},
type: {
type: SchemaType.STRING,
description: "One of the allowed media types (IMAGE, VIDEO, AUDIO, OTHER)",
}
},
required: ["query"],
};
@@ -80,9 +91,13 @@ export const parseFindQuery = async (query: string): Promise<FindQuery> => {
}
});
console.log("response from gemini", response.text());
const parsedResponse = JSON.parse(response.text()) as FindQuery;
if (parsedResponse.type) {
if (!allowedTypes.includes(parsedResponse.type)) {
delete parsedResponse.type;
}
}
const cleanedResponse = {
...parsedResponse
};
+20
View File
@@ -18,3 +18,23 @@ export const humanizeBytes = (bytes: number) => {
}
return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`
}
export const humanizeDuration = (duration: string) => {
if (!duration) {
return null;
}
// Example input : 00:00:04.350
const [hours, minutes, seconds] = duration.split(':').map(Number);
const totalSeconds = Math.round(hours * 3600 + minutes * 60 + seconds);
if (totalSeconds < 60) {
return `${totalSeconds}s`;
}
if (totalSeconds < 3600) {
// Show only minutes
return `${Math.round(minutes)}m`;
}
return `${Math.round(hours)}h ${Math.round(minutes)}m`;
}
+2
View File
@@ -1,5 +1,6 @@
import { db } from '@/config/db';
import { ENV } from '@/config/environment';
import { ASSET_VIDEO_PATH } from '@/config/routes';
import { getCurrentUser } from '@/handlers/serverUtils/user.utils';
import { cleanUpAsset, isFlipped } from '@/helpers/asset.helper';
import { parseFindQuery } from '@/helpers/gemini.helper';
@@ -48,6 +49,7 @@ export default async function search(req: NextApiRequest, res: NextApiResponse)
exifImageHeight: isFlipped(item?.exifInfo?.orientation) ? item?.exifInfo?.exifImageWidth : item?.exifInfo?.exifImageHeight,
exifImageWidth: isFlipped(item?.exifInfo?.orientation) ? item?.exifInfo?.exifImageHeight : item?.exifInfo?.exifImageWidth,
orientation: item?.exifInfo?.orientation,
downloadUrl: ASSET_VIDEO_PATH(item.id)
}
}).map(cleanUpAsset)
res.status(200).json({
+32 -4
View File
@@ -5,7 +5,6 @@ import Loader from '@/components/ui/loader';
import { ASSET_PREVIEW_PATH } from '@/config/routes';
import { useConfig } from '@/contexts/ConfigContext';
import { findAssets } from '@/handlers/api/asset.handler';
import { IAsset } from '@/types/asset';
import { Search, TriangleAlert, WandSparkles } from 'lucide-react';
import React, { useMemo, useState } from 'react'
import AssetGrid from "@/components/shared/AssetGrid";
@@ -18,6 +17,7 @@ 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
import { Button } from '@/components/ui/button'; // Import Button
interface IFindFilters {
[key: string]: string;
@@ -33,6 +33,16 @@ const FILTER_KEY_MAP = {
"model": "Model",
"personIds": "People",
}
// Add suggestion list
const SUGGESTIONS = [
{ label: "Last week's photos", query: "Photos from last week" },
{ label: "Photos from last summer", query: "Photos taken last summer" },
{ label: "Videos from New York", query: "Videos taken in New York" },
{ label: "Photos with @alex", query: "Photos with @alex" }, // Example with person tag
{ label: "Recent screenshots", query: "Screenshots taken recently" },
];
export default function FindPage() {
// Remove local selectedIds and assets state
@@ -198,10 +208,24 @@ export default function FindPage() {
<div className="flex justify-center flex-col gap-2 items-center h-full">
<Search className='w-10 h-10 text-muted-foreground' />
<p className='text-lg font-bold'>Search for photos in natural language</p>
<p className='text-sm text-muted-foreground text-center max-w-lg'> {/* Centered and max-width */}
<p className='text-sm text-muted-foreground text-center max-w-lg'> {/* Centered and max-width */}
Example: <kbd className='bg-zinc-200 text-black dark:text-white px-1 py-0.5 rounded-md dark:bg-zinc-500'>Sunset photos from last week</kbd>. Use <kbd className='bg-zinc-200 text-black dark:text-white px-1 py-0.5 rounded-md dark:bg-zinc-500'>@</kbd> to search for photos of a specific person.
</p>
<p className='text-xs text-muted-foreground text-center flex gap-1 items-center mt-2'> {/* Added margin top */}
{/* Render suggestions */}
<div className="flex flex-wrap gap-2 justify-center mt-4 max-w-lg">
{SUGGESTIONS.map((suggestion) => (
<Button
key={suggestion.query}
variant="outline"
size="sm"
className="text-xs"
onClick={() => handleSearch(suggestion.query)}
>
{suggestion.label}
</Button>
))}
</div>
<p className='text-xs text-muted-foreground text-center flex gap-1 items-center mt-4'> {/* Adjusted margin top */}
<span>Power tools uses Google Gemini only for parsing your query. None of your data is sent to Gemini.</span>
</p>
</div>
@@ -261,7 +285,11 @@ export default function FindPage() {
{geminiEnabled ? (
<>
<div className="flex flex-col gap-4 p-2">
<FindInput onSearch={handleSearch} />
<FindInput
value={query}
onChange={setQuery}
onSearch={handleSearch}
/>
</div>
{/* renderContent now includes the Provider */}
{renderContent()}