mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-24 07:26:47 +08:00
### What problem does this PR solve? Fix: Switch the default theme from light mode to dark mode and improve some styles #9869 -Update UI component styles such as input boxes, tables, and prompt boxes -Optimize login page layout and style details -Revise some of the wording, such as uniformly changing "data flow" to "pipeline" -Adjust the parser to support the markdown type ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
74 lines
1.6 KiB
TypeScript
74 lines
1.6 KiB
TypeScript
import { ThemeEnum } from '@/constants/common';
|
|
import React, { createContext, useContext, useEffect, useState } from 'react';
|
|
|
|
type ThemeProviderProps = {
|
|
children: React.ReactNode;
|
|
defaultTheme?: ThemeEnum;
|
|
storageKey?: string;
|
|
};
|
|
|
|
type ThemeProviderState = {
|
|
theme: ThemeEnum;
|
|
setTheme: (theme: ThemeEnum) => void;
|
|
};
|
|
|
|
const initialState: ThemeProviderState = {
|
|
theme: ThemeEnum.Light,
|
|
setTheme: () => null,
|
|
};
|
|
|
|
const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
|
|
|
|
export function ThemeProvider({
|
|
children,
|
|
defaultTheme = ThemeEnum.Dark,
|
|
storageKey = 'vite-ui-theme',
|
|
...props
|
|
}: ThemeProviderProps) {
|
|
const [theme, setTheme] = useState<ThemeEnum>(
|
|
() => (localStorage.getItem(storageKey) as ThemeEnum) || defaultTheme,
|
|
);
|
|
|
|
useEffect(() => {
|
|
const root = window.document.documentElement;
|
|
root.classList.remove(ThemeEnum.Light, ThemeEnum.Dark);
|
|
localStorage.setItem(storageKey, theme);
|
|
root.classList.add(theme);
|
|
}, [storageKey, theme]);
|
|
|
|
return (
|
|
<ThemeProviderContext.Provider
|
|
{...props}
|
|
value={{
|
|
theme,
|
|
setTheme,
|
|
}}
|
|
>
|
|
{children}
|
|
</ThemeProviderContext.Provider>
|
|
);
|
|
}
|
|
|
|
export const useTheme = () => {
|
|
const context = useContext(ThemeProviderContext);
|
|
|
|
if (context === undefined)
|
|
throw new Error('useTheme must be used within a ThemeProvider');
|
|
|
|
return context;
|
|
};
|
|
|
|
export const useIsDarkTheme = () => {
|
|
const { theme } = useTheme();
|
|
|
|
return theme === ThemeEnum.Dark;
|
|
};
|
|
|
|
export function useSwitchToDarkThemeOnMount() {
|
|
const { setTheme } = useTheme();
|
|
|
|
useEffect(() => {
|
|
setTheme(ThemeEnum.Dark);
|
|
}, [setTheme]);
|
|
}
|