Skip to content

Commit de2eeb3

Browse files
committed
fix: Look up params as soon as the href changes
Without this, we were getting the params at a delayed rate, so we were swapping out the variables inconsistently. Plus, if you navigated around the database, you wouldn’t get multiple view starts. https://harperdb.atlassian.net/browse/STUDIO-381
1 parent fb747e7 commit de2eeb3

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

src/integrations/datadog/datadog.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { excludeFalsy } from '@/lib/arrays/excludeFalsy';
55
import { isLocalUser } from '@/lib/types/isLocalUser';
66
import { datadogRum } from '@datadog/browser-rum';
77
import { reactPlugin } from '@datadog/browser-rum-react';
8-
import { useLocation, useParams } from '@tanstack/react-router';
9-
import { useEffect, useMemo } from 'react';
8+
import { useLocation, useRouter } from '@tanstack/react-router';
9+
import { useEffect } from 'react';
1010

1111
let initialized = false;
1212
const enabled = !import.meta.env.DEV && !isLocalStudio;
@@ -41,11 +41,12 @@ export function useDatadog() {
4141

4242
export function useOnRouteLoadTracker() {
4343
const location = useLocation();
44+
const router = useRouter();
4445
const { user } = useOverallAuth();
45-
const params = useParams({ strict: false });
46-
const name = useMemo(() => translateUrlForDatadog(location.href, params), [location.href, params]);
4746

4847
useEffect(() => {
48+
const currentMatches = router.matchRoutes(router.state.location);
49+
const name = translateUrlForDatadog(location.href, currentMatches.map(m => m.params));
4950
if (!enabled) {
5051
return;
5152
}
@@ -54,7 +55,7 @@ export function useOnRouteLoadTracker() {
5455
version: import.meta.env.VITE_STUDIO_VERSION,
5556
name,
5657
});
57-
}, [name]);
58+
}, [location.href, router]);
5859

5960
useEffect(() => {
6061
if (!enabled) {

src/integrations/datadog/translateUrlForDatadog.test.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ describe('translateUrlForDatadog', () => {
1010
databaseName: 'data',
1111
tableName: 'Users',
1212
};
13-
const result = translateUrlForDatadog(href, params);
13+
const result = translateUrlForDatadog(href, [params]);
1414
expect(result).toBe('/orgs/$organizationId/$clusterId/browse/$databaseName/$tableName/');
1515
});
1616

1717
it('adds a trailing slash if missing', () => {
1818
const href = '/orgs/acme/';
19-
const result = translateUrlForDatadog(href, {});
19+
const result = translateUrlForDatadog(href, [{}]);
2020
expect(result).toBe('/orgs/acme/');
2121

2222
const href2 = '/orgs/acme';
23-
const result2 = translateUrlForDatadog(href2, {});
23+
const result2 = translateUrlForDatadog(href2, [{}]);
2424
expect(result2).toBe('/orgs/acme/');
2525
});
2626

@@ -31,7 +31,7 @@ describe('translateUrlForDatadog', () => {
3131
clusterId: 'clu-2',
3232
instanceId: 'ins-3',
3333
};
34-
const result = translateUrlForDatadog(href, params);
34+
const result = translateUrlForDatadog(href, [params]);
3535
expect(result).toBe('/orgs/$organizationId/$clusterId/instances/$instanceId/');
3636
});
3737

@@ -40,7 +40,7 @@ describe('translateUrlForDatadog', () => {
4040
const params = {
4141
organizationId: 'org-2',
4242
};
43-
const result = translateUrlForDatadog(href, params);
43+
const result = translateUrlForDatadog(href, [params]);
4444
expect(result).toBe('/orgs/org-1/other/');
4545
});
4646

@@ -49,7 +49,7 @@ describe('translateUrlForDatadog', () => {
4949
const params = {
5050
organizationId: 'org-1',
5151
};
52-
const result = translateUrlForDatadog(href, params);
52+
const result = translateUrlForDatadog(href, [params]);
5353
expect(result).toBe('/files/my-org-1-file/');
5454
});
5555

@@ -59,7 +59,7 @@ describe('translateUrlForDatadog', () => {
5959
databaseName: 'data',
6060
tableName: 'Users',
6161
};
62-
const result = translateUrlForDatadog(href, params);
62+
const result = translateUrlForDatadog(href, [params]);
6363
expect(result).toBe('/dbs/$databaseName/tables/$tableName/');
6464
});
6565

@@ -70,7 +70,21 @@ describe('translateUrlForDatadog', () => {
7070
clusterId: 'clu-2',
7171
};
7272
// The function simply splits on '?', so the query is dropped; the rest remains intact, including protocol and host.
73-
const result = translateUrlForDatadog(href, params);
73+
const result = translateUrlForDatadog(href, [params]);
7474
expect(result).toBe('https://example.com/app#/orgs/$organizationId/$clusterId/');
7575
});
76+
77+
it('works with multiple params', () => {
78+
const href = '/orgs/org-1/clu-2?x=1&y=2';
79+
const params: Record<string, string>[] = [
80+
{
81+
organizationId: 'org-1',
82+
},
83+
{
84+
clusterId: 'clu-2',
85+
},
86+
];
87+
const result = translateUrlForDatadog(href, params);
88+
expect(result).toBe('/orgs/$organizationId/$clusterId/');
89+
});
7690
});
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
export function translateUrlForDatadog(href: string, params: Record<string, string>): string {
1+
export function translateUrlForDatadog(href: string, allParams: Record<string, string>[]): string {
22
let translated = href.split('?')[0];
33
if (!translated.endsWith('/')) {
44
translated = translated + '/';
55
}
6-
for (const key in params) {
7-
translated = translated.replace(`/${params[key]}/`, `/$${key}/`);
6+
for (const params of allParams) {
7+
for (const key in params) {
8+
translated = translated.replace(`/${params[key]}/`, `/$${key}/`);
9+
}
810
}
911
return translated;
1012
}

0 commit comments

Comments
 (0)