Skip to content

refactor(site): expose time values in render functions as centralized, pure state #17587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
27 changes: 25 additions & 2 deletions site/.storybook/preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
* Storybook decorator function used to inject baseline data dependencies into
* our React components during testing.
*/
import React from "react";
import "../src/index.css";
import { ThemeProvider as EmotionThemeProvider } from "@emotion/react";
import CssBaseline from "@mui/material/CssBaseline";
import {
ThemeProvider as MuiThemeProvider,
StyledEngineProvider,
// biome-ignore lint/nursery/noRestrictedImports: we extend the MUI theme
} from "@mui/material/styles";
import { DecoratorHelpers } from "@storybook/addon-themes";
import isChromatic from "chromatic/isChromatic";
Expand All @@ -31,12 +31,19 @@ import { HelmetProvider } from "react-helmet-async";
import { QueryClient, QueryClientProvider, parseQueryArgs } from "react-query";
import { withRouter } from "storybook-addon-remix-react-router";
import "theme/globalFonts";
import { TimeSyncProvider } from "../src/hooks/useTimeSync";
import themes from "../src/theme";

DecoratorHelpers.initializeThemeState(Object.keys(themes), "dark");

/** @type {readonly Decorator[]} */
export const decorators = [withRouter, withQuery, withHelmet, withTheme];
export const decorators = [
withRouter,
withQuery,
withHelmet,
withTheme,
withTimeSyncProvider,
];

/** @type {Preview["parameters"]} */
export const parameters = {
Expand Down Expand Up @@ -101,6 +108,22 @@ function withHelmet(Story) {
);
}

const storyDate = new Date("March 15, 2022");

/** @type {Decorator} */
function withTimeSyncProvider(Story) {
return (
<TimeSyncProvider
options={{
initialDatetime: storyDate,
resyncOnNewSubscription: false,
}}
>
<Story />
</TimeSyncProvider>
);
}

/** @type {Decorator} */
function withQuery(Story, { parameters }) {
const queryClient = new QueryClient({
Expand Down
23 changes: 14 additions & 9 deletions site/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "./theme/globalFonts";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { TimeSyncProvider } from "hooks/useTimeSync";
import {
type FC,
type ReactNode,
Expand Down Expand Up @@ -37,6 +38,8 @@ declare global {
}
}

const initialDatetime = new Date();

export const AppProviders: FC<AppProvidersProps> = ({
children,
queryClient = defaultQueryClient,
Expand Down Expand Up @@ -64,15 +67,17 @@ export const AppProviders: FC<AppProvidersProps> = ({

return (
<HelmetProvider>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<ThemeProvider>
{children}
<GlobalSnackbar />
</ThemeProvider>
</AuthProvider>
{showDevtools && <ReactQueryDevtools initialIsOpen={showDevtools} />}
</QueryClientProvider>
<TimeSyncProvider options={{ initialDatetime }}>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<ThemeProvider>
{children}
<GlobalSnackbar />
</ThemeProvider>
</AuthProvider>
{showDevtools && <ReactQueryDevtools initialIsOpen={showDevtools} />}
</QueryClientProvider>
</TimeSyncProvider>
</HelmetProvider>
);
};
Expand Down
8 changes: 7 additions & 1 deletion site/src/components/SignInLayout/SignInLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type { Interpolation, Theme } from "@emotion/react";
import { useTimeSync } from "hooks/useTimeSync";
import type { FC, PropsWithChildren } from "react";

export const SignInLayout: FC<PropsWithChildren> = ({ children }) => {
const year = useTimeSync({
idealRefreshIntervalMs: Number.POSITIVE_INFINITY,
select: (date) => date.getFullYear(),
});

return (
<div css={styles.container}>
<div css={styles.content}>
<div css={styles.signIn}>{children}</div>
<div css={styles.copyright}>
{"\u00a9"} {new Date().getFullYear()} Coder Technologies, Inc.
{"\u00a9"} {year} Coder Technologies, Inc.
</div>
</div>
</div>
Expand Down
43 changes: 0 additions & 43 deletions site/src/hooks/useTime.ts

This file was deleted.

Loading
Loading