import React from 'react'; import { useLanguage } from '@/contexts/LanguageContext'; import { AlertCircle, CheckCircle, Gauge, Search, Wrench } from 'lucide-react'; import { useToast } from '@/hooks/use-toast'; import { updateIncidentStatus } from '@/services/incident/incidentOperations'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { IncidentStatusBadge } from './IncidentStatusBadge'; interface IncidentStatusDropdownProps { status: string; id: string; onStatusUpdated: () => void; disabled?: boolean; } export const IncidentStatusDropdown = ({ status, id, onStatusUpdated, disabled = false }: IncidentStatusDropdownProps) => { const { t } = useLanguage(); const { toast } = useToast(); const [localStatus, setLocalStatus] = React.useState(status); // Update local status when prop changes React.useEffect(() => { setLocalStatus(status); }, [status]); const statusOptions = [ { value: 'investigating', label: t('investigating'), icon: }, { value: 'identified', label: t('identified'), icon: }, { value: 'found_root_cause', label: t('foundRootCause'), icon: }, { value: 'in_progress', label: t('inProgress'), icon: }, { value: 'monitoring', label: t('monitoring'), icon: }, { value: 'resolved', label: t('resolved'), icon: }, ]; const handleStatusChange = async (newStatus: string) => { try { // Don't update if the status is the same if (localStatus === newStatus) { return; } console.log(`Changing incident status from ${localStatus} to ${newStatus}`); // Optimistically update the UI immediately setLocalStatus(newStatus); // Make the API call in the background await updateIncidentStatus(id, newStatus); toast({ title: t('statusUpdated'), description: t('incidentStatusUpdated'), }); // Notify parent components about the status change onStatusUpdated(); console.log('Status update complete, UI refresh triggered'); } catch (error) { console.error('Error updating incident status:', error); // Revert to the original status on error setLocalStatus(status); toast({ title: t('error'), description: t('failedToUpdateStatus'), variant: 'destructive', }); } }; return ( {statusOptions.map((option) => ( { e.stopPropagation(); // Prevent event from bubbling to table row click handleStatusChange(option.value); }} > {option.icon} {option.label} ))} ); };