Files
website/src/routes/projects/worldwide-todo/queue/+page.svelte
2026-10-26 08:38:51 +00:00

602 lines
15 KiB
Svelte

<script lang="ts">
import { enhance } from '$app/forms';
import TaskFieldsEditor from '#lib/worldtodo/TaskFieldsEditor.svelte';
import PrivateLock from '#lib/components/PrivateLock.svelte';
import {
EDITABLE_FIELDS,
FIELD_LABELS,
fieldsOf,
formatValue,
relativeTime,
type EditableField
} from '#lib/worldtodo/shared';
import Check from 'lucide-svelte/icons/check';
import X from 'lucide-svelte/icons/x';
import PencilLine from 'lucide-svelte/icons/pencil-line';
import TriangleAlert from 'lucide-svelte/icons/triangle-alert';
import Copy from 'lucide-svelte/icons/copy';
let { data, form } = $props();
type Item = (typeof data.items)[number];
const tabs = [
{ key: 'pending', label: 'Pending' },
{ key: 'approved', label: 'Approved' },
{ key: 'rejected', label: 'Rejected' }
] as const;
const kindLabel = { add: 'Add', edit: 'Edit', delete: 'Delete' } as const;
let selected = $state(0);
let tweaking = $state<number | null>(null);
let busy = $state<number | null>(null);
let now = $state(Date.now());
const cards: HTMLElement[] = $state([]);
$effect(() => {
const t = setInterval(() => (now = Date.now()), 30_000);
return () => clearInterval(t);
});
$effect(() => {
if (selected >= data.items.length) selected = Math.max(0, data.items.length - 1);
});
/** Field rows: old → new. For decided requests, what was applied. */
function diff(item: Item) {
const change = (
item.status === 'pending' ? item.payload : (item.applied ?? item.payload)
) as Record<string, unknown>;
const current = item.task ? fieldsOf(item.task) : null;
if (item.kind === 'delete')
return current
? (['title', 'location_name', 'status'] as EditableField[]).map((k) => ({
key: k,
old: formatValue(k, current[k]),
new: '—'
}))
: [];
return EDITABLE_FIELDS.filter((k) => k in change).map((k) => ({
key: k,
old: item.kind === 'add' || !current ? '—' : formatValue(k, current[k]),
new: formatValue(k, change[k])
}));
}
/** Coordinates the request proposes (picked on the map or typed), to check before approving. */
function proposedPosition(item: Item) {
const p = item.payload as Record<string, unknown>;
if (item.kind === 'delete' || !('lat' in p || 'lon' in p)) return null;
const lat = Number(p.lat ?? item.task?.lat);
const lon = Number(p.lon ?? item.task?.lon);
return p.lat !== null && p.lon !== null && Number.isFinite(lat) && Number.isFinite(lon)
? { lat, lon }
: null;
}
function tweakValues(item: Item) {
const base = item.task ? fieldsOf(item.task) : {};
return { ...base, ...item.payload } as Record<string, unknown>;
}
const sources = (item: Item) =>
((item.payload.source_urls as string[] | undefined) ?? item.task?.sourceUrls ?? []).slice(0, 5);
function submit(i: number, action: 'approve' | 'reject') {
const card = cards[i];
const btn = card?.querySelector<HTMLButtonElement>(`button[data-action="${action}"]`);
if (btn && !btn.disabled) btn.click();
}
function onKey(e: KeyboardEvent) {
if (e.metaKey || e.ctrlKey || e.altKey) return;
const el = e.target as HTMLElement;
if (el.closest('input, textarea, select, [contenteditable]')) return;
if (data.status !== 'pending' || !data.items.length) return;
if (e.key === 'j' || e.key === 'k') {
e.preventDefault();
selected = Math.min(data.items.length - 1, Math.max(0, selected + (e.key === 'j' ? 1 : -1)));
cards[selected]?.focus();
cards[selected]?.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
} else if (e.key === 'a') {
e.preventDefault();
submit(selected, 'approve');
} else if (e.key === 'r') {
e.preventDefault();
submit(selected, 'reject');
} else if (e.key === 'e') {
e.preventDefault();
tweaking = data.items[selected]?.id ?? null;
}
}
const host = (url: string) => {
try {
return new URL(url).hostname.replace(/^www\./, '');
} catch {
return url;
}
};
</script>
<svelte:window onkeydown={onKey} />
<section class="queue" aria-labelledby="queue-h">
<div class="top">
<h2 id="queue-h">Review queue <PrivateLock size={13} /></h2>
{#if data.status === 'pending'}
<p class="keys" aria-label="Keyboard shortcuts">
<kbd>j</kbd>/<kbd>k</kbd> move · <kbd>a</kbd> approve · <kbd>e</kbd> edit &amp; approve ·
<kbd>r</kbd> reject
</p>
{/if}
</div>
<nav class="tabs" aria-label="Request status">
{#each tabs as t (t.key)}
<a
href="?status={t.key}"
class={{ on: data.status === t.key }}
aria-current={data.status === t.key ? 'page' : undefined}
>{t.label} <span class="n">{data.counts[t.key]}</span></a
>
{/each}
</nav>
{#if form?.ok}<p class="flash" role="status">{form.ok} #{form.id}.</p>{/if}
{#if !data.items.length}
<p class="empty">Nothing {data.status} right now.</p>
{:else}
<ol class="items">
{#each data.items as item, i (item.id)}
<li>
<article
class={['card', { sel: i === selected && data.status === 'pending' }]}
tabindex="-1"
bind:this={cards[i]}
onfocusin={() => (selected = i)}
aria-labelledby="req-{item.id}-h"
>
<header>
<span class="kind {item.kind}">{kindLabel[item.kind]}</span>
<h3 id="req-{item.id}-h">
{#if item.task}
<a href="/projects/worldwide-todo/{item.task.slug}">{item.task.title}</a>
{:else}
{String(item.payload.title ?? 'New task')}
{/if}
</h3>
<span class="age">#{item.id} · {relativeTime(item.createdAt, now) || 'just now'}</span
>
</header>
{#if item.stale}
<p class="warn">
<TriangleAlert size={14} aria-hidden="true" />
{item.taskDeleted
? 'Stale: the task was removed since this request was filed.'
: 'Stale: the task changed since this request was filed. The diff below is against the current version.'}
</p>
{/if}
{#if item.similar.length}
<p class="dupe">
<Copy size={14} aria-hidden="true" /> Similar:
{#each item.similar as s, j (s.slug)}{j ? ', ' : ''}<a
href="/projects/worldwide-todo/{s.slug}">{s.title}</a
>
({Math.round(s.similarity * 100)}%){/each}
</p>
{/if}
{#if diff(item).length}
<table class="diff">
<caption class="sr-only">Changes</caption>
<thead
><tr
><th scope="col">Field</th><th scope="col">Old</th><th scope="col">New</th></tr
></thead
>
<tbody>
{#each diff(item) as row (row.key)}
<tr>
<th scope="row">{FIELD_LABELS[row.key]}</th>
<td
>{#if row.old !== '—'}<del><span class="sr-only">Old: </span>{row.old}</del
>{:else}—{/if}</td
>
<td
>{#if row.new !== '—'}<ins><span class="sr-only">New: </span>{row.new}</ins
>{:else}—{/if}</td
>
</tr>
{/each}
</tbody>
</table>
{@const pos = proposedPosition(item)}
{#if pos}
<a
class="osm"
href="https://www.openstreetmap.org/?mlat={pos.lat}&mlon={pos.lon}#map=15/{pos.lat}/{pos.lon}"
target="_blank"
rel="noopener noreferrer">Check the proposed map position on OpenStreetMap ↗</a
>
{/if}
{/if}
<dl class="meta">
<dt>Reason</dt>
<dd class="reason">{item.reason}</dd>
{#if item.displayName}<dt>From</dt>
<dd>{item.displayName}</dd>{/if}
{#if sources(item).length}
<dt>Sources</dt>
<dd class="srcs">
{#each sources(item) as url (url)}
<a href={url} target="_blank" rel="noopener nofollow noreferrer">{host(url)}</a>
{/each}
</dd>
{/if}
{#if item.ownerNote}<dt>Note</dt>
<dd>{item.ownerNote}</dd>{/if}
</dl>
{#if item.status === 'pending'}
{#if form?.message && form.id === item.id}
<p class="error" role="alert">{form.message}</p>
{/if}
<form
method="POST"
use:enhance={() => {
busy = item.id;
return async ({ update }) => {
await update();
busy = null;
tweaking = null;
};
}}
>
<input type="hidden" name="id" value={item.id} />
{#if tweaking === item.id}
<div class="tweak">
<TaskFieldsEditor
values={tweakValues(item)}
categories={data.categories}
id="t{item.id}"
/>
</div>
{/if}
<label class="note">
<span>Note (optional, shown on the public request page)</span>
<textarea name="note" rows="2" maxlength="1000"></textarea>
</label>
<div class="actions">
{#if tweaking === item.id}
<button
type="submit"
formaction="?/tweak"
class="primary"
disabled={busy === item.id}
><Check size={15} aria-hidden="true" />Apply tweaks &amp; approve</button
>
<button type="button" onclick={() => (tweaking = null)}>Cancel</button>
{:else}
<button
type="submit"
formaction="?/approve"
data-action="approve"
class="primary"
disabled={busy === item.id}
><Check size={15} aria-hidden="true" />Approve <kbd>a</kbd></button
>
{#if item.kind !== 'delete'}
<button type="button" onclick={() => (tweaking = item.id)}
><PencilLine size={15} aria-hidden="true" />Edit &amp; approve
<kbd>e</kbd></button
>
{/if}
{/if}
<button
type="submit"
formaction="?/reject"
data-action="reject"
class="danger"
formnovalidate
disabled={busy === item.id}
><X size={15} aria-hidden="true" />Reject <kbd>r</kbd></button
>
</div>
</form>
{:else if item.decidedAt}
<p class="decided">
{item.status === 'approved' ? 'Approved' : 'Rejected'}
{relativeTime(item.decidedAt, now) || 'just now'}
</p>
{/if}
</article>
</li>
{/each}
</ol>
{/if}
</section>
<style>
.queue {
display: flex;
flex-direction: column;
gap: 0.9rem;
}
.top {
display: flex;
flex-wrap: wrap;
align-items: baseline;
justify-content: space-between;
gap: 0.5rem;
}
h2 {
display: inline-flex;
align-items: center;
gap: 0.4rem;
font-size: 1.3rem;
font-weight: 650;
}
.keys {
font-size: 0.78rem;
color: var(--site-muted);
}
kbd {
display: inline-block;
min-width: 1.2rem;
padding: 0 0.3rem;
border: 1px solid var(--site-border);
border-bottom-width: 2px;
border-radius: 0.3rem;
font-family: inherit;
font-size: 0.72rem;
text-align: center;
line-height: 1.3;
}
.tabs {
display: flex;
gap: 0.35rem;
}
.tabs a {
padding: 0.3rem 0.75rem;
border: 1px solid var(--site-border);
border-radius: 999px;
font-size: 0.83rem;
color: var(--site-muted);
text-decoration: none;
}
.tabs a:hover,
.tabs a:focus-visible {
color: var(--site-text);
border-color: var(--site-muted);
}
.tabs a.on {
background: var(--site-text);
color: var(--site-bg);
border-color: var(--site-text);
}
.n {
opacity: 0.7;
font-variant-numeric: tabular-nums;
}
.flash {
font-size: 0.85rem;
padding: 0.5rem 0.75rem;
border: 1px solid var(--site-text);
border-radius: 0.6rem;
}
.empty {
color: var(--site-muted);
padding: 1rem 0;
}
.items {
display: flex;
flex-direction: column;
gap: 0.75rem;
}
.card {
display: flex;
flex-direction: column;
gap: 0.6rem;
padding: 1rem;
border: 1px solid var(--site-border);
border-radius: 0.9rem;
background: var(--site-surface);
transition:
border-color 0.15s,
box-shadow 0.15s;
}
.card.sel,
.card:focus-visible {
border-color: var(--site-text);
box-shadow: inset 3px 0 0 var(--site-text);
outline: none;
}
header {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.5rem;
}
h3 {
flex: 1;
min-width: 12rem;
font-size: 0.98rem;
font-weight: 600;
}
h3 a {
color: var(--site-text);
}
.kind {
padding: 0.1rem 0.5rem;
border-radius: 999px;
font-size: 0.72rem;
font-weight: 650;
color: #fff;
}
.kind.add {
background: #15803d;
}
.kind.edit {
background: #1d4ed8;
}
.kind.delete {
background: #b91c1c;
}
.age,
.decided {
font-size: 0.75rem;
color: var(--site-muted);
}
.warn,
.dupe {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.35rem;
font-size: 0.8rem;
}
.warn {
padding: 0.4rem 0.6rem;
border: 1px dashed #d97706;
border-radius: 0.5rem;
}
.dupe a {
color: var(--site-text);
}
.diff {
width: 100%;
border-collapse: collapse;
font-size: 0.82rem;
table-layout: fixed;
}
.diff th,
.diff td {
padding: 0.35rem 0.5rem;
border-top: 1px solid var(--site-border);
text-align: left;
vertical-align: top;
white-space: pre-line;
overflow-wrap: anywhere;
}
.diff thead th {
font-size: 0.7rem;
text-transform: uppercase;
letter-spacing: 0.06em;
color: var(--site-muted);
border-top: 0;
}
.diff tbody th {
width: 7rem;
font-weight: 550;
}
del {
background: color-mix(in srgb, #dc2626 16%, transparent);
text-decoration-color: #dc2626;
}
ins {
background: color-mix(in srgb, #16a34a 18%, transparent);
text-decoration: none;
}
.meta {
display: grid;
grid-template-columns: auto 1fr;
gap: 0.3rem 0.9rem;
font-size: 0.83rem;
}
.meta dt {
color: var(--site-muted);
}
.reason {
white-space: pre-line;
}
.srcs {
display: flex;
flex-wrap: wrap;
gap: 0.25rem 0.75rem;
}
.srcs a {
color: var(--site-text);
}
.error {
color: #dc2626;
font-size: 0.83rem;
font-weight: 550;
}
form {
display: flex;
flex-direction: column;
gap: 0.6rem;
}
.tweak {
padding: 0.8rem;
border: 1px dashed var(--site-muted);
border-radius: 0.75rem;
}
.note {
display: flex;
flex-direction: column;
gap: 0.25rem;
font-size: 0.78rem;
color: var(--site-muted);
}
.note textarea {
width: 100%;
padding: 0.45rem 0.6rem;
border: 1px solid var(--site-border);
border-radius: 0.6rem;
background: var(--site-bg);
color: var(--site-text);
font-size: 0.85rem;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.4rem;
}
.actions button {
display: inline-flex;
align-items: center;
gap: 0.35rem;
padding: 0.45rem 0.8rem;
border: 1px solid var(--site-border);
border-radius: 0.6rem;
background: var(--site-bg);
color: var(--site-text);
font-size: 0.84rem;
cursor: pointer;
transition:
border-color 0.15s,
transform 0.15s;
}
.actions button:hover,
.actions button:focus-visible {
border-color: var(--site-muted);
transform: translateY(-1px);
}
.actions .primary {
background: var(--site-text);
color: var(--site-bg);
border-color: var(--site-text);
}
.actions .primary kbd {
border-color: currentColor;
}
.actions .danger:hover {
border-color: #dc2626;
color: #dc2626;
}
.actions button:disabled {
opacity: 0.6;
cursor: progress;
}
@media (prefers-reduced-motion: reduce) {
.actions button {
transition: none;
}
.actions button:hover {
transform: none;
}
}
.osm {
display: inline-block;
margin-top: 0.4rem;
font-size: 0.78rem;
color: var(--site-text);
}
</style>