Skip to content

Commit 49ba6fe

Browse files
authored
feat(ui): Provide aria router for tabs, links (#83181)
1 parent 0387cb8 commit 49ba6fe

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

static/app/routes.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {t} from 'sentry/locale';
77
import HookStore from 'sentry/stores/hookStore';
88
import type {HookName} from 'sentry/types/hooks';
99
import errorHandler from 'sentry/utils/errorHandler';
10+
import {ProvideAriaRouter} from 'sentry/utils/provideAriaRouter';
1011
import retryableImport from 'sentry/utils/retryableImport';
1112
import withDomainRedirect from 'sentry/utils/withDomainRedirect';
1213
import withDomainRequired from 'sentry/utils/withDomainRequired';
@@ -2450,15 +2451,17 @@ function buildRoutes() {
24502451
);
24512452

24522453
const appRoutes = (
2453-
<Route>
2454-
{experimentalSpaRoutes}
2455-
<Route path="/" component={errorHandler(App)}>
2456-
{rootRoutes}
2457-
{organizationRoutes}
2458-
{legacyRedirectRoutes}
2459-
<Route path="*" component={errorHandler(RouteNotFound)} />
2454+
<ProvideAriaRouter>
2455+
<Route>
2456+
{experimentalSpaRoutes}
2457+
<Route path="/" component={errorHandler(App)}>
2458+
{rootRoutes}
2459+
{organizationRoutes}
2460+
{legacyRedirectRoutes}
2461+
<Route path="*" component={errorHandler(RouteNotFound)} />
2462+
</Route>
24602463
</Route>
2461-
</Route>
2464+
</ProvideAriaRouter>
24622465
);
24632466

24642467
return appRoutes;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {useHref} from 'react-router-dom';
2+
import {RouterProvider as AriaRouterProvider} from '@react-aria/utils';
3+
4+
import {useNavigate} from 'sentry/utils/useNavigate';
5+
/**
6+
* React aira has its own context for useTabs, useMenuItem, useLink, etc.
7+
* We need to provide the router instance so it knows how to navigate.
8+
* @link https://react-spectrum.adobe.com/react-aria/routing.html#react-router
9+
*/
10+
export function ProvideAriaRouter({children}: {children: React.ReactNode}) {
11+
const navigate = useNavigate();
12+
return (
13+
<AriaRouterProvider navigate={navigate} useHref={useHref}>
14+
{children}
15+
</AriaRouterProvider>
16+
);
17+
}

static/app/views/insights/common/components/chart.spec.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ describe('Chart', function () {
2727
const parsingError = new Error('Could not parse chart data');
2828

2929
render(
30-
<Chart error={parsingError} data={[]} loading={false} type={ChartType.LINE} />
30+
<Chart error={parsingError} data={[]} loading={false} type={ChartType.LINE} />,
31+
// Mocked useRef breaks router
32+
{disableRouterMocks: true}
3133
);
3234

3335
expect(screen.getByTestId('chart-error-panel')).toBeInTheDocument();
@@ -55,7 +57,10 @@ describe('Chart', function () {
5557
}),
5658
},
5759
];
58-
render(<Chart data={mockedSeries} loading={false} type={ChartType.LINE} />);
60+
render(<Chart data={mockedSeries} loading={false} type={ChartType.LINE} />, {
61+
// Mocked useRef breaks router
62+
disableRouterMocks: true,
63+
});
5964
expect(jest.mocked(BaseChart).mock.calls[0]![0].series?.[0]).toHaveProperty(
6065
'markLine'
6166
);

tests/js/sentry-test/reactTestingLibrary.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import GlobalModal from 'sentry/components/globalModal';
1313
import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
1414
import type {Organization} from 'sentry/types/organization';
1515
import {DANGEROUS_SET_TEST_HISTORY} from 'sentry/utils/browserHistory';
16+
import {ProvideAriaRouter} from 'sentry/utils/provideAriaRouter';
1617
import {QueryClientProvider} from 'sentry/utils/queryClient';
1718
import {lightTheme} from 'sentry/utils/theme';
1819
import {OrganizationContext} from 'sentry/views/organizationContext';
@@ -69,7 +70,8 @@ function makeAllTheProviders(options: ProviderOptions) {
6970
routes: router.routes,
7071
}}
7172
>
72-
{content}
73+
{/* ProvideAriaRouter may not be necessary in tests but matches routes.tsx */}
74+
<ProvideAriaRouter>{content}</ProvideAriaRouter>
7375
</TestRouteContext.Provider>
7476
);
7577

0 commit comments

Comments
 (0)