import React, { useState } from 'react'; import { View, TouchableOpacity, Alert } from 'react-native'; import { MyText, tw, BottomDialog, MyTextInput } from 'common-ui'; import { trpc } from '@/src/trpc-client'; import MaterialIcons from '@expo/vector-icons/MaterialIcons'; import dayjs from 'dayjs'; function UserIncidentDialog({ userId, orderId, open, onClose, onSuccess }: { userId: number; orderId: number | null; open: boolean; onClose: () => void; onSuccess?: () => void }) { const [adminComment, setAdminComment] = useState(''); const [negativityScore, setNegativityScore] = useState(''); const addIncidentMutation = trpc.admin.user.addUserIncident.useMutation({ onSuccess: () => { Alert.alert('Success', 'Incident added successfully'); setAdminComment(''); setNegativityScore(''); onClose(); onSuccess?.(); }, onError: (error: any) => { Alert.alert('Error', error.message || 'Failed to add incident'); }, }); const handleAddIncident = () => { const score = negativityScore ? parseInt(negativityScore) : undefined; if (!adminComment.trim() && !negativityScore) { Alert.alert('Error', 'Please enter a comment or negativity score'); return; } addIncidentMutation.mutate({ userId, orderId: orderId || undefined, adminComment: adminComment || undefined, negativityScore: score, }); }; return ( Add User Incident Record an incident for this user. This will be visible in their profile. Higher negativity scores indicate more serious incidents (e.g., repeated cancellations, abusive behavior). Cancel {addIncidentMutation.isPending ? 'Adding...' : 'Add Incident'} ); } export function UserIncidentsView({ userId, orderId }: { userId: number; orderId: number | null }) { const [incidentDialogOpen, setIncidentDialogOpen] = useState(false); const { data: incidentsData, refetch: refetchIncidents } = trpc.admin.user.getUserIncidents.useQuery( { userId }, { enabled: !!userId } ); return ( <> User Incidents setIncidentDialogOpen(true)} style={tw`flex-row items-center bg-amber-200 px-3 py-1.5 rounded-lg`} > Add Incident {incidentsData?.incidents && incidentsData.incidents.length > 0 ? ( {incidentsData.incidents.map((incident: any, index: number) => ( {dayjs(incident.dateAdded).format('MMM DD, YYYY • h:mm A')} {incident.negativityScore && ( Score: {incident.negativityScore} )} {incident.adminComment && ( {incident.adminComment} )} Added by {incident.addedBy} {incident.orderId && ( <> Order #{incident.orderId} )} ))} ) : ( No incidents recorded for this user )} setIncidentDialogOpen(false)} onSuccess={refetchIncidents} /> ); }