feat: minimum assets filter in potential albums list

Signed-off-by: Varun Raj <varun@skcript.com>
This commit is contained in:
Varun Raj committed 2025-01-26 17:32:14 +05:30
1 parent 0715257048
commit f17356252d
10 files changed
+186 -144

No files matched your search

@@ -12,7 +12,7 @@ import { ArrowDown, ArrowUp, ArrowUpDownIcon, SortAsc, SortDesc } from "lucide-r
export default function PotentialAlbumsDates() { export default function PotentialAlbumsDates() {
const router = useRouter(); const router = useRouter();
const { updateContext } = usePotentialAlbumContext(); const { updateContext, minAssets } = usePotentialAlbumContext();
const [dateRecords, setDateRecords] = React.useState< const [dateRecords, setDateRecords] = React.useState<
IPotentialAlbumsDatesResponse[] IPotentialAlbumsDatesResponse[]
>([]); >([]);
@@ -26,6 +26,7 @@ export default function PotentialAlbumsDates() {
return listPotentialAlbumsDates({ return listPotentialAlbumsDates({
sortBy: filters.sortBy, sortBy: filters.sortBy,
sortOrder: filters.sortOrder, sortOrder: filters.sortOrder,
minAssets,
}) })
.then(setDateRecords) .then(setDateRecords)
.catch(setErrorMessage) .catch(setErrorMessage)
@@ -43,7 +44,7 @@ export default function PotentialAlbumsDates() {
useEffect(() => { useEffect(() => {
fetchData(); fetchData();
}, [filters]); }, [filters, minAssets]);
return ( return (
<div className="min-w-[200px] py-4 max-h-[calc(100vh-60px)] min-h-[calc(100vh-60px)] border-r border-gray-200 dark:border-zinc-800 flex flex-col gap-2 px-1"> <div className="min-w-[200px] py-4 max-h-[calc(100vh-60px)] min-h-[calc(100vh-60px)] border-r border-gray-200 dark:border-zinc-800 flex flex-col gap-2 px-1">
@@ -36,7 +36,7 @@ export default function MissingLocationDateItem({ record, onSelect, groupBy }: I
} }
> >
<p className="text-sm truncate">{dateLabel}</p> <p className="text-sm truncate">{dateLabel}</p>
<p className="text-xs text-foreground/50">{record.asset_count} Orphan Assets</p> <p className="text-xs text-foreground/50">{record.asset_count} Assets</p>
</div> </div>
); );
} }
+17 -8
View File
@@ -20,6 +20,15 @@ export default function AssetGrid({ assets, isInternal = true, selectable = fals
const [selectedIds, setSelectedIds] = useState<string[]>([]); const [selectedIds, setSelectedIds] = useState<string[]>([]);
const [lastSelectedIndex, setLastSelectedIndex] = useState(-1); const [lastSelectedIndex, setLastSelectedIndex] = useState(-1);
const handleClick = (index: number, asset: IAsset, event: React.MouseEvent<HTMLElement>) => {
if (selectedIds.length > 0) {
handleSelect(index, asset, event);
} else {
setIndex(index);
}
}
const handleSelect = (_idx: number, asset: IAsset, event: React.MouseEvent<HTMLElement>) => { const handleSelect = (_idx: number, asset: IAsset, event: React.MouseEvent<HTMLElement>) => {
event.stopPropagation(); event.stopPropagation();
@@ -55,13 +64,13 @@ export default function AssetGrid({ assets, isInternal = true, selectable = fals
type: (asset.type === "VIDEO" ? "video" : "image") as any, type: (asset.type === "VIDEO" ? "video" : "image") as any,
sources: sources:
asset.type === "VIDEO" asset.type === "VIDEO"
? [ ? [
{ {
src: asset.downloadUrl as string, src: asset.downloadUrl as string,
type: "video/mp4", type: "video/mp4",
}, },
] ]
: undefined, : undefined,
height: asset.exifImageHeight as number, height: asset.exifImageHeight as number,
width: asset.exifImageWidth as number, width: asset.exifImageWidth as number,
downloadUrl: asset.downloadUrl as string, downloadUrl: asset.downloadUrl as string,
@@ -104,7 +113,7 @@ export default function AssetGrid({ assets, isInternal = true, selectable = fals
/> />
<Gallery <Gallery
images={images} images={images}
onClick={setIndex} onClick={handleClick}
enableImageSelection={selectable} enableImageSelection={selectable}
thumbnailImageComponent={LazyGridImage} thumbnailImageComponent={LazyGridImage}
onSelect={handleSelect} onSelect={handleSelect}
+14 -5
View File
@@ -5,6 +5,7 @@ import { useConfig } from "@/contexts/ConfigContext";
import { logoutUser } from "@/handlers/api/user.handler" import { logoutUser } from "@/handlers/api/user.handler"
import { useCurrentUser } from "@/contexts/CurrentUserContext"; import { useCurrentUser } from "@/contexts/CurrentUserContext";
import { useToast } from "../ui/use-toast"; import { useToast } from "../ui/use-toast";
import { HandshakeIcon } from "lucide-react";
export default function ProfileInfo() { export default function ProfileInfo() {
const { updateContext, ...user } = useCurrentUser() const { updateContext, ...user } = useCurrentUser()
@@ -36,11 +37,19 @@ export default function ProfileInfo() {
{user && ( {user && (
<> <>
<p className="text-sm">{user?.name}</p> <p className="text-sm">{user?.name}</p>
{!user.isUsingAPIKey && ( <div className="flex items-center justify-between gap-2 px-2">
<Button variant="destructive" className="mx-4 h-6 text-xs" onClick={handleLogout}> {!user.isUsingAPIKey && (
Log Out <Button variant="destructive" className="h-6 flex-1 text-xs" onClick={handleLogout}>
</Button> Log Out
)} </Button>
)}
<Button asChild variant="secondary" className="h-6 flex-1 text-xs flex items-center justify-center gap-2" onClick={handleLogout}>
<Link href="https://buymeacoffee.com/varunraj" target="_blank">
<HandshakeIcon className="w-4 h-4" />
Support
</Link>
</Button>
</div>
</> </>
)} )}
</div> </div>
+2
View File
@@ -5,6 +5,7 @@ export interface IPotentialAlbumConfig {
startDate?: string; startDate?: string;
selectedIds: string[]; selectedIds: string[];
assets: IAsset[]; assets: IAsset[];
minAssets?: number;
} }
export interface IPotentialAlbumContext extends IPotentialAlbumConfig { export interface IPotentialAlbumContext extends IPotentialAlbumConfig {
@@ -14,6 +15,7 @@ const PotentialAlbumContext = createContext<IPotentialAlbumContext>({
startDate: undefined, startDate: undefined,
selectedIds: [], selectedIds: [],
assets: [], assets: [],
minAssets: 1,
updateContext: () => { }, updateContext: () => { },
}); });
+1
View File
@@ -9,6 +9,7 @@ interface IPotentialAlbumsDatesFilters {
endDate?: string; endDate?: string;
sortBy?: string; sortBy?: string;
sortOrder?: string; sortOrder?: string;
minAssets?: number;
} }
export interface IPotentialAlbumsDatesResponse { export interface IPotentialAlbumsDatesResponse {
date: string; date: string;
+63 -48
View File
@@ -8,6 +8,7 @@ import Header from "@/components/shared/Header";
import { AlertDialog } from "@/components/ui/alert-dialog"; import { AlertDialog } from "@/components/ui/alert-dialog";
import { Badge } from "@/components/ui/badge"; import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useToast } from "@/components/ui/use-toast"; import { useToast } from "@/components/ui/use-toast";
import { useCurrentUser } from "@/contexts/CurrentUserContext"; import { useCurrentUser } from "@/contexts/CurrentUserContext";
import PotentialAlbumContext, { import PotentialAlbumContext, {
@@ -29,6 +30,7 @@ export default function PotentialAlbums() {
startDate: startDate || undefined, startDate: startDate || undefined,
selectedIds: [], selectedIds: [],
assets: [], assets: [],
minAssets: 1,
}); });
const selectedAssets = useMemo(() => config.assets.filter((a) => config.selectedIds.includes(a.id)), [config.assets, config.selectedIds]); const selectedAssets = useMemo(() => config.assets.filter((a) => config.selectedIds.includes(a.id)), [config.assets, config.selectedIds]);
@@ -108,6 +110,19 @@ export default function PotentialAlbums() {
<PageLayout className="!p-0 !mb-0 relative"> <PageLayout className="!p-0 !mb-0 relative">
<Header <Header
leftComponent="Potential Albums" leftComponent="Potential Albums"
rightComponent={(
<div className="flex items-center gap-2">
<Input
placeholder="Minimum Assets Count"
defaultValue={config.minAssets}
onChange={(e) => {
if (e.target.value) {
setConfig({ ...config, minAssets: parseInt(e.target.value) });
}
}}
/>
</div>
)}
/> />
<PotentialAlbumContext.Provider <PotentialAlbumContext.Provider
value={{ value={{
@@ -120,57 +135,57 @@ export default function PotentialAlbums() {
<PotentialAlbumsDates /> <PotentialAlbumsDates />
<PotentialAlbumsAssets /> <PotentialAlbumsAssets />
</div> </div>
<FloatingBar> {selectedAssets.length &&
<div className="flex items-center gap-2 justify-between w-full"> <FloatingBar>
<p className="text-sm text-muted-foreground"> <div className="flex items-center gap-2 justify-between w-full">
{config.selectedIds.length} Selected <p className="text-sm text-muted-foreground">
</p> {config.selectedIds.length} Selected
<div className="flex items-center gap-2"> </p>
{config.selectedIds.length && config.selectedIds.length > 0 ? ( <div className="flex items-center gap-2">
<Button <Button
variant={"outline"} variant={"outline"}
size={"sm"} size={"sm"}
onClick={() => onClick={() =>
setConfig({ setConfig({
...config, ...config,
selectedIds: [], selectedIds: [],
}) })
} }
> >
Unselect all Unselect all
</Button> </Button>
) : (
<Button
variant={"outline"}
size={"sm"}
onClick={() =>
setConfig({
...config,
selectedIds: config.assets.map((a) => a.id),
})
}
> <Button
Select all variant={"outline"}
</Button> size={"sm"}
)} onClick={() =>
<AlbumSelectorDialog onSelected={handleSelect} onSubmit={handleCreate}/> setConfig({
<AssetOffsetDialog assets={selectedAssets} onComplete={handleOffsetComplete} /> ...config,
<div className="h-[10px] w-[1px] bg-zinc-500 dark:bg-zinc-600"></div> selectedIds: config.assets.map((a) => a.id),
})
}
<AlertDialog >
title="Delete the selected assets?" Select all
description="This action will delete the selected assets and cannot be undone." </Button>
onConfirm={handleDelete} <AlbumSelectorDialog onSelected={handleSelect} onSubmit={handleCreate} />
disabled={config.selectedIds.length === 0} <AssetOffsetDialog assets={selectedAssets} onComplete={handleOffsetComplete} />
> <div className="h-[10px] w-[1px] bg-zinc-500 dark:bg-zinc-600"></div>
<Button variant={"destructive"} size={"sm"} disabled={config.selectedIds.length === 0}>
Delete <AlertDialog
</Button> title="Delete the selected assets?"
</AlertDialog> description="This action will delete the selected assets and cannot be undone."
onConfirm={handleDelete}
disabled={config.selectedIds.length === 0}
>
<Button variant={"destructive"} size={"sm"} disabled={config.selectedIds.length === 0}>
Delete
</Button>
</AlertDialog>
</div>
</div> </div>
</div> </FloatingBar>
</FloatingBar> }
</PotentialAlbumContext.Provider> </PotentialAlbumContext.Provider>
</PageLayout> </PageLayout>
); );
+28 -31
View File
@@ -3,52 +3,49 @@ import { db } from "@/config/db";
import { IPotentialAlbumsDatesResponse } from "@/handlers/api/album.handler"; import { IPotentialAlbumsDatesResponse } from "@/handlers/api/album.handler";
import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; import { getCurrentUser } from "@/handlers/serverUtils/user.utils";
import { parseDate } from "@/helpers/date.helper"; import { parseDate } from "@/helpers/date.helper";
import { sql } from "drizzle-orm"; import { albumsAssetsAssets } from "@/schema/albumAssetsAssets.schema";
import { albums } from "@/schema/albums.schema";
import { assets } from "@/schema/assets.schema";
import { exif } from "@/schema/exif.schema";
import { and, count, desc, eq, gte, isNotNull, isNull, sql } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
import { number } from "zod";
const SELECT_ORPHAN_PHOTOS = (ownerId: string) => sql`
SELECT
DATE(e."dateTimeOriginal") AS "date",
COUNT(a."id") AS "asset_count"
FROM
"assets" a
LEFT JOIN
"albums_assets_assets" aaa ON a."id" = aaa."assetsId"
LEFT JOIN
"exif" e ON a."id" = e."assetId"
WHERE
aaa."albumsId" IS NULL
AND a."ownerId" = ${ownerId}
AND a."isVisible" = true
AND e."dateTimeOriginal" IS NOT NULL
GROUP BY
DATE(e."dateTimeOriginal")
ORDER BY
"asset_count" DESC;
`;
export default async function handler( export default async function handler(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse res: NextApiResponse
) { ) {
try { try {
const { sortBy = "date", sortOrder = "desc" } = req.query; const { sortBy = "date", sortOrder = "desc", minAssets = 1 } = req.query as any;
const currentUser = await getCurrentUser(req) const currentUser = await getCurrentUser(req)
const data = await db.execute(SELECT_ORPHAN_PHOTOS(currentUser.id)) as any; const rows = await db.select({
const rows = data.rows as IPotentialAlbumsDatesResponse[]; date: sql`DATE(${exif.dateTimeOriginal})`,
asset_count: desc(count(assets.id)),
}).from(assets)
.leftJoin(albumsAssetsAssets, eq(assets.id, albumsAssetsAssets.assetsId))
.leftJoin(exif, eq(assets.id, exif.assetId))
.where(and (
eq(assets.ownerId, currentUser.id),
eq(assets.isVisible, true),
isNull(albumsAssetsAssets.albumsId),
isNotNull(exif.dateTimeOriginal),
))
.groupBy(sql`DATE(${exif.dateTimeOriginal})`) as IPotentialAlbumsDatesResponse[];
const filteredRows = rows.filter((row) => Number(row.asset_count) >= Number(minAssets));
if (sortBy === "date") { if (sortBy === "date") {
rows.sort((a, b) => { filteredRows.sort((a, b) => {
const aDate = parseDate(a.date, "yyyy-MM-dd"); const aDate = parseDate(a.date as string, "yyyy-MM-dd");
const bDate = parseDate(b.date, "yyyy-MM-dd"); const bDate = parseDate(b.date as string, "yyyy-MM-dd");
return sortOrder === "asc" ? aDate.getTime() - bDate.getTime() : bDate.getTime() - aDate.getTime(); return sortOrder === "asc" ? aDate.getTime() - bDate.getTime() : bDate.getTime() - aDate.getTime();
}); });
} else if (sortBy === "asset_count") { } else if (sortBy === "asset_count") {
rows.sort((a, b) => { filteredRows.sort((a, b) => sortOrder === "asc" ? a.asset_count - b.asset_count : b.asset_count - a.asset_count);
return sortOrder === "asc" ? a.asset_count - b.asset_count : b.asset_count - a.asset_count;
});
} }
return res.status(200).json(rows); return res.status(200).json(filteredRows);
} catch (error: any) { } catch (error: any) {
res.status(500).json({ res.status(500).json({
error: error?.message, error: error?.message,
+51 -49
View File
@@ -19,6 +19,7 @@ import FloatingBar from "@/components/shared/FloatingBar";
import { Select, SelectValue, SelectTrigger, SelectContent, SelectItem } from "@/components/ui/select"; import { Select, SelectValue, SelectTrigger, SelectContent, SelectItem } from "@/components/ui/select";
import { AlertDialog } from "@/components/ui/alert-dialog"; import { AlertDialog } from "@/components/ui/alert-dialog";
import AssetOffsetDialog from "@/components/assets/assets-options/AssetOffsetDialog"; import AssetOffsetDialog from "@/components/assets/assets-options/AssetOffsetDialog";
import { Input } from "@/components/ui/input";
export default function MissingLocations() { export default function MissingLocations() {
const { query, push } = useRouter(); const { query, push } = useRouter();
@@ -110,57 +111,58 @@ export default function MissingLocations() {
<MissingLocationDates groupBy={groupBy as "date" | "album"} /> <MissingLocationDates groupBy={groupBy as "date" | "album"} />
<MissingLocationAssets groupBy={groupBy as "date" | "album"} /> <MissingLocationAssets groupBy={groupBy as "date" | "album"} />
</div> </div>
<FloatingBar> {selectedAssets.length &&
<div className="flex items-center gap-2 justify-between w-full"> <FloatingBar>
<p className="text-sm text-muted-foreground"> <div className="flex items-center gap-2 justify-between w-full">
{config.selectedIds.length} Selected <p className="text-sm text-muted-foreground">
</p> {config.selectedIds.length} Selected
<div className="flex items-center gap-2"> </p>
{config.selectedIds.length === config.assets.length ? ( <div className="flex items-center gap-2">
<Button {config.selectedIds.length === config.assets.length ? (
variant={"outline"} <Button
size={"sm"} variant={"outline"}
onClick={() => size={"sm"}
setConfig({ onClick={() =>
...config, setConfig({
selectedIds: [], ...config,
}) selectedIds: [],
} })
}
>
Unselect all
</Button>
) : (
<Button
variant={"outline"}
size={"sm"}
onClick={() =>
setConfig({
...config,
selectedIds: config.assets.map((a) => a.id),
})
}
>
Select all
</Button>
)}
{/* Seperator */}
<TagMissingLocationDialog onSubmit={handleSubmit} />
<AssetOffsetDialog assets={selectedAssets} onComplete={handleOffsetComplete} />
<div className="h-[10px] w-[1px] bg-zinc-500 dark:bg-zinc-600"></div>
<AlertDialog
title="Delete the selected assets?"
description="This action will delete the selected assets and cannot be undone."
onConfirm={handleDelete}
disabled={config.selectedIds.length === 0}
> >
Unselect all <Button variant={"destructive"} size={"sm"} disabled={config.selectedIds.length === 0}>
</Button> Delete
) : ( </Button>
<Button </AlertDialog>
variant={"outline"} </div>
size={"sm"}
onClick={() =>
setConfig({
...config,
selectedIds: config.assets.map((a) => a.id),
})
}
>
Select all
</Button>
)}
{/* Seperator */}
<TagMissingLocationDialog onSubmit={handleSubmit} />
<AssetOffsetDialog assets={selectedAssets} onComplete={handleOffsetComplete} />
<div className="h-[10px] w-[1px] bg-zinc-500 dark:bg-zinc-600"></div>
<AlertDialog
title="Delete the selected assets?"
description="This action will delete the selected assets and cannot be undone."
onConfirm={handleDelete}
disabled={config.selectedIds.length === 0}
>
<Button variant={"destructive"} size={"sm"} disabled={config.selectedIds.length === 0}>
Delete
</Button>
</AlertDialog>
</div> </div>
</div> </FloatingBar>
</FloatingBar> }
</MissingLocationContext.Provider> </MissingLocationContext.Provider>
</PageLayout> </PageLayout>
); );
+6
View File
@@ -93,3 +93,9 @@
max-width: none !important; max-width: none !important;
height: none !important; height: none !important;
} }
.dark {
.ReactGridGallery_tile {
@apply bg-zinc-800 !important;
}
}