mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
refactor: reduce config change callback change
This commit is contained in:
parent
5e2c580cce
commit
4cda6eaf62
1 changed files with 10 additions and 4 deletions
|
|
@ -13,15 +13,21 @@ export function ConfigsContextWrapper(props: React.PropsWithChildren<Props>) {
|
|||
React.useEffect(() => {
|
||||
configHelper.get().then(setConfigs)
|
||||
}, [])
|
||||
|
||||
const everUpdatedRef = React.useRef(false)
|
||||
React.useEffect(() => {
|
||||
if (everUpdatedRef.current && configs) configHelper.set(configs)
|
||||
}, [configs])
|
||||
const onChange = React.useCallback(
|
||||
(updatedConfigs: Partial<Config>) => {
|
||||
const mergedConfigs = { ...configs, ...updatedConfigs } as Config
|
||||
configHelper.set(mergedConfigs)
|
||||
setConfigs(mergedConfigs)
|
||||
everUpdatedRef.current = true
|
||||
setConfigs(configs => (configs ? { ...configs, ...updatedConfigs } : configs))
|
||||
},
|
||||
[configs, setConfigs],
|
||||
[setConfigs],
|
||||
)
|
||||
|
||||
if (configs === null) return null
|
||||
|
||||
return (
|
||||
<ConfigsContext.Provider value={{ value: configs, onChange }}>
|
||||
{props.children}
|
||||
|
|
|
|||
Loading…
Reference in a new issue