Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(mastery): polish migrated topics and live cleanup

Bingxi Zhao (Frank)e67643ba11ec
diff --git a/web/app/(utility)/space/learning/[pathId]/page.tsx b/web/app/(utility)/space/learning/[pathId]/page.tsx
index ea148dffe..9163dc8bd 100644
--- a/web/app/(utility)/space/learning/[pathId]/page.tsx
+++ b/web/app/(utility)/space/learning/[pathId]/page.tsx
@@ -20,7 +20,10 @@ import {
import { AdventureMap } from "@/components/space/learning/AdventureMap";
import { EditTopicRouteDialog } from "@/components/space/learning/EditTopicRouteDialog";
-import type { Translate } from "@/components/space/learning/format";
+import {
+ topicDisplayName,
+ type Translate,
+} from "@/components/space/learning/format";
import { ReviewTrail } from "@/components/space/learning/ReviewTrail";
import { SessionCamp } from "@/components/space/learning/SessionCamp";
import { useMasteryPathActivity } from "@/hooks/useMasteryPathActivity";
@@ -97,6 +100,7 @@ export default function MasteryTopicPage() {
() => topic?.sources.filter((source) => source.kind !== "goal").slice(0, 5) ?? [],
[topic],
);
+ const displayName = topic ? topicDisplayName(topic, tr) : "";
const refresh = async () => {
await Promise.all([loadTopic(), loadSessions()]);
@@ -223,7 +227,7 @@ export default function MasteryTopicPage() {
</span>
<div className="min-w-0">
<h1 className="truncate text-3xl font-semibold tracking-[-0.035em] text-[var(--foreground)] sm:text-[36px]">
- {topic.name}
+ {displayName}
</h1>
<p className="mt-1 max-w-3xl text-sm text-[var(--muted-foreground)]">
{topic.metadata.description || topic.metadata.goal}
diff --git a/web/components/space/learning/MasteryStudy.tsx b/web/components/space/learning/MasteryStudy.tsx
index 7d4f3cf6a..29b0b6dc0 100644
--- a/web/components/space/learning/MasteryStudy.tsx
+++ b/web/components/space/learning/MasteryStudy.tsx
@@ -31,7 +31,7 @@ import { fetchMasteryTopic, type MasteryTopic } from "@/lib/learning-api";
import { shouldSubmitOnEnter } from "@/lib/composer-keyboard";
import { useImeComposing } from "@/lib/use-ime-composing";
-import type { Translate } from "./format";
+import { topicDisplayName, type Translate } from "./format";
const STARTERS = [
{
@@ -51,7 +51,7 @@ const STARTERS = [
},
] as const;
-function currentWaypoint(topic: MasteryTopic) {
+function currentWaypoint(topic: MasteryTopic, fallback: string) {
if (topic.next.knowledge_point_name) return topic.next.knowledge_point_name;
for (const region of topic.map.modules) {
const point = region.knowledge_points.find(
@@ -59,7 +59,7 @@ function currentWaypoint(topic: MasteryTopic) {
);
if (point) return point.name;
}
- return topic.map.complete ? "Journey complete" : topic.name;
+ return topic.map.complete ? "Journey complete" : fallback;
}
export function MasteryStudy({
@@ -267,7 +267,8 @@ export function MasteryStudy({
);
}
- const waypoint = currentWaypoint(topic);
+ const displayName = topicDisplayName(topic, tr);
+ const waypoint = currentWaypoint(topic, displayName);
const completed = topic.map.counts.mastered;
const total = topic.map.counts.total;
const progress = total ? Math.round((completed / total) * 100) : 0;
@@ -289,7 +290,7 @@ export function MasteryStudy({
<div className="min-w-0 flex-1">
<div className="flex items-center gap-2">
<h1 className="truncate text-sm font-semibold text-[var(--foreground)]">
- {topic.name}
+ {displayName}
</h1>
<span className="hidden rounded-full bg-[var(--mastery-route)]/10 px-2 py-0.5 text-[9px] font-bold uppercase tracking-[0.13em] text-[var(--mastery-route)] sm:inline">
{tr("半引导学习", "Guided trail")}
diff --git a/web/components/space/learning/TopicMapCard.tsx b/web/components/space/learning/TopicMapCard.tsx
index ea800bdfe..b96852014 100644
--- a/web/components/space/learning/TopicMapCard.tsx
+++ b/web/components/space/learning/TopicMapCard.tsx
@@ -12,7 +12,7 @@ import {
import type { MasteryTopic } from "@/lib/learning-api";
-import type { Translate } from "./format";
+import { topicDisplayName, type Translate } from "./format";
const ACCENTS = ["#6f7f58", "#567e7a", "#9a6b4d", "#76688a", "#8a7650"];
@@ -49,13 +49,14 @@ export function TopicMapCard({
const travelled = Math.max(0, Math.min(points.length - 1, Math.floor(progress * points.length)));
const accent = ACCENTS[metadata.map_seed % ACCENTS.length];
const due = topic.reviews.filter((review) => review.due).length;
+ const displayName = topicDisplayName(topic, tr);
return (
<Link
href={`/space/learning/${encodeURIComponent(topic.path_id)}`}
aria-label={tr(
- `打开 ${topic.name},已完成 ${mastered}/${total} 个关卡`,
- `Open ${topic.name}, ${mastered} of ${total} waypoints complete`,
+ `打开 ${displayName},已完成 ${mastered}/${total} 个关卡`,
+ `Open ${displayName}, ${mastered} of ${total} waypoints complete`,
)}
className="mastery-map-card group block overflow-hidden rounded-[22px] border border-[var(--border)] bg-[var(--card)] focus:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ring)]"
>
@@ -143,7 +144,7 @@ export function TopicMapCard({
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<h2 className="truncate text-[17px] font-semibold tracking-[-0.01em] text-[var(--foreground)]">
- {topic.name}
+ {displayName}
</h2>
<p className="mt-1 line-clamp-2 min-h-10 text-[13px] leading-5 text-[var(--muted-foreground)]">
{metadata.description || metadata.goal}
diff --git a/web/components/space/learning/format.ts b/web/components/space/learning/format.ts
index 7a0d5fa4f..d2de9e4ad 100644
--- a/web/components/space/learning/format.ts
+++ b/web/components/space/learning/format.ts
@@ -2,6 +2,23 @@
export type Translate = (cn: string, en: string) => string;
+/**
+ * Older paths did not require a human-readable title and sometimes stored the
+ * generated path id as the name. Keep a short trace suffix without exposing a
+ * database-shaped identifier as the primary label.
+ */
+export function topicDisplayName(
+ topic: { name: string; path_id: string },
+ tr: Translate,
+): string {
+ const name = topic.name.trim();
+ if (name && name !== topic.path_id && !/^unified_\d+_[a-z0-9]+$/i.test(name)) {
+ return name;
+ }
+ const suffix = topic.path_id.split("_").at(-1)?.slice(-4) || "map";
+ return `${tr("探索路线", "Exploration trail")} · ${suffix}`;
+}
+
const MINUTE = 60;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
diff --git a/web/lib/learning-api.ts b/web/lib/learning-api.ts
index 0cf9c7878..5be55bd60 100644
--- a/web/lib/learning-api.ts
+++ b/web/lib/learning-api.ts
@@ -441,6 +441,9 @@ export async function fetchMasteryTopics(
init,
"load topics",
);
+ if (!Array.isArray(result.topics)) {
+ throw new Error("Failed to load topics: the server returned an invalid response");
+ }
return result.topics;
}
@@ -527,5 +530,10 @@ export async function fetchMasteryTopicSessions(
init,
"load topic sessions",
);
+ if (!Array.isArray(result.sessions)) {
+ throw new Error(
+ "Failed to load topic sessions: the server returned an invalid response",
+ );
+ }
return result.sessions;
}
diff --git a/web/lib/reconnecting-websocket.ts b/web/lib/reconnecting-websocket.ts
index b3a6de688..1d4b98a7e 100644
--- a/web/lib/reconnecting-websocket.ts
+++ b/web/lib/reconnecting-websocket.ts
@@ -121,7 +121,10 @@ export class ReconnectingWebSocket {
if (this.stopped) return;
this.stopped = true;
this.clearRetry();
- this.releaseSocket();
+ // Closing a browser WebSocket while it is still CONNECTING emits a noisy
+ // console warning. Let that one finish its handshake, then close it without
+ // allowing any application callbacks to run.
+ this.releaseSocket(true);
this.retryAttempt = 0;
}
@@ -186,14 +189,18 @@ export class ReconnectingWebSocket {
this.retryHandle = null;
}
- private releaseSocket(): void {
+ private releaseSocket(deferConnectingClose = false): void {
const socket = this.socket;
this.socket = null;
if (!socket) return;
- socket.onopen = null;
socket.onmessage = null;
socket.onerror = null;
socket.onclose = null;
+ if (deferConnectingClose && socket.readyState === CONNECTING) {
+ socket.onopen = () => socket.close();
+ return;
+ }
+ socket.onopen = null;
if (socket.readyState <= OPEN) socket.close();
}
}
diff --git a/web/tests/mastery-format.test.ts b/web/tests/mastery-format.test.ts
new file mode 100644
index 000000000..1b664a7e2
--- /dev/null
+++ b/web/tests/mastery-format.test.ts
@@ -0,0 +1,29 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+import { topicDisplayName } from "../components/space/learning/format";
+
+const tr = (cn: string) => cn;
+
+test("mastery topic display names preserve authored titles", () => {
+ assert.equal(
+ topicDisplayName(
+ { name: "Agentic RAG 核心架构设计", path_id: "unified_1_abcd" },
+ tr,
+ ),
+ "Agentic RAG 核心架构设计",
+ );
+});
+
+test("legacy generated topic ids become friendly traceable labels", () => {
+ assert.equal(
+ topicDisplayName(
+ {
+ name: "unified_1787837164040_28bb00dc",
+ path_id: "unified_1787837164040_28bb00dc",
+ },
+ tr,
+ ),
+ "探索路线 · 00dc",
+ );
+});
diff --git a/web/tests/reconnecting-websocket.test.ts b/web/tests/reconnecting-websocket.test.ts
index b7e885817..9461f7dd2 100644
--- a/web/tests/reconnecting-websocket.test.ts
+++ b/web/tests/reconnecting-websocket.test.ts
@@ -157,6 +157,17 @@ test("stop cancels pending retries and closes without reconnecting", () => {
assert.equal(sockets.length, 1);
});
+test("stop defers closing a connecting browser socket until it opens", () => {
+ const { client, sockets } = createHarness();
+ client.start();
+ client.stop();
+
+ assert.equal(sockets[0].readyState, 0);
+ sockets[0].open();
+ assert.equal(sockets[0].readyState, 3);
+ assert.equal(sockets.length, 1);
+});
+
test("partner composer restores focus after streaming and page return", () => {
const source = readFileSync(
path.resolve(process.cwd(), "components/partners/PartnerComposer.tsx"),