All files / src/routes workspace.ts

100% Statements 32/32
90.62% Branches 29/32
100% Functions 9/9
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155                                                          118x                       50x 50x                           4x 4x 2x   2x                     118x               303x 3305x                         2845x 302x 302x 302x 297x 291x       118x                                   5x     5x 5x                               16x 16x 8x 4x   12x           64x 7x    
import { encodeResourceId } from "@/components/detail/useResourceId";
import { APP_PREFIXES, APP_ROUTES } from "./const";
 
/**
 * Workspaces: Personal, an organisation, or a project
 * (svc-core `app/services/workspace.py`). Every page shows exactly one, and
 * the URL says which:
 *
 *   /user/…                          Personal
 *   /organisation/<base64 id>/…      an organisation
 *   /project/<base64 id>/…           a project
 *
 * The same page set is mounted under all three (`WorkspaceApp`), so a page
 * is written once against `APP_ROUTES.USER` and `pathIn` moves its links
 * into whichever workspace it is showing — `routes/router.tsx` does that for
 * every `Link`, `NavLink`, `Navigate` and `useNavigate` in the app.
 */
export type WorkspaceKind = "personal" | "organisation" | "project";
 
export interface WorkspaceRef {
  kind: WorkspaceKind;
  /** The organisation's or project's id; null for Personal. */
  id: string | null;
  /** What a request's `workspace` parameter takes. */
  key: string;
  /** Where its pages are mounted. */
  prefix: string;
}
 
export const PERSONAL_WORKSPACE: WorkspaceRef = {
  kind: "personal",
  id: null,
  key: "personal",
  prefix: APP_PREFIXES.USER,
};
 
export function workspaceRef(
  kind: Exclude<WorkspaceKind, "personal">,
  id: string,
): WorkspaceRef {
  const base =
    kind === "organisation" ? APP_PREFIXES.ORGANISATION : APP_PREFIXES.PROJECT;
  return {
    kind,
    id,
    key: `${kind}:${id}`,
    prefix: `${base}/${encodeResourceId(id)}`,
  };
}
 
/**
 * A workspace from the key svc-core gives a row's `home_workspace`:
 * `user:<id>` is somebody's Personal — only ever the caller's own here,
 * since nobody else's personal work is listed outside a project.
 */
export function workspaceFromKey(key: string): WorkspaceRef {
  const [kind, id] = key.split(":");
  if (kind === "organisation" || kind === "project") {
    return workspaceRef(kind, id);
  }
  return PERSONAL_WORKSPACE;
}
 
/**
 * Pages that belong to the person rather than to any workspace — their
 * account settings tabs, invites, the list of organisations — reached the
 * same way from wherever they are linked. Usage (Stats) is not one: each
 * workspace has its own. Nor are an organisation's or project's own
 * settings tabs (`APP_ROUTES.USER.WORKSPACE_SETTINGS`), which sit beside
 * the account tabs under `/settings`.
 */
const ACCOUNT_ROUTES: readonly string[] = [
  ...Object.values(APP_ROUTES.USER.SETTINGS),
  APP_ROUTES.USER.invite,
  APP_ROUTES.USER.organisations,
  APP_ROUTES.USER.developer,
];
 
export function isAccountRoute(pathname: string): boolean {
  return ACCOUNT_ROUTES.some((route) =>
    route.endsWith("/")
      ? pathname.startsWith(route)
      : pathname === route || pathname.startsWith(`${route}/`),
  );
}
 
/**
 * `to` inside workspace `ws`: a link to a workspace page (anything under
 * `/user` that is not an account route) moves under the workspace's prefix;
 * everything else — account pages, other apps, relative links — is left
 * alone.
 */
export function pathIn(ws: WorkspaceRef, to: string): string {
  if (ws.kind === "personal") return to;
  const user = APP_PREFIXES.USER;
  const pathname = to.split(/[?#]/)[0];
  if (pathname !== user && !pathname.startsWith(`${user}/`)) return to;
  if (isAccountRoute(pathname)) return to;
  return ws.prefix + to.slice(user.length);
}
 
/** Sections every workspace has, kept when switching between them. */
const SHARED_SECTIONS = new Set([
  "activity",
  "dashboards",
  "inference",
  "knowledge",
  "governance",
  "stats",
]);
 
/** Where switching from the page at `currentPath` (in the workspace mounted
 *  at `currentPrefix`) to the workspace at `target` lands: the same section,
 *  if every workspace has it, else the target's home. Never a detail page —
 *  that id belongs to the workspace being left. */
export function switchTarget(
  currentPath: string,
  currentPrefix: string,
  target: string,
): string {
  const rest = currentPath.startsWith(currentPrefix)
    ? currentPath.slice(currentPrefix.length)
    : "";
  const section = rest.split("/").filter(Boolean)[0];
  return SHARED_SECTIONS.has(section ?? "") ? `${target}/${section}` : `${target}/`;
}
 
/**
 * Whether a navigation link to `href` — written, like every link, against
 * `APP_ROUTES.USER` — is the page at `pathname` in workspace `ws`. Matched
 * where the link goes (`pathIn`), not where it is written: an
 * organisation's Inference is `/organisation/<id>/inference`, never
 * `/user/inference`. A link ending in "/" (a home) is that page alone;
 * any other is itself and the pages below it.
 */
export function linkActive(
  ws: WorkspaceRef,
  href: string,
  pathname: string,
): boolean {
  const target = pathIn(ws, href).split(/[?#]/)[0];
  if (target.endsWith("/")) {
    const bare = (p: string) => p.replace(/\/+$/, "");
    return bare(pathname) === bare(target);
  }
  return pathname === target || pathname.startsWith(`${target}/`);
}
 
/** `pathname` as the Personal page it is mounted from: an organisation's
 *  or project's prefix swapped back for `/user`. The inverse of `pathIn`. */
export function asPersonalPath(ws: WorkspaceRef, pathname: string): string {
  if (ws.kind === "personal" || !pathname.startsWith(ws.prefix)) return pathname;
  return APP_PREFIXES.USER + pathname.slice(ws.prefix.length);
}