mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 06:51:13 -05:00
Connect to Lovable Cloud
Fix security definer view issue by enabling security_invoker on grouped_alerts_view and implement grouped alerts system with new keys, hooks, components, and monitoring overview integration. Added migration to adjust view, updated query keys, created useGroupedAlerts, useAlertGroupActions, GroupedAlertsPanel, and updated MonitoringOverview to include grouped alerts.
This commit is contained in:
110
src/hooks/admin/useAlertGroupActions.ts
Normal file
110
src/hooks/admin/useAlertGroupActions.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { supabase } from '@/lib/supabaseClient';
|
||||
import { queryKeys } from '@/lib/queryKeys';
|
||||
import { toast } from 'sonner';
|
||||
import type { GroupedAlert } from './useGroupedAlerts';
|
||||
|
||||
export function useResolveAlertGroup() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
alertIds,
|
||||
source
|
||||
}: {
|
||||
alertIds: string[];
|
||||
source: 'system' | 'rate_limit';
|
||||
}) => {
|
||||
const table = source === 'system' ? 'system_alerts' : 'rate_limit_alerts';
|
||||
const { error } = await supabase
|
||||
.from(table)
|
||||
.update({ resolved_at: new Date().toISOString() })
|
||||
.in('id', alertIds);
|
||||
|
||||
if (error) throw error;
|
||||
return { count: alertIds.length };
|
||||
},
|
||||
onMutate: async ({ alertIds }) => {
|
||||
// Cancel any outgoing refetches
|
||||
await queryClient.cancelQueries({
|
||||
queryKey: queryKeys.monitoring.groupedAlerts()
|
||||
});
|
||||
|
||||
const previousData = queryClient.getQueryData(
|
||||
queryKeys.monitoring.groupedAlerts()
|
||||
);
|
||||
|
||||
// Optimistically update to the new value
|
||||
queryClient.setQueryData(
|
||||
queryKeys.monitoring.groupedAlerts(),
|
||||
(old: GroupedAlert[] | undefined) => {
|
||||
if (!old) return old;
|
||||
return old.map(alert => {
|
||||
const hasMatchingIds = alert.alert_ids.some(id =>
|
||||
alertIds.includes(id)
|
||||
);
|
||||
if (hasMatchingIds) {
|
||||
return {
|
||||
...alert,
|
||||
unresolved_count: 0,
|
||||
has_resolved: true,
|
||||
};
|
||||
}
|
||||
return alert;
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
return { previousData };
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
toast.success(`Resolved ${data.count} alert${data.count > 1 ? 's' : ''}`);
|
||||
},
|
||||
onError: (error, variables, context) => {
|
||||
// Rollback on error
|
||||
if (context?.previousData) {
|
||||
queryClient.setQueryData(
|
||||
queryKeys.monitoring.groupedAlerts(),
|
||||
context.previousData
|
||||
);
|
||||
}
|
||||
toast.error('Failed to resolve alerts');
|
||||
console.error('Error resolving alert group:', error);
|
||||
},
|
||||
onSettled: () => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.monitoring.groupedAlerts()
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.monitoring.combinedAlerts()
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useSnoozeAlertGroup() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
groupKey,
|
||||
duration
|
||||
}: {
|
||||
groupKey: string;
|
||||
duration: number;
|
||||
}) => {
|
||||
const snoozedAlerts = JSON.parse(
|
||||
localStorage.getItem('snoozed_alerts') || '{}'
|
||||
);
|
||||
snoozedAlerts[groupKey] = Date.now() + duration;
|
||||
localStorage.setItem('snoozed_alerts', JSON.stringify(snoozedAlerts));
|
||||
return { groupKey, until: snoozedAlerts[groupKey] };
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: queryKeys.monitoring.groupedAlerts()
|
||||
});
|
||||
toast.success('Alert group snoozed');
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user