reduce prop drilling

This commit is contained in:
DaKheera47
2026-01-20 22:46:50 +00:00
parent 29b259352f
commit 158e4b108b
8 changed files with 72 additions and 34 deletions
@@ -0,0 +1,58 @@
import { useEffect, useState } from 'react';
import type { AppSettings } from '../../shared/types';
import * as api from '../api';
let settingsCache: AppSettings | null = null;
let subscribers: Set<(settings: AppSettings) => void> = new Set();
let isFetching = false;
export function useSettings() {
const [settings, setSettings] = useState<AppSettings | null>(settingsCache);
useEffect(() => {
if (settingsCache) {
setSettings(settingsCache);
}
const handleUpdate = (newSettings: AppSettings) => {
setSettings(newSettings);
};
subscribers.add(handleUpdate);
if (!settingsCache && !isFetching) {
isFetching = true;
api.getSettings()
.then((data) => {
settingsCache = data;
subscribers.forEach(sub => sub(data));
})
.finally(() => {
isFetching = false;
});
}
return () => {
subscribers.delete(handleUpdate);
};
}, []);
const refreshSettings = async () => {
isFetching = true;
try {
const data = await api.getSettings();
settingsCache = data;
subscribers.forEach(sub => sub(data));
return data;
} finally {
isFetching = false;
}
};
return {
settings,
isLoading: !settings && isFetching,
showSponsorInfo: settings?.showSponsorInfo ?? true,
refreshSettings,
};
}