Skip to content

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.

refactor(ui): move the last fetching pollers onto usePolling

Juan Denis89c2e13233da
diff --git a/frontend/scripts/check-frontend-boundaries.mjs b/frontend/scripts/check-frontend-boundaries.mjs
index 415d749e..e67ead1c 100644
--- a/frontend/scripts/check-frontend-boundaries.mjs
+++ b/frontend/scripts/check-frontend-boundaries.mjs
@@ -49,11 +49,9 @@ const LEGACY_POLLERS = new Map(Object.entries({
'hooks/useContainerStatus.js': 1,
'hooks/useDeployJobStream.js': 1,
'hooks/useMetrics.js': 1,
- 'pages/Dashboard.jsx': 4,
+ 'pages/Dashboard.jsx': 2,
'pages/DeployConsole.jsx': 1,
- 'pages/ImportWizard.jsx': 1,
'pages/Monitors.jsx': 1,
- 'pages/TestSandbox.jsx': 2,
'plugins/serverkit-gui/components/ServerGui.jsx': 1,
}));
diff --git a/frontend/src/pages/Dashboard.jsx b/frontend/src/pages/Dashboard.jsx
index a4ea3502..97effe9a 100644
--- a/frontend/src/pages/Dashboard.jsx
+++ b/frontend/src/pages/Dashboard.jsx
@@ -23,6 +23,7 @@ import {
} from '../components/dashboard/grid/layout';
import { useWidgetTypes, getWidgetType } from '../components/dashboard/widgets/registry';
import { RANGES } from '../components/dashboard/widgets/metrics';
+import { usePolling } from '@/hooks/usePolling';
// Auto-refresh choices, in seconds. 0 means "don't".
const REFRESH_OPTIONS = [
@@ -129,12 +130,15 @@ const Dashboard = () => {
if (!isRemote) {
setRemoteMetrics(null);
setRemoteSystemInfo(null);
- return undefined;
+ return;
}
fetchRemote();
- const id = refreshInterval > 0 ? setInterval(fetchRemote, refreshInterval * 1000) : null;
- return () => { if (id) clearInterval(id); };
- }, [fetchRemote, isRemote, refreshInterval]);
+ }, [fetchRemote, isRemote]);
+
+ usePolling(fetchRemote, refreshInterval * 1000, {
+ enabled: isRemote && refreshInterval > 0,
+ immediate: false,
+ });
// Board-wide refresh pulse: every widget watches ctx.tick.
useEffect(() => {
@@ -144,11 +148,10 @@ const Dashboard = () => {
}, [refreshInterval]);
// HTTP polling fallback for live metrics when the socket is down.
- useEffect(() => {
- if (!refreshInterval || connected || isRemote) return undefined;
- const id = setInterval(() => refreshMetrics(), refreshInterval * 1000);
- return () => clearInterval(id);
- }, [refreshInterval, connected, refreshMetrics, isRemote]);
+ usePolling(refreshMetrics, refreshInterval * 1000, {
+ enabled: Boolean(refreshInterval) && !connected && !isRemote,
+ immediate: false,
+ });
useEffect(() => {
const uptime = metrics?.system?.uptime_seconds;
diff --git a/frontend/src/pages/ImportWizard.jsx b/frontend/src/pages/ImportWizard.jsx
index 4b830e19..639e22dd 100644
--- a/frontend/src/pages/ImportWizard.jsx
+++ b/frontend/src/pages/ImportWizard.jsx
@@ -15,6 +15,7 @@ import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import Spinner from '../components/Spinner';
+import { usePolling } from '@/hooks/usePolling';
const POLL_MS = 2000;
@@ -218,20 +219,20 @@ function ImportWizard() {
// Poll the active import every 2s while the backend is working on it.
const status = imp?.status;
- useEffect(() => {
- if (!imp?.id || (status !== 'analyzing' && status !== 'running' && status !== 'created')) return undefined;
- // 'created' is only polled right after we fire analyze, so a slow
- // status flip to 'analyzing' doesn't strand the wizard.
- const timer = setInterval(async () => {
- try {
- const data = await api.getImport(imp.id);
- if (data.import) setImp(data.import);
- } catch {
- // transient poll failure — keep polling
- }
- }, POLL_MS);
- return () => clearInterval(timer);
- }, [imp?.id, status]);
+ // 'created' is only polled right after we fire analyze, so a slow status
+ // flip to 'analyzing' doesn't strand the wizard.
+ usePolling(async () => {
+ try {
+ const data = await api.getImport(imp.id);
+ if (data.import) setImp(data.import);
+ } catch {
+ // transient poll failure — keep polling
+ }
+ }, POLL_MS, {
+ enabled: Boolean(imp?.id)
+ && (status === 'analyzing' || status === 'running' || status === 'created'),
+ immediate: false,
+ });
// Autoscroll the live log pane as new lines arrive.
const logText = imp?.log_text;
diff --git a/frontend/src/pages/TestSandbox.jsx b/frontend/src/pages/TestSandbox.jsx
index 94b81837..6e53a49c 100644
--- a/frontend/src/pages/TestSandbox.jsx
+++ b/frontend/src/pages/TestSandbox.jsx
@@ -7,6 +7,7 @@ import PageLayout from '../layouts/PageLayout';
import { Button } from '@/components/ui/button';
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card';
import { timeAgo, formatDuration } from '../utils/time';
+import { usePolling } from '@/hooks/usePolling';
import {
FlaskConical, Play, Square, ChevronDown, ChevronRight,
Zap, Package, AlertTriangle, RefreshCw,
@@ -189,32 +190,27 @@ const TestSandbox = () => {
}, []);
// Poll the active run until it leaves the 'running' state.
- useEffect(() => {
- if (!activeRun || activeRun.status !== 'running') return undefined;
- let stopped = false;
- const tick = async () => {
- try {
- const res = await api.getTestSandboxRun(activeRun.id);
- if (stopped) return;
- setActiveRun(res.run);
- if (res.run.status !== 'running') {
- const { passed, failed, total } = summarizeRun(res.run);
- if (res.run.status === 'done') {
- toast[failed > 0 ? 'error' : 'success'](
- `Run finished: ${passed}/${total} passed${failed ? ` (${failed} failed)` : ''}`
- );
- } else if (res.run.status === 'error') {
- toast.error(res.run.error || 'Run failed');
- }
- loadRuns();
- }
- } catch (err) {
- if (!stopped) toast.error(err.message);
+ usePolling(async () => {
+ try {
+ const res = await api.getTestSandboxRun(activeRun.id);
+ setActiveRun(res.run);
+ if (res.run.status === 'running') return;
+ const { passed, failed, total } = summarizeRun(res.run);
+ if (res.run.status === 'done') {
+ toast[failed > 0 ? 'error' : 'success'](
+ `Run finished: ${passed}/${total} passed${failed ? ` (${failed} failed)` : ''}`
+ );
+ } else if (res.run.status === 'error') {
+ toast.error(res.run.error || 'Run failed');
}
- };
- const id = setInterval(tick, POLL_MS);
- return () => { stopped = true; clearInterval(id); };
- }, [activeRun?.id, activeRun?.status]); // eslint-disable-line react-hooks/exhaustive-deps
+ loadRuns();
+ } catch (err) {
+ toast.error(err.message);
+ }
+ }, POLL_MS, {
+ enabled: activeRun?.status === 'running',
+ immediate: false,
+ });
const fetchLog = useCallback(async (runId, distroKey, { silent = false } = {}) => {
const key = `${runId}:${distroKey}`;
@@ -234,22 +230,25 @@ const TestSandbox = () => {
}, []);
// Auto-refresh open logs for distros still executing in the active run.
- useEffect(() => {
- if (!activeRun || activeRun.status !== 'running') return undefined;
- const id = setInterval(() => {
- for (const key of openLogsRef.current) {
- const sep = key.indexOf(':');
- const runId = key.slice(0, sep);
- const distroKey = key.slice(sep + 1);
- if (runId !== String(activeRun.id)) continue;
- const status = activeRun.results?.[distroKey]?.status;
- if (status === 'running' || status === 'queued') {
- fetchLog(runId, distroKey, { silent: true });
- }
+ usePolling(() => {
+ const pending = [];
+ for (const key of openLogsRef.current) {
+ const sep = key.indexOf(':');
+ const runId = key.slice(0, sep);
+ const distroKey = key.slice(sep + 1);
+ if (runId !== String(activeRun.id)) continue;
+ const status = activeRun.results?.[distroKey]?.status;
+ if (status === 'running' || status === 'queued') {
+ pending.push(fetchLog(runId, distroKey, { silent: true }));
}
- }, POLL_MS);
- return () => clearInterval(id);
- }, [activeRun, fetchLog]);
+ }
+ // One promise for the whole fan-out, so the in-flight guard waits for
+ // every open log rather than reporting done after the first.
+ return Promise.all(pending);
+ }, POLL_MS, {
+ enabled: activeRun?.status === 'running',
+ immediate: false,
+ });
const toggleLog = useCallback((runId, distroKey) => {
const key = `${runId}:${distroKey}`;