NTP_V2/web/src/components/BusyIndicators.tsx

46 lines
1.7 KiB
TypeScript

import { useEffect, useState } from 'react';
import { Backdrop, Box, CircularProgress, LinearProgress, Paper, Typography } from '@mui/material';
import { useActivity } from '@/api/activity';
import { useUi } from '@/i18n/useUi';
/** True only after `active` has been continuously true for `delay` ms (so quick calls never flash). */
export function useDelayed(active: boolean, delay: number): boolean {
const [on, setOn] = useState(false);
useEffect(() => {
if (!active) { setOn(false); return; }
const id = window.setTimeout(() => setOn(true), delay);
return () => window.clearTimeout(id);
}, [active, delay]);
return on;
}
/** Slim global progress bar for any fetch, shown after a 250 ms delay. */
export function GlobalProgress() {
const { reads, writes } = useActivity();
const show = useDelayed(reads + writes > 0, 250);
if (!show) return null;
return (
<LinearProgress
aria-hidden
sx={{ position: 'fixed', top: 0, insetInline: 0, height: 3, zIndex: (t) => t.zIndex.tooltip + 20 }}
/>
);
}
/** Blocking "Saving..." card for writes; also prevents double submits. */
export function SavingOverlay() {
const { writes } = useActivity();
const { t } = useUi();
return (
<Backdrop open={writes > 0} sx={{ zIndex: (th) => th.zIndex.modal + 20, color: 'text.primary', backgroundColor: 'rgba(0,0,0,0.35)' }}>
<Paper role="status" aria-live="polite" sx={{ p: 3, display: 'flex', gap: 2, alignItems: 'center', minWidth: 260 }}>
<CircularProgress size={28} />
<Box>
<Typography sx={{ fontWeight: 700 }}>{t('busy.saving')}</Typography>
<Typography variant="body2" color="text.secondary">{t('busy.savingHint')}</Typography>
</Box>
</Paper>
</Backdrop>
);
}