diff --git a/apps/web/client/CommunityBoard.tsx b/apps/web/client/CommunityBoard.tsx index f9730a41..7a03b37c 100644 --- a/apps/web/client/CommunityBoard.tsx +++ b/apps/web/client/CommunityBoard.tsx @@ -27,6 +27,11 @@ function useStatusFocus(text: string | undefined) { useEffect(() => { if (text && (!document.activeElement || document.activeElement === document.body)) ref.current?.focus({ preventScroll: true }); }, [text]); return ref; } +type Filter = 'open' | 'solved' | 'all'; +const route = () => { + const task = /^#task-(T\d+)$/u.exec(location.hash)?.[1]; + return { tab: task ? 'tasks' : (['tasks', 'boards', 'activity', 'join'] as Tab[]).find(item => location.hash === `#${item}`) ?? 'tasks', open: task }; +}; const oneLine = (text: string) => text.replace(/\s+/gu, ' ').trim(); const when = (iso: string, language: string) => new Date(iso).toLocaleString(language === 'en' ? 'en' : 'zh-CN', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); @@ -35,8 +40,9 @@ export function CommunityBoard({ language }: { language: 'zh' | 'en' }) { const [overview, setOverview] = useState(); const [session, setSession] = useState(); const [me, setMe] = useState(); - const [tab, setTab] = useState(() => (['tasks', 'boards', 'activity', 'join'] as Tab[]).find(item => location.hash === `#${item}`) ?? 'tasks'); - const [open, setOpen] = useState(() => /^#task-(T\d+)$/u.exec(location.hash)?.[1]); + const [{ tab, open }, setRoute] = useState(route); + const [filter, setFilter] = useState('open'); + const tabs = useRef(null), came = useRef(), moved = useRef(false); const [error, setError] = useState(''); const refresh = async () => { try { @@ -46,8 +52,32 @@ export function CommunityBoard({ language }: { language: 'zh' | 'en' }) { } catch (failure) { setError(localizeMessage((failure as Error).message)); } }; useEffect(() => { void refresh(); const timer = setInterval(() => { if (!document.hidden) void refresh(); }, 15_000); return () => clearInterval(timer); }, []); - const go = (next: Tab) => { setTab(next); setOpen(undefined); history.replaceState(null, '', `#${next}`); }; - const show = (id?: string) => { setOpen(id); history.replaceState(null, '', id ? `#task-${id}` : '#tasks'); }; + useEffect(() => { + const follow = () => { + const next = route(); moved.current = true; + setRoute(previous => { if (previous.open && !next.open) came.current = previous.open; return next; }); + if ((tabs.current?.getBoundingClientRect().top ?? 0) < 0) tabs.current?.scrollIntoView(); + }; + addEventListener('hashchange', follow); return () => removeEventListener('hashchange', follow); + }, []); + useEffect(() => { + if (tab !== 'tasks' || open || !came.current) return; + document.querySelector(`[data-task="${came.current}"]`)?.focus({ preventScroll: true }); came.current = undefined; + }, [tab, open]); + const go = (next: Tab) => { location.hash = next; }; + const show = (id?: string) => { + const task = id && overview?.tasks.find(item => item.id === id); + if (task && filter !== 'all' && task.status !== filter) setFilter('all'); + location.hash = id ? `task-${id}` : 'tasks'; + }; + const names: [Tab, string][] = [['tasks', tx('题目', 'Problems')], ['boards', tx('排行榜', 'Leaderboards')], ['activity', tx('动态', 'Activity')], ['join', tx('参与', 'Join')]]; + const keys = (event: KeyboardEvent) => { + const buttons = [...tabs.current?.querySelectorAll('[role=tab]') ?? []], last = buttons.length - 1; + const at = buttons.indexOf(event.target as HTMLElement); // the focused tab, since the route updates only after hashchange + const next = at < 0 ? undefined : { ArrowRight: at === last ? 0 : at + 1, ArrowLeft: at === 0 ? last : at - 1, Home: 0, End: last }[event.key]; + if (next === undefined) return; + event.preventDefault(); go(names[next]![0]); buttons[next]?.focus(); + }; if (!overview) return
{error || tx('正在读取社区题目…', 'Loading community problems…')}
; const signedIn = Boolean(session?.account); return
@@ -66,24 +96,26 @@ export function CommunityBoard({ language }: { language: 'zh' | 'en' }) { {!overview.checker &&

{tx('证明校验服务当前不可用,暂不接收提交。', 'The proof checker is unavailable; submissions are paused.')}

} {error && } - - {tab === 'tasks' && (open - ? show()} changed={refresh} /> - : item.taskId))} />)} - {tab === 'boards' && } - {tab === 'activity' && { setTab('tasks'); show(id); }} />} - {tab === 'join' && } +
+ {names.map(([id, name]) => )} +
+
+ {tab === 'tasks' && (open + ? show()} changed={refresh} /> + : item.taskId))} />)} + {tab === 'boards' && } + {tab === 'activity' && } + {tab === 'join' && } +
; } const statusText = (tx: Tx, status: string) => ({ open: tx('开放', 'Open'), solved: tx('已解出', 'Solved'), withdrawn: tx('已撤下', 'Withdrawn'), queued: tx('排队中', 'Queued'), checking: tx('校验中', 'Checking'), accepted: tx('通过', 'Accepted'), rejected: tx('未通过', 'Rejected'), late: tx('通过但晚了', 'Accepted, too late') } as Record)[status] ?? status; -function TaskList({ tasks, tx, open, mine }: { tasks: CommunityPublicTask[]; tx: Tx; open: (id: string) => void; mine: Set }) { - const [filter, setFilter] = useState<'open' | 'solved' | 'all'>('open'); +function TaskList({ tasks, tx, open, filter, setFilter, mine }: { tasks: CommunityPublicTask[]; tx: Tx; open: (id: string) => void; + filter: Filter; setFilter: (filter: Filter) => void; mine: Set }) { const shown = tasks.filter(task => filter === 'all' || task.status === filter); return
{(['open', 'solved', 'all'] as const).map(id => @@ -92,7 +124,7 @@ function TaskList({ tasks, tx, open, mine }: { tasks: CommunityPublicTask[]; tx: {tasks.filter(task => id === 'all' || task.status === id).length})}
{!shown.length ?

{filter === 'open' ? tx('目前没有开放的题目。', 'No open problems right now.') : tx('还没有题目。', 'No problems yet.')}

:
    {shown.map(task =>
  1. -
; } -function TaskDetail({ id, tx, language, me, signedIn, checker, back, changed }: { id: string; tx: Tx; language: string; me?: Me; signedIn: boolean; +function TaskDetail({ id, focus, tx, language, me, signedIn, checker, back, changed }: { id: string; focus: boolean; tx: Tx; language: string; me?: Me; signedIn: boolean; checker: boolean; back: () => void; changed: () => Promise }) { const [detail, setDetail] = useState(); const [source, setSource] = useState(''); @@ -116,7 +148,10 @@ function TaskDetail({ id, tx, language, me, signedIn, checker, back, changed }: const [harness, setHarness] = useState(() => localStorage.getItem('theoremos.community.harness') ?? ''); const [message, setMessage] = useState(''), [busy, setBusy] = useState(false); const status = useStatusFocus(message); + const title = useRef(null); const load = () => call(`/api/community/tasks/${id}`).then(setDetail, failure => setMessage(localizeMessage((failure as Error).message))); + const ready = Boolean(detail); + useEffect(() => { if (ready && focus && (!document.activeElement || document.activeElement === document.body)) title.current?.focus({ preventScroll: true }); }, [ready]); useEffect(() => { void load(); const timer = setInterval(() => { if (!document.hidden) void load(); }, 8_000); return () => clearInterval(timer); }, [id]); useEffect(() => { if (detail && !source) setSource(`import ${detail.task.module}\n\ntheorem solution : ${detail.task.module}.Goal := by\n sorry\n`); }, [detail]); if (!detail) return

{message || tx('正在读取题目…', 'Loading problem…')}

; @@ -131,7 +166,7 @@ function TaskDetail({ id, tx, language, me, signedIn, checker, back, changed }: }, tx('已提交,正在排队校验。结果会显示在下方。', 'Submitted. It is queued for checking; the result appears below.')); return
-
{task.id}

{task.title}

+
{task.id}

{task.title}

{statusText(tx, task.status)}
{task.description &&

{task.description}

}
@@ -161,7 +196,7 @@ function TaskDetail({ id, tx, language, me, signedIn, checker, back, changed }: {me.admin && }