"use client";
/** The media library: pick an image, or upload one (with its alt text) and pick it. */
import { useRef, useState } from "react";
import { Move } from "lucide-react";
import { api } from "./api";
import { Framed, FramingDialog, useFraming } from "./framing";
import { Btn, cx, Field, inputClass, Notice, useAction, useAdmin } from "./ui";
export type MediaItem = { id: string; url: string; alt: string; width: number | null; height: number | null; bytes: number; usedIn?: string[] };
export type Storage = { usedBytes: number; limitBytes: number; full: boolean; percent: number };
export const gb = (n: number) => `${(n / 1024 ** 3).toFixed(n < 1024 ** 3 ? 2 : 1)} GB`;
export function StorageMeter({ s }: { s: Storage }) {
return (
= 100 ? "bg-red-600" : s.percent >= 80 ? "bg-amber-500" : "bg-brand")} style={{ width: `${s.percent}%` }} />
{gb(s.usedBytes)} of {gb(s.limitBytes)} used
);
}
/** Upload form: a photo and what it shows (alt text is required, §3 "Every image needs alt text"). */
export function Uploader({ onUploaded, disabled }: { onUploaded: (m: MediaItem) => void; disabled?: boolean }) {
const file = useRef
(null);
const [alt, setAlt] = useState("");
const { busy, message, run } = useAction();
const go = () => run(async () => {
const f = file.current?.files?.[0];
if (!f) throw new Error("Choose a photo first.");
if (!alt.trim()) throw new Error("Say what the photo shows (for people who can't see it).");
const m = await api(`/media?alt=${encodeURIComponent(alt.trim())}`, { raw: f });
setAlt("");
if (file.current) file.current.value = "";
onUploaded(m);
}, "Uploaded.");
return (
setAlt(e.target.value)} maxLength={500} disabled={disabled} />
Upload
{message && {message.text} }
);
}
/**
* One image field: shows the chosen image, with Choose and Remove. In a section (`slot` and a
* FramingContext), it's shown in the shape of its frame on the site, with Move and zoom.
*/
export function ImagePicker({ value, onChange, label, slot }: { value: string | null; onChange: (id: string | null) => void; label: string; slot?: { key: string; index?: number } }) {
const [open, setOpen] = useState(false);
const [framingOpen, setFramingOpen] = useState(false);
const { data, reload } = useAdmin<{ storage: Storage; items: MediaItem[] }>(open || value ? "/media" : null);
const chosen = data?.items.find((m) => m.id === value);
const framing = useFraming();
const frame = framing && slot ? framing.frame(slot.key, slot.index) : null;
const focus = value ? framing?.get(value) : undefined;
return (
{label}
{chosen && frame ? (
setFramingOpen(true)} title="Move and zoom" className="shrink-0 rounded border border-gray-200"
style={{ width: `${Math.round(Math.min(160, Math.max(48, 72 * frame.ratio)))}px` }}>
) : (
{chosen ?
:
No image }
)}
setOpen(true)}>{value ? "Change" : "Choose"}
{chosen && frame &&
setFramingOpen(true)}> Move and zoom }
{value &&
onChange(null)}>Remove }
{chosen && frame && focus &&
Moved{focus.zoom > 1 ? ` and zoomed to ${Math.round(focus.zoom * 100)}%` : ""}. Save to see it in the preview.
}
{framingOpen && chosen && frame && framing && (
{ framing.set(chosen.id, f); setFramingOpen(false); }} onClose={() => setFramingOpen(false)} />
)}
{open && (
Choose an image
setOpen(false)}>Close
{data?.items.map((m) => (
{ onChange(m.id); setOpen(false); }}
className={cx("overflow-hidden rounded border-2 text-left", m.id === value ? "border-brand" : "border-transparent hover:border-gray-300")}>
{m.alt || "No description"}
))}
Upload a new one
{data?.storage.full && Your storage is full, so uploads have stopped. An Owner or Admin can get more under Media library. }
{ reload(); onChange(m.id); setOpen(false); }} />
)}
);
}