| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- type DetailReturnState = { from?: unknown; previousState?: unknown };
- const detailPath = /^\/(?:orders\/[^/]+|contracts\/[^/]+|customers\/[^/]+(?:\/orders)?|work-orders\/(?!overview$|mine$|queue$|delivery$)[^/]+)$/;
- /**
- * 可返回的列表路径。
- *
- * ⚠️ 工单组现有五个入口:`/work-orders`(台账)与 `/work-orders/{overview,mine,queue,delivery}`。
- * 这四条**不是详情**(详情是 `/work-orders/:id`),返回链必须能停在它们上面并保留筛选/页码;
- * `detailPath` 也已把四段排除,否则从总览进详情再「返回」会被当成「回工单详情」。
- */
- const listPath = /^\/(customers|contracts|orders|work-orders(?:\/(?:overview|mine|queue|delivery))?|workspace\/(?:progress|plan|queue))(?:\?[^#]*)?$/;
- const listLabels: Record<string, string> = { customers: "返回客户清单", contracts: "返回合同清单", orders: "返回全部订阅", "work-orders": "返回工单列表", "work-orders/overview": "返回工单总览", "work-orders/mine": "返回我的待办", "work-orders/queue": "返回待分派队列", "work-orders/delivery": "返回交付与回执", "workspace/progress": "返回工单进度", "workspace/plan": "返回我的套餐", "workspace/queue": "返回待分派队列" };
- export function detailLinkState(pathname: string, previousState: unknown) {
- return { from: pathname, previousState: previousState ?? null };
- }
- export function detailBack(state: unknown, fallback: string, fallbackLabel: string) {
- const { from, previousState } = (state && typeof state === "object" ? state : {}) as DetailReturnState;
- const listMatch = typeof from === "string" ? from.match(listPath) : null;
- if (listMatch) {
- return { to: from as string, state: previousState ?? null, label: (from as string).split("?")[0] === fallback.split("?")[0] ? fallbackLabel : listLabels[listMatch[1]] };
- }
- if (typeof from !== "string" || !detailPath.test(from)) {
- return { to: fallback, state: null, label: fallbackLabel };
- }
- const label = from.startsWith("/orders/") ? "返回订阅详情"
- : from.startsWith("/contracts/") ? "返回合同详情"
- : from.startsWith("/work-orders/") ? "返回工单详情"
- : from.endsWith("/orders") ? "返回套餐订阅管理"
- : "返回客户详情";
- return { to: from, state: previousState ?? null, label };
- }
- /**
- * 客户侧工单详情的「返回」目标。
- *
- * 客户的工单列表**只有一张**=`/workspace/progress`(门户册 9「工单进度」);
- * `/work-orders` 与其四个子列表是**运营口径**的列集(责任人/承接团队/下一步/待办计数),
- * 客户进去只会在同一批工单上看到另一种格式。来路若落在这几条上(旧深链、站内通知、
- * 台账页里点进来),一律回客户自己的列表,⛔ 照原样送回去。
- *
- * 来路是 `/workspace/progress`(含筛选与页码)则原样带回;从套餐详情等处进来时
- * 仍按通用链返回(如「返回订阅详情」),⛔ 把客户所有来路都改成工单列表。
- */
- export function customerTicketBack(state: unknown, list = "/workspace/progress", listLabel = "返回工单进度") {
- const back = detailBack(state, list, listLabel);
- const opsTicketList = /^\/work-orders(?:\/(?:overview|mine|queue|delivery))?(?:\?|$)/;
- return opsTicketList.test(back.to) ? { to: list, state: null, label: listLabel } : back;
- }
- /**
- * 表单页(客户建档/编辑档案等 `/:id/edit` 页)的返回目标:与详情页共用同一套状态链,
- * 「从哪进就回哪」——
- * · 从清单进(`from`=清单 URL)⇒ 回清单,URL 上的搜索词/筛选/分页原样保留;
- * · 从详情进(`from`=详情 URL)⇒ 回该详情,并把详情自己的返回链(它从哪个清单来)一并带回,
- * 否则详情页的「返回」会退化成兜底目标、丢掉上一层筛选;
- * · 既无 `from` 或不认识的路径(直接敲 URL)⇒ 回 `detail`(兜底)。
- *
- * 调用方一律用 `navigate(back.to, { state: back.state })` 或
- * `<Link to={back.to} state={back.state}>`,⛔ 只看文案不看 state。
- */
- export function formBack({ state, detail, detailLabel, list, listLabel }: { state: unknown; detail: string; detailLabel: string; list: string; listLabel: string }) {
- const { from, previousState } = (state && typeof state === "object" ? state : {}) as DetailReturnState;
- if (typeof from === "string") {
- if (listPath.test(from) && from.split("?")[0] === list.split("?")[0]) return { to: from, state: previousState ?? null, label: listLabel };
- if (detailPath.test(from)) return { to: from, state: previousState ?? null, label: detailLabel };
- }
- return { to: detail, state: null, label: detailLabel };
- }
|