fix: design and performance fixes in album list

Signed-off-by: Varun Raj <varun@skcript.com>
This commit is contained in:
Varun Raj committed 2025-08-23 17:41:45 +05:30
1 parent 53fa52cfd7
commit c64959f6d6
16 files changed
+795 -29

No files matched your search

@@ -0,0 +1,87 @@
import React, { useRef } from 'react';
import { IAsset } from '@/types/asset';
import VirtualizedAssetGrid from './VirtualizedAssetGrid';
import ResponsiveVirtualizedAssetGrid from './ResponsiveVirtualizedAssetGrid';
import { Button } from '@/components/ui/button';
interface AssetGridExampleProps {
assets: IAsset[];
onSelectionChange?: (ids: string[]) => void;
}
export default function AssetGridExample({ assets, onSelectionChange }: AssetGridExampleProps) {
const gridRef = useRef<any>(null);
const handleSelectAll = () => {
gridRef.current?.selectAll();
};
const handleUnselectAll = () => {
gridRef.current?.unselectAll();
};
return (
<div className="space-y-4">
<div className="flex gap-2">
<Button onClick={handleSelectAll} variant="outline" size="sm">
Select All
</Button>
<Button onClick={handleUnselectAll} variant="outline" size="sm">
Unselect All
</Button>
</div>
{/* Example 1: Basic Virtualized Grid with fixed dimensions */}
<div className="border rounded-lg p-4">
<h3 className="text-lg font-semibold mb-2">Fixed Size Virtualized Grid</h3>
<div style={{ height: '600px', width: '100%' }}>
<VirtualizedAssetGrid
ref={gridRef}
assets={assets}
selectable={true}
onSelectionChange={onSelectionChange}
columnCount={5}
itemSize={200}
height={600}
/>
</div>
</div>
{/* Example 2: Responsive Virtualized Grid */}
<div className="border rounded-lg p-4">
<h3 className="text-lg font-semibold mb-2">Responsive Virtualized Grid</h3>
<div style={{ height: '600px', width: '100%' }}>
<ResponsiveVirtualizedAssetGrid
ref={gridRef}
assets={assets}
selectable={true}
onSelectionChange={onSelectionChange}
targetItemSize={200}
gap={8}
minColumns={2}
maxColumns={8}
className="w-full h-full"
/>
</div>
</div>
{/* Example 3: Responsive Grid with different target sizes */}
<div className="border rounded-lg p-4">
<h3 className="text-lg font-semibold mb-2">Small Items Virtualized Grid</h3>
<div style={{ height: '400px', width: '100%' }}>
<ResponsiveVirtualizedAssetGrid
ref={gridRef}
assets={assets}
selectable={true}
onSelectionChange={onSelectionChange}
targetItemSize={120}
gap={4}
minColumns={3}
maxColumns={12}
className="w-full h-full"
/>
</div>
</div>
</div>
);
}
+1 -1
View File
@@ -8,7 +8,7 @@ interface FloatingBarProps {
export default function FloatingBar({ children, className }: FloatingBarProps) {
return (
<div className={clsx('absolute bottom-10 left-0 right-0 max-w-2xl mx-auto py-2 px-2 rounded-lg flex justify-between items-center bg-white dark:bg-zinc-900 border-t border-zinc-200 dark:border-zinc-800 border shadow-lg', className)}>
<div className={clsx('fixed bottom-10 left-0 right-0 max-w-2xl mx-auto py-2 px-2 rounded-lg flex justify-between items-center bg-white dark:bg-zinc-900 border-t border-zinc-200 dark:border-zinc-800 border shadow-lg z-50', className)}>
{children}
</div>
)
@@ -0,0 +1,254 @@
import "yet-another-react-lightbox/styles.css";
import { IAsset } from '@/types/asset';
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useState, useCallback, useRef } from 'react'
import Lightbox from 'yet-another-react-lightbox';
import Download from "yet-another-react-lightbox/plugins/download";
import Video from "yet-another-react-lightbox/plugins/video";
import { usePhotoSelectionContext } from '@/contexts/PhotoSelectionContext';
import { FixedSizeGrid as Grid } from 'react-window';
import LazyImage from '../ui/lazy-image';
import { humanizeDuration } from '@/helpers/string.helper';
import { PlayIcon } from '@radix-ui/react-icons';
import { useGridDimensions } from '@/hooks';
interface ResponsiveVirtualizedAssetGridProps {
assets: IAsset[];
isInternal?: boolean;
selectable?: boolean;
onSelectionChange?: (ids: string[]) => void;
targetItemSize?: number;
gap?: number;
minColumns?: number;
maxColumns?: number;
className?: string;
}
interface ResponsiveVirtualizedAssetGridRef {
getSelectedIds: () => string[];
selectAll: () => void;
unselectAll: () => void;
}
const ResponsiveVirtualizedAssetGrid = forwardRef<ResponsiveVirtualizedAssetGridRef, ResponsiveVirtualizedAssetGridProps>(({
assets,
isInternal = true,
selectable = false,
onSelectionChange,
targetItemSize = 200,
gap = 8,
minColumns = 2,
maxColumns = 8,
className = ""
}, ref) => {
const [index, setIndex] = useState(-1);
const [lastSelectedIndex, setLastSelectedIndex] = useState(-1);
const containerRef = useRef<HTMLDivElement>(null);
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 });
// Use context for selection state
const { selectedIds, updateContext } = usePhotoSelectionContext();
// Calculate grid dimensions
const gridDimensions = useGridDimensions({
containerWidth: containerSize.width,
containerHeight: containerSize.height,
itemSize: targetItemSize,
gap,
minColumns,
maxColumns
});
// Update row count when assets change
useEffect(() => {
gridDimensions.updateRowCount(assets.length);
}, [assets.length, gridDimensions]);
// Resize observer to handle container size changes
useEffect(() => {
if (!containerRef.current) return;
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
const { width, height } = entry.contentRect;
setContainerSize({ width, height });
}
});
resizeObserver.observe(containerRef.current);
return () => {
resizeObserver.disconnect();
};
}, []);
useImperativeHandle(ref, () => ({
getSelectedIds: () => selectedIds,
selectAll: () => {
const allIds = assets.map((asset) => asset.id);
updateContext({ selectedIds: allIds });
onSelectionChange?.(allIds);
},
unselectAll: () => {
updateContext({ selectedIds: [] });
onSelectionChange?.([]);
},
}), [assets, selectedIds, updateContext]);
const slides = useMemo(() => {
return assets.map((asset) => ({
...asset,
orientation: 1,
src: asset.previewUrl as string,
type: (asset.type === "VIDEO" ? "video" : "image") as any,
sources:
asset.type === "VIDEO"
? [
{
src: asset.downloadUrl as string,
type: "video/mp4",
},
]
: undefined,
height: asset.exifImageHeight as number,
width: asset.exifImageWidth as number,
downloadUrl: asset.downloadUrl as string,
}));
}, [assets]);
const handleClick = useCallback((index: number, asset: IAsset, event: React.MouseEvent<HTMLElement>) => {
if (selectedIds.length > 0) {
handleSelect(index, asset, event);
} else {
setIndex(index);
}
}, [selectedIds]);
const handleSelect = useCallback((_idx: number, asset: IAsset, event: React.MouseEvent<HTMLElement>) => {
event.stopPropagation();
const isPresent = selectedIds.includes(asset.id);
if (isPresent) {
const newSelectedIds = selectedIds.filter((id) => id !== asset.id);
updateContext({ selectedIds: newSelectedIds });
onSelectionChange?.(newSelectedIds);
} else {
const clickedIndex = assets.findIndex((a) => a.id === asset.id);
if (event.shiftKey) {
const startIndex = Math.min(clickedIndex, lastSelectedIndex);
const endIndex = Math.max(clickedIndex, lastSelectedIndex);
const rangeSelectedIds = assets.slice(startIndex, endIndex + 1).map((a) => a.id);
const allSelectedIds = [...selectedIds, ...rangeSelectedIds];
const uniqueSelectedIds = [...new Set(allSelectedIds)];
updateContext({ selectedIds: uniqueSelectedIds });
onSelectionChange?.(uniqueSelectedIds);
} else {
const newSelectedIds = [...selectedIds, asset.id];
updateContext({ selectedIds: newSelectedIds });
onSelectionChange?.(newSelectedIds);
}
setLastSelectedIndex(clickedIndex);
}
}, [selectedIds, assets, lastSelectedIndex, updateContext, onSelectionChange]);
const handleEsc = useCallback((event: KeyboardEvent) => {
if (event.key === "Escape") {
updateContext({ selectedIds: [] });
onSelectionChange?.([]);
}
}, [updateContext, onSelectionChange]);
useEffect(() => {
window.addEventListener("keydown", handleEsc);
return () => window.removeEventListener("keydown", handleEsc);
}, [handleEsc]);
// Cell renderer for React Window
const Cell = useCallback(({ columnIndex, rowIndex, style }: { columnIndex: number; rowIndex: number; style: React.CSSProperties }) => {
const assetIndex = rowIndex * gridDimensions.columnCount + columnIndex;
const asset = assets[assetIndex];
if (!asset) {
return <div style={style} />;
}
const isSelected = selectedIds.includes(asset.id);
return (
<div
style={{
...style,
padding: `${gap / 2}px`,
}}
className={`relative ${isSelected ? 'ring-2 ring-blue-500 rounded-lg' : ''}`}
onClick={(e) => handleClick(assetIndex, asset, e)}
>
<div className="relative w-full h-full rounded-lg overflow-hidden">
<LazyImage
src={asset.url as string}
alt={asset.originalFileName || ''}
style={{
width: '100%',
height: '100%',
objectFit: 'cover',
}}
/>
{asset.type === "VIDEO" && (
<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" />
{asset.duration && (
<span className="text-xs text-white">
{humanizeDuration(asset.duration)}
</span>
)}
</div>
)}
{selectable && (
<div
className={`absolute top-2 left-2 w-4 h-4 rounded-full border-2 ${
isSelected
? 'bg-blue-500 border-blue-500'
: 'bg-white/80 border-gray-300'
}`}
/>
)}
</div>
</div>
);
}, [assets, selectedIds, gridDimensions.columnCount, handleClick, selectable, gap]);
// Don't render grid until we have container dimensions
if (containerSize.width === 0 || gridDimensions.rowCount === 0) {
return (
<div ref={containerRef} className={`w-full h-full ${className}`}>
<div className="flex items-center justify-center h-full">
<div className="text-gray-500">Loading...</div>
</div>
</div>
);
}
return (
<div ref={containerRef} className={`w-full h-full ${className}`}>
<Lightbox
slides={slides}
plugins={[Download, Video]}
open={index >= 0}
index={index}
close={() => setIndex(-1)}
/>
<Grid
columnCount={gridDimensions.columnCount}
columnWidth={gridDimensions.itemSize}
height={containerSize.height}
rowCount={gridDimensions.rowCount}
rowHeight={gridDimensions.itemSize}
width={containerSize.width}
>
{Cell}
</Grid>
</div>
);
});
ResponsiveVirtualizedAssetGrid.displayName = "ResponsiveVirtualizedAssetGrid";
export default ResponsiveVirtualizedAssetGrid;
@@ -0,0 +1,205 @@
import "yet-another-react-lightbox/styles.css";
import { IAsset } from '@/types/asset';
import React, { forwardRef, useEffect, useImperativeHandle, useMemo, useState, useCallback } from 'react'
import Lightbox from 'yet-another-react-lightbox';
import Download from "yet-another-react-lightbox/plugins/download";
import Video from "yet-another-react-lightbox/plugins/video";
import { usePhotoSelectionContext } from '@/contexts/PhotoSelectionContext';
import { FixedSizeGrid as Grid } from 'react-window';
import LazyImage from '../ui/lazy-image';
import { humanizeDuration } from '@/helpers/string.helper';
import { PlayIcon } from '@radix-ui/react-icons';
interface VirtualizedAssetGridProps {
assets: IAsset[];
isInternal?: boolean;
selectable?: boolean;
onSelectionChange?: (ids: string[]) => void;
columnCount?: number;
itemSize?: number;
height?: number;
}
interface VirtualizedAssetGridRef {
getSelectedIds: () => string[];
selectAll: () => void;
unselectAll: () => void;
}
const VirtualizedAssetGrid = forwardRef<VirtualizedAssetGridRef, VirtualizedAssetGridProps>(({
assets,
isInternal = true,
selectable = false,
onSelectionChange,
columnCount = 5,
itemSize = 200,
height = 600
}, ref) => {
const [index, setIndex] = useState(-1);
const [lastSelectedIndex, setLastSelectedIndex] = useState(-1);
// Use context for selection state
const { selectedIds, updateContext } = usePhotoSelectionContext();
useImperativeHandle(ref, () => ({
getSelectedIds: () => selectedIds,
selectAll: () => {
const allIds = assets.map((asset) => asset.id);
updateContext({ selectedIds: allIds });
onSelectionChange?.(allIds);
},
unselectAll: () => {
updateContext({ selectedIds: [] });
onSelectionChange?.([]);
},
}), [assets, selectedIds, updateContext]);
const slides = useMemo(() => {
return assets.map((asset) => ({
...asset,
orientation: 1,
src: asset.previewUrl as string,
type: (asset.type === "VIDEO" ? "video" : "image") as any,
sources:
asset.type === "VIDEO"
? [
{
src: asset.downloadUrl as string,
type: "video/mp4",
},
]
: undefined,
height: asset.exifImageHeight as number,
width: asset.exifImageWidth as number,
downloadUrl: asset.downloadUrl as string,
}));
}, [assets]);
const handleClick = useCallback((index: number, asset: IAsset, event: React.MouseEvent<HTMLElement>) => {
if (selectedIds.length > 0) {
handleSelect(index, asset, event);
} else {
setIndex(index);
}
}, [selectedIds]);
const handleSelect = useCallback((_idx: number, asset: IAsset, event: React.MouseEvent<HTMLElement>) => {
event.stopPropagation();
const isPresent = selectedIds.includes(asset.id);
if (isPresent) {
const newSelectedIds = selectedIds.filter((id) => id !== asset.id);
updateContext({ selectedIds: newSelectedIds });
onSelectionChange?.(newSelectedIds);
} else {
const clickedIndex = assets.findIndex((a) => a.id === asset.id);
if (event.shiftKey) {
const startIndex = Math.min(clickedIndex, lastSelectedIndex);
const endIndex = Math.max(clickedIndex, lastSelectedIndex);
const rangeSelectedIds = assets.slice(startIndex, endIndex + 1).map((a) => a.id);
const allSelectedIds = [...selectedIds, ...rangeSelectedIds];
const uniqueSelectedIds = [...new Set(allSelectedIds)];
updateContext({ selectedIds: uniqueSelectedIds });
onSelectionChange?.(uniqueSelectedIds);
} else {
const newSelectedIds = [...selectedIds, asset.id];
updateContext({ selectedIds: newSelectedIds });
onSelectionChange?.(newSelectedIds);
}
setLastSelectedIndex(clickedIndex);
}
}, [selectedIds, assets, lastSelectedIndex, updateContext, onSelectionChange]);
const handleEsc = useCallback((event: KeyboardEvent) => {
if (event.key === "Escape") {
updateContext({ selectedIds: [] });
onSelectionChange?.([]);
}
}, [updateContext, onSelectionChange]);
useEffect(() => {
window.addEventListener("keydown", handleEsc);
return () => window.removeEventListener("keydown", handleEsc);
}, [handleEsc]);
// Calculate grid dimensions
const rowCount = Math.ceil(assets.length / columnCount);
const width = columnCount * itemSize;
// Cell renderer for React Window
const Cell = useCallback(({ columnIndex, rowIndex, style }: { columnIndex: number; rowIndex: number; style: React.CSSProperties }) => {
const assetIndex = rowIndex * columnCount + columnIndex;
const asset = assets[assetIndex];
if (!asset) {
return <div style={style} />;
}
const isSelected = selectedIds.includes(asset.id);
return (
<div
style={style}
className={`relative p-1 ${isSelected ? 'ring-2 ring-blue-500' : ''}`}
onClick={(e) => handleClick(assetIndex, asset, e)}
>
<div className="relative w-full h-full">
<LazyImage
src={asset.url as string}
alt={asset.originalFileName || ''}
style={{
width: '100%',
height: '100%',
objectFit: 'cover',
borderRadius: '4px',
}}
/>
{asset.type === "VIDEO" && (
<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" />
{asset.duration && (
<span className="text-xs text-white">
{humanizeDuration(asset.duration)}
</span>
)}
</div>
)}
{selectable && (
<div
className={`absolute top-2 left-2 w-4 h-4 rounded-full border-2 ${
isSelected
? 'bg-blue-500 border-blue-500'
: 'bg-white/80 border-gray-300'
}`}
/>
)}
</div>
</div>
);
}, [assets, selectedIds, columnCount, handleClick, selectable]);
return (
<div>
<Lightbox
slides={slides}
plugins={[Download, Video]}
open={index >= 0}
index={index}
close={() => setIndex(-1)}
/>
<Grid
columnCount={columnCount}
columnWidth={itemSize}
height={height}
rowCount={rowCount}
rowHeight={itemSize}
width={width}
>
{Cell}
</Grid>
</div>
);
});
VirtualizedAssetGrid.displayName = "VirtualizedAssetGrid";
export default VirtualizedAssetGrid;