추세검색 설정 수정
This commit is contained in:
@@ -49,6 +49,22 @@ import {
|
||||
// 전역 캐시 — 여러 컴포넌트에서 공유하여 중복 요청 방지
|
||||
let _cache: AppSettingsDto | null = null;
|
||||
let _loadPromise: Promise<AppSettingsDto> | null = null;
|
||||
type AppSettingsListener = () => void;
|
||||
const _listeners = new Set<AppSettingsListener>();
|
||||
|
||||
function notifyAppSettingsListeners() {
|
||||
_listeners.forEach(fn => fn());
|
||||
}
|
||||
|
||||
/** 훅 외부에서 최신 캐시 읽기 (추세검색 설정 병합 등) */
|
||||
export function getAppSettingsCache(): AppSettingsDto {
|
||||
return _cache ?? {};
|
||||
}
|
||||
|
||||
export function subscribeAppSettings(listener: AppSettingsListener): () => void {
|
||||
_listeners.add(listener);
|
||||
return () => _listeners.delete(listener);
|
||||
}
|
||||
|
||||
function ensureLoaded(): Promise<AppSettingsDto> {
|
||||
if (_cache !== null) return Promise.resolve(_cache);
|
||||
@@ -56,6 +72,7 @@ function ensureLoaded(): Promise<AppSettingsDto> {
|
||||
_loadPromise = loadAppSettings().then(data => {
|
||||
_cache = data ?? {};
|
||||
_loadPromise = null;
|
||||
notifyAppSettingsListeners();
|
||||
return _cache;
|
||||
});
|
||||
return _loadPromise;
|
||||
@@ -141,7 +158,13 @@ export function useAppSettings(sessionKey = 0) {
|
||||
console.error('[useAppSettings] load failed', err);
|
||||
if (mountedRef.current) setIsLoaded(true);
|
||||
});
|
||||
return () => { mountedRef.current = false; };
|
||||
const unsub = subscribeAppSettings(() => {
|
||||
if (mountedRef.current) setSettings({ ...(getAppSettingsCache()) });
|
||||
});
|
||||
return () => {
|
||||
mountedRef.current = false;
|
||||
unsub();
|
||||
};
|
||||
}, [sessionKey]);
|
||||
|
||||
/**
|
||||
@@ -151,14 +174,18 @@ export function useAppSettings(sessionKey = 0) {
|
||||
const save = useCallback((patch: AppSettingsDto) => {
|
||||
const merged = { ...(_cache ?? {}), ...patch };
|
||||
_cache = merged;
|
||||
notifyAppSettingsListeners();
|
||||
setSettings(merged);
|
||||
if (patch.displayTimezone != null) {
|
||||
setDisplayTimezone(normalizeTimezone(patch.displayTimezone));
|
||||
}
|
||||
saveAppSettings(patch).then(updated => {
|
||||
if (updated && mountedRef.current) {
|
||||
if (updated) {
|
||||
_cache = { ...(_cache ?? {}), ...updated };
|
||||
setSettings(prev => ({ ...prev, ...updated }));
|
||||
notifyAppSettingsListeners();
|
||||
if (mountedRef.current) {
|
||||
setSettings(prev => ({ ...prev, ...updated }));
|
||||
}
|
||||
}
|
||||
}).catch(err =>
|
||||
console.error('[useAppSettings] save failed', err)
|
||||
|
||||
Reference in New Issue
Block a user