Split multi-hook frontend modules into single-hook files

This commit is contained in:
2026-04-18 20:47:44 +02:00
parent fba7675eb8
commit 3f197b1ad7
20 changed files with 1175 additions and 1180 deletions

View File

@@ -0,0 +1,68 @@
/**
* React hook for loading and updating global configuration.
*/
import { useCallback, useEffect, useRef, useState } from "react";
import { fetchGlobalConfig, updateGlobalConfig } from "../api/config";
import { handleFetchError } from "../utils/fetchError";
import type { GlobalConfig, GlobalConfigUpdate } from "../types/config";
export interface UseGlobalConfigResult {
config: GlobalConfig | null;
loading: boolean;
error: string | null;
refresh: () => void;
updateConfig: (update: GlobalConfigUpdate) => Promise<void>;
}
/**
* Load global configuration and expose update operations.
*/
export function useGlobalConfig(): UseGlobalConfigResult {
const [config, setConfig] = useState<GlobalConfig | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const abortRef = useRef<AbortController | null>(null);
const load = useCallback((): void => {
abortRef.current?.abort();
const ctrl = new AbortController();
abortRef.current = ctrl;
setLoading(true);
setError(null);
fetchGlobalConfig()
.then((resp) => {
if (!ctrl.signal.aborted) {
setConfig(resp);
}
})
.catch((err: unknown) => {
if (!ctrl.signal.aborted) {
handleFetchError(err, setError, "Failed to fetch global config");
}
})
.finally(() => {
if (!abortRef.current?.signal.aborted) {
setLoading(false);
}
});
}, []);
useEffect(() => {
load();
return (): void => {
abortRef.current?.abort();
};
}, [load]);
const updateConfig = useCallback(
async (update: GlobalConfigUpdate): Promise<void> => {
await updateGlobalConfig(update);
load();
},
[load],
);
return { config, loading, error, refresh: load, updateConfig };
}