77} from '@/themes/theme'
88import { useUser } from '@clerk/nextjs'
99import { useRouter } from 'next/router'
10- import { createContext , useContext , useEffect , useMemo , useState } from 'react'
10+ import { createContext , useCallback , useContext , useEffect , useMemo , useState } from 'react'
1111import { generateLocaleDict , initLocale , redirectUserLang } from './utils/lang'
1212
1313/**
@@ -52,7 +52,7 @@ export function GlobalContextProvider(props) {
5252 const fullWidth = post ?. fullWidth ?? false
5353
5454 // 切换主题
55- function switchTheme ( ) {
55+ const switchTheme = useCallback ( ( ) => {
5656 const query = router . query
5757 const currentTheme = query . theme || theme
5858 const currentIndex = THEMES . indexOf ( currentTheme )
@@ -61,30 +61,30 @@ export function GlobalContextProvider(props) {
6161 query . theme = newTheme
6262 router . push ( { pathname : router . pathname , query } )
6363 return newTheme
64- }
64+ } , [ router , theme , THEMES ] )
6565
6666 // 抓取主题配置
67- const updateThemeConfig = async theme => {
67+ const updateThemeConfig = useCallback ( async theme => {
6868 const config = await getThemeConfig ( theme )
6969 SET_THEME_CONFIG ( config )
70- }
70+ } , [ ] )
7171
7272 // 切换深色模式
73- const toggleDarkMode = ( ) => {
73+ const toggleDarkMode = useCallback ( ( ) => {
7474 const newStatus = ! isDarkMode
7575 saveDarkModeToLocalStorage ( newStatus )
7676 updateDarkMode ( newStatus )
7777 const htmlElement = document . getElementsByTagName ( 'html' ) [ 0 ]
7878 htmlElement . classList ?. remove ( newStatus ? 'light' : 'dark' )
7979 htmlElement . classList ?. add ( newStatus ? 'dark' : 'light' )
80- }
80+ } , [ isDarkMode ] )
8181
82- function changeLang ( lang ) {
82+ const changeLang = useCallback ( lang => {
8383 if ( lang ) {
8484 updateLang ( lang )
8585 updateLocale ( generateLocaleDict ( lang ) )
8686 }
87- }
87+ } , [ ] )
8888
8989 // 添加路由变化时的语言处理
9090 useEffect ( ( ) => {
@@ -133,7 +133,7 @@ export function GlobalContextProvider(props) {
133133 }
134134 } , [ router . events ] )
135135
136- const contextValue = {
136+ const contextValue = useMemo ( ( ) => ( {
137137 isLiteMode,
138138 isLoaded,
139139 isSignedIn,
@@ -156,7 +156,7 @@ export function GlobalContextProvider(props) {
156156 siteInfo,
157157 categoryOptions,
158158 tagOptions
159- }
159+ } ) , [ isLiteMode , isLoaded , isSignedIn , user , fullWidth , NOTION_CONFIG , THEME_CONFIG , toggleDarkMode , onLoading , setOnLoading , lang , changeLang , locale , updateLocale , isDarkMode , updateDarkMode , theme , setTheme , switchTheme , siteInfo , categoryOptions , tagOptions ] )
160160 globalSnapshot = contextValue
161161
162162 return (
0 commit comments