From 463d79ac2e4ef11d39b7611cacf2f01f0606d047 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 23:56:30 +0800 Subject: [PATCH] fix(people): adding a person from a flow returns to it with the new person current (BUG-2397) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8 --- docs/BUG_HISTORY.md | 12 ++++ .../chart-choice/chart-choice-page.tsx | 4 +- .../src/components/people/people-page.tsx | 28 +++++++- frontend/src/components/subject-switcher.tsx | 16 ++++- frontend/src/lib/person-return.ts | 71 +++++++++++++++++++ .../tests/add-profile-return-2397.test.ts | 69 ++++++++++++++++++ 6 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 frontend/src/lib/person-return.ts create mode 100644 frontend/tests/add-profile-return-2397.test.ts diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index 3a09f387f..07525f989 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -32583,3 +32583,15 @@ - **防复发**:合同测试钉住权限、操作人核对、原因必填、金额上限、幂等、审计;账本类型与数据库约束双向一致。 - **关联记录**:BUG-2057(账本)、BUG-2360(后台角色与身份角色同步)、订单补偿 `admin_adjust_order`。 - **修复版本**:1.5(待发)。 + +## BUG-2397 | 在生时纠正、报告、年运、星盘、问答里「新增一个人」,保存后停在星盘档案详情页,回不到原来的流程,新人也不是当前人物 + +- **状态**:resolved(staging 待负责人验收) +- **现象**:负责人 2026-11-25 手机测 staging:在生时纠正里从顶栏人物切换「新增一个人」,保存后落到星盘档案的详情页(填报时间 / 排盘时间、和 TA 对话、看星盘……),要自己回生时纠正、再选这个人才能继续。其他流程同样如此。负责人补充:新建的人应默认成为全站当前人物(顶栏「XX ⌄」)。 +- **触发条件**:任何页面顶栏人物切换(`SubjectSwitcher`:星盘页、生时纠正、报告、年运、合盘、星历的顶栏,问答的人物选择)里的「新增一个人」或「编辑」;生时纠正的「去补全资料」。 +- **根因**:这些入口都只链接到 `/people?add=1` / `/people?edit=`,不带来源;星盘档案的保存只会停在新人的详情,也不改当前人物。 +- **修复**:新 `frontend/src/lib/person-return.ts`:入口带 `?return=<站内路径>`,只认 `/`、`/chart`、`/rectification`、`/reports`、`/synastry`、`/ephemeris` 及其子路径(拒绝 `//`、协议、反斜杠、`/people` 自身、超长),不会跳出站外。人物切换的「新增一个人」「编辑」和生时纠正「去补全资料」都带上当前页;星盘档案读一次 `return` 后从地址里去掉。保存新增的人:设为全站当前人物(`setCurrentSubject`,存在本机),有来源就回到来源页,没有就照旧停在详情;编辑已有的人不改当前人物;取消也回到来源页。直接在星盘档案页新增的人同样成为当前人物(负责人规则)。合盘页自带的「新增星盘」(保存后直接选进合盘栏)不改全站当前人物:那是合盘的另一方。 +- **验证**:新测试 `frontend/tests/add-profile-return-2397.test.ts`(生时纠正往返、各流程路径、拒绝的路径、编辑不改当前人物、档案页新增成为当前人物、各入口接线);人物切换、档案、生时纠正相关测试 109 条通过;tsc 无错;改动文件 ESLint 0 错;`next build --webpack` 成功。 +- **防复发**:`afterPersonSave` 是唯一的「保存后去哪、谁成为当前」规则,测试钉住;返回路径只走白名单。 +- **关联记录**:issue #150(人物切换「编辑」)。 +- **修复版本**:staging 待推。 diff --git a/frontend/src/components/chart-choice/chart-choice-page.tsx b/frontend/src/components/chart-choice/chart-choice-page.tsx index 38380ab16..a955c3fcc 100644 --- a/frontend/src/components/chart-choice/chart-choice-page.tsx +++ b/frontend/src/components/chart-choice/chart-choice-page.tsx @@ -1,5 +1,6 @@ "use client"; +import { personEditHrefWithReturn } from "@/lib/person-return"; import { RECTIFICATION_EDITION_TAG } from "@/lib/chart-choice/edition"; import { Fragment, useCallback, useEffect, useRef, useState, type CSSProperties, type ReactNode } from "react"; import { ChevronLeft, ChevronRight } from "lucide-react"; @@ -96,7 +97,8 @@ type Step = const COPY = { title: "生时纠正", - profileHref: (subjectId: string) => (subjectId === "self" ? "/people?self=1" : `/people?edit=${encodeURIComponent(subjectId)}`), + // BUG-2397: after 保存 the archive form comes back here. + profileHref: (subjectId: string) => personEditHrefWithReturn(subjectId, "/rectification"), }; class ChartChoiceRequestError extends Error { diff --git a/frontend/src/components/people/people-page.tsx b/frontend/src/components/people/people-page.tsx index 6bcbc3cfd..11e8adf7c 100644 --- a/frontend/src/components/people/people-page.tsx +++ b/frontend/src/components/people/people-page.tsx @@ -55,6 +55,8 @@ import { } from "@/components/ui/alert-dialog"; import { Button } from "@/components/ui/button"; import { PERSON_DELETE_COPY, personDeleteDescription } from "@/lib/person-delete"; +import { navigateInApp } from "@/lib/app-navigation"; +import { PERSON_RETURN_PARAM, afterPersonSave, safePersonReturnPath } from "@/lib/person-return"; import { Card, CardAction } from "@/components/ui/card"; import { PersonFormDialog } from "@/components/person-form-dialog"; import { @@ -127,6 +129,8 @@ export function PeoplePage() { const [saving, setSaving] = useState(false); const pushedDetail = useRef(false); const accountIdRef = useRef(""); + // BUG-2397: the flow that opened 添加 / 编辑 (`?return=`), taken back to after 保存 or 取消. + const returnToRef = useRef(null); const { account, setAccount } = useSessionList(); const selfAvatar: PeopleSelfAvatar | null = account?.avatar ? { avatar: account.avatar, imageVersion: avatarImageVersion(account) } @@ -172,6 +176,8 @@ export function PeoplePage() { const editId = url.searchParams.get("self") === "1" ? "self" : url.searchParams.get("edit"); const editRow = editId ? nextPeople.find((person) => person.id === editId) : undefined; if (add || editId) { + returnToRef.current = safePersonReturnPath(url.searchParams.get(PERSON_RETURN_PARAM)); + url.searchParams.delete(PERSON_RETURN_PARAM); url.searchParams.delete("add"); url.searchParams.delete("self"); url.searchParams.delete("edit"); @@ -262,6 +268,20 @@ export function PeoplePage() { showDetail(id); } + /** BUG-2397: back to the flow that opened the form, if one did. */ + function leaveToFlow(): boolean { + const target = returnToRef.current; + if (!target) return false; + returnToRef.current = null; + navigateInApp(target); + return true; + } + + function cancelEdit() { + setEditing(false); + leaveToFlow(); + } + function startAdd() { if (atLimit) { notifyInfo(LIMIT_COPY); @@ -289,6 +309,11 @@ export function PeoplePage() { setEditing(false); invalidateSubjectCatalog(); await load(); + // BUG-2397 (owner 11-25): a person just added becomes the current person everywhere, + // and a form opened from a flow goes back to it. + const next = afterPersonSave({ addedId: editingOther ? null : saved.id || null, returnTo: returnToRef.current }); + if (next.currentSubject) setCurrentSubject(next.currentSubject); + if (next.navigateTo && leaveToFlow()) return; if (saved.id) { setSelectedId(saved.id); replaceDetailParam(saved.id); @@ -330,6 +355,7 @@ export function PeoplePage() { invalidateAccountFlight(); invalidateSubjectCatalog(); await load(); + leaveToFlow(); } catch (caught) { notifyError(caught instanceof Error && caught.message ? caught.message : "保存失败,请重试"); } finally { @@ -426,7 +452,7 @@ export function PeoplePage() { onBack={showList} onAdd={startAdd} onEdit={startEdit} - onCancelEdit={() => setEditing(false)} + onCancelEdit={cancelEdit} onDraftChange={setDraft} saveOther={saveOther} saveSelf={saveSelf} diff --git a/frontend/src/components/subject-switcher.tsx b/frontend/src/components/subject-switcher.tsx index 87162abec..85ed32c3c 100644 --- a/frontend/src/components/subject-switcher.tsx +++ b/frontend/src/components/subject-switcher.tsx @@ -27,9 +27,9 @@ import { deletePerson, fetchPersonDeleteUsage, personDeleteDescription, - personEditHref, type PersonDeleteUsage, } from "@/lib/person-delete"; +import { currentAppPath, personAddHref, personEditHrefWithReturn } from "@/lib/person-return"; import { chatProfilePickerRows, subjectChipBirthLine, @@ -137,7 +137,8 @@ export function SubjectSwitcher({ function edit(row: ChatProfilePickerRow) { setOpen(false); - navigateInApp(personEditHref(row.id)); + // BUG-2397: 保存 or 取消 brings the user back to this page. + navigateInApp(personEditHrefWithReturn(row.id, currentAppPath())); } async function askDelete(row: ChatProfilePickerRow) { @@ -231,7 +232,16 @@ export function SubjectSwitcher({ ) : null} {showAddPerson ? (
- setOpen(false)}> + { + setOpen(false); + // BUG-2397: the form returns to this flow with the new person as the current one. + if (event.defaultPrevented || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button !== 0) return; + event.preventDefault(); + navigateInApp(personAddHref(currentAppPath())); + }} + >
) : null} diff --git a/frontend/src/lib/person-return.ts b/frontend/src/lib/person-return.ts new file mode 100644 index 000000000..c627423b0 --- /dev/null +++ b/frontend/src/lib/person-return.ts @@ -0,0 +1,71 @@ +/** + * BUG-2397: adding (or editing) a person from inside a flow — 生时纠正, a + * report or 年运 page, 合盘, the chart page, the chat picker — opens the + * archive form at `/people?add=1`. After 保存 the user goes back to that flow + * with the new person as the current person, instead of staying on the + * archive detail page. + * + * The way back travels as `?return=`. Only the app's own flow + * pages are accepted, so the parameter can never send anyone off the site + * (no `//host`, no scheme, no backslash) or into the archive itself. + */ + +export const PERSON_RETURN_PARAM = "return"; + +/** In-app pages a person can be added from. Each one also matches its sub-paths. */ +const RETURN_ROOTS = ["/chart", "/rectification", "/reports", "/synastry", "/ephemeris"] as const; +const MAX_RETURN_LENGTH = 512; + +export function safePersonReturnPath(raw: string | null | undefined): string | null { + const value = (raw ?? "").trim(); + if (!value || value.length > MAX_RETURN_LENGTH) return null; + if (!value.startsWith("/") || value.startsWith("//") || value.includes("\\")) return null; + let url: URL; + try { + url = new URL(value, "https://app.invalid"); + } catch { + return null; + } + if (url.origin !== "https://app.invalid") return null; + const path = url.pathname; + const allowed = path === "/" || RETURN_ROOTS.some((root) => path === root || path.startsWith(`${root}/`)); + if (!allowed) return null; + return `${path}${url.search}${url.hash}`; +} + +function withReturn(base: string, returnTo: string | null | undefined): string { + const safe = safePersonReturnPath(returnTo); + return safe ? `${base}&${PERSON_RETURN_PARAM}=${encodeURIComponent(safe)}` : base; +} + +export function personAddHref(returnTo?: string | null): string { + return withReturn("/people?add=1", returnTo); +} + +export function personEditHrefWithReturn(id: string, returnTo?: string | null): string { + const base = id === "self" ? "/people?self=1" : `/people?edit=${encodeURIComponent(id)}`; + return withReturn(base, returnTo); +} + +/** Where the user is now, as an in-app path (browser only). */ +export function currentAppPath(): string { + if (typeof window === "undefined") return ""; + const { pathname, search, hash } = window.location; + return `${pathname}${search}${hash}`; +} + +/** + * What happens after 保存 on the archive form (BUG-2397, owner 11-25): + * a person just added becomes the current person everywhere (wherever the form + * was opened from), and a form opened from a flow goes back to that flow. + * Editing an existing person does not change who is current. + */ +export function afterPersonSave(input: { + readonly addedId: string | null; + readonly returnTo: string | null; +}): { readonly currentSubject: string | null; readonly navigateTo: string | null } { + return { + currentSubject: input.addedId || null, + navigateTo: safePersonReturnPath(input.returnTo), + }; +} diff --git a/frontend/tests/add-profile-return-2397.test.ts b/frontend/tests/add-profile-return-2397.test.ts new file mode 100644 index 000000000..97ec2806f --- /dev/null +++ b/frontend/tests/add-profile-return-2397.test.ts @@ -0,0 +1,69 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import test from "node:test"; + +import { + afterPersonSave, + personAddHref, + personEditHrefWithReturn, + safePersonReturnPath, +} from "../src/lib/person-return"; + +const read = (path: string) => readFileSync(new URL(path, import.meta.url), "utf8"); + +// BUG-2397: adding a person from a flow used to land on the archive detail page. + +test("生时纠正 first: 新增一个人 carries the flow back, and 保存 returns there with the new person current", () => { + const href = personAddHref("/rectification"); + assert.equal(href, "/people?add=1&return=%2Frectification"); + const next = afterPersonSave({ addedId: "p-new", returnTo: new URL(`https://x${href}`).searchParams.get("return") }); + assert.deepEqual(next, { currentSubject: "p-new", navigateTo: "/rectification" }); +}); + +test("every flow page is a valid way back, with its query kept", () => { + for (const path of ["/", "/?c=s-1", "/chart", "/chart?layer=d9", "/reports", "/reports/annual", "/synastry", "/ephemeris", "/rectification#step"]) { + assert.equal(safePersonReturnPath(path), path, path); + } +}); + +test("the way back never leaves the site or loops into the archive", () => { + for (const bad of [ + "https://evil.example/", "//evil.example/x", "/\\evil.example", "javascript:alert(1)", + "/people", "/people?add=1", "/admin", "/login", "", "rectification", `/${"a".repeat(600)}`, + ]) { + assert.equal(safePersonReturnPath(bad), null, bad); + } + assert.equal(personAddHref("https://evil.example/"), "/people?add=1"); +}); + +test("editing from a flow returns there but does not change who is current", () => { + assert.equal(personEditHrefWithReturn("p-1", "/reports"), "/people?edit=p-1&return=%2Freports"); + assert.equal(personEditHrefWithReturn("self", "/chart"), "/people?self=1&return=%2Fchart"); + assert.deepEqual(afterPersonSave({ addedId: null, returnTo: "/reports" }), { currentSubject: null, navigateTo: "/reports" }); +}); + +test("adding on the 档案 page itself stays there, and the new person still becomes current (owner 11-25)", () => { + assert.deepEqual(afterPersonSave({ addedId: "p-2", returnTo: null }), { currentSubject: "p-2", navigateTo: null }); +}); + +test("the switcher (every flow's header and the chat picker) adds and edits with the current page as the way back", () => { + const switcher = read("../src/components/subject-switcher.tsx"); + assert.match(switcher, /navigateInApp\(personAddHref\(currentAppPath\(\)\)\)/); + assert.match(switcher, /navigateInApp\(personEditHrefWithReturn\(row\.id, currentAppPath\(\)\)\)/); + // A modified click (new tab) keeps the plain link. + assert.match(switcher, /event\.metaKey \|\| event\.ctrlKey \|\| event\.shiftKey \|\| event\.altKey \|\| event\.button !== 0\) return;/); +}); + +test("生时纠正's 去补全资料 comes back to 生时纠正", () => { + const page = read("../src/components/chart-choice/chart-choice-page.tsx"); + assert.match(page, /personEditHrefWithReturn\(subjectId, "\/rectification"\)/); +}); + +test("the archive page reads ?return once, sets the new person current, and goes back on 保存 and 取消", () => { + const people = read("../src/components/people/people-page.tsx"); + assert.match(people, /returnToRef\.current = safePersonReturnPath\(url\.searchParams\.get\(PERSON_RETURN_PARAM\)\);\s*url\.searchParams\.delete\(PERSON_RETURN_PARAM\);/); + assert.match(people, /afterPersonSave\(\{ addedId: editingOther \? null : saved\.id \|\| null, returnTo: returnToRef\.current \}\)/); + assert.match(people, /if \(next\.currentSubject\) setCurrentSubject\(next\.currentSubject\);\s*if \(next\.navigateTo && leaveToFlow\(\)\) return;/); + assert.match(people, /function cancelEdit\(\) \{\s*setEditing\(false\);\s*leaveToFlow\(\);/); + assert.match(people, /onCancelEdit=\{cancelEdit\}/); +});