Files
thrilltrack-explorer/src/hooks/reports/useReportMutation.ts
2025-10-31 12:03:22 +00:00

49 lines
1.5 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query';
import { supabase } from '@/integrations/supabase/client';
import { useAuth } from '@/hooks/useAuth';
import { toast } from 'sonner';
import { getErrorMessage } from '@/lib/errorHandler';
import { useQueryInvalidation } from '@/lib/queryInvalidation';
interface ReportParams {
entityType: 'review' | 'profile' | 'content_submission';
entityId: string;
reportType: string;
reason?: string;
}
export function useReportMutation() {
const { user } = useAuth();
const queryClient = useQueryClient();
const { invalidateModerationQueue, invalidateModerationStats } = useQueryInvalidation();
return useMutation({
mutationFn: async ({ entityType, entityId, reportType, reason }: ReportParams) => {
if (!user) throw new Error('Authentication required');
const { error } = await supabase.from('reports').insert({
reporter_id: user.id,
reported_entity_type: entityType,
reported_entity_id: entityId,
report_type: reportType,
reason: reason?.trim() || null,
});
if (error) throw error;
},
onSuccess: () => {
invalidateModerationQueue();
invalidateModerationStats();
toast.success("Report Submitted", {
description: "Thank you for your report. We'll review it shortly.",
});
},
onError: (error: unknown) => {
toast.error("Error", {
description: getErrorMessage(error),
});
},
});
}