@@ -17,7 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1717For commercial licensing, please contact [email protected] 1818*/
1919
20- import { useState , useEffect , useMemo , useContext } from 'react' ;
20+ import { useState , useEffect , useMemo , useContext , useRef } from 'react' ;
2121import { StatusContext } from '../../context/Status' ;
2222import { API } from '../../helpers' ;
2323
@@ -29,6 +29,13 @@ export const useSidebar = () => {
2929 const [ statusState ] = useContext ( StatusContext ) ;
3030 const [ userConfig , setUserConfig ] = useState ( null ) ;
3131 const [ loading , setLoading ] = useState ( true ) ;
32+ const instanceIdRef = useRef ( null ) ;
33+ const hasLoadedOnceRef = useRef ( false ) ;
34+
35+ if ( ! instanceIdRef . current ) {
36+ const randomPart = Math . random ( ) . toString ( 16 ) . slice ( 2 ) ;
37+ instanceIdRef . current = `sidebar-${ Date . now ( ) } -${ randomPart } ` ;
38+ }
3239
3340 // 默认配置
3441 const defaultAdminConfig = {
@@ -74,9 +81,17 @@ export const useSidebar = () => {
7481 } , [ statusState ?. status ?. SidebarModulesAdmin ] ) ;
7582
7683 // 加载用户配置的通用方法
77- const loadUserConfig = async ( ) => {
84+ const loadUserConfig = async ( { withLoading } = { } ) => {
85+ const shouldShowLoader =
86+ typeof withLoading === 'boolean'
87+ ? withLoading
88+ : ! hasLoadedOnceRef . current ;
89+
7890 try {
79- setLoading ( true ) ;
91+ if ( shouldShowLoader ) {
92+ setLoading ( true ) ;
93+ }
94+
8095 const res = await API . get ( '/api/user/self' ) ;
8196 if ( res . data . success && res . data . data . sidebar_modules ) {
8297 let config ;
@@ -122,18 +137,25 @@ export const useSidebar = () => {
122137 } ) ;
123138 setUserConfig ( defaultUserConfig ) ;
124139 } finally {
125- setLoading ( false ) ;
140+ if ( shouldShowLoader ) {
141+ setLoading ( false ) ;
142+ }
143+ hasLoadedOnceRef . current = true ;
126144 }
127145 } ;
128146
129147 // 刷新用户配置的方法(供外部调用)
130148 const refreshUserConfig = async ( ) => {
131- if ( Object . keys ( adminConfig ) . length > 0 ) {
132- await loadUserConfig ( ) ;
149+ if ( Object . keys ( adminConfig ) . length > 0 ) {
150+ await loadUserConfig ( { withLoading : false } ) ;
133151 }
134152
135153 // 触发全局刷新事件,通知所有useSidebar实例更新
136- sidebarEventTarget . dispatchEvent ( new CustomEvent ( SIDEBAR_REFRESH_EVENT ) ) ;
154+ sidebarEventTarget . dispatchEvent (
155+ new CustomEvent ( SIDEBAR_REFRESH_EVENT , {
156+ detail : { sourceId : instanceIdRef . current , skipLoader : true } ,
157+ } ) ,
158+ ) ;
137159 } ;
138160
139161 // 加载用户配置
@@ -146,9 +168,15 @@ export const useSidebar = () => {
146168
147169 // 监听全局刷新事件
148170 useEffect ( ( ) => {
149- const handleRefresh = ( ) => {
171+ const handleRefresh = ( event ) => {
172+ if ( event ?. detail ?. sourceId === instanceIdRef . current ) {
173+ return ;
174+ }
175+
150176 if ( Object . keys ( adminConfig ) . length > 0 ) {
151- loadUserConfig ( ) ;
177+ loadUserConfig ( {
178+ withLoading : event ?. detail ?. skipLoader ? false : undefined ,
179+ } ) ;
152180 }
153181 } ;
154182
0 commit comments