Files
sass-mohfarawati/components/admin/media-field-picker.tsx
T
moh 077e1c5836 STYLED - Flatten media picker and use a two-column media layout
Fix the deeply nested cards in the project form (screenshot: Cover Media >
Cover > preview card, with duplicate labels):

- MediaFieldPicker no longer wraps itself in AppCards. It renders one flat
  bordered box laid out in two columns (preview | controls), dropping two
  nested card layers and the duplicate title.
- Cover Media and Assets now sit side by side in a two-column grid instead
  of stacked with a separator.

All 375 tests pass; tsc and eslint clean.
2026-09-20 17:30:44 +02:00

215 lines
7.3 KiB
TypeScript

"use client";
/* eslint-disable @next/next/no-img-element */
import type { MediaKind } from "@/lib/db/enums";
import { Check, ImageIcon, Search, Trash2 } from "lucide-react";
import { useEffect, useMemo, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import type { MediaOption } from "@/lib/media";
import { cn } from "@/lib/utils";
export type MediaFieldState = {
mode: "upload" | "external" | "library";
assetId: string;
url: string;
label: string;
kind: MediaKind;
isCleared?: boolean;
};
type MediaFieldPickerProps = {
title: string;
value: MediaFieldState;
onChange: (nextValue: MediaFieldState) => void;
options: MediaOption[];
hasInitialValue?: boolean;
inputName: string;
fileFieldName: string;
allowClear?: boolean;
clearLabel?: string;
emptyValue?: Partial<MediaFieldState>;
};
export function MediaFieldPicker({
title,
value,
onChange,
options,
hasInitialValue = false,
inputName,
allowClear = false,
clearLabel = "Remove",
emptyValue,
}: MediaFieldPickerProps) {
const hiddenInputRef = useRef<HTMLInputElement | null>(null);
const [open, setOpen] = useState(false);
const [query, setQuery] = useState("");
const filteredOptions = options.filter((option) => option.kind === value.kind);
const selectedOption = filteredOptions.find((option) => option.id === value.assetId) ?? null;
const visibleOptions = useMemo(() => {
const normalizedQuery = query.trim().toLowerCase();
if (!normalizedQuery) {
return filteredOptions;
}
return filteredOptions.filter((option) => option.label.toLowerCase().includes(normalizedQuery));
}, [filteredOptions, query]);
const serializedValue = JSON.stringify({
mode: value.mode,
assetId: value.assetId,
url: value.url,
label: value.label,
kind: value.kind,
});
const canClear = allowClear && (Boolean(value.assetId) || (hasInitialValue && !value.isCleared));
useEffect(() => {
const hiddenInput = hiddenInputRef.current;
if (!hiddenInput) {
return;
}
hiddenInput.dispatchEvent(new Event("input", { bubbles: true }));
hiddenInput.dispatchEvent(new Event("change", { bubbles: true }));
}, [serializedValue]);
return (
<div className="space-y-2">
<input ref={hiddenInputRef} type="hidden" name={inputName} value={serializedValue} />
<div className="flex flex-col gap-3 rounded-nested border border-border/70 bg-background p-3 sm:flex-row sm:items-center sm:justify-between">
<div className="flex min-w-0 items-center gap-3">
{selectedOption ? (
<>
<img
src={selectedOption.url}
alt={selectedOption.label}
className="h-14 w-14 shrink-0 rounded-nested border border-border/60 object-cover"
/>
<div className="min-w-0">
<p className="truncate text-sm font-medium text-foreground">{selectedOption.label}</p>
<p className="truncate text-xs text-muted-foreground">{selectedOption.source}</p>
</div>
</>
) : (
<div className="flex items-center gap-3 text-muted-foreground">
<div className="flex h-14 w-14 shrink-0 items-center justify-center rounded-nested border border-dashed border-border/70">
<ImageIcon className="h-5 w-5" />
</div>
<span className="text-sm">Kein Medium ausgewählt</span>
</div>
)}
</div>
<div className="flex shrink-0 gap-2">
<Button type="button" variant="outline" size="sm" onClick={() => setOpen(true)}>
<ImageIcon className="h-4 w-4" />
{selectedOption ? "Ändern" : "Auswählen"}
</Button>
{canClear ? (
<Button
type="button"
variant="ghost"
size="sm"
className="text-destructive hover:text-destructive"
title={clearLabel}
onClick={() =>
onChange({
...value,
mode: emptyValue?.mode ?? "upload",
assetId: emptyValue?.assetId ?? "",
url: emptyValue?.url ?? "",
label: emptyValue?.label ?? "",
isCleared: true,
})
}
>
<Trash2 className="h-4 w-4" />
<span className="sr-only">{clearLabel}</span>
</Button>
) : null}
</div>
</div>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="max-w-4xl">
<DialogHeader>
<DialogTitle>{title}</DialogTitle>
<DialogDescription>Select an existing item from the media library.</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div className="relative">
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
<Input
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder="Search media"
className="pl-9"
/>
</div>
<div className="grid max-h-[55vh] gap-3 overflow-y-auto md:grid-cols-2 xl:grid-cols-3">
{visibleOptions.map((option) => {
const isActive = option.id === value.assetId;
return (
<button
key={option.id}
type="button"
onClick={() => {
onChange({
...value,
mode: "library",
assetId: option.id,
url: option.url,
label: option.label,
isCleared: false,
});
setOpen(false);
}}
className={cn(
"overflow-hidden rounded-surface border text-left transition-colors",
isActive
? "border-input bg-accent/20"
: "border-border/70 bg-card hover:border-input hover:bg-accent/10",
)}
>
<img src={option.url} alt={option.label} className="h-40 w-full object-cover" />
<div className="flex items-center justify-between gap-3 p-4">
<div className="min-w-0">
<p className="truncate text-sm font-medium text-foreground">{option.label}</p>
<p className="truncate text-xs text-muted-foreground">{option.source}</p>
</div>
{isActive ? <Check className="h-4 w-4 text-brand-primary" /> : null}
</div>
</button>
);
})}
</div>
</div>
<DialogFooter>
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
Close
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
);
}