From d250aed26a1e8f4b9ea24c18ca80869c2ebab2dd Mon Sep 17 00:00:00 2001 From: shinchan-zhai Date: Tue, 31 Mar 2026 16:12:01 +0800 Subject: [PATCH] fix: auto re-fetch system config after invalidation - invalidateSystemConfig() now dispatches a custom event - useSystemConfig() listens for the event and re-fetches automatically - Fixes stale initialized=false after register/logout causing incorrect redirect to SetupPage --- web/src/hooks/useSystemConfig.ts | 19 ++++++++++++++++--- web/src/lib/config.ts | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/web/src/hooks/useSystemConfig.ts b/web/src/hooks/useSystemConfig.ts index 4c33620b..237089b2 100644 --- a/web/src/hooks/useSystemConfig.ts +++ b/web/src/hooks/useSystemConfig.ts @@ -1,13 +1,15 @@ -import { useEffect, useState } from 'react' -import { getSystemConfig, type SystemConfig } from '../lib/config' +import { useEffect, useState, useCallback } from 'react' +import { getSystemConfig, invalidateSystemConfig, type SystemConfig } from '../lib/config' export function useSystemConfig() { const [config, setConfig] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) + const [fetchKey, setFetchKey] = useState(0) useEffect(() => { let mounted = true + setLoading(true) getSystemConfig() .then((data) => { if (!mounted) return @@ -23,7 +25,18 @@ export function useSystemConfig() { return () => { mounted = false } + }, [fetchKey]) + + // Listen for invalidation events and re-fetch automatically + useEffect(() => { + const handler = () => setFetchKey((k) => k + 1) + window.addEventListener('system-config-invalidated', handler) + return () => window.removeEventListener('system-config-invalidated', handler) }, []) - return { config, loading, error } + const refresh = useCallback(() => { + invalidateSystemConfig() + }, []) + + return { config, loading, error, refresh } } diff --git a/web/src/lib/config.ts b/web/src/lib/config.ts index 54d138e0..ed4b2c67 100644 --- a/web/src/lib/config.ts +++ b/web/src/lib/config.ts @@ -26,4 +26,5 @@ export function getSystemConfig(): Promise { export function invalidateSystemConfig() { cachedConfig = null configPromise = null + window.dispatchEvent(new Event('system-config-invalidated')) }