mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 16:31:12 -05:00
feat: Implement Cronitor health monitor
This commit is contained in:
63
src/contexts/CronitorHealthContext.tsx
Normal file
63
src/contexts/CronitorHealthContext.tsx
Normal file
@@ -0,0 +1,63 @@
|
||||
import { createContext, useContext, ReactNode, useState, useEffect } from 'react';
|
||||
import { useCronitorHealth as useCronitorHealthQuery } from '@/hooks/useCronitorHealth';
|
||||
|
||||
interface CronitorHealthContextType {
|
||||
passing: boolean | null; // null = loading/unknown
|
||||
isLoading: boolean;
|
||||
error: Error | null;
|
||||
lastChecked: Date | null;
|
||||
isBannerDismissed: boolean;
|
||||
dismissBanner: () => void;
|
||||
}
|
||||
|
||||
const CronitorHealthContext = createContext<CronitorHealthContextType | undefined>(undefined);
|
||||
|
||||
const DISMISSAL_DURATION = 15 * 60 * 1000; // 15 minutes
|
||||
const DISMISSAL_KEY = 'cronitor-banner-dismissed';
|
||||
|
||||
export function CronitorHealthProvider({ children }: { children: ReactNode }) {
|
||||
const { data, isLoading, error } = useCronitorHealthQuery();
|
||||
const [dismissedUntil, setDismissedUntil] = useState<number | null>(() => {
|
||||
const stored = localStorage.getItem(DISMISSAL_KEY);
|
||||
return stored ? parseInt(stored) : null;
|
||||
});
|
||||
|
||||
const dismissBanner = () => {
|
||||
const until = Date.now() + DISMISSAL_DURATION;
|
||||
localStorage.setItem(DISMISSAL_KEY, until.toString());
|
||||
setDismissedUntil(until);
|
||||
};
|
||||
|
||||
const isBannerDismissed = dismissedUntil ? Date.now() < dismissedUntil : false;
|
||||
|
||||
// Auto-clear dismissal when API is healthy again
|
||||
useEffect(() => {
|
||||
if (data?.passing === true && dismissedUntil) {
|
||||
localStorage.removeItem(DISMISSAL_KEY);
|
||||
setDismissedUntil(null);
|
||||
}
|
||||
}, [data?.passing, dismissedUntil]);
|
||||
|
||||
return (
|
||||
<CronitorHealthContext.Provider
|
||||
value={{
|
||||
passing: data?.passing ?? null,
|
||||
isLoading,
|
||||
error: error as Error | null,
|
||||
lastChecked: data ? new Date() : null,
|
||||
isBannerDismissed,
|
||||
dismissBanner,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</CronitorHealthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useCronitorHealth() {
|
||||
const context = useContext(CronitorHealthContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useCronitorHealth must be used within CronitorHealthProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user