diff --git a/src/lib/helpers/project.ts b/src/lib/helpers/project.ts index 91cfc90ac..8f71eb2f1 100644 --- a/src/lib/helpers/project.ts +++ b/src/lib/helpers/project.ts @@ -1,6 +1,30 @@ +import { page } from '$app/stores'; +import { get } from 'svelte/store'; + +/** + * Returns the current project ID. + * + * The function first checks for a `project` parameter in the Svelte `$page` store. + * If not found, it extracts the project ID from the pathname. + * + * Supports: + * - Legacy structure: `/project-{projectID}/` + * - Multi-region structure: `/project-{region}-{projectID}/` + * + * Example: + * - `/project-console/` → `console` + * - `/project-fra-console/` → `console` + * - `/project-nyc-console/` → `console` + */ export function getProjectId() { + // safety check! + const projectFromParams = get(page)?.params?.project; + if (projectFromParams) { + return projectFromParams; + } + const pathname = window.location.pathname + '/'; - const projectMatch = pathname.match(/\/project-(.*?)\//); + const projectMatch = pathname.match(/\/project-(?:[a-z]{2,3}-)?([^/]+)/); return projectMatch?.[1] || null; } diff --git a/src/routes/(console)/project-[region]-[project]/+layout.svelte b/src/routes/(console)/project-[region]-[project]/+layout.svelte index 45464ca7c..8e0bdba63 100644 --- a/src/routes/(console)/project-[region]-[project]/+layout.svelte +++ b/src/routes/(console)/project-[region]-[project]/+layout.svelte @@ -28,7 +28,6 @@ return realtime .forProject($page.params.region, $page.params.project) .subscribe(['project', 'console'], (response) => { - console.log(response); if (response.events.includes('stats.connections')) { for (const [projectId, value] of Object.entries(response.payload)) { stats.add(projectId, [new Date(response.timestamp).toISOString(), value]); diff --git a/tests/unit/helpers/project.test.ts b/tests/unit/helpers/project.test.ts index 585514e25..91bbeea92 100644 --- a/tests/unit/helpers/project.test.ts +++ b/tests/unit/helpers/project.test.ts @@ -6,6 +6,7 @@ test('getProjectId', () => { pathname: string; expected: string | null; }> = [ + // legacy { pathname: '/console/project-6374e9031588b25d1841/databases', expected: '6374e9031588b25d1841' @@ -34,6 +35,28 @@ test('getProjectId', () => { { pathname: '/console', expected: null + }, + + // multi-region + { + pathname: '/console/project-fra-6374e9031588b25d1841/databases', + expected: '6374e9031588b25d1841' + }, + { + pathname: '/console/project-/databases', + expected: null + }, + { + pathname: '/console/project-nyc-project-project-abc/databases', + expected: 'project-project-abc' + }, + { + pathname: '/console/project-fra-abc/databases', + expected: 'abc' + }, + { + pathname: '/console/project-syd-abc/databases/project-123', + expected: 'abc' } ];