Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c00e5ca2
authored
Sep 29, 2025
by
RedwindA
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: Optimize sidebar refresh to avoid redundant loading states
parent
59e2c884
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
11 deletions
+39
-11
web/src/components/layout/SiderBar.jsx
+1
-1
web/src/hooks/common/useHeaderBar.js
+1
-1
web/src/hooks/common/useSidebar.js
+37
-9
No files found.
web/src/components/layout/SiderBar.jsx
View file @
c00e5ca2
...
@@ -58,7 +58,7 @@ const SiderBar = ({ onNavigate = () => {} }) => {
...
@@ -58,7 +58,7 @@ const SiderBar = ({ onNavigate = () => {} }) => {
loading
:
sidebarLoading
,
loading
:
sidebarLoading
,
}
=
useSidebar
();
}
=
useSidebar
();
const
showSkeleton
=
useMinimumLoadingTime
(
sidebarLoading
);
const
showSkeleton
=
useMinimumLoadingTime
(
sidebarLoading
,
200
);
const
[
selectedKeys
,
setSelectedKeys
]
=
useState
([
'home'
]);
const
[
selectedKeys
,
setSelectedKeys
]
=
useState
([
'home'
]);
const
[
chatItems
,
setChatItems
]
=
useState
([]);
const
[
chatItems
,
setChatItems
]
=
useState
([]);
...
...
web/src/hooks/common/useHeaderBar.js
View file @
c00e5ca2
...
@@ -40,7 +40,7 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
...
@@ -40,7 +40,7 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
const
location
=
useLocation
();
const
location
=
useLocation
();
const
loading
=
statusState
?.
status
===
undefined
;
const
loading
=
statusState
?.
status
===
undefined
;
const
isLoading
=
useMinimumLoadingTime
(
loading
);
const
isLoading
=
useMinimumLoadingTime
(
loading
,
200
);
const
systemName
=
getSystemName
();
const
systemName
=
getSystemName
();
const
logo
=
getLogo
();
const
logo
=
getLogo
();
...
...
web/src/hooks/common/useSidebar.js
View file @
c00e5ca2
...
@@ -17,7 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
...
@@ -17,7 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
For commercial licensing, please contact support@quantumnous.com
*/
*/
import
{
useState
,
useEffect
,
useMemo
,
useContext
}
from
'react'
;
import
{
useState
,
useEffect
,
useMemo
,
useContext
,
useRef
}
from
'react'
;
import
{
StatusContext
}
from
'../../context/Status'
;
import
{
StatusContext
}
from
'../../context/Status'
;
import
{
API
}
from
'../../helpers'
;
import
{
API
}
from
'../../helpers'
;
...
@@ -29,6 +29,13 @@ export const useSidebar = () => {
...
@@ -29,6 +29,13 @@ export const useSidebar = () => {
const
[
statusState
]
=
useContext
(
StatusContext
);
const
[
statusState
]
=
useContext
(
StatusContext
);
const
[
userConfig
,
setUserConfig
]
=
useState
(
null
);
const
[
userConfig
,
setUserConfig
]
=
useState
(
null
);
const
[
loading
,
setLoading
]
=
useState
(
true
);
const
[
loading
,
setLoading
]
=
useState
(
true
);
const
instanceIdRef
=
useRef
(
null
);
const
hasLoadedOnceRef
=
useRef
(
false
);
if
(
!
instanceIdRef
.
current
)
{
const
randomPart
=
Math
.
random
().
toString
(
16
).
slice
(
2
);
instanceIdRef
.
current
=
`sidebar-
${
Date
.
now
()}
-
${
randomPart
}
`
;
}
// 默认配置
// 默认配置
const
defaultAdminConfig
=
{
const
defaultAdminConfig
=
{
...
@@ -74,9 +81,17 @@ export const useSidebar = () => {
...
@@ -74,9 +81,17 @@ export const useSidebar = () => {
},
[
statusState
?.
status
?.
SidebarModulesAdmin
]);
},
[
statusState
?.
status
?.
SidebarModulesAdmin
]);
// 加载用户配置的通用方法
// 加载用户配置的通用方法
const
loadUserConfig
=
async
()
=>
{
const
loadUserConfig
=
async
({
withLoading
}
=
{})
=>
{
const
shouldShowLoader
=
typeof
withLoading
===
'boolean'
?
withLoading
:
!
hasLoadedOnceRef
.
current
;
try
{
try
{
setLoading
(
true
);
if
(
shouldShowLoader
)
{
setLoading
(
true
);
}
const
res
=
await
API
.
get
(
'/api/user/self'
);
const
res
=
await
API
.
get
(
'/api/user/self'
);
if
(
res
.
data
.
success
&&
res
.
data
.
data
.
sidebar_modules
)
{
if
(
res
.
data
.
success
&&
res
.
data
.
data
.
sidebar_modules
)
{
let
config
;
let
config
;
...
@@ -122,18 +137,25 @@ export const useSidebar = () => {
...
@@ -122,18 +137,25 @@ export const useSidebar = () => {
});
});
setUserConfig
(
defaultUserConfig
);
setUserConfig
(
defaultUserConfig
);
}
finally
{
}
finally
{
setLoading
(
false
);
if
(
shouldShowLoader
)
{
setLoading
(
false
);
}
hasLoadedOnceRef
.
current
=
true
;
}
}
};
};
// 刷新用户配置的方法(供外部调用)
// 刷新用户配置的方法(供外部调用)
const
refreshUserConfig
=
async
()
=>
{
const
refreshUserConfig
=
async
()
=>
{
if
(
Object
.
keys
(
adminConfig
).
length
>
0
)
{
if
(
Object
.
keys
(
adminConfig
).
length
>
0
)
{
await
loadUserConfig
();
await
loadUserConfig
(
{
withLoading
:
false
}
);
}
}
// 触发全局刷新事件,通知所有useSidebar实例更新
// 触发全局刷新事件,通知所有useSidebar实例更新
sidebarEventTarget
.
dispatchEvent
(
new
CustomEvent
(
SIDEBAR_REFRESH_EVENT
));
sidebarEventTarget
.
dispatchEvent
(
new
CustomEvent
(
SIDEBAR_REFRESH_EVENT
,
{
detail
:
{
sourceId
:
instanceIdRef
.
current
,
skipLoader
:
true
},
}),
);
};
};
// 加载用户配置
// 加载用户配置
...
@@ -146,9 +168,15 @@ export const useSidebar = () => {
...
@@ -146,9 +168,15 @@ export const useSidebar = () => {
// 监听全局刷新事件
// 监听全局刷新事件
useEffect
(()
=>
{
useEffect
(()
=>
{
const
handleRefresh
=
()
=>
{
const
handleRefresh
=
(
event
)
=>
{
if
(
event
?.
detail
?.
sourceId
===
instanceIdRef
.
current
)
{
return
;
}
if
(
Object
.
keys
(
adminConfig
).
length
>
0
)
{
if
(
Object
.
keys
(
adminConfig
).
length
>
0
)
{
loadUserConfig
();
loadUserConfig
({
withLoading
:
event
?.
detail
?.
skipLoader
?
false
:
undefined
,
});
}
}
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment