mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 08:11:13 -05:00
Refactor: Use consolidated escalateSubmission action
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { AlertTriangle } from 'lucide-react';
|
import { AlertTriangle, AlertCircle } from 'lucide-react';
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
DialogContent,
|
DialogContent,
|
||||||
@@ -18,12 +18,14 @@ import {
|
|||||||
SelectTrigger,
|
SelectTrigger,
|
||||||
SelectValue,
|
SelectValue,
|
||||||
} from '@/components/ui/select';
|
} from '@/components/ui/select';
|
||||||
|
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
|
||||||
|
|
||||||
interface EscalationDialogProps {
|
interface EscalationDialogProps {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
onOpenChange: (open: boolean) => void;
|
onOpenChange: (open: boolean) => void;
|
||||||
onEscalate: (reason: string) => Promise<void>;
|
onEscalate: (reason: string) => Promise<void>;
|
||||||
submissionType: string;
|
submissionType: string;
|
||||||
|
error?: { message: string; errorId?: string } | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const escalationReasons = [
|
const escalationReasons = [
|
||||||
@@ -40,6 +42,7 @@ export function EscalationDialog({
|
|||||||
onOpenChange,
|
onOpenChange,
|
||||||
onEscalate,
|
onEscalate,
|
||||||
submissionType,
|
submissionType,
|
||||||
|
error,
|
||||||
}: EscalationDialogProps) {
|
}: EscalationDialogProps) {
|
||||||
const [selectedReason, setSelectedReason] = useState('');
|
const [selectedReason, setSelectedReason] = useState('');
|
||||||
const [additionalNotes, setAdditionalNotes] = useState('');
|
const [additionalNotes, setAdditionalNotes] = useState('');
|
||||||
@@ -76,6 +79,23 @@ export function EscalationDialog({
|
|||||||
</DialogDescription>
|
</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<Alert variant="destructive" className="mt-4">
|
||||||
|
<AlertCircle className="h-4 w-4" />
|
||||||
|
<AlertTitle>Escalation Failed</AlertTitle>
|
||||||
|
<AlertDescription>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<p className="text-sm">{error.message}</p>
|
||||||
|
{error.errorId && (
|
||||||
|
<p className="text-xs font-mono bg-destructive/10 px-2 py-1 rounded">
|
||||||
|
Reference: {error.errorId.slice(0, 8)}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="space-y-4 py-4">
|
<div className="space-y-4 py-4">
|
||||||
<div className="space-y-2">
|
<div className="space-y-2">
|
||||||
<Label>Escalation Reason</Label>
|
<Label>Escalation Reason</Label>
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ import {
|
|||||||
detectDependencyConflicts,
|
detectDependencyConflicts,
|
||||||
approveSubmissionItems,
|
approveSubmissionItems,
|
||||||
rejectSubmissionItems,
|
rejectSubmissionItems,
|
||||||
escalateSubmission,
|
|
||||||
checkSubmissionConflict,
|
checkSubmissionConflict,
|
||||||
type SubmissionItemWithDeps,
|
type SubmissionItemWithDeps,
|
||||||
type DependencyConflict,
|
type DependencyConflict,
|
||||||
type ConflictCheckResult
|
type ConflictCheckResult
|
||||||
} from '@/lib/submissionItemsService';
|
} from '@/lib/submissionItemsService';
|
||||||
|
import { useModerationActions } from '@/hooks/moderation/useModerationActions';
|
||||||
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetDescription } from '@/components/ui/sheet';
|
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetDescription } from '@/components/ui/sheet';
|
||||||
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
@@ -77,6 +77,10 @@ export function SubmissionReviewManager({
|
|||||||
const [conflictData, setConflictData] = useState<ConflictCheckResult | null>(null);
|
const [conflictData, setConflictData] = useState<ConflictCheckResult | null>(null);
|
||||||
const [showConflictResolutionModal, setShowConflictResolutionModal] = useState(false);
|
const [showConflictResolutionModal, setShowConflictResolutionModal] = useState(false);
|
||||||
const [lastModifiedTimestamp, setLastModifiedTimestamp] = useState<string | null>(null);
|
const [lastModifiedTimestamp, setLastModifiedTimestamp] = useState<string | null>(null);
|
||||||
|
const [escalationError, setEscalationError] = useState<{
|
||||||
|
message: string;
|
||||||
|
errorId?: string;
|
||||||
|
} | null>(null);
|
||||||
|
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
const { isAdmin, isSuperuser } = useUserRole();
|
const { isAdmin, isSuperuser } = useUserRole();
|
||||||
@@ -87,6 +91,17 @@ export function SubmissionReviewManager({
|
|||||||
// Lock monitoring integration
|
// Lock monitoring integration
|
||||||
const { extendLock } = useLockMonitor(state, dispatch, submissionId);
|
const { extendLock } = useLockMonitor(state, dispatch, submissionId);
|
||||||
|
|
||||||
|
// Moderation actions
|
||||||
|
const { escalateSubmission } = useModerationActions({
|
||||||
|
user,
|
||||||
|
onActionStart: (itemId: string) => {
|
||||||
|
logger.log(`Starting escalation for ${itemId}`);
|
||||||
|
},
|
||||||
|
onActionComplete: () => {
|
||||||
|
logger.log('Escalation complete');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Auto-claim on mount
|
// Auto-claim on mount
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (open && submissionId && state.status === 'idle') {
|
if (open && submissionId && state.status === 'idle') {
|
||||||
@@ -425,50 +440,35 @@ export function SubmissionReviewManager({
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { supabase } = await import('@/integrations/supabase/client');
|
setEscalationError(null);
|
||||||
|
|
||||||
// Call the escalation notification edge function
|
|
||||||
const { data, error, requestId } = await invokeWithTracking(
|
|
||||||
'send-escalation-notification',
|
|
||||||
{
|
|
||||||
submissionId,
|
|
||||||
escalationReason: reason,
|
|
||||||
escalatedBy: user.id
|
|
||||||
},
|
|
||||||
user.id
|
|
||||||
);
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
handleError(error, {
|
|
||||||
action: 'Send escalation notification',
|
|
||||||
userId: user.id,
|
|
||||||
metadata: { submissionId }
|
|
||||||
});
|
|
||||||
// Fallback to direct database update if email fails
|
|
||||||
await escalateSubmission(submissionId, reason, user.id);
|
|
||||||
toast({
|
|
||||||
title: 'Escalated (Email Failed)',
|
|
||||||
description: 'Submission escalated but notification email failed to send',
|
|
||||||
variant: 'default',
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
toast({
|
|
||||||
title: 'Escalated Successfully',
|
|
||||||
description: 'Submission escalated and admin notified via email',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Use consolidated action from useModerationActions
|
||||||
|
// This handles: edge function call, fallback, error logging, cache invalidation
|
||||||
|
await escalateSubmission(
|
||||||
|
{
|
||||||
|
id: submissionId,
|
||||||
|
submission_type: submissionType,
|
||||||
|
type: 'submission'
|
||||||
|
} as any,
|
||||||
|
reason
|
||||||
|
);
|
||||||
|
|
||||||
|
// Success - close dialog
|
||||||
onComplete();
|
onComplete();
|
||||||
onOpenChange(false);
|
onOpenChange(false);
|
||||||
} catch (error: unknown) {
|
} catch (error: any) {
|
||||||
handleError(error, {
|
// Track error for retry UI
|
||||||
action: 'Escalate Submission',
|
setEscalationError({
|
||||||
userId: user?.id,
|
message: getErrorMessage(error),
|
||||||
metadata: {
|
errorId: error.errorId
|
||||||
submissionId,
|
|
||||||
reason: reason.substring(0, 100)
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
logger.error('Escalation failed in SubmissionReviewManager', {
|
||||||
|
submissionId,
|
||||||
|
error: getErrorMessage(error)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Don't close dialog on error - let user retry
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -587,6 +587,7 @@ export function SubmissionReviewManager({
|
|||||||
onOpenChange={setShowEscalationDialog}
|
onOpenChange={setShowEscalationDialog}
|
||||||
onEscalate={handleEscalate}
|
onEscalate={handleEscalate}
|
||||||
submissionType={submissionType}
|
submissionType={submissionType}
|
||||||
|
error={escalationError}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<RejectionDialog
|
<RejectionDialog
|
||||||
|
|||||||
@@ -372,7 +372,10 @@ export const useModerationQueue = (config?: UseModerationQueueConfig) => {
|
|||||||
return Math.max(0, currentLock.expiresAt.getTime() - Date.now());
|
return Math.max(0, currentLock.expiresAt.getTime() - Date.now());
|
||||||
}, [currentLock]);
|
}, [currentLock]);
|
||||||
|
|
||||||
// Escalate submission
|
/**
|
||||||
|
* @deprecated Use escalateSubmission from useModerationActions instead
|
||||||
|
* This method only updates the database and doesn't send email notifications
|
||||||
|
*/
|
||||||
const escalateSubmission = useCallback(async (submissionId: string, reason: string): Promise<boolean> => {
|
const escalateSubmission = useCallback(async (submissionId: string, reason: string): Promise<boolean> => {
|
||||||
if (!user?.id) return false;
|
if (!user?.id) return false;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user