feat: add album and media management features, including album creation, media upload, and routing

This commit is contained in:
2025-11-16 01:19:17 +01:00
parent 252491bd2f
commit 43157cef4e
18 changed files with 814 additions and 8 deletions

View File

@@ -0,0 +1,80 @@
import { useState, useEffect } from "react";
import apiClient from "@/services/api-client";
import { Skeleton } from "@/components/ui/skeleton";
import { ImageIcon } from "lucide-react";
import { cn } from "@/lib/utils";
type AuthenticatedImageProps = React.ComponentProps<"img"> & {
src: string; // The protected URL, e.g., /media/123/thumbnail
};
export function AuthenticatedImage({ src, ...props }: AuthenticatedImageProps) {
const [objectUrl, setObjectUrl] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState(false);
useEffect(() => {
if (!src) {
setIsLoading(false);
setError(true);
return;
}
let isCancelled = false;
let currentObjectUrl: string | null = null;
const fetchImage = async () => {
setIsLoading(true);
setError(false);
try {
const response = await apiClient.get(src, {
responseType: "blob",
});
if (isCancelled) return;
const blob = response.data;
currentObjectUrl = URL.createObjectURL(blob);
setObjectUrl(currentObjectUrl);
} catch (err) {
if (!isCancelled) {
console.error("Failed to fetch authenticated image:", src, err);
setError(true);
}
} finally {
if (!isCancelled) {
setIsLoading(false);
}
}
};
fetchImage();
return () => {
isCancelled = true;
if (currentObjectUrl) {
URL.revokeObjectURL(currentObjectUrl);
}
setObjectUrl(null);
};
}, [src]);
if (isLoading) {
return <Skeleton className={cn("h-full w-full", props.className)} />;
}
if (error || !objectUrl) {
return (
<div
className={cn(
"flex h-full w-full items-center justify-center bg-gray-200",
props.className
)}
>
<ImageIcon className="h-1/2 w-1/2 text-gray-400" />
</div>
);
}
return <img {...props} src={objectUrl} />;
}

View File

@@ -0,0 +1,41 @@
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { type Media } from "@/domain/types";
import { AuthenticatedImage } from "./authenticated-image";
import { Skeleton } from "../ui/skeleton";
type MediaViewerProps = {
media: Media | null;
onOpenChange: (open: boolean) => void;
};
export function MediaViewer({ media, onOpenChange }: MediaViewerProps) {
const isOpen = media !== null;
return (
<Dialog open={isOpen} onOpenChange={onOpenChange}>
<DialogContent className="min-w-[90vw] max-w-full h-[90vh] flex flex-col p-4">
<DialogHeader>
<DialogTitle className="truncate">
{media?.original_filename}
</DialogTitle>
</DialogHeader>
<div className="flex-1 flex items-center justify-center overflow-hidden relative">
{media ? (
<AuthenticatedImage
src={media.file_url}
alt={media.original_filename}
className="max-w-full max-h-full object-contain"
/>
) : (
<Skeleton className="w-full h-full" />
)}
</div>
</DialogContent>
</Dialog>
);
}

View File

@@ -0,0 +1,76 @@
import { useState } from "react";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
DialogFooter,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useUploadMedia } from "@/features/media/use-media";
import { UploadCloud } from "lucide-react";
export function UploadDialog() {
const [isOpen, setIsOpen] = useState(false);
const [file, setFile] = useState<File | null>(null);
const { mutate: upload, isPending } = useUploadMedia();
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files) {
setFile(e.target.files[0]);
}
};
const handleSubmit = () => {
if (file) {
upload(
{ file },
{
onSuccess: () => {
setIsOpen(false);
setFile(null);
// TODO: Add toast notification
},
}
);
}
};
return (
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button>
<UploadCloud size={18} className="mr-2" />
Upload
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>Upload Photo</DialogTitle>
<DialogDescription>
Select a photo from your computer to upload.
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="grid w-full max-w-sm items-center gap-1.5">
<Label htmlFor="picture">Picture</Label>
<Input id="picture" type="file" onChange={handleFileChange} />
</div>
</div>
<DialogFooter>
<Button
type="submit"
onClick={handleSubmit}
disabled={!file || isPending}
>
{isPending ? "Uploading..." : "Upload"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}