Manage request files: mark missing ones, remove them, list them all

Forms → Content requests and a new Tools → Media → Requests tab (by
type folder, each linked to its request) mark a file Drive no longer
has under Requests as Missing from Drive. Remove (admins and up)
trashes a file and takes it off its request. Drive is asked at most
every 30 seconds, and folder lookups now cache every level walked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-29 23:25:58 -05:00
parent 9ff1cd6e99
commit e1542d8d87
6 changed files with 292 additions and 32 deletions

View file

@ -52,10 +52,13 @@ const sizeOf = (bytes) => (bytes < 1024 * 1024 ? `${Math.ceil(bytes / 1024)} KB`
/* A content request's files: each downloads from Drive through the
server, and a Word document can become a draft blog post
(the same import as Blog posts → Import from Word). */
function RequestFiles({ row, canWrite }) {
function RequestFiles({ row, canWrite, canRemove }) {
const navigate = useNavigate();
const [files, setFiles] = useState<any[]>(row.files);
const [busy, setBusy] = useState<number | null>(null);
const [confirming, setConfirming] = useState<number | null>(null);
const [error, setError] = useState<string | null>(null);
useEffect(() => setFiles(row.files), [row.files]);
async function makePost(file) {
setBusy(file.id);
@ -69,29 +72,81 @@ function RequestFiles({ row, canWrite }) {
}
}
async function remove(file) {
setBusy(file.id);
setError(null);
try {
await del(`/admin/feedback/${row.id}/files/${file.id}`);
setFiles((current) => current.filter((f) => f.id !== file.id));
} catch (err) {
setError(err instanceof ApiError ? err.message : "Couldn't remove it.");
} finally {
setBusy(null);
setConfirming(null);
}
}
if (files.length === 0) return null;
const small = "rounded-full border px-3 py-0.5 text-xs font-semibold transition-colors disabled:opacity-60";
return (
<div className="mt-4">
<ul className="space-y-2">
{row.files.map((file) => (
{files.map((file) => (
<li key={file.id} className="flex flex-wrap items-center gap-x-3 gap-y-1 text-sm">
<a
href={`/api/admin/feedback/${row.id}/files/${file.id}`}
download={file.name}
className="font-medium text-link underline underline-offset-2"
>
{file.name}
</a>
{file.missing ? (
// Deleted or moved out of Requests in Drive: nothing to open.
<span className="font-medium text-muted line-through">{file.name}</span>
) : (
<a
href={`/api/admin/feedback/${row.id}/files/${file.id}`}
download={file.name}
className="font-medium text-link underline underline-offset-2"
>
{file.name}
</a>
)}
<span className="text-xs text-muted">{sizeOf(file.size)}</span>
{canWrite && /\.docx$/i.test(file.name) && (
{file.missing && (
<span className="rounded-full bg-danger-tint px-2 py-0.5 text-xs font-medium text-danger">
Missing from Drive
</span>
)}
{canWrite && !file.missing && /\.docx$/i.test(file.name) && (
<button
type="button"
disabled={busy !== null}
onClick={() => makePost(file)}
className="rounded-full border border-link px-3 py-0.5 text-xs font-semibold text-link transition-colors hover:bg-tint disabled:opacity-60"
className={`${small} border-link text-link hover:bg-tint`}
>
{busy === file.id ? "Making the post…" : "Make a blog post from it"}
</button>
)}
{canRemove &&
(confirming === file.id ? (
<>
<button
type="button"
disabled={busy !== null}
onClick={() => remove(file)}
className={`${small} border-danger bg-danger text-white`}
>
{busy === file.id ? "Removing…" : "Remove it"}
</button>
<button type="button" onClick={() => setConfirming(null)} className="text-xs text-muted hover:underline">
Keep it
</button>
</>
) : (
<button
type="button"
disabled={busy !== null}
onClick={() => setConfirming(file.id)}
className="text-xs text-muted hover:text-danger"
title={file.missing ? "Take it off this request" : "Into Drive's trash, and off this request"}
>
Remove
</button>
))}
</li>
))}
</ul>
@ -276,7 +331,7 @@ function FeedbackCard({ row, onChange, onRemove, canWrite, canRemove }) {
{row.subject && <h2 className="mt-4 text-lg font-semibold text-body">{row.subject}</h2>}
<p className={`${row.subject ? "mt-2" : "mt-4"} whitespace-pre-wrap text-body`}>{row.message}</p>
{row.feedback_type === "content" && <RequestDetails row={row} />}
{row.files?.length > 0 && <RequestFiles row={row} canWrite={canWrite} />}
{row.files?.length > 0 && <RequestFiles row={row} canWrite={canWrite} canRemove={canRemove} />}
{row.feedback_type === "content" && canWrite && <RequestRecord row={row} />}
<p className="mt-4 text-sm text-muted">

View file

@ -7,7 +7,9 @@
(MediaLibrary.tsx), so this is housekeeping rather than the way
images get in. Files: documents, audio and zips in the shared
Google Drive, for pages to link to (DriveFiles.tsx); link fields'
"Choose a file" picks from them.
"Choose a file" picks from them. Requests: every file sent with
the Request form, by type folder in Drive, each with its request;
a file Drive no longer has is marked missing.
Two ranks, as on the server: editors and up upload, admins and
up delete. A delete first asks the server which records still
@ -15,7 +17,8 @@
a page (an image field stores the path, not a foreign key).
═══════════════════════════════════════════════════════════════ */
import { useState } from "react";
import { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import { ApiError, del, get, patch, post } from "../../lib/api.ts";
import { useAuth } from "../../lib/auth.tsx";
@ -33,6 +36,7 @@ import {
} from "../../components/admin/DriveFiles.tsx";
import {
FolderTabs,
formatBytes,
MediaGrid,
UPLOAD_HELP,
UploadButton,
@ -356,14 +360,131 @@ function FilesTab() {
);
}
/* Request form files, by their type's folder in Drive (Requests/…). */
function RequestFilesTab() {
const { user } = useAuth();
const [data, setData] = useState<any>(null);
const [error, setError] = useState<string | null>(null);
const [folder, setFolder] = useState("*");
const [confirming, setConfirming] = useState<number | null>(null);
const [busy, setBusy] = useState(false);
useEffect(() => {
get("/admin/request-files", { ttl: 0 })
.then(setData)
.catch((err) => setError(err instanceof ApiError ? err.message : "Couldn't load the request files."));
}, []);
async function remove(file) {
setBusy(true);
setError(null);
try {
await del(`/admin/feedback/${file.feedback_id}/files/${file.id}`);
setData((current) => ({ ...current, files: current.files.filter((f) => f.id !== file.id) }));
} catch (err) {
setError(err instanceof ApiError ? err.message : "Couldn't remove it.");
} finally {
setBusy(false);
setConfirming(null);
}
}
if (error && !data) return <p className="mt-6 text-sm text-danger">{error}</p>;
if (!data) return <p className="mt-6 text-sm text-muted">Loading…</p>;
const counts = new Map<string, number>();
for (const f of data.files) counts.set(f.folder, (counts.get(f.folder) ?? 0) + 1);
const shown = folder === "*" ? data.files : data.files.filter((f) => f.folder === folder);
return (
<div className="mt-6">
<div role="tablist" aria-label="Request folders" className="flex flex-wrap gap-1">
{[["*", "All"], ...data.folders.map((f) => [f, f])].map(([id, label]) => (
<button
key={id}
type="button"
role="tab"
aria-selected={folder === id}
onClick={() => setFolder(id)}
className={
"rounded-full px-3 py-1 text-sm transition-colors " +
(folder === id ? "bg-action font-semibold text-white" : "border border-muted/25 text-muted hover:border-brand/50")
}
>
{label}
{id !== "*" && counts.get(id) ? ` (${counts.get(id)})` : ""}
</button>
))}
</div>
{!data.checked && (
<p className="mt-3 text-sm text-muted">Drive couldn't be asked just now, so missing files aren't marked.</p>
)}
{error && <p className="mt-3 text-sm text-danger">{error}</p>}
{shown.length === 0 ? (
<p className="mt-6 text-sm text-muted">No request files here.</p>
) : (
<ul className="mt-6 divide-y divide-brand/10 rounded-2xl border border-brand/20 bg-surface">
{shown.map((file) => (
<li key={file.id} className="flex flex-wrap items-center gap-x-4 gap-y-1 px-4 py-3 text-sm">
{file.missing ? (
<span className="font-medium text-muted line-through">{file.name}</span>
) : (
<a
href={`/api/admin/feedback/${file.feedback_id}/files/${file.id}`}
download={file.name}
className="font-medium text-link underline underline-offset-2"
>
{file.name}
</a>
)}
<span className="text-xs text-muted">{formatBytes(file.size)}</span>
{file.missing && (
<span className="rounded-full bg-danger-tint px-2 py-0.5 text-xs font-medium text-danger">Missing from Drive</span>
)}
<Link to="/admin/requests" className="text-xs text-muted hover:text-link hover:underline">
Request #{file.feedback_id}
{file.subject ? `: ${file.subject}` : ""} ({file.status})
</Link>
{canDelete(user) && (
<span className="ml-auto">
{confirming === file.id ? (
<>
<button
type="button"
disabled={busy}
onClick={() => remove(file)}
className="rounded-full bg-danger px-3 py-0.5 text-xs font-semibold text-white disabled:opacity-60"
>
{busy ? "Removing…" : "Remove it"}
</button>{" "}
<button type="button" onClick={() => setConfirming(null)} className="text-xs text-muted hover:underline">
Keep it
</button>
</>
) : (
<button type="button" onClick={() => setConfirming(file.id)} className="text-xs text-muted hover:text-danger">
Remove
</button>
)}
</span>
)}
</li>
))}
</ul>
)}
</div>
);
}
const TABS = [
["images", "Images"],
["files", "Files"],
["requests", "Requests"],
] as const;
export default function AdminMedia() {
const { user } = useAuth();
const [tab, setTab] = useState<"images" | "files">("images");
const [tab, setTab] = useState<(typeof TABS)[number][0]>("images");
// The image folder showing; "" is all of them, clips included.
const [folder, setFolder] = useState("");
const [query, setQuery] = useState("");
@ -376,7 +497,9 @@ export default function AdminMedia() {
<p className="text-sm text-muted">
{tab === "images"
? "Uploaded images. Image fields in every editor pick from here."
: "Files in the shared Google Drive's Media folder, for pages to link to, including any put there in Drive itself. Link fields pick from here with Choose a file."}
: tab === "files"
? "Files in the shared Google Drive's Media folder, for pages to link to, including any put there in Drive itself. Link fields pick from here with Choose a file."
: "Files sent with the Request form, in the shared Google Drive's Requests folder. Removing one takes it off its request too."}
</p>
</div>
@ -397,7 +520,9 @@ export default function AdminMedia() {
))}
</nav>
{tab === "files" ? (
{tab === "requests" ? (
<RequestFilesTab />
) : tab === "files" ? (
<FilesTab />
) : (
<>