freshyo/apps/admin-web/src/components/UserIncidentsView.tsx
2026-09-06 23:05:20 +05:30

207 lines
7.2 KiB
TypeScript

import React, { useState } from 'react'
import { p as P, BottomDialog, pInput as PInput } from 'web-components'
import { trpc } from '@/lib/trpc-client'
import { TriangleAlert, Info, Plus, Calendar, User, ShoppingCart, CircleCheck } from 'lucide-react'
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: () => {
window.alert('Success: Incident added successfully')
setAdminComment('')
setNegativityScore('')
onClose()
onSuccess?.()
},
onError: (error: any) => {
window.alert(`Error: ${error.message || 'Failed to add incident'}`)
},
})
const handleAddIncident = () => {
const score = negativityScore ? parseInt(negativityScore) : undefined
if (!adminComment.trim() && !negativityScore) {
window.alert('Error: Please enter a comment or negativity score')
return
}
addIncidentMutation.mutate({
userId,
orderId: orderId || undefined,
adminComment: adminComment || undefined,
negativityScore: score,
})
}
return (
<BottomDialog open={open} onClose={onClose}>
<div className="p-6">
<div className="items-center mb-6 flex flex-col">
<div className="w-12 h-12 bg-amber-100 rounded-full items-center justify-center mb-3 flex">
<TriangleAlert className="h-6 w-6 text-amber-600" />
</div>
<P className="text-xl font-bold text-gray-900 text-center">
Add User Incident
</P>
<P className="text-gray-500 text-center mt-2 text-sm leading-5">
Record an incident for this user. This will be visible in their profile.
</P>
</div>
<PInput
topLabel="Admin Comment"
value={adminComment}
onChange={(e) => setAdminComment(e.target.value)}
placeholder="Enter details about the incident..."
multiline
className="h-24"
/>
<PInput
topLabel="Negativity Score (Optional)"
value={negativityScore}
onChange={(e) => setNegativityScore(e.target.value)}
placeholder="0"
type="number"
inputMode="numeric"
className="mt-4"
/>
<div className="bg-amber-50 p-4 rounded-xl border border-amber-100 mb-6 mt-4 flex flex-row items-start">
<Info className="h-5 w-5 text-amber-600 mt-0.5 shrink-0" />
<P className="text-sm text-amber-800 ml-2 flex-1 leading-5">
Higher negativity scores indicate more serious incidents (e.g., repeated cancellations, abusive behavior).
</P>
</div>
<div className="flex flex-row gap-3">
<button
type="button"
onClick={onClose}
className="flex-1 bg-gray-100 py-3.5 rounded-xl items-center text-gray-700 font-bold"
>
Cancel
</button>
<button
type="button"
onClick={handleAddIncident}
disabled={addIncidentMutation.isPending}
className={`flex-1 bg-amber-500 py-3.5 rounded-xl items-center shadow-sm text-white font-bold disabled:opacity-50 ${addIncidentMutation.isPending ? 'opacity-50' : ''}`}
>
{addIncidentMutation.isPending ? 'Adding...' : 'Add Incident'}
</button>
</div>
</div>
</BottomDialog>
)
}
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 (
<>
<div className="bg-amber-50 p-5 rounded-2xl shadow-sm mb-4 border border-amber-100">
<div className="flex flex-row items-center justify-between mb-4">
<P className="text-base font-bold text-amber-900">
User Incidents
</P>
<button
type="button"
onClick={() => setIncidentDialogOpen(true)}
className="flex flex-row items-center bg-amber-200 px-3 py-1.5 rounded-lg"
>
<Plus className="h-4 w-4 text-amber-700" />
<P className="text-xs font-bold text-amber-800 ml-1">Add Incident</P>
</button>
</div>
{incidentsData?.incidents && incidentsData.incidents.length > 0 ? (
<div className="space-y-3">
{incidentsData.incidents.map((incident: any, index: number) => (
<div
key={incident.id}
className={`bg-white p-4 rounded-xl border border-amber-200 ${index === incidentsData.incidents.length - 1 ? 'mb-0' : 'mb-3'}`}
>
<div className="flex flex-row justify-between items-start mb-2">
<div className="flex flex-row items-center">
<Calendar className="h-3.5 w-3.5 text-gray-500" />
<P className="text-xs text-gray-600 ml-1">
{dayjs(incident.dateAdded).format('MMM DD, YYYY • h:mm A')}
</P>
</div>
{incident.negativityScore && (
<div className="px-2 py-1 bg-red-100 rounded-md">
<P className="text-xs font-bold text-red-700">
Score: {incident.negativityScore}
</P>
</div>
)}
</div>
{incident.adminComment && (
<div className="mt-2">
<P className="text-sm text-gray-900 leading-5 block">
{incident.adminComment}
</P>
</div>
)}
<div className="flex flex-row items-center mt-2 pt-2 border-t border-gray-100">
<User className="h-3 w-3 text-gray-400" />
<P className="text-xs text-gray-500 ml-1">
Added by {incident.addedBy}
</P>
{incident.orderId && (
<>
<ShoppingCart className="h-3 w-3 text-gray-400 ml-3" />
<P className="text-xs text-gray-500 ml-1">
Order #{incident.orderId}
</P>
</>
)}
</div>
</div>
))}
</div>
) : (
<div className="flex flex-col items-center py-6">
<CircleCheck className="h-8 w-8 text-amber-600" />
<P className="text-sm text-amber-700 mt-2">
No incidents recorded for this user
</P>
</div>
)}
</div>
<UserIncidentDialog
orderId={orderId}
userId={userId}
open={incidentDialogOpen}
onClose={() => setIncidentDialogOpen(false)}
onSuccess={refetchIncidents}
/>
</>
)
}