| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- 'use client'
- import { useTableKeyboardNav } from '@/hooks/use-table-keyboard-nav'
- import { interactiveRow } from '@/lib/interactive-row'
- import { useState } from 'react'
- import { useRouter } from 'next/navigation'
- import { useTranslations } from 'next-intl'
- import {
- Plus,
- Copy,
- Send,
- ExternalLink,
- FileVideo,
- Video,
- Trash2,
- MessageCircle,
- } from 'lucide-react'
- import { Card, CardContent } from '@/components/ui/card'
- import { useConfirm } from '@/components/confirm-dialog'
- import { Button } from '@/components/ui/button'
- import { Badge } from '@/components/ui/badge'
- import {
- Table,
- TableBody,
- TableCell,
- TableHead,
- TableHeader,
- TableRow,
- } from '@/components/ui/table'
- import {
- ContextMenu,
- ContextMenuContent,
- ContextMenuItem,
- ContextMenuSeparator,
- ContextMenuTrigger,
- } from '@/components/ui/context-menu'
- import { TableContextMenuHint } from '@/components/table-context-menu-hint'
- import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
- import { toast } from 'sonner'
- import { CreateStatusReportDialog } from './CreateStatusReportDialog'
- import { SendStatusReportDialog } from './SendStatusReportDialog'
- import { deleteStatusReport } from '../Actions/deleteStatusReport'
- interface StatusReportSummary {
- id: string
- title: string | null
- message: string | null
- status: string
- videoUrl: string | null
- createdAt: string
- expiresAt: string | null
- publicToken: string
- customerFeedback: string | null
- feedbackAt: string | null
- sentVia: string | null
- sentAt: string | null
- }
- interface StatusReportListProps {
- /** The work order the reports are about, or... */
- serviceRecordId?: string
- /** ...the inspection. One of the two. */
- inspectionId?: string
- organizationId: string
- vehicleName: string
- customer: {
- id: string
- name: string
- email: string | null
- phone: string | null
- telegramChatId: string | null
- } | null
- smsEnabled: boolean
- emailEnabled: boolean
- telegramEnabled: boolean
- initialReports: StatusReportSummary[]
- }
- const STATUS_VARIANT: Record<string, string> = {
- draft: 'bg-muted text-muted-foreground',
- published: 'bg-blue-500/10 text-blue-600',
- sent: 'bg-green-500/10 text-green-600',
- viewed: 'bg-purple-500/10 text-purple-600',
- }
- export function StatusReportList({
- serviceRecordId,
- inspectionId,
- organizationId,
- vehicleName,
- customer,
- smsEnabled,
- emailEnabled,
- telegramEnabled,
- initialReports,
- }: StatusReportListProps) {
- const t = useTranslations('statusReport.list')
- const router = useRouter()
- const [showCreate, setShowCreate] = useState(false)
- const tableNav = useTableKeyboardNav()
- const [sendReportId, setSendReportId] = useState<string | null>(null)
- const confirm = useConfirm()
- const [detailReport, setDetailReport] = useState<StatusReportSummary | null>(null)
- function copyLink(publicToken: string) {
- const url = `${window.location.origin}/share/status-report/${organizationId}/${publicToken}`
- navigator.clipboard.writeText(url)
- toast.success(t('linkCopied'))
- }
- function openReport(publicToken: string) {
- window.open(`/share/status-report/${organizationId}/${publicToken}`, '_blank')
- }
- async function handleDelete(reportId: string) {
- const ok = await confirm({
- title: t('deleteTitle'),
- description: t('deleteConfirm'),
- confirmLabel: t('delete'),
- cancelLabel: t('cancel'),
- icon: Trash2,
- destructive: true,
- })
- if (!ok) return
- try {
- const result = await deleteStatusReport(reportId)
- if (result.success) {
- toast.success(t('deleted'))
- router.refresh()
- } else {
- toast.error(result.error || t('deleteFailed'))
- }
- } catch {
- toast.error(t('deleteFailed'))
- }
- }
- function fmtDate(iso: string) {
- return new Date(iso).toLocaleDateString(undefined, {
- month: 'short',
- day: 'numeric',
- year: 'numeric',
- hour: '2-digit',
- minute: '2-digit',
- })
- }
- return (
- <>
- <div className="space-y-3">
- <div className="flex items-center justify-between">
- <div className="flex items-baseline gap-2">
- <h3 className="text-sm font-medium">{t('title')}</h3>
- <a
- href="https://torqvoice.com/docs/features/status-reports"
- target="_blank"
- rel="noopener noreferrer"
- className="shrink-0 rounded text-[11px] text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
- >
- {t('readMore')} →
- </a>
- </div>
- <Button
- type="button"
- size="sm"
- onClick={() => setShowCreate(true)}
- aria-label={t('newReport')}
- title={t('newReport')}
- className="h-9 w-9 shrink-0 p-0 md:h-8 md:w-auto md:px-3"
- >
- <Plus className="h-4 w-4 md:mr-1.5" />
- <span className="hidden md:inline">{t('newReport')}</span>
- </Button>
- </div>
- {initialReports.length === 0 ? (
- <Card>
- <CardContent className="flex flex-col items-center justify-center py-10 text-center">
- <FileVideo className="mb-3 h-10 w-10 text-muted-foreground/40" />
- <p className="font-medium text-sm">{t('empty')}</p>
- <p className="mt-1 text-xs text-muted-foreground">{t('emptyDescription')}</p>
- </CardContent>
- </Card>
- ) : (
- <>
- {/* Card list (phones + small tablets) */}
- <div className="space-y-2 md:hidden">
- {initialReports.map((report) => (
- <div key={report.id} className="rounded-lg border bg-card p-3">
- <button
- type="button"
- className="w-full text-left"
- onClick={() => setDetailReport(report)}
- >
- <div className="flex min-w-0 items-center gap-1.5">
- {report.videoUrl && (
- <Video className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
- )}
- <span className="truncate text-sm font-medium">
- {report.title || t('title')}
- </span>
- </div>
- <div className="mt-2 flex flex-wrap items-center gap-x-3 gap-y-1.5">
- <Badge
- variant="outline"
- className={`text-[10px] ${STATUS_VARIANT[report.status] || ''}`}
- >
- {t(report.status as 'draft' | 'published' | 'sent' | 'viewed')}
- </Badge>
- {report.customerFeedback && (
- <Badge
- variant="outline"
- className="bg-green-500/10 text-[10px] text-green-600"
- >
- <MessageCircle className="mr-1 h-3 w-3" />
- {t('hasComment')}
- </Badge>
- )}
- <span className="text-xs text-muted-foreground" suppressHydrationWarning>
- {new Date(report.createdAt).toLocaleDateString(undefined, {
- month: 'short',
- day: 'numeric',
- hour: '2-digit',
- minute: '2-digit',
- })}
- </span>
- </div>
- </button>
- <div className="mt-2 flex items-center justify-end gap-1">
- <Button
- type="button"
- variant="ghost"
- size="icon"
- className="h-9 w-9"
- onClick={() => copyLink(report.publicToken)}
- aria-label={t('copyLink')}
- >
- <Copy className="h-4 w-4" />
- </Button>
- {customer && (
- <Button
- type="button"
- variant="ghost"
- size="icon"
- className="h-9 w-9"
- onClick={() => setSendReportId(report.id)}
- aria-label={t('send')}
- >
- <Send className="h-4 w-4" />
- </Button>
- )}
- <Button
- type="button"
- variant="ghost"
- size="icon"
- className="h-9 w-9"
- onClick={() => openReport(report.publicToken)}
- aria-label={t('view')}
- >
- <ExternalLink className="h-4 w-4" />
- </Button>
- <Button
- type="button"
- variant="ghost"
- size="icon"
- className="h-9 w-9 text-destructive hover:text-destructive"
- onClick={() => handleDelete(report.id)}
- aria-label={t('delete')}
- >
- <Trash2 className="h-4 w-4" />
- </Button>
- </div>
- </div>
- ))}
- </div>
- {/* Table (md and up) */}
- <div className="hidden rounded-md border md:block" {...tableNav.containerProps}>
- <TableContextMenuHint />
- <Table>
- <TableHeader>
- <TableRow>
- <TableHead className="w-[35%]">{t('title')}</TableHead>
- <TableHead>{t('statusLabel')}</TableHead>
- <TableHead className="hidden sm:table-cell">{t('created')}</TableHead>
- <TableHead className="hidden md:table-cell">{t('feedback')}</TableHead>
- <TableHead className="text-right" />
- </TableRow>
- </TableHeader>
- <TableBody>
- {initialReports.map((report) => (
- <ContextMenu key={report.id} modal={false}>
- <ContextMenuTrigger asChild>
- <TableRow
- className="cursor-pointer"
- {...interactiveRow(() => setDetailReport(report))}
- >
- <TableCell className="py-2">
- <div className="flex items-center gap-1.5 min-w-0">
- {report.videoUrl && (
- <Video className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
- )}
- <span className="truncate text-sm font-medium">
- {report.title || t('title')}
- </span>
- </div>
- </TableCell>
- <TableCell className="py-2">
- <Badge
- variant="outline"
- className={`text-[10px] ${STATUS_VARIANT[report.status] || ''}`}
- >
- {t(report.status as 'draft' | 'published' | 'sent' | 'viewed')}
- </Badge>
- </TableCell>
- <TableCell
- className="py-2 text-xs text-muted-foreground hidden sm:table-cell"
- suppressHydrationWarning
- >
- {new Date(report.createdAt).toLocaleDateString(undefined, {
- month: 'short',
- day: 'numeric',
- hour: '2-digit',
- minute: '2-digit',
- })}
- </TableCell>
- <TableCell className="py-2 hidden md:table-cell">
- {report.customerFeedback ? (
- <Badge
- variant="outline"
- className="text-[10px] bg-green-500/10 text-green-600"
- >
- <MessageCircle className="h-3 w-3 mr-1" />
- {t('hasComment')}
- </Badge>
- ) : (
- <span className="text-xs text-muted-foreground">-</span>
- )}
- </TableCell>
- <TableCell
- className="py-2 text-right"
- onClick={(e) => e.stopPropagation()}
- >
- <div className="flex items-center justify-end gap-1">
- <Button
- type="button"
- variant="ghost"
- size="sm"
- className="h-7 px-2"
- onClick={() => copyLink(report.publicToken)}
- >
- <Copy className="h-3.5 w-3.5" />
- <span className="hidden xl:inline ml-1.5">{t('copyLink')}</span>
- </Button>
- {customer && (
- <Button
- type="button"
- variant="ghost"
- size="sm"
- className="h-7 px-2"
- onClick={() => setSendReportId(report.id)}
- >
- <Send className="h-3.5 w-3.5" />
- <span className="hidden xl:inline ml-1.5">{t('send')}</span>
- </Button>
- )}
- <Button
- type="button"
- variant="ghost"
- size="sm"
- className="h-7 px-2"
- onClick={() => openReport(report.publicToken)}
- >
- <ExternalLink className="h-3.5 w-3.5" />
- <span className="hidden xl:inline ml-1.5">{t('view')}</span>
- </Button>
- <Button
- type="button"
- variant="ghost"
- size="sm"
- className="h-7 px-2 text-destructive hover:text-destructive"
- onClick={() => handleDelete(report.id)}
- >
- <Trash2 className="h-3.5 w-3.5" />
- <span className="hidden xl:inline ml-1.5">{t('delete')}</span>
- </Button>
- </div>
- </TableCell>
- </TableRow>
- </ContextMenuTrigger>
- <ContextMenuContent className="min-w-52">
- <ContextMenuItem onClick={() => openReport(report.publicToken)}>
- <ExternalLink className="mr-2 h-4 w-4" />
- {t('view')}
- </ContextMenuItem>
- <ContextMenuItem onClick={() => copyLink(report.publicToken)}>
- <Copy className="mr-2 h-4 w-4" />
- {t('copyLink')}
- </ContextMenuItem>
- {customer && (
- <ContextMenuItem onClick={() => setSendReportId(report.id)}>
- <Send className="mr-2 h-4 w-4" />
- {t('send')}
- </ContextMenuItem>
- )}
- <ContextMenuSeparator />
- <ContextMenuItem
- variant="destructive"
- onClick={() => handleDelete(report.id)}
- >
- <Trash2 className="mr-2 h-4 w-4" />
- {t('delete')}
- </ContextMenuItem>
- </ContextMenuContent>
- </ContextMenu>
- ))}
- </TableBody>
- </Table>
- </div>
- </>
- )}
- </div>
- {/* Detail dialog */}
- <Dialog
- open={!!detailReport}
- onOpenChange={(open) => {
- if (!open) setDetailReport(null)
- }}
- >
- <DialogContent className="sm:max-w-lg" aria-describedby={undefined}>
- {detailReport && (
- <>
- <DialogHeader>
- <DialogTitle>{detailReport.title || t('title')}</DialogTitle>
- </DialogHeader>
- <div className="space-y-4">
- {/* Status & dates */}
- <div className="grid grid-cols-2 gap-3 text-sm">
- <div>
- <p className="text-xs text-muted-foreground">{t('statusLabel')}</p>
- <Badge
- variant="outline"
- className={`mt-0.5 text-[10px] ${STATUS_VARIANT[detailReport.status] || ''}`}
- >
- {t(detailReport.status as 'draft' | 'published' | 'sent' | 'viewed')}
- </Badge>
- </div>
- <div>
- <p className="text-xs text-muted-foreground">{t('created')}</p>
- <p className="mt-0.5">{fmtDate(detailReport.createdAt)}</p>
- </div>
- {detailReport.sentAt && (
- <div>
- <p className="text-xs text-muted-foreground">{t('sentAt')}</p>
- <p className="mt-0.5">{fmtDate(detailReport.sentAt)}</p>
- </div>
- )}
- {detailReport.sentVia && (
- <div>
- <p className="text-xs text-muted-foreground">{t('sentVia')}</p>
- <p className="mt-0.5 capitalize">{detailReport.sentVia}</p>
- </div>
- )}
- {detailReport.expiresAt && (
- <div>
- <p className="text-xs text-muted-foreground">{t('expires')}</p>
- <p className="mt-0.5">
- {new Date(detailReport.expiresAt).toLocaleDateString(undefined, {
- month: 'short',
- day: 'numeric',
- year: 'numeric',
- })}
- </p>
- </div>
- )}
- </div>
- {/* Message */}
- {detailReport.message && (
- <div>
- <p className="text-xs text-muted-foreground mb-1">{t('messageLabel')}</p>
- <p className="text-sm whitespace-pre-wrap rounded-md bg-muted/50 p-3">
- {detailReport.message}
- </p>
- </div>
- )}
- {/* Video */}
- {detailReport.videoUrl && (
- <div>
- <p className="text-xs text-muted-foreground mb-1">{t('videoLabel')}</p>
- <video
- src={detailReport.videoUrl}
- controls
- preload="metadata"
- playsInline
- className="w-full rounded-md max-h-64"
- />
- </div>
- )}
- {/* Customer feedback */}
- <div>
- <p className="text-xs text-muted-foreground mb-1">{t('feedback')}</p>
- {detailReport.customerFeedback ? (
- <div className="rounded-md border border-green-200 bg-green-50 dark:border-green-900 dark:bg-green-950/30 p-3">
- <p className="text-sm whitespace-pre-wrap">{detailReport.customerFeedback}</p>
- {detailReport.feedbackAt && (
- <p className="mt-2 text-xs text-muted-foreground">
- {fmtDate(detailReport.feedbackAt)}
- </p>
- )}
- </div>
- ) : (
- <p className="text-sm text-muted-foreground">{t('noFeedback')}</p>
- )}
- </div>
- </div>
- </>
- )}
- </DialogContent>
- </Dialog>
- <CreateStatusReportDialog
- open={showCreate}
- onOpenChange={setShowCreate}
- serviceRecordId={serviceRecordId}
- inspectionId={inspectionId}
- vehicleName={vehicleName}
- customer={customer}
- smsEnabled={smsEnabled}
- emailEnabled={emailEnabled}
- telegramEnabled={telegramEnabled}
- onCreated={(reportId) => {
- if (customer) setSendReportId(reportId)
- else router.refresh()
- }}
- />
- {sendReportId && customer && (
- <SendStatusReportDialog
- open={!!sendReportId}
- onOpenChange={(open) => {
- if (!open) {
- setSendReportId(null)
- router.refresh()
- }
- }}
- reportId={sendReportId}
- customer={customer}
- smsEnabled={smsEnabled}
- emailEnabled={emailEnabled}
- telegramEnabled={telegramEnabled}
- />
- )}
- </>
- )
- }
|