detail-navigation.ts 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. type DetailReturnState = { from?: unknown; previousState?: unknown };
  2. const detailPath = /^\/(?:orders\/[^/]+|contracts\/[^/]+|customers\/[^/]+(?:\/orders)?|work-orders\/(?!overview$|mine$|queue$|delivery$)[^/]+)$/;
  3. /**
  4. * 可返回的列表路径。
  5. *
  6. * ⚠️ 工单组现有五个入口:`/work-orders`(台账)与 `/work-orders/{overview,mine,queue,delivery}`。
  7. * 这四条**不是详情**(详情是 `/work-orders/:id`),返回链必须能停在它们上面并保留筛选/页码;
  8. * `detailPath` 也已把四段排除,否则从总览进详情再「返回」会被当成「回工单详情」。
  9. */
  10. const listPath = /^\/(customers|contracts|orders|work-orders(?:\/(?:overview|mine|queue|delivery))?|workspace\/(?:progress|plan|queue))(?:\?[^#]*)?$/;
  11. 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": "返回待分派队列" };
  12. export function detailLinkState(pathname: string, previousState: unknown) {
  13. return { from: pathname, previousState: previousState ?? null };
  14. }
  15. export function detailBack(state: unknown, fallback: string, fallbackLabel: string) {
  16. const { from, previousState } = (state && typeof state === "object" ? state : {}) as DetailReturnState;
  17. const listMatch = typeof from === "string" ? from.match(listPath) : null;
  18. if (listMatch) {
  19. return { to: from as string, state: previousState ?? null, label: (from as string).split("?")[0] === fallback.split("?")[0] ? fallbackLabel : listLabels[listMatch[1]] };
  20. }
  21. if (typeof from !== "string" || !detailPath.test(from)) {
  22. return { to: fallback, state: null, label: fallbackLabel };
  23. }
  24. const label = from.startsWith("/orders/") ? "返回订阅详情"
  25. : from.startsWith("/contracts/") ? "返回合同详情"
  26. : from.startsWith("/work-orders/") ? "返回工单详情"
  27. : from.endsWith("/orders") ? "返回套餐订阅管理"
  28. : "返回客户详情";
  29. return { to: from, state: previousState ?? null, label };
  30. }
  31. /**
  32. * 客户侧工单详情的「返回」目标。
  33. *
  34. * 客户的工单列表**只有一张**=`/workspace/progress`(门户册 9「工单进度」);
  35. * `/work-orders` 与其四个子列表是**运营口径**的列集(责任人/承接团队/下一步/待办计数),
  36. * 客户进去只会在同一批工单上看到另一种格式。来路若落在这几条上(旧深链、站内通知、
  37. * 台账页里点进来),一律回客户自己的列表,⛔ 照原样送回去。
  38. *
  39. * 来路是 `/workspace/progress`(含筛选与页码)则原样带回;从套餐详情等处进来时
  40. * 仍按通用链返回(如「返回订阅详情」),⛔ 把客户所有来路都改成工单列表。
  41. */
  42. export function customerTicketBack(state: unknown, list = "/workspace/progress", listLabel = "返回工单进度") {
  43. const back = detailBack(state, list, listLabel);
  44. const opsTicketList = /^\/work-orders(?:\/(?:overview|mine|queue|delivery))?(?:\?|$)/;
  45. return opsTicketList.test(back.to) ? { to: list, state: null, label: listLabel } : back;
  46. }
  47. /**
  48. * 表单页(客户建档/编辑档案等 `/:id/edit` 页)的返回目标:与详情页共用同一套状态链,
  49. * 「从哪进就回哪」——
  50. * · 从清单进(`from`=清单 URL)⇒ 回清单,URL 上的搜索词/筛选/分页原样保留;
  51. * · 从详情进(`from`=详情 URL)⇒ 回该详情,并把详情自己的返回链(它从哪个清单来)一并带回,
  52. * 否则详情页的「返回」会退化成兜底目标、丢掉上一层筛选;
  53. * · 既无 `from` 或不认识的路径(直接敲 URL)⇒ 回 `detail`(兜底)。
  54. *
  55. * 调用方一律用 `navigate(back.to, { state: back.state })` 或
  56. * `<Link to={back.to} state={back.state}>`,⛔ 只看文案不看 state。
  57. */
  58. export function formBack({ state, detail, detailLabel, list, listLabel }: { state: unknown; detail: string; detailLabel: string; list: string; listLabel: string }) {
  59. const { from, previousState } = (state && typeof state === "object" ? state : {}) as DetailReturnState;
  60. if (typeof from === "string") {
  61. if (listPath.test(from) && from.split("?")[0] === list.split("?")[0]) return { to: from, state: previousState ?? null, label: listLabel };
  62. if (detailPath.test(from)) return { to: from, state: previousState ?? null, label: detailLabel };
  63. }
  64. return { to: detail, state: null, label: detailLabel };
  65. }