MCPcopy Create free account
hub / github.com/chokcoco/iCSS / AppProvider

Function AppProvider

website/app/contexts/AppContext.tsx:18–68  ·  view source on GitHub ↗
({ children }: { children: React.ReactNode })

Source from the content-addressed store, hash-verified

16const AppContext = createContext<AppContextType | undefined>(undefined);
17
18export function AppProvider({ children }: { children: React.ReactNode }) {
19 const [theme, setThemeState] = useState<Theme>('system');
20 const [language, setLanguageState] = useState<Language>('zh');
21
22 useEffect(() => {
23 try {
24 // 初始化主题和语言
25 initializeTheme();
26 setThemeState(getStoredTheme());
27 setLanguageState(getStoredLanguage());
28 } catch (error) {
29 console.warn('Failed to initialize theme/language:', error);
30 }
31 }, []);
32
33 const setTheme = (newTheme: Theme) => {
34 try {
35 setThemeState(newTheme);
36 applyTheme(newTheme);
37 } catch (error) {
38 console.warn('Failed to set theme:', error);
39 }
40 };
41
42 const setLanguage = (newLanguage: Language) => {
43 try {
44 setLanguageState(newLanguage);
45 setStoredLanguage(newLanguage);
46 } catch (error) {
47 console.warn('Failed to set language:', error);
48 }
49 };
50
51 const t = (key: keyof Translations): string => {
52 return getTranslation(language, key);
53 };
54
55 const contextValue: AppContextType = {
56 theme,
57 setTheme,
58 language,
59 setLanguage,
60 t
61 };
62
63 return (
64 <AppContext.Provider value={contextValue}>
65 {children}
66 </AppContext.Provider>
67 );
68}
69
70export function useApp() {
71 const context = useContext(AppContext);

Callers

nothing calls this directly

Calls 3

initializeThemeFunction · 0.90
getStoredThemeFunction · 0.90
getStoredLanguageFunction · 0.90

Tested by

no test coverage detected