Files
usaco/site/web/src/views/ConceptPage.vue
T
adamfandClaude Opus 5.5 cae5ec8e5a
Test and publish the site / test (push) Successful in 2m15s
Test and publish the site / publish (push) Successful in 9m5s
Keep site jobs across restarts, meter Claude spend, harden admin routes
- internal/jobs replaces explain.Jobs: one registry for every kind of
  background job, with records in DATA_DIR/jobs. A job's draft is written
  before its commit, so a failed commit can be saved again (POST
  /jobs/<kind>/<key>/save) and a restart mid-commit resumes it, without
  paying Claude twice.
- Graceful shutdown: SIGTERM stops new jobs and waits SHUTDOWN_GRACE for
  running ones while still serving the pages that poll them.
- internal/usage records every Claude call's tokens and estimated cost in
  DATA_DIR/usage.jsonl; CLAUDE_MONTHLY_BUDGET refuses new jobs (429) once a
  month reaches it. The stats page shows the admin the spend.
- auth.SameOrigin refuses cross-origin writes to admin routes.
- The LeetCode importer runs the clone's script without the server's
  secrets in its environment.
- A sync that leaves HEAD where it was skips the re-index.
- docs_test.go checks README's route table against the routes, and the CLI
  page against the root README's commands.
- The repo is going private: Gitea links on the site are admin-only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-23 22:10:01 +01:00

259 lines
6.7 KiB
Vue

