StatusReportList.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. 'use client'
  2. import { useTableKeyboardNav } from '@/hooks/use-table-keyboard-nav'
  3. import { interactiveRow } from '@/lib/interactive-row'
  4. import { useState } from 'react'
  5. import { useRouter } from 'next/navigation'
  6. import { useTranslations } from 'next-intl'
  7. import {
  8. Plus,
  9. Copy,
  10. Send,
  11. ExternalLink,
  12. FileVideo,
  13. Video,
  14. Trash2,
  15. MessageCircle,
  16. } from 'lucide-react'
  17. import { Card, CardContent } from '@/components/ui/card'
  18. import { useConfirm } from '@/components/confirm-dialog'
  19. import { Button } from '@/components/ui/button'
  20. import { Badge } from '@/components/ui/badge'
  21. import {
  22. Table,
  23. TableBody,
  24. TableCell,
  25. TableHead,
  26. TableHeader,
  27. TableRow,
  28. } from '@/components/ui/table'
  29. import {
  30. ContextMenu,
  31. ContextMenuContent,
  32. ContextMenuItem,
  33. ContextMenuSeparator,
  34. ContextMenuTrigger,
  35. } from '@/components/ui/context-menu'
  36. import { TableContextMenuHint } from '@/components/table-context-menu-hint'
  37. import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
  38. import { toast } from 'sonner'
  39. import { CreateStatusReportDialog } from './CreateStatusReportDialog'
  40. import { SendStatusReportDialog } from './SendStatusReportDialog'
  41. import { deleteStatusReport } from '../Actions/deleteStatusReport'
  42. interface StatusReportSummary {
  43. id: string
  44. title: string | null
  45. message: string | null
  46. status: string
  47. videoUrl: string | null
  48. createdAt: string
  49. expiresAt: string | null
  50. publicToken: string
  51. customerFeedback: string | null
  52. feedbackAt: string | null
  53. sentVia: string | null
  54. sentAt: string | null
  55. }
  56. interface StatusReportListProps {
  57. /** The work order the reports are about, or... */
  58. serviceRecordId?: string
  59. /** ...the inspection. One of the two. */
  60. inspectionId?: string
  61. organizationId: string
  62. vehicleName: string
  63. customer: {
  64. id: string
  65. name: string
  66. email: string | null
  67. phone: string | null
  68. telegramChatId: string | null
  69. } | null
  70. smsEnabled: boolean
  71. emailEnabled: boolean
  72. telegramEnabled: boolean
  73. initialReports: StatusReportSummary[]
  74. }
  75. const STATUS_VARIANT: Record<string, string> = {
  76. draft: 'bg-muted text-muted-foreground',
  77. published: 'bg-blue-500/10 text-blue-600',
  78. sent: 'bg-green-500/10 text-green-600',
  79. viewed: 'bg-purple-500/10 text-purple-600',
  80. }
  81. export function StatusReportList({
  82. serviceRecordId,
  83. inspectionId,
  84. organizationId,
  85. vehicleName,
  86. customer,
  87. smsEnabled,
  88. emailEnabled,
  89. telegramEnabled,
  90. initialReports,
  91. }: StatusReportListProps) {
  92. const t = useTranslations('statusReport.list')
  93. const router = useRouter()
  94. const [showCreate, setShowCreate] = useState(false)
  95. const tableNav = useTableKeyboardNav()
  96. const [sendReportId, setSendReportId] = useState<string | null>(null)
  97. const confirm = useConfirm()
  98. const [detailReport, setDetailReport] = useState<StatusReportSummary | null>(null)
  99. function copyLink(publicToken: string) {
  100. const url = `${window.location.origin}/share/status-report/${organizationId}/${publicToken}`
  101. navigator.clipboard.writeText(url)
  102. toast.success(t('linkCopied'))
  103. }
  104. function openReport(publicToken: string) {
  105. window.open(`/share/status-report/${organizationId}/${publicToken}`, '_blank')
  106. }
  107. async function handleDelete(reportId: string) {
  108. const ok = await confirm({
  109. title: t('deleteTitle'),
  110. description: t('deleteConfirm'),
  111. confirmLabel: t('delete'),
  112. cancelLabel: t('cancel'),
  113. icon: Trash2,
  114. destructive: true,
  115. })
  116. if (!ok) return
  117. try {
  118. const result = await deleteStatusReport(reportId)
  119. if (result.success) {
  120. toast.success(t('deleted'))
  121. router.refresh()
  122. } else {
  123. toast.error(result.error || t('deleteFailed'))
  124. }
  125. } catch {
  126. toast.error(t('deleteFailed'))
  127. }
  128. }
  129. function fmtDate(iso: string) {
  130. return new Date(iso).toLocaleDateString(undefined, {
  131. month: 'short',
  132. day: 'numeric',
  133. year: 'numeric',
  134. hour: '2-digit',
  135. minute: '2-digit',
  136. })
  137. }
  138. return (
  139. <>
  140. <div className="space-y-3">
  141. <div className="flex items-center justify-between">
  142. <div className="flex items-baseline gap-2">
  143. <h3 className="text-sm font-medium">{t('title')}</h3>
  144. <a
  145. href="https://torqvoice.com/docs/features/status-reports"
  146. target="_blank"
  147. rel="noopener noreferrer"
  148. 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"
  149. >
  150. {t('readMore')} →
  151. </a>
  152. </div>
  153. <Button
  154. type="button"
  155. size="sm"
  156. onClick={() => setShowCreate(true)}
  157. aria-label={t('newReport')}
  158. title={t('newReport')}
  159. className="h-9 w-9 shrink-0 p-0 md:h-8 md:w-auto md:px-3"
  160. >
  161. <Plus className="h-4 w-4 md:mr-1.5" />
  162. <span className="hidden md:inline">{t('newReport')}</span>
  163. </Button>
  164. </div>
  165. {initialReports.length === 0 ? (
  166. <Card>
  167. <CardContent className="flex flex-col items-center justify-center py-10 text-center">
  168. <FileVideo className="mb-3 h-10 w-10 text-muted-foreground/40" />
  169. <p className="font-medium text-sm">{t('empty')}</p>
  170. <p className="mt-1 text-xs text-muted-foreground">{t('emptyDescription')}</p>
  171. </CardContent>
  172. </Card>
  173. ) : (
  174. <>
  175. {/* Card list (phones + small tablets) */}
  176. <div className="space-y-2 md:hidden">
  177. {initialReports.map((report) => (
  178. <div key={report.id} className="rounded-lg border bg-card p-3">
  179. <button
  180. type="button"
  181. className="w-full text-left"
  182. onClick={() => setDetailReport(report)}
  183. >
  184. <div className="flex min-w-0 items-center gap-1.5">
  185. {report.videoUrl && (
  186. <Video className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
  187. )}
  188. <span className="truncate text-sm font-medium">
  189. {report.title || t('title')}
  190. </span>
  191. </div>
  192. <div className="mt-2 flex flex-wrap items-center gap-x-3 gap-y-1.5">
  193. <Badge
  194. variant="outline"
  195. className={`text-[10px] ${STATUS_VARIANT[report.status] || ''}`}
  196. >
  197. {t(report.status as 'draft' | 'published' | 'sent' | 'viewed')}
  198. </Badge>
  199. {report.customerFeedback && (
  200. <Badge
  201. variant="outline"
  202. className="bg-green-500/10 text-[10px] text-green-600"
  203. >
  204. <MessageCircle className="mr-1 h-3 w-3" />
  205. {t('hasComment')}
  206. </Badge>
  207. )}
  208. <span className="text-xs text-muted-foreground" suppressHydrationWarning>
  209. {new Date(report.createdAt).toLocaleDateString(undefined, {
  210. month: 'short',
  211. day: 'numeric',
  212. hour: '2-digit',
  213. minute: '2-digit',
  214. })}
  215. </span>
  216. </div>
  217. </button>
  218. <div className="mt-2 flex items-center justify-end gap-1">
  219. <Button
  220. type="button"
  221. variant="ghost"
  222. size="icon"
  223. className="h-9 w-9"
  224. onClick={() => copyLink(report.publicToken)}
  225. aria-label={t('copyLink')}
  226. >
  227. <Copy className="h-4 w-4" />
  228. </Button>
  229. {customer && (
  230. <Button
  231. type="button"
  232. variant="ghost"
  233. size="icon"
  234. className="h-9 w-9"
  235. onClick={() => setSendReportId(report.id)}
  236. aria-label={t('send')}
  237. >
  238. <Send className="h-4 w-4" />
  239. </Button>
  240. )}
  241. <Button
  242. type="button"
  243. variant="ghost"
  244. size="icon"
  245. className="h-9 w-9"
  246. onClick={() => openReport(report.publicToken)}
  247. aria-label={t('view')}
  248. >
  249. <ExternalLink className="h-4 w-4" />
  250. </Button>
  251. <Button
  252. type="button"
  253. variant="ghost"
  254. size="icon"
  255. className="h-9 w-9 text-destructive hover:text-destructive"
  256. onClick={() => handleDelete(report.id)}
  257. aria-label={t('delete')}
  258. >
  259. <Trash2 className="h-4 w-4" />
  260. </Button>
  261. </div>
  262. </div>
  263. ))}
  264. </div>
  265. {/* Table (md and up) */}
  266. <div className="hidden rounded-md border md:block" {...tableNav.containerProps}>
  267. <TableContextMenuHint />
  268. <Table>
  269. <TableHeader>
  270. <TableRow>
  271. <TableHead className="w-[35%]">{t('title')}</TableHead>
  272. <TableHead>{t('statusLabel')}</TableHead>
  273. <TableHead className="hidden sm:table-cell">{t('created')}</TableHead>
  274. <TableHead className="hidden md:table-cell">{t('feedback')}</TableHead>
  275. <TableHead className="text-right" />
  276. </TableRow>
  277. </TableHeader>
  278. <TableBody>
  279. {initialReports.map((report) => (
  280. <ContextMenu key={report.id} modal={false}>
  281. <ContextMenuTrigger asChild>
  282. <TableRow
  283. className="cursor-pointer"
  284. {...interactiveRow(() => setDetailReport(report))}
  285. >
  286. <TableCell className="py-2">
  287. <div className="flex items-center gap-1.5 min-w-0">
  288. {report.videoUrl && (
  289. <Video className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
  290. )}
  291. <span className="truncate text-sm font-medium">
  292. {report.title || t('title')}
  293. </span>
  294. </div>
  295. </TableCell>
  296. <TableCell className="py-2">
  297. <Badge
  298. variant="outline"
  299. className={`text-[10px] ${STATUS_VARIANT[report.status] || ''}`}
  300. >
  301. {t(report.status as 'draft' | 'published' | 'sent' | 'viewed')}
  302. </Badge>
  303. </TableCell>
  304. <TableCell
  305. className="py-2 text-xs text-muted-foreground hidden sm:table-cell"
  306. suppressHydrationWarning
  307. >
  308. {new Date(report.createdAt).toLocaleDateString(undefined, {
  309. month: 'short',
  310. day: 'numeric',
  311. hour: '2-digit',
  312. minute: '2-digit',
  313. })}
  314. </TableCell>
  315. <TableCell className="py-2 hidden md:table-cell">
  316. {report.customerFeedback ? (
  317. <Badge
  318. variant="outline"
  319. className="text-[10px] bg-green-500/10 text-green-600"
  320. >
  321. <MessageCircle className="h-3 w-3 mr-1" />
  322. {t('hasComment')}
  323. </Badge>
  324. ) : (
  325. <span className="text-xs text-muted-foreground">-</span>
  326. )}
  327. </TableCell>
  328. <TableCell
  329. className="py-2 text-right"
  330. onClick={(e) => e.stopPropagation()}
  331. >
  332. <div className="flex items-center justify-end gap-1">
  333. <Button
  334. type="button"
  335. variant="ghost"
  336. size="sm"
  337. className="h-7 px-2"
  338. onClick={() => copyLink(report.publicToken)}
  339. >
  340. <Copy className="h-3.5 w-3.5" />
  341. <span className="hidden xl:inline ml-1.5">{t('copyLink')}</span>
  342. </Button>
  343. {customer && (
  344. <Button
  345. type="button"
  346. variant="ghost"
  347. size="sm"
  348. className="h-7 px-2"
  349. onClick={() => setSendReportId(report.id)}
  350. >
  351. <Send className="h-3.5 w-3.5" />
  352. <span className="hidden xl:inline ml-1.5">{t('send')}</span>
  353. </Button>
  354. )}
  355. <Button
  356. type="button"
  357. variant="ghost"
  358. size="sm"
  359. className="h-7 px-2"
  360. onClick={() => openReport(report.publicToken)}
  361. >
  362. <ExternalLink className="h-3.5 w-3.5" />
  363. <span className="hidden xl:inline ml-1.5">{t('view')}</span>
  364. </Button>
  365. <Button
  366. type="button"
  367. variant="ghost"
  368. size="sm"
  369. className="h-7 px-2 text-destructive hover:text-destructive"
  370. onClick={() => handleDelete(report.id)}
  371. >
  372. <Trash2 className="h-3.5 w-3.5" />
  373. <span className="hidden xl:inline ml-1.5">{t('delete')}</span>
  374. </Button>
  375. </div>
  376. </TableCell>
  377. </TableRow>
  378. </ContextMenuTrigger>
  379. <ContextMenuContent className="min-w-52">
  380. <ContextMenuItem onClick={() => openReport(report.publicToken)}>
  381. <ExternalLink className="mr-2 h-4 w-4" />
  382. {t('view')}
  383. </ContextMenuItem>
  384. <ContextMenuItem onClick={() => copyLink(report.publicToken)}>
  385. <Copy className="mr-2 h-4 w-4" />
  386. {t('copyLink')}
  387. </ContextMenuItem>
  388. {customer && (
  389. <ContextMenuItem onClick={() => setSendReportId(report.id)}>
  390. <Send className="mr-2 h-4 w-4" />
  391. {t('send')}
  392. </ContextMenuItem>
  393. )}
  394. <ContextMenuSeparator />
  395. <ContextMenuItem
  396. variant="destructive"
  397. onClick={() => handleDelete(report.id)}
  398. >
  399. <Trash2 className="mr-2 h-4 w-4" />
  400. {t('delete')}
  401. </ContextMenuItem>
  402. </ContextMenuContent>
  403. </ContextMenu>
  404. ))}
  405. </TableBody>
  406. </Table>
  407. </div>
  408. </>
  409. )}
  410. </div>
  411. {/* Detail dialog */}
  412. <Dialog
  413. open={!!detailReport}
  414. onOpenChange={(open) => {
  415. if (!open) setDetailReport(null)
  416. }}
  417. >
  418. <DialogContent className="sm:max-w-lg" aria-describedby={undefined}>
  419. {detailReport && (
  420. <>
  421. <DialogHeader>
  422. <DialogTitle>{detailReport.title || t('title')}</DialogTitle>
  423. </DialogHeader>
  424. <div className="space-y-4">
  425. {/* Status & dates */}
  426. <div className="grid grid-cols-2 gap-3 text-sm">
  427. <div>
  428. <p className="text-xs text-muted-foreground">{t('statusLabel')}</p>
  429. <Badge
  430. variant="outline"
  431. className={`mt-0.5 text-[10px] ${STATUS_VARIANT[detailReport.status] || ''}`}
  432. >
  433. {t(detailReport.status as 'draft' | 'published' | 'sent' | 'viewed')}
  434. </Badge>
  435. </div>
  436. <div>
  437. <p className="text-xs text-muted-foreground">{t('created')}</p>
  438. <p className="mt-0.5">{fmtDate(detailReport.createdAt)}</p>
  439. </div>
  440. {detailReport.sentAt && (
  441. <div>
  442. <p className="text-xs text-muted-foreground">{t('sentAt')}</p>
  443. <p className="mt-0.5">{fmtDate(detailReport.sentAt)}</p>
  444. </div>
  445. )}
  446. {detailReport.sentVia && (
  447. <div>
  448. <p className="text-xs text-muted-foreground">{t('sentVia')}</p>
  449. <p className="mt-0.5 capitalize">{detailReport.sentVia}</p>
  450. </div>
  451. )}
  452. {detailReport.expiresAt && (
  453. <div>
  454. <p className="text-xs text-muted-foreground">{t('expires')}</p>
  455. <p className="mt-0.5">
  456. {new Date(detailReport.expiresAt).toLocaleDateString(undefined, {
  457. month: 'short',
  458. day: 'numeric',
  459. year: 'numeric',
  460. })}
  461. </p>
  462. </div>
  463. )}
  464. </div>
  465. {/* Message */}
  466. {detailReport.message && (
  467. <div>
  468. <p className="text-xs text-muted-foreground mb-1">{t('messageLabel')}</p>
  469. <p className="text-sm whitespace-pre-wrap rounded-md bg-muted/50 p-3">
  470. {detailReport.message}
  471. </p>
  472. </div>
  473. )}
  474. {/* Video */}
  475. {detailReport.videoUrl && (
  476. <div>
  477. <p className="text-xs text-muted-foreground mb-1">{t('videoLabel')}</p>
  478. <video
  479. src={detailReport.videoUrl}
  480. controls
  481. preload="metadata"
  482. playsInline
  483. className="w-full rounded-md max-h-64"
  484. />
  485. </div>
  486. )}
  487. {/* Customer feedback */}
  488. <div>
  489. <p className="text-xs text-muted-foreground mb-1">{t('feedback')}</p>
  490. {detailReport.customerFeedback ? (
  491. <div className="rounded-md border border-green-200 bg-green-50 dark:border-green-900 dark:bg-green-950/30 p-3">
  492. <p className="text-sm whitespace-pre-wrap">{detailReport.customerFeedback}</p>
  493. {detailReport.feedbackAt && (
  494. <p className="mt-2 text-xs text-muted-foreground">
  495. {fmtDate(detailReport.feedbackAt)}
  496. </p>
  497. )}
  498. </div>
  499. ) : (
  500. <p className="text-sm text-muted-foreground">{t('noFeedback')}</p>
  501. )}
  502. </div>
  503. </div>
  504. </>
  505. )}
  506. </DialogContent>
  507. </Dialog>
  508. <CreateStatusReportDialog
  509. open={showCreate}
  510. onOpenChange={setShowCreate}
  511. serviceRecordId={serviceRecordId}
  512. inspectionId={inspectionId}
  513. vehicleName={vehicleName}
  514. customer={customer}
  515. smsEnabled={smsEnabled}
  516. emailEnabled={emailEnabled}
  517. telegramEnabled={telegramEnabled}
  518. onCreated={(reportId) => {
  519. if (customer) setSendReportId(reportId)
  520. else router.refresh()
  521. }}
  522. />
  523. {sendReportId && customer && (
  524. <SendStatusReportDialog
  525. open={!!sendReportId}
  526. onOpenChange={(open) => {
  527. if (!open) {
  528. setSendReportId(null)
  529. router.refresh()
  530. }
  531. }}
  532. reportId={sendReportId}
  533. customer={customer}
  534. smsEnabled={smsEnabled}
  535. emailEnabled={emailEnabled}
  536. telegramEnabled={telegramEnabled}
  537. />
  538. )}
  539. </>
  540. )
  541. }