quick-create-menu.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. 'use client'
  2. import { useGuardedPush } from '@/hooks/use-guarded-push'
  3. import { useState } from 'react'
  4. import { useTranslations } from 'next-intl'
  5. import { Button } from '@/components/ui/button'
  6. import {
  7. DropdownMenu,
  8. DropdownMenuContent,
  9. DropdownMenuItem,
  10. DropdownMenuSeparator,
  11. DropdownMenuLabel,
  12. DropdownMenuTrigger,
  13. } from '@/components/ui/dropdown-menu'
  14. import {
  15. Plus,
  16. Loader2,
  17. CalendarPlus,
  18. Wrench,
  19. Users,
  20. Car,
  21. FileText,
  22. Package,
  23. ClipboardCheck,
  24. UserPlus,
  25. } from 'lucide-react'
  26. import { VehiclePickerDialog } from '@/components/vehicle-picker-dialog'
  27. import { NewInspectionDialog } from '@/features/inspections/Components/NewInspectionDialog'
  28. import { getTemplates } from '@/features/inspections/Actions/templateActions'
  29. import { openBooking, useBookingAccess } from '@/components/booking-launcher'
  30. import { ShortcutKeys } from '@/features/shortcuts/Components/ShortcutKeys'
  31. import { useShortcut } from '@/features/shortcuts/Components/ShortcutsProvider'
  32. interface TemplateOption {
  33. id: string
  34. name: string
  35. isDefault: boolean
  36. }
  37. export function QuickCreateMenu() {
  38. const t = useTranslations('navigation')
  39. // Through the save guard: the menu is in the header of every page, an
  40. // open work order or quote with unsaved edits included.
  41. const go = useGuardedPush()
  42. const { canBook } = useBookingAccess()
  43. const [vehicleDialogOpen, setVehicleDialogOpen] = useState(false)
  44. const [inspectionDialogOpen, setInspectionDialogOpen] = useState(false)
  45. const [loading, setLoading] = useState(false)
  46. const [templates, setTemplates] = useState<TemplateOption[]>([])
  47. // The picker searches vehicles itself, so there is nothing to load first.
  48. const handleNewWorkOrder = () => {
  49. setVehicleDialogOpen(true)
  50. }
  51. const handleNewInspection = async () => {
  52. setLoading(true)
  53. try {
  54. const result = await getTemplates()
  55. setTemplates(
  56. result.success && result.data
  57. ? result.data.map((t) => ({
  58. id: t.id,
  59. name: t.name,
  60. isDefault: t.isDefault,
  61. }))
  62. : []
  63. )
  64. setInspectionDialogOpen(true)
  65. } finally {
  66. setLoading(false)
  67. }
  68. }
  69. // The same two dialogs on their shortcuts. The rest of the menu opens a
  70. // page, which the shortcut registry does on its own.
  71. useShortcut('create.workOrder', handleNewWorkOrder)
  72. useShortcut('create.inspection', () => void handleNewInspection())
  73. return (
  74. <>
  75. <DropdownMenu>
  76. <DropdownMenuTrigger asChild>
  77. <Button size="sm" disabled={loading}>
  78. {loading ? (
  79. <Loader2 className="mr-1 h-3.5 w-3.5 animate-spin" />
  80. ) : (
  81. <Plus className="mr-1 h-3.5 w-3.5" />
  82. )}
  83. {t('quickCreate.title')}
  84. </Button>
  85. </DropdownMenuTrigger>
  86. <DropdownMenuContent align="end" className="w-56">
  87. <DropdownMenuLabel>{t('quickCreate.title')}</DropdownMenuLabel>
  88. <DropdownMenuSeparator />
  89. <DropdownMenuItem onSelect={handleNewWorkOrder}>
  90. <Wrench className="h-4 w-4" />
  91. {t('quickCreate.workOrder')}
  92. <ShortcutKeys id="create.workOrder" className="ml-auto" />
  93. </DropdownMenuItem>
  94. {/* Beside the work order: the same car, for another day. */}
  95. {canBook && (
  96. <DropdownMenuItem onSelect={() => openBooking()}>
  97. <CalendarPlus className="h-4 w-4" />
  98. {t('quickCreate.appointment')}
  99. </DropdownMenuItem>
  100. )}
  101. <DropdownMenuItem onSelect={() => go('/customers?create=true')}>
  102. <Users className="h-4 w-4" />
  103. {t('quickCreate.customer')}
  104. <ShortcutKeys id="create.customer" className="ml-auto" />
  105. </DropdownMenuItem>
  106. <DropdownMenuItem onSelect={() => go('/vehicles?create=true')}>
  107. <Car className="h-4 w-4" />
  108. {t('quickCreate.vehicle')}
  109. <ShortcutKeys id="create.vehicle" className="ml-auto" />
  110. </DropdownMenuItem>
  111. <DropdownMenuItem onSelect={() => go('/quotes?create=true')}>
  112. <FileText className="h-4 w-4" />
  113. {t('quickCreate.quote')}
  114. <ShortcutKeys id="create.quote" className="ml-auto" />
  115. </DropdownMenuItem>
  116. <DropdownMenuItem onSelect={() => go('/inventory?create=true')}>
  117. <Package className="h-4 w-4" />
  118. {t('quickCreate.part')}
  119. <ShortcutKeys id="create.part" className="ml-auto" />
  120. </DropdownMenuItem>
  121. <DropdownMenuItem onSelect={handleNewInspection}>
  122. <ClipboardCheck className="h-4 w-4" />
  123. {t('quickCreate.inspection')}
  124. <ShortcutKeys id="create.inspection" className="ml-auto" />
  125. </DropdownMenuItem>
  126. {/* Everything above creates a record. This creates a person, which
  127. is a different kind of act and belongs behind its own heading:
  128. adding a mechanic is not the same errand as booking a car in. */}
  129. <DropdownMenuSeparator />
  130. <DropdownMenuLabel>{t('quickCreate.shopActions')}</DropdownMenuLabel>
  131. <DropdownMenuItem onSelect={() => go('/settings/team?add=true')}>
  132. <UserPlus className="mr-2 h-4 w-4" />
  133. {t('quickCreate.person')}
  134. </DropdownMenuItem>
  135. </DropdownMenuContent>
  136. </DropdownMenu>
  137. <VehiclePickerDialog open={vehicleDialogOpen} onOpenChange={setVehicleDialogOpen} />
  138. <NewInspectionDialog
  139. open={inspectionDialogOpen}
  140. onOpenChange={setInspectionDialogOpen}
  141. templates={templates}
  142. />
  143. </>
  144. )
  145. }