Add Drive folders: per request type, and managed from the Media page

Request files go in Requests/<type> (Chapters, Events...). Media →
Files shows a tab per folder under Media, up to three deep, and can
make, rename (files follow) and delete empty folders, move a file with
its Folder dropdown, and upload into the folder showing; Choose a file
has the same tabs. A folder made, or a file moved, in Drive by hand is
followed on the next look (migration 050 records each file's folder).

Deletes now move to Drive's trash: a shared drive's Content manager,
the service account's role, can't delete outright, so request and file
deletes would have failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 22:34:27 -05:00
parent 9854da965e
commit 7f05076a86
7 changed files with 479 additions and 38 deletions

View file

@ -11,6 +11,9 @@
or an icon for its type
FilePicker the dialog behind a link field's "Choose a
file" (a field with `files: true`, fields.tsx)
DriveFolderTabs a tab per folder under Media in Drive; both
the picker and the Files tab filter by it, and
upload into the one showing
A link field stores the file's Drive link, a plain URL, so nothing
else needs to know where it came from. Files put in the Drive
@ -42,10 +45,42 @@ export type DriveFile = {
bytes: number;
url: string;
preview: string | null;
/** Its folder under Media: "" for Media itself. */
folder: string;
uploaded_by: string | null;
created_at: string;
};
/* Which tab: every file, Media's own (not in a folder), or a folder's. */
export const ALL_FOLDERS = "*";
export function inFolder(files: DriveFile[], tab: string) {
return tab === ALL_FOLDERS ? files : files.filter((f) => f.folder === tab);
}
export function DriveFolderTabs({ folders, value, onChange }: { folders: string[]; value: string; onChange: (tab: string) => void }) {
const tabs: [string, string][] = [[ALL_FOLDERS, "All"], ["", "Not in a folder"], ...folders.map((f): [string, string] => [f, f])];
return (
<div role="tablist" aria-label="Folders" className="flex flex-wrap gap-1">
{tabs.map(([tab, label]) => (
<button
key={tab}
type="button"
role="tab"
aria-selected={value === tab}
onClick={() => onChange(tab)}
className={
"rounded-full px-3 py-1 text-sm transition-colors " +
(value === tab ? "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";
@ -75,6 +110,7 @@ function TypeIcon({ mime }: { mime: string }) {
export function useDriveFiles(query: string) {
const [items, setItems] = useState<DriveFile[] | null>(null);
const [folders, setFolders] = useState<string[]>([]);
const [ready, setReady] = useState(true);
// Files in the Drive folder that Drive wouldn't share publicly.
const [refused, setRefused] = useState<string[]>([]);
@ -86,6 +122,7 @@ export function useDriveFiles(query: string) {
get(`/admin/files${q ? `?q=${encodeURIComponent(q)}` : ""}`, { ttl: 0 })
.then((data) => {
setItems(data.files);
setFolders(data.folders ?? []);
setReady(data.ready);
setRefused(data.refused ?? []);
})
@ -93,12 +130,21 @@ export function useDriveFiles(query: string) {
}, [query]);
useEffect(reload, [reload]);
return { items, ready, refused, error, reload, setItems };
return { items, folders, ready, refused, error, reload, setItems };
}
/* ── Upload ──────────────────────────────────────────────────── */
export function FileUploadButton({ onUploaded, label = "Upload files" }: { onUploaded: (file: DriveFile) => void; label?: string }) {
export function FileUploadButton({
onUploaded,
label = "Upload files",
folder = "",
}: {
onUploaded: (file: DriveFile) => void;
label?: string;
/** Under Media; "" for Media itself. */
folder?: string;
}) {
const inputRef = useRef<HTMLInputElement>(null);
const [busy, setBusy] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
@ -112,6 +158,7 @@ export function FileUploadButton({ onUploaded, label = "Upload files" }: { onUpl
setBusy(file.name);
const form = new FormData();
form.append("file", file);
form.append("folder", folder);
try {
onUploaded((await upload("/admin/files", form)).file);
} catch (err) {
@ -206,7 +253,8 @@ export function FilePicker({
}) {
const { user } = useAuth();
const [query, setQuery] = useState("");
const { items, ready, refused, error, setItems } = useDriveFiles(query);
const { items, folders, ready, refused, error, setItems } = useDriveFiles(query);
const [tab, setTab] = useState(ALL_FOLDERS);
const dialogRef = useRef<HTMLDialogElement>(null);
useEffect(() => {
@ -247,10 +295,17 @@ export function FilePicker({
</button>
</div>
{folders.length > 0 && (
<div className="border-b border-brand/10 px-5 py-3">
<DriveFolderTabs folders={folders} value={tab} onChange={setTab} />
</div>
)}
{roleCanWrite(user) && ready && (
<div className="border-b border-brand/10 px-5 py-3">
<FileUploadButton
label="Upload new"
label={tab && tab !== ALL_FOLDERS ? `Upload into ${tab}` : "Upload new"}
folder={tab === ALL_FOLDERS ? "" : tab}
onUploaded={(file) => {
setItems((current) => [file, ...(current ?? [])]);
pick(file);
@ -268,7 +323,7 @@ export function FilePicker({
) : items === null ? (
<p className="text-sm text-muted">Loading…</p>
) : (
<FileList items={items} selected={selected} onPick={pick} />
<FileList items={inFolder(items, tab)} selected={selected} onPick={pick} />
)}
</div>
</div>