ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
import { useWorkspace } from '../contexts/useWorkspace.js';
import { queryClient, serializeQueryKey } from '../services/queryClient';
import { usePolling } from './usePolling';
const workspaceKey = (workspaceId, queryKey) => ['workspace', workspaceId, ...queryKey];
export function useServerQuery(queryKey, queryFn, options = {}) {
const {
enabled = true,
staleTime = 0,
// Keep this key fresh on an interval. Two things a hand-rolled
// setInterval next to a useState does not get: the query client already
// dedupes concurrent fetches of the same key, and the scheduler below
// stops entirely while the tab is hidden, catching up once on return.
refetchInterval = 0,
onError,
} = options;
const { activeWorkspaceId } = useWorkspace();
const localKeyHash = serializeQueryKey(queryKey);
const scopedKey = useMemo(
() => workspaceKey(activeWorkspaceId, queryKey),
// queryKey is commonly declared inline; its serialized value is the
// semantic dependency and avoids refetching on every render.
// eslint-disable-next-line react-hooks/exhaustive-deps
[activeWorkspaceId, localKeyHash],
);
const scopedKeyHash = serializeQueryKey(scopedKey);
const queryFnRef = useRef(queryFn);
const onErrorRef = useRef(onError);
queryFnRef.current = queryFn;
onErrorRef.current = onError;
const subscribe = useCallback(
(listener) => queryClient.subscribe(scopedKey, listener),
// eslint-disable-next-line react-hooks/exhaustive-deps
[scopedKeyHash],
);
const getSnapshot = useCallback(
() => queryClient.getSnapshot(scopedKey),
// eslint-disable-next-line react-hooks/exhaustive-deps
[scopedKeyHash],
);
const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
useEffect(() => {
if (!enabled) return undefined;
let active = true;
queryClient.fetchQuery(
scopedKey,
(context) => queryFnRef.current(context),
{ staleTime },
).catch((error) => {
if (active) onErrorRef.current?.(error);
});
return () => { active = false; };
}, [enabled, scopedKeyHash, staleTime, state.revision, scopedKey]);
const refetch = useCallback(
() => queryClient.fetchQuery(
scopedKey,
(context) => queryFnRef.current(context),
{ force: true, staleTime },
),
// eslint-disable-next-line react-hooks/exhaustive-deps
[scopedKeyHash, staleTime],
);
usePolling(refetch, refetchInterval, {
enabled: enabled && refetchInterval > 0,
// The mount effect above already fetched; polling adds the *repeat*.
immediate: false,
onError: (error) => onErrorRef.current?.(error),
});
return {
...state,
isLoading: state.status === 'idle' || state.status === 'loading',
isError: state.status === 'error',
refetch,
};
}
export function useServerMutation(mutationFn, options = {}) {
const { invalidate = [], onSuccess, onError } = options;
const { activeWorkspaceId } = useWorkspace();
const mutationFnRef = useRef(mutationFn);
const optionsRef = useRef({ invalidate, onSuccess, onError });
mutationFnRef.current = mutationFn;
optionsRef.current = { invalidate, onSuccess, onError };
const [state, setState] = useState({ status: 'idle', data: undefined, error: null });
const mutate = useCallback(async (variables) => {
setState((previous) => ({ ...previous, status: 'loading', error: null }));
try {
const data = await mutationFnRef.current(variables);
setState({ status: 'success', data, error: null });
optionsRef.current.invalidate.forEach((prefix) => {
queryClient.invalidateQueries(workspaceKey(activeWorkspaceId, prefix));
});
await optionsRef.current.onSuccess?.(data, variables);
return data;
} catch (error) {
setState({ status: 'error', data: undefined, error });
optionsRef.current.onError?.(error, variables);
throw error;
}
}, [activeWorkspaceId]);
const reset = useCallback(() => {
setState({ status: 'idle', data: undefined, error: null });
}, []);
return {
...state,
isPending: state.status === 'loading',
mutate,
reset,
};
}