Files
Convivo/apps/admin/src/lib/useSilentRefresh.ts
d3083b7940 fix(points): welcome check-in awards +10; add Sessions view, AI connection test, silent reload
The front-desk check-in set checked_in and printed badges but awarded nothing,
so the +10 depended on which check-in a person happened to get. Every award
path was reviewed:

- welcome_checkin awards +10 (category checkin) on a genuine first check-in and
  returns points + points_awarded; checkin_attendee and preview_welcome_checkin
  return points_awarded too.
- set_registration_checked_in(false) deleted all checkin points while keeping
  the checkins rows, and switching back on awarded nothing -- one mistaken
  toggle destroyed those points permanently. ON now recomputes from the rows
  that still exist and awards only the shortfall: reversible, still unfarmable.
- The staff/Admin/@appe.at exclusion stays (product decision) but is no longer
  silent: three-way points sheets in the scanner, "no points" with a tooltip in
  Registrations, and the Contacts points modal refuses to apply rather than
  letting adjust_points (void) no-op quietly.

Also in this change:
- admin silent refresh (5s poll + 500ms after writes) across all data views;
  Registrations patches in place so rows never slide under the cursor
- Settings "Validate AI connection" via a new sidecar POST /llm-test verdict
  endpoint (never leaks the key, distinguishes "no usage block" from "off")
- new Sessions view between Schedule and Activity: speakers, attendance,
  ratings, attendee filter, and one button that summarises every session with
  feedback (one request each, concurrency 3, UI blocked until all settle)
- provider requests disable thinking through one shared body builder sending
  both reasoning_effort:"none" and chat_template_kwargs.enable_thinking

SQL: 113 / 57 / 24 assertions (set_registration_checked_in section 3 was
rewritten, it asserted the bug). Sidecar 34 unit tests. Full e2e 202 passed,
0 failed. Migration applied locally only -- not deployed.

Co-authored-by: Stephan Charles <[email protected]>
Co-authored-by: Amelia Hernandez <[email protected]>
Co-authored-by: Markus Franz <[email protected]>
Co-authored-by: Sabrina Hota <[email protected]>
Co-authored-by: Nilüfer Doğan <[email protected]>
2026-10-19 20:14:03 +02:00

93 lines
3.5 KiB
TypeScript

import { useCallback, useEffect, useRef } from 'react';
/** Background poll interval while the tab is visible. */
export const SILENT_POLL_MS = 5000;
/** Relaxed poll for views whose refresh pulls many tables at once. */
export const SLOW_POLL_MS = 15000;
/** How long after a write we wait before pulling fresh data (batches bursts). */
export const WRITE_SETTLE_MS = 500;
/**
* Silent background refresh for admin data.
*
* Two triggers, both deliberately invisible:
* 1. a poll every `SILENT_POLL_MS` while the tab is visible (paused when
* hidden, and refreshed the moment it becomes visible again), so the table
* shows staff scans performed on another device;
* 2. `refreshSoon()`, a debounced pull `WRITE_SETTLE_MS` after a write, which
* lets a burst of edits settle on the server before we read it back.
*
* "Silent" is the contract, and it has teeth:
* - the interval never fires on mount — the view's own `load()` owns the first
* fetch and the loading state;
* - errors are swallowed. A background tick must not write the view's `error`
* state, because these views render `if (error) return <Empty/>` and a
* single failed poll would replace a perfectly good table with an error
* screen;
* - overlapping ticks are dropped rather than queued (`inflight`), so a slow
* response can never pile up behind a fast one;
* - the callback is read from a ref, so an inline `() => load(...)` does not
* reschedule the timer on every render.
*
* Callers must supply a refresh that patches data in place: no `setLoaded(false)`,
* no re-sort that moves rows under the cursor. See Registrations, which keeps the
* existing row order and skips rows with a write in flight.
*
* `intervalMs` relaxes the poll for views whose refresh is expensive (Reports
* pulls nine tables at once). The 500 ms-after-write path is unaffected.
*/
export function useSilentRefresh(
refresh: () => Promise<unknown> | void,
options: { enabled?: boolean; refresher?: () => Promise<unknown> | void; intervalMs?: number } = {},
): { refreshSoon: () => void } {
const { enabled = true, intervalMs = SILENT_POLL_MS } = options;
const tick = options.refresher ?? refresh;
const saved = useRef(tick);
saved.current = tick;
const inflight = useRef(false);
const timer = useRef<number | null>(null);
const run = useCallback(async () => {
if (inflight.current) return;
inflight.current = true;
try {
await saved.current();
} catch {
// Silent by design — see the doc comment.
} finally {
inflight.current = false;
}
}, []);
useEffect(() => {
if (!enabled) return;
const visible = () => typeof document === 'undefined' || document.visibilityState === 'visible';
const id = window.setInterval(() => {
if (visible()) void run();
}, intervalMs);
const onVisible = () => {
if (visible()) void run();
};
document.addEventListener('visibilitychange', onVisible);
return () => {
window.clearInterval(id);
document.removeEventListener('visibilitychange', onVisible);
};
}, [enabled, intervalMs, run]);
useEffect(() => () => {
if (timer.current !== null) window.clearTimeout(timer.current);
}, []);
/** Schedule one debounced pull; repeated calls within the window collapse. */
const refreshSoon = useCallback(() => {
if (timer.current !== null) window.clearTimeout(timer.current);
timer.current = window.setTimeout(() => {
timer.current = null;
void run();
}, WRITE_SETTLE_MS);
}, [run]);
return { refreshSoon };
}