import { WORK_ORDERS_ROUTE } from "@/domain/dashboard/utils/work-order-drilldown-links"; const WORK_ORDER_PARAM = "wo"; const TAB_PARAM = "tab"; export const WORK_ORDER_DEEP_LINK_TABS = [ "info", "comments", "audit", "completion", "extras", ] as const; export type WorkOrderDeepLinkTab = (typeof WORK_ORDER_DEEP_LINK_TABS)[number]; export type WorkOrderDeepLink = { workOrderId: string; tab: WorkOrderDeepLinkTab; }; function isDeepLinkTab(value: string | null): value is WorkOrderDeepLinkTab { return WORK_ORDER_DEEP_LINK_TABS.some((tab) => tab === value); } /** `/workorders?wo=&tab=` opens that work order's slide-over on the given tab. */ export function workOrderDeepLinkUrl(workOrderId: string | number, tab?: string | null): string { const search = new URLSearchParams({ [WORK_ORDER_PARAM]: String(workOrderId) }); if (isDeepLinkTab(tab ?? null)) { search.set(TAB_PARAM, String(tab)); } return `${WORK_ORDERS_ROUTE}?${search.toString()}`; } export function parseWorkOrderDeepLink(params: URLSearchParams): WorkOrderDeepLink | null { const workOrderId = (params.get(WORK_ORDER_PARAM) ?? "").trim(); if (!/^\d+$/.test(workOrderId)) { return null; } const tab = params.get(TAB_PARAM); return { workOrderId, tab: isDeepLinkTab(tab) ? tab : "info" }; } export function withoutWorkOrderDeepLink(params: URLSearchParams): URLSearchParams { const next = new URLSearchParams(params); next.delete(WORK_ORDER_PARAM); next.delete(TAB_PARAM); return next; }