Files
usaco/site/web/src/views/ProblemList.vue
T
adamf 7638d88570
Test and publish the site / test (push) Successful in 51s
Test and publish the site / publish (push) Successful in 1m39s
Refresh paper UI styling
2026-10-23 20:00:46 +01:00

370 lines
11 KiB
Vue

<script setup>
/**
* The problem list. Visitors see solved problems; the admin also sees what
* is in progress or skipped (the server decides, this only shows the tabs
* when there is something behind them). Filter and sort live in the query
* string so a filtered view can be linked and survives the back button.
*/
import { computed, onMounted, ref } from "vue";
import { RouterLink, useRoute, useRouter } from "vue-router";
import { importLeetCode, listProblems, startDaily } from "@/api";
import { LANG_LABELS, STATUS_LABELS, formatDate, title } from "@/format";
const route = useRoute();
const router = useRouter();
const problems = ref([]);
const admin = ref(false);
const loading = ref(true);
const error = ref("");
// "Today's LeetCode" imports the daily question as `usaco daily` would, and
// a pasted link the problem it names as `usaco new <link>` would; either
// opens on the solve page, and one already in the repo is just opened. One
// import at a time: they share the busy flag and the error line.
const importing = ref("");
const importError = ref("");
const link = ref("");
async function startImport(kind, start) {
importing.value = kind;
importError.value = "";
try {
const { name } = await start();
router.push({ name: "solve", params: { name } });
} catch (e) {
importError.value = e.message;
importing.value = "";
}
}
const openDaily = () => startImport("daily", startDaily);
const openLink = () => link.value.trim() && startImport("link", () => importLeetCode(link.value.trim()));
const status = computed({
get: () => route.query.status || "all",
set: (v) => router.replace({ query: { ...route.query, status: v === "all" ? undefined : v } }),
});
const sort = computed({
get: () => route.query.sort || "recent",
set: (v) => router.replace({ query: { ...route.query, sort: v === "recent" ? undefined : v } }),
});
const search = computed({
get: () => route.query.q || "",
set: (v) => router.replace({ query: { ...route.query, q: v || undefined } }),
});
onMounted(async () => {
try {
const data = await listProblems();
problems.value = data.problems;
admin.value = data.admin;
} catch (e) {
error.value = e.message;
} finally {
loading.value = false;
}
});
const counts = computed(() => {
const c = { all: problems.value.length, solved: 0, solving: 0, skipped: 0 };
for (const p of problems.value) c[p.status]++;
return c;
});
// An empty tab is left out unless it is the one selected (a linked view).
const tabs = computed(() =>
admin.value ? ["all", "solving", "solved", "skipped"].filter((t) => counts.value[t] || status.value === t) : [],
);
// When a problem was last worked on, for sorting: the solve date if it has
// one, else its last commit.
const when = (p) => p.solvedAt || p.lastTouched || "";
const shown = computed(() => {
const q = search.value.trim().toLowerCase();
const list = problems.value.filter(
(p) =>
(status.value === "all" || p.status === status.value) &&
(!q || p.name.includes(q.replace(/\s+/g, "-")) || (p.source || "").toLowerCase().includes(q)),
);
if (sort.value === "name") return list.sort((a, b) => a.name.localeCompare(b.name));
return list.sort((a, b) => when(b).localeCompare(when(a)) || a.name.localeCompare(b.name));
});
// Most recent is split by month, so a long list has landmarks; by name it
// is one block. An undated problem sorts last, under "Undated".
const groups = computed(() => {
if (sort.value === "name") return [{ key: "all", label: "", items: shown.value }];
const out = [];
for (const p of shown.value) {
const w = when(p);
const key = w ? w.slice(0, 7) : "none";
if (out.at(-1)?.key !== key) {
out.push({ key, label: w ? formatDate(w, { month: "long", year: "numeric" }) : "Undated", items: [] });
}
out.at(-1).items.push(p);
}
return out;
});
</script>
<template>
<section>
<header class="page-heading">
<div>
<p class="eyebrow">Practice archive</p>
<h1>Problems</h1>
</div>
<span v-if="!loading && !error" class="heading-count">{{ counts.all }} total</span>
</header>
<p v-if="!loading && !error" class="intro muted">
{{ counts.solved }} solved problems from USACO and other judges, each with its solution and test cases.
</p>
<div class="controls">
<div v-if="tabs.length" class="tabs" role="tablist">
<button
v-for="t in tabs"
:key="t"
role="tab"
:aria-selected="status === t"
:class="{ on: status === t }"
@click="status = t"
>
{{ t === "all" ? "All" : STATUS_LABELS[t] }} <span class="n">{{ counts[t] }}</span>
</button>
</div>
<input v-model="search" type="search" placeholder="Search problems" aria-label="Search problems" />
<select v-model="sort" aria-label="Sort by">
<option value="recent">Most recent</option>
<option value="name">Name</option>
</select>
</div>
<form v-if="admin" class="controls" @submit.prevent="openLink">
<input v-model="link" type="url" placeholder="Paste a LeetCode link" aria-label="LeetCode problem link" />
<button class="btn" type="submit" :disabled="!!importing || !link.trim()">
{{ importing === "link" ? "Importing…" : "Import" }}
</button>
<button class="btn primary" type="button" :disabled="!!importing" @click="openDaily">
{{ importing === "daily" ? "Fetching today's problem…" : "Today's LeetCode" }}
</button>
</form>
<p v-if="importError" class="error import-error">{{ importError }}</p>
<p v-if="loading" class="muted">Loading…</p>
<p v-else-if="error" class="error">{{ error }}</p>
<p v-else-if="!shown.length" class="muted">Nothing matches.</p>
<div v-else class="groups">
<section v-for="g in groups" :key="g.key">
<h2 v-if="g.label" class="month">{{ g.label }} <span class="n">{{ g.items.length }}</span></h2>
<ul class="list card">
<li v-for="p in g.items" :key="p.name">
<RouterLink :to="{ name: 'problem', params: { name: p.name } }" class="row">
<span class="name">{{ title(p.name) }}</span>
<span class="sub">
<span class="meta muted">
<template v-if="p.langs.length">{{ p.langs.map((l) => LANG_LABELS[l] || l).join(", ") }}</template>
<template v-if="p.tests"> · {{ p.tests }} test{{ p.tests === 1 ? "" : "s" }}</template>
<template v-if="p.hasNotes"> · notes</template>
<template v-if="p.hasExplanation"> · explained</template>
<template v-if="p.reviewCount"> · {{ p.reviewCount }} review{{ p.reviewCount === 1 ? "" : "s" }}</template>
</span>
<span class="tags">
<span v-if="p.source" class="badge">{{ p.source }}</span>
<span v-if="admin || p.status !== 'solved'" class="badge" :class="p.status">{{ STATUS_LABELS[p.status] }}</span>
</span>
</span>
<time v-if="when(p)" class="muted" :class="{ full: !g.label }" :datetime="when(p)">
{{ g.label ? formatDate(when(p), { day: "numeric", month: "short" }) : formatDate(when(p)) }}
</time>
</RouterLink>
</li>
</ul>
</section>
</div>
</section>
</template>
<style scoped>
.intro {
max-width: 58ch;
margin: -8px 0 22px;
}
.page-heading {
display: flex;
align-items: end;
justify-content: space-between;
gap: 16px;
}
.eyebrow {
margin: 0 0 4px;
color: var(--color-accent);
font-size: 0.75rem;
font-weight: bold;
letter-spacing: 0.12em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 3rem;
}
.heading-count {
padding-bottom: 5px;
color: var(--color-ink-faint);
font-size: 0.85rem;
font-variant-numeric: tabular-nums;
}
.import-error {
margin: -8px 0 16px;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 8px 12px;
align-items: center;
margin-bottom: 12px;
padding: 8px;
border: 1px solid var(--color-line-soft);
border-radius: 8px;
background: rgb(255 255 255 / 0.42);
}
.tabs {
display: flex;
flex-wrap: wrap;
gap: 4px;
margin-right: auto;
}
.tabs button {
padding: 4px 12px;
border-radius: 999px;
border: 1px solid transparent;
color: var(--color-ink-muted);
cursor: pointer;
}
.tabs button.on {
border-color: var(--color-ink);
color: var(--color-ink);
background: var(--color-paper);
box-shadow: 0 1px 3px rgb(46 39 28 / 0.08);
}
.tabs .n {
color: var(--color-ink-faint);
margin-left: 2px;
}
input,
select {
padding: 6px 10px;
border: 1px solid var(--color-line);
border-radius: 4px;
background: var(--color-paper);
min-width: 0;
}
input {
flex: 1 1 200px;
max-width: 320px;
}
.groups {
display: grid;
gap: 20px;
}
.month {
font-size: 1.4rem;
margin-bottom: 6px;
}
.month .n {
font-family: var(--font-body);
font-size: 0.8rem;
color: var(--color-ink-faint);
margin-left: 4px;
}
.list > li + li {
border-top: 1px solid var(--color-line-soft);
}
/* Name and date on the first line, details and badges on the second, at
every width, so rows line up instead of wrapping each its own way. On a
phone the second line is one flex row, so the badges don't widen the date
column and squeeze the name. */
.row {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
grid-template-areas:
"name date"
"sub sub";
align-items: center;
gap: 2px 16px;
padding: 10px 16px;
color: inherit;
text-decoration: none;
}
.row:hover {
background: var(--color-accent-soft);
}
li:first-child .row {
border-radius: 6px 6px 0 0;
}
li:last-child .row {
border-radius: 0 0 6px 6px;
}
.name {
grid-area: name;
font-size: 1.05rem;
overflow-wrap: anywhere;
}
.sub {
grid-area: sub;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 4px 12px;
}
.meta {
grid-area: meta;
font-size: 0.8rem;
}
.tags {
grid-area: tags;
display: flex;
justify-content: flex-end;
gap: 6px;
}
time {
grid-area: date;
font-size: 0.85rem;
text-align: right;
white-space: nowrap;
}
/* Fixed widths, so the dates line up down a card. */
@media (min-width: 640px) {
time {
min-width: 4em;
}
time.full {
min-width: 6.5em;
}
}
/* Wide enough for one line: badges and date in columns of their own. */
@media (min-width: 640px) {
.row {
grid-template-columns: minmax(0, 1fr) auto auto;
grid-template-areas:
"name tags date"
"meta tags date";
}
.sub {
display: contents;
}
}
@media (max-width: 639px) {
.page-heading {
align-items: flex-start;
}
h1 {
font-size: 2.5rem;
}
.heading-count {
padding-top: 12px;
}
}
</style>