"use client"; /* eslint-disable @next/next/no-img-element */ import type { MediaKind } from "@prisma/client"; import type { MediaOption } from "@/lib/media"; import { cn } from "@/lib/utils"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; export type MediaFieldState = { mode: "upload" | "external" | "library"; assetId: string; url: string; label: string; kind: MediaKind; }; type MediaFieldPickerProps = { title: string; value: MediaFieldState; onChange: (nextValue: MediaFieldState) => void; options: MediaOption[]; inputName: string; fileFieldName: string; fileLabel?: string; externalLabel?: string; libraryLabel?: string; accept?: string; }; const modeOptions: Array = ["upload", "external", "library"]; export function MediaFieldPicker({ title, value, onChange, options, inputName, fileFieldName, fileLabel, externalLabel, libraryLabel, accept, }: MediaFieldPickerProps) { const filteredOptions = options.filter((option) => option.kind === value.kind); const selectedOption = filteredOptions.find((option) => option.id === value.assetId) ?? null; const previewUrl = value.mode === "library" ? selectedOption?.url ?? "" : value.mode === "external" ? value.url : ""; return (
{modeOptions.map((mode) => ( ))}
onChange({ ...value, label: event.target.value })} placeholder="Homepage Hero" />
{value.mode === "upload" ? (
) : null} {value.mode === "external" ? (
onChange({ ...value, url: event.target.value })} placeholder="https://example.com/image.jpg" />
) : null} {value.mode === "library" ? (
) : null} {previewUrl ? ( value.kind === "IMAGE" ? (
{value.label
) : (
{previewUrl}
) ) : null}
); }