-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathuseParams.tsx
74 lines (67 loc) · 1.9 KB
/
useParams.tsx
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
import {useMemo} from 'react';
import {useParams as useReactRouter6Params} from 'react-router-dom';
import {CUSTOMER_DOMAIN, USING_CUSTOMER_DOMAIN} from 'sentry/constants';
import {useTestRouteContext} from './useRouteContext';
/**
* List of keys used in routes.tsx `/example/:paramKey/...`
*
* Prevents misspelling of param keys
*/
type ParamKeys =
| 'apiKey'
| 'appId'
| 'appSlug'
| 'authId'
| 'codeId'
| 'dataExportId'
| 'dashboardId'
| 'docIntegrationSlug'
| 'eventId'
| 'fineTuneType'
| 'groupId'
| 'id'
| 'installationId'
| 'integrationSlug'
| 'issueId'
| 'memberId'
| 'orgId'
| 'projectId'
| 'release'
| 'scrubbingId'
| 'searchId'
| 'sentryAppSlug'
| 'shareId'
| 'spanSlug'
| 'tagKey'
| 'teamId'
| 'traceSlug'
| 'widgetIndex';
/**
* Get params from the current route. Param availability depends on the current route.
*
* @example
* ```tsx
* const params = useParams<{projectId: string}>();
* ```
*/
export function useParams<P extends Partial<Record<ParamKeys, string | undefined>>>(): P {
// When running in test mode we still read from the legacy route context to
// keep test compatability while we fully migrate to react router 6
const testRouteContext = useTestRouteContext();
let contextParams: any;
if (!testRouteContext) {
// eslint-disable-next-line react-hooks/rules-of-hooks
contextParams = useReactRouter6Params();
} else {
contextParams = testRouteContext.params;
}
// Memoize params as mutating for customer domains causes other hooks
// that depend on `useParams()` to refresh infinitely.
return useMemo(() => {
if (USING_CUSTOMER_DOMAIN && CUSTOMER_DOMAIN && contextParams.orgId === undefined) {
// We do not know if the caller of this hook requires orgId, so we populate orgId implicitly.
return {...contextParams, orgId: CUSTOMER_DOMAIN};
}
return contextParams;
}, [contextParams]);
}