Keep uploaded images in folders by what they're for
logos/ for logo and icon fields, thumbnails/ for previews the site saves (link previews, Drive first pages), images/ for everything else; each small copy beside its image. Images from before folders are sorted once at boot by their uses, and every record naming them is rewritten in one transaction after the files move. The Media page and image pickers get a tab per folder; a picker opens on its field's folder and uploads there. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4b249c9e52
commit
9854da965e
14 changed files with 283 additions and 51 deletions
|
|
@ -17,6 +17,11 @@
|
|||
description and preview image (the image lands in
|
||||
the library) and fills the row's empty fields.
|
||||
|
||||
Images live in folders by what they're for (FOLDERS in
|
||||
server/src/media.js): Logos, Images, and Previews the site saved
|
||||
itself. A field's `folder` says where its uploads go (logo fields:
|
||||
"logos"; default "images"), and its picker opens on that tab.
|
||||
|
||||
An image field stores a path, not a media id: '/uploads/<file>'
|
||||
for an upload, or, as before, a bare filename in the field's
|
||||
public/ directory or a full URL. So the text box stays: old
|
||||
|
|
@ -47,8 +52,42 @@ export type MediaItem = {
|
|||
processed: boolean;
|
||||
uploaded_by: string | null;
|
||||
created_at: string;
|
||||
/** logos, images or thumbnails; null for clips and older files in the root. */
|
||||
folder: string | null;
|
||||
};
|
||||
|
||||
/* The folder tabs, as [folder, label]; "" is every image. */
|
||||
export const IMAGE_FOLDERS = [
|
||||
["", "All"],
|
||||
["logos", "Logos"],
|
||||
["images", "Images"],
|
||||
["thumbnails", "Previews"],
|
||||
] as const;
|
||||
export type ImageFolder = (typeof IMAGE_FOLDERS)[number][0];
|
||||
|
||||
/* A tab strip over the folders; `value` is the one showing. */
|
||||
export function FolderTabs({ value, onChange }: { value: string; onChange: (folder: ImageFolder) => void }) {
|
||||
return (
|
||||
<div role="tablist" aria-label="Folders" className="flex flex-wrap gap-1">
|
||||
{IMAGE_FOLDERS.map(([folder, label]) => (
|
||||
<button
|
||||
key={folder}
|
||||
type="button"
|
||||
role="tab"
|
||||
aria-selected={value === folder}
|
||||
onClick={() => onChange(folder)}
|
||||
className={
|
||||
"rounded-full px-3 py-1 text-sm transition-colors " +
|
||||
(value === folder ? "bg-action font-semibold text-white" : "border border-muted/25 text-muted hover:border-brand/50")
|
||||
}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const button =
|
||||
"rounded-full border border-brand px-3 py-1 text-sm font-medium text-link " +
|
||||
"transition-colors hover:bg-tint disabled:cursor-default disabled:opacity-50";
|
||||
|
|
@ -77,17 +116,18 @@ export function mediaSize(item: MediaItem) {
|
|||
|
||||
/* ── Data ────────────────────────────────────────────────────── */
|
||||
|
||||
export function uploadImage(file: File, keepOriginal: boolean): Promise<MediaItem> {
|
||||
export function uploadImage(file: File, keepOriginal: boolean, folder?: string): Promise<MediaItem> {
|
||||
const form = new FormData();
|
||||
form.append("file", file);
|
||||
if (keepOriginal) form.append("keep_original", "1");
|
||||
if (folder) form.append("folder", folder);
|
||||
return upload("/admin/media", form).then((data) => data.media);
|
||||
}
|
||||
|
||||
/* `type` narrows to one kind of file: image fields pick images only.
|
||||
Blank lists everything, videos and audio saved from the clip tool
|
||||
included. */
|
||||
export function useMediaLibrary(query: string, type?: "image" | "video" | "audio") {
|
||||
export function useMediaLibrary(query: string, type?: "image" | "video" | "audio", folder?: string) {
|
||||
const [items, setItems] = useState<MediaItem[] | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
|
|
@ -96,13 +136,14 @@ export function useMediaLibrary(query: string, type?: "image" | "video" | "audio
|
|||
const params = new URLSearchParams();
|
||||
if (q) params.set("q", q);
|
||||
if (type) params.set("type", type);
|
||||
if (folder) params.set("folder", folder);
|
||||
setError(null);
|
||||
get(`/admin/media${params.size ? `?${params}` : ""}`, { ttl: 0 })
|
||||
.then((data) => setItems(data.media))
|
||||
.catch((err) =>
|
||||
setError(err instanceof ApiError ? err.message : "Couldn't load the library."),
|
||||
);
|
||||
}, [query, type]);
|
||||
}, [query, type, folder]);
|
||||
|
||||
useEffect(reload, [reload]);
|
||||
|
||||
|
|
@ -115,10 +156,13 @@ export function UploadButton({
|
|||
onUploaded,
|
||||
multiple = false,
|
||||
label = "Upload",
|
||||
folder,
|
||||
}: {
|
||||
onUploaded: (item: MediaItem) => void;
|
||||
multiple?: boolean;
|
||||
label?: string;
|
||||
/** Where it goes; blank: the server's default, images. */
|
||||
folder?: string;
|
||||
}) {
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const keepId = useId();
|
||||
|
|
@ -135,7 +179,7 @@ export function UploadButton({
|
|||
// name the file it happened to.
|
||||
for (const file of Array.from(files)) {
|
||||
try {
|
||||
onUploaded(await uploadImage(file, keepOriginal));
|
||||
onUploaded(await uploadImage(file, keepOriginal, folder));
|
||||
} catch (err) {
|
||||
const why = err instanceof ApiError ? err.message : "Upload failed.";
|
||||
setError(files.length > 1 ? `${file.name}: ${why}` : why);
|
||||
|
|
@ -265,16 +309,20 @@ export function MediaGrid({
|
|||
|
||||
export function MediaPicker({
|
||||
selected,
|
||||
folder = "images",
|
||||
onPick,
|
||||
onClose,
|
||||
}: {
|
||||
selected?: string | null;
|
||||
/** The field's folder: the tab it opens on, where uploads go. */
|
||||
folder?: string;
|
||||
onPick: (item: MediaItem) => void;
|
||||
onClose: () => void;
|
||||
}) {
|
||||
const { user } = useAuth();
|
||||
const [query, setQuery] = useState("");
|
||||
const { items, error, setItems } = useMediaLibrary(query, "image");
|
||||
const [tab, setTab] = useState<string>(folder);
|
||||
const { items, error, setItems } = useMediaLibrary(query, "image", tab);
|
||||
const dialogRef = useRef<HTMLDialogElement>(null);
|
||||
|
||||
// showModal gives focus trapping, Escape and the backdrop for free.
|
||||
|
|
@ -312,10 +360,15 @@ export function MediaPicker({
|
|||
</button>
|
||||
</div>
|
||||
|
||||
<div className="border-b border-brand/10 px-5 py-3">
|
||||
<FolderTabs value={tab} onChange={setTab} />
|
||||
</div>
|
||||
|
||||
{roleCanWrite(user) && (
|
||||
<div className="border-b border-brand/10 px-5 py-3">
|
||||
<UploadButton
|
||||
label="Upload new"
|
||||
folder={folder}
|
||||
onUploaded={(item) => {
|
||||
setItems((current) => [item, ...(current ?? [])]);
|
||||
onPick(item);
|
||||
|
|
@ -353,6 +406,7 @@ export function ImageInput({
|
|||
id,
|
||||
value,
|
||||
resolve,
|
||||
folder,
|
||||
locked,
|
||||
className,
|
||||
onChange,
|
||||
|
|
@ -360,6 +414,8 @@ export function ImageInput({
|
|||
id: string;
|
||||
value: string | null | undefined;
|
||||
resolve?: (file?: string | null) => string | null;
|
||||
/** The field's `folder` (logos for logo fields); default images. */
|
||||
folder?: string;
|
||||
locked: boolean;
|
||||
className: string;
|
||||
onChange: (value: string) => void;
|
||||
|
|
@ -399,7 +455,7 @@ export function ImageInput({
|
|||
/>
|
||||
{editable && (
|
||||
<div className="flex flex-wrap items-start gap-2">
|
||||
<UploadButton onUploaded={(item) => onChange(item.url)} />
|
||||
<UploadButton folder={folder} onUploaded={(item) => onChange(item.url)} />
|
||||
<button type="button" className={button} onClick={() => setPicking(true)}>
|
||||
Choose from library
|
||||
</button>
|
||||
|
|
@ -419,6 +475,7 @@ export function ImageInput({
|
|||
{picking && (
|
||||
<MediaPicker
|
||||
selected={value}
|
||||
folder={folder}
|
||||
onPick={(item) => onChange(item.url)}
|
||||
onClose={() => setPicking(false)}
|
||||
/>
|
||||
|
|
|
|||
|
|
@ -69,7 +69,9 @@
|
|||
`widget: "image"` is a text box with a preview and upload and
|
||||
library buttons beside it (ImageInput in MediaLibrary.tsx). Its
|
||||
`resolve` option is the media.ts helper for the field's public/
|
||||
directory, so a bare filename previews from the right place.
|
||||
directory, so a bare filename previews from the right place. Its
|
||||
`folder` ("logos" for logo and icon fields) is where uploads go and
|
||||
the tab the library opens on.
|
||||
|
||||
Two options change the box itself rather than what goes in it:
|
||||
|
||||
|
|
@ -347,6 +349,7 @@ export function Field({ field, value, row, options, error, onChange, onPatch }:
|
|||
id={id}
|
||||
value={value}
|
||||
resolve={field.resolve}
|
||||
folder={field.folder}
|
||||
locked={locked}
|
||||
className={locked ? inputLocked : common.className}
|
||||
onChange={onChange}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue