Skip to content

Commit

Permalink
fix: dark theme state for echarts
Browse files Browse the repository at this point in the history
  • Loading branch information
lideming committed Nov 27, 2023
1 parent 742f6bc commit 066d7c7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const CPUGraph = ({
const cpus = hardwareData[0].value.cpus;
const initialOption = () => {
return {
backgroundColor: 'transparent',
animation: false,
tooltip: {
trigger: "axis",
Expand Down
9 changes: 7 additions & 2 deletions neetbox/frontend/src/components/echarts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useRef, useEffect, HTMLAttributes, useState } from "react";
import type * as echarts from "echarts";
import Loading from "./loading";
import { useTheme } from "./themeSwitcher";

export interface EChartsProps {
initialOption: () => echarts.EChartsOption;
Expand All @@ -14,14 +15,18 @@ export const ECharts = (props: EChartsProps) => {
const [echartsModule, setEchartsModule] = useState<typeof echarts | null>(
null,
);
const { darkMode } = useTheme();

useEffect(() => {
import("echarts").then((mod) => setEchartsModule(mod));
});

useEffect(() => {
if (echartsModule) {
const chart = echartsModule.init(chartContainerRef.current);
const chart = echartsModule.init(
chartContainerRef.current,
darkMode ? "dark" : null,
);

chart.setOption(props.initialOption());
chartRef.current = chart;
Expand All @@ -37,7 +42,7 @@ export const ECharts = (props: EChartsProps) => {
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [echartsModule]);
}, [echartsModule, darkMode]);

useEffect(() => {
if (chartRef.current) {
Expand Down
51 changes: 44 additions & 7 deletions neetbox/frontend/src/components/themeSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
import React from "react";
import React, {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from "react";
import { Button } from "@douyinfe/semi-ui";

const Context = createContext<{
darkMode: boolean;
setDarkMode: (val: boolean) => void;
}>(null!);

export default function SwitchColorMode(): React.JSX.Element {
const switchMode = () => {
export function useTheme() {
return useContext(Context);
}

export function ThemeContextProvider(props: React.PropsWithChildren) {
const [darkMode, setDarkModeState] = useState(false);

const setDarkMode = useCallback((val) => {
setDarkModeState(val);
localStorage.setItem("neetbox-theme", val ? "dark" : "");
}, []);

useEffect(() => {
setDarkModeState(localStorage.getItem("neetbox-theme") === "dark");
}, []);

useEffect(() => {
const body = document.body;
if (body.hasAttribute("theme-mode")) {
body.removeAttribute("theme-mode");
} else {
if (darkMode) {
body.setAttribute("theme-mode", "dark");
} else {
body.removeAttribute("theme-mode");
}
};
}, [darkMode]);

return (
<Context.Provider value={{ darkMode, setDarkMode }}>
{props.children}
</Context.Provider>
);
}

export default function SwitchColorMode(): React.JSX.Element {
const { darkMode, setDarkMode } = useTheme();
const switchMode = () => {
setDarkMode(!darkMode);
};
return <Button onClick={switchMode}>Switch Mode</Button>;
}
11 changes: 7 additions & 4 deletions neetbox/frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import LoginPage from "./pages/login";
import "./index.css";
import Console, { consoleRoutes } from "./pages/console";
import { startBackgroundTasks } from "./services/projects";
import { ThemeContextProvider } from "./components/themeSwitcher";

const router = createBrowserRouter([
{
Expand Down Expand Up @@ -36,8 +37,10 @@ function ServiceProvider({ children }: PropsWithChildren) {

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<ServiceProvider>
<RouterProvider router={router} />
</ServiceProvider>
</React.StrictMode>
<ThemeContextProvider>
<ServiceProvider>
<RouterProvider router={router} />
</ServiceProvider>
</ThemeContextProvider>
</React.StrictMode>,
);

0 comments on commit 066d7c7

Please sign in to comment.