Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
auto theme
  • Loading branch information
aslilac committed Dec 12, 2023
commit 1c0e39a5f899ab4041753a91392de60730d61c2a
41 changes: 11 additions & 30 deletions site/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import CssBaseline from "@mui/material/CssBaseline";
import { QueryClient, QueryClientProvider } from "react-query";
import { AuthProvider } from "components/AuthProvider/AuthProvider";
import type { FC, PropsWithChildren, ReactNode } from "react";
import type { FC, ReactNode } from "react";
import { HelmetProvider } from "react-helmet-async";
import { AppRouter } from "./AppRouter";
import { ThemeProviders } from "./contexts/ThemeProviders";
import { AuthProvider } from "./contexts/AuthProvider/AuthProvider";
import { ErrorBoundary } from "./components/ErrorBoundary/ErrorBoundary";
import { GlobalSnackbar } from "./components/GlobalSnackbar/GlobalSnackbar";
import theme from "./theme";
import "./theme/globalFonts";
import {
StyledEngineProvider,
ThemeProvider as MuiThemeProvider,
} from "@mui/material/styles";
import { ThemeProvider as EmotionThemeProvider } from "@emotion/react";

const defaultQueryClient = new QueryClient({
defaultOptions: {
Expand All @@ -23,19 +17,6 @@ const defaultQueryClient = new QueryClient({
},
});

export const ThemeProviders: FC<PropsWithChildren> = ({ children }) => {
return (
<StyledEngineProvider injectFirst>
<MuiThemeProvider theme={theme.dark}>
<EmotionThemeProvider theme={theme.dark}>
<CssBaseline enableColorScheme />
{children}
</EmotionThemeProvider>
</MuiThemeProvider>
</StyledEngineProvider>
);
};

interface AppProvidersProps {
children: ReactNode;
queryClient?: QueryClient;
Expand All @@ -47,16 +28,16 @@ export const AppProviders: FC<AppProvidersProps> = ({
}) => {
return (
<HelmetProvider>
<ThemeProviders>
<ErrorBoundary>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<ErrorBoundary>
<QueryClientProvider client={queryClient}>
<AuthProvider>
<ThemeProviders>
{children}
<GlobalSnackbar />
</AuthProvider>
</QueryClientProvider>
</ErrorBoundary>
</ThemeProviders>
</ThemeProviders>
</AuthProvider>
</QueryClientProvider>
</ErrorBoundary>
</HelmetProvider>
);
};
Expand Down
6 changes: 3 additions & 3 deletions site/src/components/Dashboard/Navbar/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useAuth } from "components/AuthProvider/AuthProvider";
import { type FC } from "react";
import { useAuth } from "contexts/AuthProvider/AuthProvider";
import { useProxy } from "contexts/ProxyContext";
import { useDashboard } from "components/Dashboard/DashboardProvider";
import { useFeatureVisibility } from "hooks/useFeatureVisibility";
import { useMe } from "hooks/useMe";
import { usePermissions } from "hooks/usePermissions";
import { FC } from "react";
import { NavbarView } from "./NavbarView";
import { useProxy } from "contexts/ProxyContext";

export const Navbar: FC = () => {
const { appearance, buildInfo } = useDashboard();
Expand Down
10 changes: 5 additions & 5 deletions site/src/components/RequireAuth/RequireAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import axios from "axios";
import { useAuth } from "components/AuthProvider/AuthProvider";
import { FC, useEffect } from "react";
import { type FC, useEffect } from "react";
import { Outlet, Navigate, useLocation } from "react-router-dom";
import { embedRedirect } from "utils/redirect";
import { FullScreenLoader } from "../Loader/FullScreenLoader";
import { DashboardProvider } from "components/Dashboard/DashboardProvider";
import { ProxyProvider } from "contexts/ProxyContext";
import { isApiError } from "api/errors";
import { useAuth } from "contexts/AuthProvider/AuthProvider";
import { ProxyProvider } from "contexts/ProxyContext";
import { DashboardProvider } from "../Dashboard/DashboardProvider";
import { FullScreenLoader } from "../Loader/FullScreenLoader";

export const RequireAuth: FC = () => {
const { signOut, isSigningOut, isSignedOut } = useAuth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
};

if (isLoading) {
return <FullScreenLoader />;
return null;
}

return (
Expand Down
57 changes: 57 additions & 0 deletions site/src/contexts/ThemeProviders.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ThemeProvider as EmotionThemeProvider } from "@emotion/react";
import CssBaseline from "@mui/material/CssBaseline";
import {
StyledEngineProvider,
ThemeProvider as MuiThemeProvider,
} from "@mui/material/styles";
import {
type FC,
type PropsWithChildren,
useEffect,
useMemo,
useState,
} from "react";
import themes from "theme";
import { useAuth } from "./AuthProvider/AuthProvider";

export const ThemeProviders: FC<PropsWithChildren> = ({ children }) => {
const { user } = useAuth();
const themeQuery = useMemo(
() => window.matchMedia?.("(prefers-color-scheme: light)"),
[],
);
const [preferredColorScheme, setPreferredColorScheme] = useState<
"dark" | "light"
>(themeQuery?.matches ? "light" : "dark");

useEffect(() => {
const listener = (event: MediaQueryListEvent) => {
setPreferredColorScheme(event.matches ? "light" : "dark");
};

themeQuery.addEventListener("change", listener);
return () => {
themeQuery.removeEventListener("change", listener);
};
}, [themeQuery]);

// We might not be logged in yet, or the `theme_preference` could be an empty string.
const themePreference = user?.theme_preference || "auto";
// The janky casting here is find because of the much more type safe fallback
// We need to support `themePreference` being wrong anyway because the database
// value could be anything, like an empty string.
const theme =
themes[themePreference as keyof typeof themes] ??
themes[preferredColorScheme];

return (
<StyledEngineProvider injectFirst>
<MuiThemeProvider theme={theme}>
<EmotionThemeProvider theme={theme}>
<CssBaseline enableColorScheme />
{children}
</EmotionThemeProvider>
</MuiThemeProvider>
</StyledEngineProvider>
);
};
4 changes: 2 additions & 2 deletions site/src/hooks/useMe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { User } from "api/typesGenerated";
import { useAuth } from "components/AuthProvider/AuthProvider";
import type { User } from "api/typesGenerated";
import { useAuth } from "contexts/AuthProvider/AuthProvider";

export const useMe = (): User => {
const { user } = useAuth();
Expand Down
4 changes: 2 additions & 2 deletions site/src/hooks/usePermissions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAuth } from "components/AuthProvider/AuthProvider";
import { Permissions } from "components/AuthProvider/permissions";
import { useAuth } from "contexts/AuthProvider/AuthProvider";
import type { Permissions } from "contexts/AuthProvider/permissions";

export const usePermissions = (): Permissions => {
const { permissions } = useAuth();
Expand Down
6 changes: 3 additions & 3 deletions site/src/pages/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useAuth } from "components/AuthProvider/AuthProvider";
import { FC } from "react";
import { type FC } from "react";
import { Helmet } from "react-helmet-async";
import { Navigate, useLocation, useNavigate } from "react-router-dom";
import { useAuth } from "contexts/AuthProvider/AuthProvider";
import { getApplicationName } from "utils/appearance";
import { retrieveRedirect } from "utils/redirect";
import { LoginPageView } from "./LoginPageView";
import { getApplicationName } from "utils/appearance";

export const LoginPage: FC = () => {
const location = useLocation();
Expand Down
8 changes: 4 additions & 4 deletions site/src/pages/SetupPage/SetupPage.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useAuth } from "components/AuthProvider/AuthProvider";
import { FC } from "react";
import { Helmet } from "react-helmet-async";
import { pageTitle } from "utils/page";
import { SetupPageView } from "./SetupPageView";
import { Navigate, useNavigate } from "react-router-dom";
import { useMutation } from "react-query";
import { Navigate, useNavigate } from "react-router-dom";
import { pageTitle } from "utils/page";
import { createFirstUser } from "api/queries/users";
import { useAuth } from "contexts/AuthProvider/AuthProvider";
import { SetupPageView } from "./SetupPageView";

export const SetupPage: FC = () => {
const { signIn, isConfiguringTheFirstUser, isSignedIn, isSigningIn } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useOrganizationId } from "hooks";
import { useMe } from "hooks/useMe";
import { usePermissions } from "hooks/usePermissions";
import { groupsForUser } from "api/queries/groups";
import { useAuth } from "components/AuthProvider/AuthProvider";
import { useAuth } from "contexts/AuthProvider/AuthProvider";
import { useDashboard } from "components/Dashboard/DashboardProvider";
import { Section } from "../Section";
import { AccountUserGroups } from "./AccountUserGroups";
Expand Down
4 changes: 2 additions & 2 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {
type DeploymentConfig,
} from "api/api";
import { FieldError } from "api/errors";
import * as TypesGen from "api/typesGenerated";
import type * as TypesGen from "api/typesGenerated";
import range from "lodash/range";
import { Permissions } from "components/AuthProvider/permissions";
import { Permissions } from "contexts/AuthProvider/permissions";
import { TemplateVersionFiles } from "utils/templateVersion";
import { FileTree } from "utils/filetree";
import { ProxyLatencyReport } from "contexts/useProxyLatency";
Expand Down
2 changes: 1 addition & 1 deletion site/src/testHelpers/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { rest } from "msw";
import { CreateWorkspaceBuildRequest } from "api/typesGenerated";
import { permissionsToCheck } from "components/AuthProvider/permissions";
import { permissionsToCheck } from "contexts/AuthProvider/permissions";
import * as M from "./entities";
import { MockGroup, MockWorkspaceQuota } from "./entities";
import fs from "fs";
Expand Down
9 changes: 5 additions & 4 deletions site/src/testHelpers/renderHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {
waitFor,
renderHook,
} from "@testing-library/react";
import { AppProviders, ThemeProviders } from "App";
import { ReactNode, useState } from "react";
import { QueryClient } from "react-query";
import { AppProviders } from "App";
import { ThemeProviders } from "contexts/ThemeProviders";
import { DashboardLayout } from "components/Dashboard/DashboardLayout";
import { TemplateSettingsLayout } from "pages/TemplateSettingsPage/TemplateSettingsLayout";
import { WorkspaceSettingsLayout } from "pages/WorkspaceSettingsPage/WorkspaceSettingsLayout";
Expand All @@ -13,10 +16,8 @@ import {
createMemoryRouter,
RouteObject,
} from "react-router-dom";
import { RequireAuth } from "../components/RequireAuth/RequireAuth";
import { RequireAuth } from "components/RequireAuth/RequireAuth";
import { MockUser } from "./entities";
import { ReactNode, useState } from "react";
import { QueryClient } from "react-query";

function createTestQueryClient() {
// Helps create one query client for each test case, to make sure that tests
Expand Down
1 change: 1 addition & 0 deletions site/src/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Theme extends MuiTheme {
const theme = {
dark,
darkBlue,
light: darkBlue,
} satisfies Record<string, Theme>;

export default theme;