Skip to content

Commit

Permalink
[UI v2] feat: Adds router and react-query storybook decorator (#16469)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinvillarosa authored Dec 20, 2024
1 parent e1fe794 commit 8bc0923
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ui-v2/src/storybook/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { routerDecorator } from "./router-decorator";
export { reactQueryDecorator } from "./react-query-decorator";
26 changes: 26 additions & 0 deletions ui-v2/src/storybook/utils/react-query-decorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { StoryFn } from "@storybook/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";

const queryClient = new QueryClient();

/**
*
* @example
* ```ts
* import { reactQueryDecorator } from '@/storybook/utils'
*
* const meta: Meta<typeof MyComponent> = {
* title: "UI/MyComponent",
* decorators: [reactQueryDecorator],
* component: MyComponent,
* };
*
* export default meta;
*/
export const reactQueryDecorator = (Story: StoryFn) => (
<QueryClientProvider client={queryClient}>
<Story />
<ReactQueryDevtools />
</QueryClientProvider>
);
74 changes: 74 additions & 0 deletions ui-v2/src/storybook/utils/router-decorator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* eslint-disable react-refresh/only-export-components */
import {
RouterProvider,
createMemoryHistory,
createRootRoute,
createRoute,
createRouter,
useRouterState,
} from "@tanstack/react-router";
import { type ReactNode, createContext, useContext } from "react";

//#region Dummy story router
function RenderStory() {
const storyFn = useContext(CurrentStoryContext);
if (!storyFn) {
throw new Error("Storybook root not found");
}
return storyFn();
}

export const CurrentStoryContext = createContext<(() => ReactNode) | undefined>(
undefined,
);

function NotFoundComponent() {
const state = useRouterState();
return (
<div>
<i>Warning:</i> Simulated route not found for path{" "}
<code>{state.location.href}</code>
</div>
);
}

const storyPath = "/__story__";
const storyRoute = createRoute({
path: storyPath,
getParentRoute: () => rootRoute,
component: RenderStory,
});

const rootRoute = createRootRoute({
notFoundComponent: NotFoundComponent,
});
rootRoute.addChildren([storyRoute]);

export const storyRouter = createRouter({
history: createMemoryHistory({ initialEntries: [storyPath] }),
routeTree: rootRoute,
});
//#endregion

/**
*
* @example
* ```ts
* import { routerDecorator } from '@/storybook/utils'
*
* const meta: Meta<typeof MyComponent> = {
* title: "UI/MyComponent",
* decorators: [routerDecorator],
* component: MyComponent,
* };
*
* export default meta;
*/
export function routerDecorator(storyFn: () => ReactNode) {
return (
<CurrentStoryContext.Provider value={storyFn}>
{/** @ts-expect-error Ignoring for now to get decorator working */}
<RouterProvider router={storyRouter} />
</CurrentStoryContext.Provider>
);
}

0 comments on commit 8bc0923

Please sign in to comment.