mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-24 23:46:52 +08:00
### What problem does this PR solve? This PR closing feature request #11286. It implements ability to choose the background theme of the _Full screen chat_ which is Embed into webpage. Looks like that: <img width="501" height="349" alt="image" src="https://github.com/user-attachments/assets/e5fdfb14-9ed9-43bb-a40d-4b580985b9d4" /> It works similar to `Locale`, using url parameter to set the theme. if the parameter is invalid then is using the default theme. ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Co-authored-by: Your Name <you@example.com>
84 lines
1.9 KiB
TypeScript
84 lines
1.9 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]);
|
|
}
|
|
|
|
export function useSyncThemeFromParams(theme: string | null) {
|
|
const { setTheme } = useTheme();
|
|
|
|
useEffect(() => {
|
|
if (theme && (theme === ThemeEnum.Light || theme === ThemeEnum.Dark)) {
|
|
setTheme(theme as ThemeEnum);
|
|
}
|
|
}, [theme, setTheme]);
|
|
}
|