chore: commit local changes
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
* React hook for loading and updating a single parsed jail.d config file.
|
||||
*/
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { useConfigItem } from "./useConfigItem";
|
||||
import { fetchParsedJailFile, updateParsedJailFile } from "../api/config";
|
||||
import type { JailFileConfig, JailFileConfigUpdate } from "../types/config";
|
||||
|
||||
@@ -21,56 +21,23 @@ export interface UseJailFileConfigResult {
|
||||
* @param filename - Filename including extension (e.g. ``"sshd.conf"``).
|
||||
*/
|
||||
export function useJailFileConfig(filename: string): UseJailFileConfigResult {
|
||||
const [config, setConfig] = useState<JailFileConfig | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const abortRef = useRef<AbortController | null>(null);
|
||||
const { data, loading, error, refresh, save } = useConfigItem<
|
||||
JailFileConfig,
|
||||
JailFileConfigUpdate
|
||||
>({
|
||||
fetchFn: () => fetchParsedJailFile(filename),
|
||||
saveFn: (update) => updateParsedJailFile(filename, update),
|
||||
mergeOnSave: (prev, update) =>
|
||||
update.jails != null && prev
|
||||
? { ...prev, jails: { ...prev.jails, ...update.jails } }
|
||||
: prev,
|
||||
});
|
||||
|
||||
const load = useCallback((): void => {
|
||||
abortRef.current?.abort();
|
||||
const ctrl = new AbortController();
|
||||
abortRef.current = ctrl;
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
fetchParsedJailFile(filename)
|
||||
.then((data) => {
|
||||
if (!ctrl.signal.aborted) {
|
||||
setConfig(data);
|
||||
setLoading(false);
|
||||
}
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
if (!ctrl.signal.aborted) {
|
||||
setError(err instanceof Error ? err.message : "Failed to load jail file config");
|
||||
setLoading(false);
|
||||
}
|
||||
});
|
||||
}, [filename]);
|
||||
|
||||
useEffect(() => {
|
||||
load();
|
||||
return (): void => {
|
||||
abortRef.current?.abort();
|
||||
};
|
||||
}, [load]);
|
||||
|
||||
const save = useCallback(
|
||||
async (update: JailFileConfigUpdate): Promise<void> => {
|
||||
try {
|
||||
await updateParsedJailFile(filename, update);
|
||||
// Optimistically merge updated jails into local state.
|
||||
if (update.jails != null) {
|
||||
setConfig((prev) =>
|
||||
prev ? { ...prev, jails: { ...prev.jails, ...update.jails } } : prev
|
||||
);
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
throw err instanceof Error ? err : new Error("Failed to save jail file config");
|
||||
}
|
||||
},
|
||||
[filename]
|
||||
);
|
||||
|
||||
return { config, loading, error, refresh: load, save };
|
||||
return {
|
||||
config: data,
|
||||
loading,
|
||||
error,
|
||||
refresh,
|
||||
save,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user