Import LeetCode problems from a pasted link on the site
Test and publish the site / test (push) Successful in 1m9s
Test and publish the site / publish (push) Successful in 1m31s

The problem list's admin row gains a link box beside "Today's LeetCode":
POST /leetcode/import takes {url} and does what `usaco new
<leetcode-link>` does, through the same lib/leetcode.py run and "Start
<slug>" commit as the daily question, then opens the solve page. A
problem already in the repo is only opened.

leetcode.Slug reads the link as slug_of in lib/leetcode.py does: full
links with /description/ and query strings, www., no scheme, or a bare
slug. Anything else is a 400.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 13:02:11 +01:00
co-authored by Claude Opus 5.5
parent ccf4e4dd74
commit 3a8f651a6f
8 changed files with 179 additions and 23 deletions
+27 -16
View File
@@ -7,7 +7,7 @@
*/
import { computed, onMounted, ref } from "vue";
import { RouterLink, useRoute, useRouter } from "vue-router";
import { listProblems, startDaily } from "@/api";
import { importLeetCode, listProblems, startDaily } from "@/api";
import { LANG_LABELS, STATUS_LABELS, formatDate, title } from "@/format";
const route = useRoute();
@@ -18,21 +18,26 @@ const admin = ref(false);
const loading = ref(true);
const error = ref("");
// "Today's LeetCode" imports the daily question as `usaco daily` would and
// opens it on the solve page; one already in the repo is just opened.
const starting = ref(false);
const dailyError = ref("");
async function openDaily() {
starting.value = true;
dailyError.value = "";
// "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 startDaily();
const { name } = await start();
router.push({ name: "solve", params: { name } });
} catch (e) {
dailyError.value = e.message;
starting.value = false;
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",
@@ -126,11 +131,17 @@ const groups = computed(() => {
<option value="recent">Most recent</option>
<option value="name">Name</option>
</select>
<button v-if="admin" class="btn primary" :disabled="starting" @click="openDaily">
{{ starting ? "Fetching today's problem…" : "Today's LeetCode" }}
</button>
</div>
<p v-if="dailyError" class="error daily-error">{{ dailyError }}</p>
<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>
@@ -171,7 +182,7 @@ const groups = computed(() => {
.intro {
margin-bottom: 16px;
}
.daily-error {
.import-error {
margin: -8px 0 16px;
}
.controls {