fix(sidebar): skip the auto-update next-run poll for non-admins (#1388)

The sidebar next-run indicator polled GET /scheduled-tasks on mount, on a
60s interval, and on every state-invalidate event. That route is admin-only,
so non-admin users hit a 403 on every poll, leaving a steady stream of
resource errors in the browser console with no functional purpose.

Gate the poll on the admin role read from useAuth(): when the user is not an
admin, skip the fetch, interval, and listener entirely and report no
scheduled run. The effect re-evaluates on role change, so polling starts or
stops cleanly on login, logout, or a role update.
This commit is contained in:
Anso
2026-06-17 13:26:11 -04:00
committed by GitHub
parent c0f84cb04b
commit 2960f9f853
2 changed files with 73 additions and 1 deletions
@@ -1,5 +1,6 @@
import { useEffect, useRef, useState } from 'react';
import { apiFetch } from '@/lib/api';
import { useAuth } from '@/context/AuthContext';
import type { ScheduledTask } from '@/types/scheduling';
const POLL_INTERVAL_MS = 60_000;
@@ -19,10 +20,18 @@ async function fetchNextRun(signal: AbortSignal): Promise<number | null> {
}
export function useNextAutoUpdateRun(): number | null {
const { isAdmin } = useAuth();
const [nextRunAt, setNextRunAt] = useState<number | null>(null);
const abortRef = useRef<AbortController | null>(null);
useEffect(() => {
// The list endpoint is admin-only; non-admins would 403 on every poll.
// Skip all fetching/polling/listeners for them and report no scheduled run.
if (!isAdmin) {
setNextRunAt(null); // eslint-disable-line react-hooks/set-state-in-effect
return;
}
let active = true;
let invalidateTimer: ReturnType<typeof setTimeout> | null = null;
@@ -57,7 +66,7 @@ export function useNextAutoUpdateRun(): number | null {
clearInterval(interval);
abortRef.current?.abort();
};
}, []);
}, [isAdmin]);
return nextRunAt;
}