|
| 1 | +/** |
| 2 | + * How the legacy organization surface maps onto the Platform zone. Dependency-free: the edge proxy, |
| 3 | + * `next.config.ts` (loaded before the `@/` alias resolves), and client code all read it. |
| 4 | + */ |
| 5 | + |
| 6 | +export const PLATFORM_BASE_PATH = '/platform' |
| 7 | + |
| 8 | +/** |
| 9 | + * Carries the requested `/o/...` URL from the proxy to the organization layout, which Next gives no |
| 10 | + * way to learn its own sub-path. A forged value can only choose among the mapped routes of an |
| 11 | + * organization the viewer already belongs to. |
| 12 | + */ |
| 13 | +export const LEGACY_URL_HEADER = 'x-sim-legacy-url' |
| 14 | + |
| 15 | +/** Legacy settings paths that keep their name on the Platform. */ |
| 16 | +const SETTINGS_SECTIONS: ReadonlySet<string> = new Set([ |
| 17 | + 'knowledge-graphs', |
| 18 | + 'recently-deleted', |
| 19 | + 'members', |
| 20 | + 'billing', |
| 21 | + 'usage', |
| 22 | + 'whitelabeling', |
| 23 | + 'requests', |
| 24 | + 'audit-logs', |
| 25 | + 'access-control', |
| 26 | + 'sso', |
| 27 | + 'security', |
| 28 | + 'data-retention', |
| 29 | + 'data-drains', |
| 30 | +]) |
| 31 | + |
| 32 | +/** The legacy surface's own aliases, resolved here so a saved link lands in one hop. */ |
| 33 | +const SETTINGS_ALIASES: Readonly<Record<string, string>> = { |
| 34 | + organization: 'members', |
| 35 | + team: 'members', |
| 36 | + subscription: 'billing', |
| 37 | + domains: 'sso', |
| 38 | + sessions: 'security', |
| 39 | + 'authorized-apps': 'account', |
| 40 | +} |
| 41 | + |
| 42 | +/** Legacy General sub-views, each its own section on the Platform. */ |
| 43 | +const GENERAL_VIEWS: Readonly<Record<string, string>> = { |
| 44 | + privacy: 'privacy', |
| 45 | + 'authorized-apps': 'account', |
| 46 | + 'organization-accounts': 'account', |
| 47 | +} |
| 48 | + |
| 49 | +function mapSettingsRoute(rest: readonly string[], query: URLSearchParams): string | null { |
| 50 | + const [section, detail] = rest |
| 51 | + if (section === undefined) return '/settings' |
| 52 | + if (section === 'usage' && detail === 'events' && rest.length === 2) |
| 53 | + return '/settings/usage/events' |
| 54 | + if (rest.length !== 1) return null |
| 55 | + if (section === 'general') { |
| 56 | + const view = GENERAL_VIEWS[query.get('view') ?? ''] |
| 57 | + query.delete('view') |
| 58 | + return view ? `/settings/${view}` : '/settings' |
| 59 | + } |
| 60 | + const target = SETTINGS_ALIASES[section] ?? (SETTINGS_SECTIONS.has(section) ? section : null) |
| 61 | + return target && `/settings/${target}` |
| 62 | +} |
| 63 | + |
| 64 | +function mapRoute(rest: readonly string[], query: URLSearchParams): string | null { |
| 65 | + const [head, detail] = rest |
| 66 | + if (head === undefined) return '/' |
| 67 | + if (head === 'settings') return mapSettingsRoute(rest.slice(1), query) |
| 68 | + if (rest.length === 1) { |
| 69 | + if (head === 'home' || head === 'people' || head === 'integrations') return `/${head}` |
| 70 | + if (head === 'links') return '/settings/links' |
| 71 | + return null |
| 72 | + } |
| 73 | + if (rest.length === 2 && (head === 'chat' || head === 'people')) return `/${head}/${detail}` |
| 74 | + return null |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * The Platform URL for a legacy URL under `/o/{organizationId}`, keeping its query, or `null` for a |
| 79 | + * route that stays legacy: Search, Benchmark, Skills, platform administration, and the Search and |
| 80 | + * credential pages the Integrations owner has not mapped. Null also covers another organization's |
| 81 | + * URL. The Platform must never link back to a mapped route, or the two surfaces would redirect to |
| 82 | + * each other. |
| 83 | + */ |
| 84 | +export function mapLegacyOrganizationUrl(url: string, organizationId: string): string | null { |
| 85 | + const queryStart = url.indexOf('?') |
| 86 | + const pathname = queryStart === -1 ? url : url.slice(0, queryStart) |
| 87 | + const query = new URLSearchParams(queryStart === -1 ? '' : url.slice(queryStart + 1)) |
| 88 | + const [surface, organization, ...rest] = pathname.split('/').filter(Boolean) |
| 89 | + if (surface !== 'o' || organization !== organizationId) return null |
| 90 | + const route = mapRoute(rest, query) |
| 91 | + if (route === null) return null |
| 92 | + const search = query.toString() |
| 93 | + return `${PLATFORM_BASE_PATH}${route === '/' ? '' : route}${search ? `?${search}` : ''}` |
| 94 | +} |
0 commit comments