<script setup>
/**
* One concept: the snippet to reuse first, since that is what a return
* visit is usually for, then the explanation folded into sections like a
* problem's. The admin can have it rewritten, with a note on what to
* change, or forget it.
*/
import { onUnmounted, ref, watch } from "vue";
import { RouterLink, useRouter } from "vue-router";
import { deleteConcept, getConcept, getMe, rewriteConcept, rewriteStatus, saveJobAgain } from "@/api";
import { formatDate } from "@/format";
import CodeBlock from "@/components/CodeBlock.vue";
import Explanation from "@/components/Explanation.vue";
const props = defineProps({ slug: { type: String, required: true } });
const router = useRouter();
const concept = ref(null);
const admin = ref(false);
const loading = ref(true);
const error = ref("");
const panel = ref("");
const note = ref("");
const job = ref({ state: "idle" });
const actionError = ref("");
let poll = null;
function stopPolling() {
clearTimeout(poll);
poll = null;
}
onUnmounted(stopPolling);
async function load() {
stopPolling();
loading.value = true;
error.value = "";
panel.value = "";
job.value = { state: "idle" };
try {
const [c, me] = await Promise.all([getConcept(props.slug), getMe().catch(() => ({ admin: false }))]);
concept.value = c;
admin.value = me.admin;
// A rewrite started before a reload is still worth waiting for.
if (me.admin) checkRewrite();
} catch (e) {
error.value = e.message;
} finally {
loading.value = false;
}
}
watch(() => props.slug, load, { immediate: true });
async function checkRewrite() {
stopPolling();
const slug = props.slug;
try {
const s = await rewriteStatus(slug);
if (slug !== props.slug) return;
const finished = job.value.state === "running" && s.state === "done";
job.value = s;
if (s.state === "running") {
poll = setTimeout(checkRewrite, 3000);
} else if (finished) {
concept.value = await getConcept(slug);
}
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
async function rewrite() {
try {
job.value = await rewriteConcept(props.slug, { note: note.value });
panel.value = "";
note.value = "";
poll = setTimeout(checkRewrite, 3000);
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
async function saveAgain() {
try {
job.value = await saveJobAgain(job.value);
poll = setTimeout(checkRewrite, 3000);
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
async function forget() {
if (!confirm(`Delete “${concept.value.title}” and its snippet?`)) return;
actionError.value = "";
try {
await deleteConcept(props.slug);
router.push({ name: "concepts" });
} catch (e) {
actionError.value = e.message;
}
}
</script>
<template>
<div class="page">
<RouterLink to="/concepts" class="back">← All concepts</RouterLink>
<p v-if="loading && !concept" class="muted">Loading…</p>
<p v-else-if="error" class="error">{{ error }}</p>
<template v-else-if="concept">
<header>
<p class="eyebrow">Reference guide</p>
<h1>{{ concept.title }}</h1>
<div class="facts">
<span v-for="t in concept.tags" :key="t" class="badge">{{ t }}</span>
<time v-if="concept.updated" class="muted" :datetime="concept.updated">
Updated {{ formatDate(concept.updated) }}
</time>
</div>
</header>
<section v-if="admin" class="admin">
<div class="admin-row">
<button class="btn" :disabled="job.state === 'running'" @click="panel = panel === 'rewrite' ? '' : 'rewrite'">
{{ job.state === "running" ? "Claude is rewriting…" : "Rewrite" }}
</button>
<button class="btn" :disabled="job.state === 'running'" @click="forget">Delete</button>
</div>
<div v-if="panel === 'rewrite'" class="panel">
<p class="muted">
Claude writes it again from the same question, starting from this version. Say what to change, or leave it
empty for a fresh take.
</p>
<textarea v-model="note" rows="3" placeholder="e.g. step 3's diagram is wrong; add range updates"></textarea>
<div class="admin-row">
<button class="btn primary" @click="rewrite">Rewrite</button>
<button class="btn" @click="panel = ''">Cancel</button>
</div>
</div>
<p v-if="job.state === 'error'" class="error">
Rewrite: {{ job.error }}
<button v-if="job.unsaved" class="btn" @click="saveAgain">Save again</button>
</p>
<p v-if="actionError" class="error">{{ actionError }}</p>
</section>
<section v-if="concept.snippet">
<h2>Code</h2>
<p class="filename muted">snippet.cpp</p>
<CodeBlock :content="concept.snippet.content" lang="cpp" :truncated="concept.snippet.truncated" />
<p class="cli muted">
From the CLI: <code>usaco snippet {{ concept.slug }} &lt;problem&gt;</code> pastes it above
<code>main</code>.
</p>
</section>
<section v-if="concept.content">
<h2>Explanation</h2>
<Explanation :source="concept.content" :open="['Intuition']" />
</section>
</template>
</div>
</template>
<style scoped>
.page {
display: grid;
gap: 24px;
min-width: 0;
}
.page > section {
min-width: 0;
}
.back {
font-size: 0.875rem;
color: var(--color-ink-muted);
text-decoration: none;
}
.back:hover {
color: var(--color-ink);
}
.eyebrow {
margin: 0 0 5px;
color: var(--color-accent);
font-size: 0.75rem;
font-weight: bold;
letter-spacing: 0.12em;
text-transform: uppercase;
}
h1 {
font-size: 3rem;
margin: 0 0 12px;
overflow-wrap: anywhere;
}
h2 {
font-size: 1.6rem;
margin-bottom: 8px;
}
.facts {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 6px 10px;
font-size: 0.9rem;
}
.admin {
display: grid;
gap: 10px;
padding: 14px 16px;
border: 1px solid var(--color-line-soft);
border-top: 3px solid var(--color-accent);
border-radius: 8px;
background: var(--color-paper);
box-shadow: var(--shadow-paper);
}
.admin-row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.panel {
display: grid;
gap: 10px;
}
.panel p {
margin: 0;
font-size: 0.9rem;
}
textarea {
padding: 8px 10px;
border: 1px solid var(--color-line);
border-radius: 4px;
font-family: var(--font-mono);
font-size: 0.85rem;
color: var(--color-ink);
background: var(--color-paper);
}
.filename {
margin: 0 0 4px;
font-family: var(--font-mono);
font-size: 0.8rem;
}
.cli {
margin: 8px 0 0;
font-size: 0.85rem;
}
.cli code {
font-family: var(--font-mono);
}
@media (max-width: 639px) {
h1 {
font-size: 2.5rem;
}
}
</style>