Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.0 KB
Raw Blame
import type { NavigateOptions } from 'react-router';

import { parseRootSegment, WORKSPACE_MIRRORED_ROOT_SEGMENTS } from './rootSegments';

export interface WorkspaceAwareNavigateOptions extends NavigateOptions {
  /** When true, navigate to the literal `to` path without applying the workspace prefix. */
  escape?: boolean;
}

/**
 * Top-level path segments that are never mirrored under `/:workspaceSlug`.
 *
 * Kept in sync with `sharedMainAreaChildren` in the router configs. If you add
 * a new top-level personal-only route, append it here.
 *
 * `/settings` is handled separately via {@link WORKSPACE_SETTINGS_TABS} β€”
 * sub-paths in the allowlist get auto-prefixed; everything else (llm,
 * referral, system-tools, etc.) stays personal.
 */
const PERSONAL_PATH_REGEX =
  /^\/(?:apps|invite|onboarding|me|share|devtools|desktop-onboarding)(?:[/?#]|$)/;

const isPersonalPath = (to: string): boolean => PERSONAL_PATH_REGEX.test(to);

/**
 * Settings sub-paths that have a `/:workspaceSlug/settings/<tab>` mirror in
 * the SPA routers. Kept in sync with the workspace settings subtree in
 * `src/spa/router/desktopRouter.shared.tsx` and `mobileRouter.config.tsx`.
 *
 * Tabs absent from this set (llm, referral, system-tools, security, sync,
 * plugin, tts, agent, common, system-agent, memory, ...) are personal-only
 * and never prefixed.
 */
export const WORKSPACE_SETTINGS_TABS: ReadonlySet<string> = new Set([
  // About and the developer tools (advanced / labs) are user-level pages
  // mirrored under the workspace alongside the account-level tabs.
  'about',
  'advanced',
  'apikey',
  // Account-level tabs (profile / appearance / hotkey) are
  // mirrored under the workspace so members can reach them without leaving
  // the workspace.
  'appearance',
  'audit-log',
  // Automations (event subscriptions) are listed per workspace.
  'automations',
  'billing',
  'budget',
  // Legacy alias: chat preferences became `conversation`; the routers keep a
  // redirect, so prefixed deep-links still land on `/:slug/settings/conversation`.
  'chat-preference',
  // Legacy alias: connectors moved to `/marketplace/connector`; the routers keep
  // a redirect, so prefixed deep-links land on `/:slug/marketplace/connector`.
  'connector',
  // Personal conversation preferences, reachable by every member.
  'conversation',
  'credential',
  // Legacy alias for `credential` β€” the routers keep a redirect, so prefixed
  // deep-links still land on `/:slug/settings/credential`.
  'creds',
  'credits',
  'devices',
  'drive',
  'environments',
  'general',
  'hotkey',
  // Legacy alias: integrations moved to `/marketplace/connector`; the routers
  // keep a redirect, and the install callbacks still return here.
  'integrations',
  'labels',
  'labs',
  'members',
  // Legacy settings bookmarks redirect to the app within the same workspace.
  'messenger',
  'notification',
  'oauth-apps',
  'plans',
  'profile',
  'provider',
  'service-model',
  // Legacy alias: installed skills moved to `/marketplace/skill/installed`; the
  // routers keep a redirect from `/:slug/settings/skill`.
  'skill',
  'statistics',
  // Legacy alias for `statistics` β€” the routers keep a redirect, so prefixed
  // deep-links still land on `/:slug/settings/statistics`.
  'stats',
  'storage',
  'subscription-accounts',
  'teams',
  // Trashed rows are recorded per workspace; the bin follows the active one.
  'trash',
  'usage',
  // Agents' webhooks are listed per workspace, like automations.
  'webhooks',
]);

const SETTINGS_PREFIX_REGEX = /^\/settings\/([^/?#]+)/;

/**
 * Returns `true` for `/settings/<tab>` where `<tab>` is NOT in
 * {@link WORKSPACE_SETTINGS_TABS} (llm, referral, system-tools, …).
 * `/settings` index (or with query/hash) gets prefixed too β€” workspace
 * `/${slug}/settings` redirects to `/${slug}/settings/general`, personal
 * `/settings` redirects to `/settings/profile`.
 */
const isPersonalSettingsPath = (to: string): boolean => {
  const match = SETTINGS_PREFIX_REGEX.exec(to);
  if (!match) return false;
  return !WORKSPACE_SETTINGS_TABS.has(match[1]);
};

/**
 * Prefix an absolute path with `/${slug}` unless the path is already prefixed,
 * the path targets a personal-only surface, `escape` is set, or no active slug
 * exists. Returns the path unchanged when `to` is a relative path (so
 * `react-router` can resolve it itself).
 *
 * Pure function β€” extracted so it can be unit-tested without pulling in the
 * Zustand store / React tree.
 */
export const buildWorkspaceAwarePath = (
  to: string,
  activeSlug: string | null | undefined,
  options?: WorkspaceAwareNavigateOptions,
): string => {
  if (options?.escape) return to;
  if (!activeSlug) return to;
  if (!to.startsWith('/')) return to;
  if (isPersonalPath(to)) return to;
  if (isPersonalSettingsPath(to)) return to;
  if (to === `/${activeSlug}` || to.startsWith(`/${activeSlug}/`)) return to;

  const firstSegment = parseRootSegment(to);
  if (firstSegment && !WORKSPACE_MIRRORED_ROOT_SEGMENTS.has(firstSegment)) return to;

  return `/${activeSlug}${to}`;
};