| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180 |
- 'use client'
- import { useGuardedPush } from '@/hooks/use-guarded-push'
- import { Fragment, type ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'
- import { useTranslations } from 'next-intl'
- import {
- Bell,
- Car,
- ClipboardCheck,
- CornerDownLeft,
- Disc3,
- FileText,
- type LucideIcon,
- Package,
- Plug,
- Keyboard,
- ScanLine,
- Search,
- Users,
- Wrench,
- } from 'lucide-react'
- import {
- CommandDialog,
- CommandGroup,
- CommandInput,
- CommandItem,
- CommandList,
- } from '@/components/ui/command'
- import { formatQuantity } from '@/lib/format-quantity'
- import { cn } from '@/lib/utils'
- import { globalSearch, getRecentCustomers } from '@/features/search/Actions/searchActions'
- import { formatTypeKey } from '@/features/vehicles/Lib/typeKey'
- import { useTypeKeyEnabled } from '@/components/type-key-context'
- import { openPlateLookup, usePlateLookupAccess } from '@/components/plate-lookup-context'
- import { requestSpotlight } from '@/features/settings/Components/SettingsSpotlight'
- import { CRUMB, useSettingsSearchItems } from '@/features/search/Lib/settingsItems'
- import {
- type CommandContext,
- visibleActionCommands,
- visiblePageCommands,
- } from '@/features/search/Lib/commands'
- import {
- type PreparedItem,
- type Range,
- prepareItem,
- prepareQuery,
- rank,
- splitByRanges,
- } from '@/features/search/Lib/matcher'
- import { pushRecent, readRecent, type RecentPick } from '@/features/search/Lib/recent'
- import { SEARCH_OPEN_EVENT } from '@/features/search/Lib/open'
- import { useShortcut, useShortcuts } from '@/features/shortcuts/Components/ShortcutsProvider'
- import { ShortcutKeys } from '@/features/shortcuts/Components/ShortcutKeys'
- import { SHORTCUTS_HELP_COMMAND, shortcutForCommand } from '@/features/shortcuts/Lib/registry'
- import { searchFaultCodes } from '@/features/fault-codes/Actions/searchFaultCodes'
- import {
- isManufacturerCode,
- looksLikeFaultCode,
- looksLikeOemCode,
- normalizeFaultCode,
- } from '@/features/fault-codes/Lib/codes'
- import type { OemSearchHit } from '@/features/fault-codes/Lib/types'
- type Vehicle = {
- id: string
- make: string
- model: string
- year: number
- licensePlate: string | null
- hsn?: string | null
- tsn?: string | null
- }
- interface SearchResult {
- vehicles: Vehicle[]
- customers: {
- id: string
- name: string
- email: string | null
- phone: string | null
- company: string | null
- vehicles: Vehicle[]
- }[]
- services: {
- id: string
- title: string
- invoiceNumber: string | null
- vehicle: Vehicle | null
- }[]
- parts: {
- id: string
- name: string
- partNumber: string | null
- quantity: number
- /** False for a fee or consumable that is charged but never counted. */
- trackStock?: boolean
- unit: string | null
- }[]
- quotes: {
- id: string
- title: string
- quoteNumber: string | null
- status: string
- }[]
- reminders: {
- id: string
- title: string
- dueDate: Date | null
- isCompleted: boolean
- customer: { id: string; name: string } | null
- vehicle: Vehicle | null
- }[]
- inspections: {
- id: string
- status: string
- createdAt: Date
- template: { name: string }
- vehicle: Vehicle
- }[]
- tireSets: {
- id: string
- reference: string | null
- season: string
- size: string | null
- quantity: number
- status: string
- location: { code: string } | null
- customer: { id: string; name: string } | null
- vehicle: Vehicle | null
- }[]
- }
- /** The palette's fault code groups, asked for on their own beside the normal search. */
- interface FaultCodeResult {
- query: string
- /** The code service answered. */
- available: boolean
- codes: { code: string; title: string }[]
- /** A make's own code typed in full (BMW's 283700), from each make that has it. */
- oem: OemSearchHit[]
- jobs: {
- findingId: string
- serviceRecordId: string | null
- title: string | null
- vehicleId: string
- vehicle: string
- licensePlate: string | null
- }[]
- }
- const NO_FAULT_CODES: FaultCodeResult = {
- query: '',
- available: false,
- codes: [],
- oem: [],
- jobs: [],
- }
- const EMPTY: SearchResult = {
- vehicles: [],
- customers: [],
- services: [],
- parts: [],
- quotes: [],
- reminders: [],
- inspections: [],
- tireSets: [],
- }
- type RecentCustomer = {
- id: string
- name: string
- email: string | null
- phone: string | null
- company: string | null
- }
- /** A connector the catalog offers, trimmed to what the palette shows. */
- export type SearchConnector = { id: string; name: string; category: string; logo: string }
- export type SearchCommandProps = CommandContext & {
- isCloud?: boolean
- supportEnabled?: boolean
- isMarine?: boolean
- connectors?: SearchConnector[]
- }
- /**
- * Anything the palette knows without asking the server: a settings page or
- * a setting on it, a connector, a page of the app, or something to start.
- */
- type LocalItem = {
- kind: 'setting' | 'settingsPage' | 'connector' | 'page' | 'action'
- id: string
- href: string
- title: string
- subtitle?: string
- icon?: LucideIcon
- logo?: string
- locked?: boolean
- anchor?: string
- run?: () => void
- }
- type PreparedLocal = { item: LocalItem; prepared: PreparedItem }
- /** How many to show per group when searching. Everything else is a scroll away in the app itself. */
- const LIMITS: Record<LocalItem['kind'], number> = {
- setting: 8,
- settingsPage: 8,
- connector: 4,
- page: 5,
- action: 4,
- }
- /** Enough recent customers to be useful before typing, few enough that the actions stay in view. */
- const RECENT_CUSTOMERS_SHOWN = 8
- /** Wait this long after the last keystroke before asking the server. Local matches never wait. */
- const REMOTE_DEBOUNCE_MS = 150
- function Highlight({ text, ranges }: { text: string; ranges: Range[] }) {
- return (
- <>
- {splitByRanges(text, ranges).map((part, i) =>
- part.hit ? (
- <mark key={i} className="bg-transparent font-semibold text-primary">
- {part.text}
- </mark>
- ) : (
- <Fragment key={i}>{part.text}</Fragment>
- )
- )}
- </>
- )
- }
- /** The shortcut for a page or an action, on the right of its row. */
- function LocalShortcut({ id }: { id: string }) {
- const shortcut = shortcutForCommand(id)
- return shortcut ? <ShortcutKeys id={shortcut.id} /> : null
- }
- function Kbd({ children }: { children: ReactNode }) {
- return (
- <kbd className="pointer-events-none inline-flex h-5 min-w-5 items-center justify-center rounded border bg-muted px-1 font-mono text-[10px] font-medium text-muted-foreground">
- {children}
- </kbd>
- )
- }
- function Chip({ icon: Icon, logo }: { icon?: LucideIcon; logo?: string }) {
- return (
- <span className="flex h-7 w-7 shrink-0 items-center justify-center rounded-md bg-muted text-muted-foreground ring-1 ring-inset ring-foreground/5 group-data-[selected=true]/row:bg-primary/10 group-data-[selected=true]/row:text-primary group-data-[selected=true]/row:ring-primary/20">
- {logo ? (
- <img src={logo} alt="" className="h-4 w-4 object-contain" />
- ) : Icon ? (
- <Icon className="h-4! w-4!" />
- ) : null}
- </span>
- )
- }
- /**
- * One row. The chip lights up in the theme colour on the selected row, and
- * the return-key hint appears there and nowhere else, so the eye always
- * knows which row Enter will open.
- */
- function Row({
- value,
- onSelect,
- icon,
- logo,
- title,
- subtitle,
- trailing,
- locked,
- className,
- proLabel,
- }: {
- value: string
- onSelect: () => void
- icon?: LucideIcon
- logo?: string
- title: ReactNode
- subtitle?: ReactNode
- /** A mono tag on the right: a plate, a shelf code, an invoice number. */
- trailing?: ReactNode
- locked?: boolean
- className?: string
- proLabel?: string
- }) {
- return (
- <CommandItem
- value={value}
- onSelect={onSelect}
- className={cn('group/row cursor-pointer gap-3 rounded-lg', className)}
- >
- <Chip icon={icon} logo={logo} />
- <div className="min-w-0 flex-1">
- <div className="flex items-center gap-2">
- <span className="truncate text-sm">{title}</span>
- {locked && proLabel && (
- <span className="shrink-0 rounded bg-amber-500/15 px-1.5 py-px text-[10px] font-semibold text-amber-600">
- {proLabel}
- </span>
- )}
- </div>
- {subtitle && <div className="truncate text-xs text-muted-foreground">{subtitle}</div>}
- </div>
- {trailing && (
- <span className="hidden shrink-0 font-mono text-xs text-muted-foreground sm:inline">
- {trailing}
- </span>
- )}
- <span className="hidden shrink-0 items-center gap-1 text-[11px] text-muted-foreground opacity-0 transition-opacity group-data-[selected=true]/row:opacity-100 sm:flex">
- <CornerDownLeft className="h-3! w-3!" />
- </span>
- </CommandItem>
- )
- }
- function vehicleLine(v: Vehicle) {
- return `${v.year} ${v.make} ${v.model}`
- }
- function iconForHref(href: string): LucideIcon {
- if (href.startsWith('/customers')) return Users
- if (href.startsWith('/vehicles')) return Car
- if (href.startsWith('/quotes')) return FileText
- if (href.startsWith('/inventory')) return Package
- if (href.startsWith('/tire-hotel')) return Disc3
- if (href.startsWith('/inspections')) return ClipboardCheck
- if (href.startsWith('/reminders')) return Bell
- if (href.startsWith('/settings')) return Search
- return Wrench
- }
- export function SearchCommand({
- visibleSubjects,
- features,
- tireHotelEnabled,
- aiEnabled,
- isAdminOrOwner,
- isCloud,
- supportEnabled,
- isMarine,
- connectors = [],
- faultCodesEnabled,
- }: SearchCommandProps) {
- const go = useGuardedPush()
- const t = useTranslations('search')
- const tNav = useTranslations('navigation.sidebar')
- const tSettings = useTranslations('settings')
- const tIntegrations = useTranslations('integrations')
- const tTire = useTranslations('tireHotel')
- const tPlate = useTranslations('vehicles.plateLookup')
- const tShortcuts = useTranslations('settings.shortcuts')
- const { openHelp: openShortcutsHelp } = useShortcuts()
- const tFault = useTranslations('faultCodes.search')
- const tCodes = useTranslations('faultCodes')
- const typeKeyEnabled = useTypeKeyEnabled()
- const plateLookup = usePlateLookupAccess()
- const [open, setOpen] = useState(false)
- const [query, setQuery] = useState('')
- const [results, setResults] = useState<SearchResult>(EMPTY)
- const [loading, setLoading] = useState(false)
- const [recentCustomers, setRecentCustomers] = useState<RecentCustomer[]>([])
- const [recent, setRecent] = useState<RecentPick[]>([])
- const cacheRef = useRef(new Map<string, SearchResult>())
- const seqRef = useRef(0)
- const [faultResults, setFaultResults] = useState<FaultCodeResult>(NO_FAULT_CODES)
- const faultCacheRef = useRef(new Map<string, FaultCodeResult>())
- const faultSeqRef = useRef(0)
- const canSettings = !visibleSubjects || visibleSubjects.includes('settings')
- const ctx: CommandContext = useMemo(
- () => ({
- visibleSubjects,
- features,
- tireHotelEnabled,
- aiEnabled,
- isAdminOrOwner,
- faultCodesEnabled,
- }),
- [visibleSubjects, features, tireHotelEnabled, aiEnabled, isAdminOrOwner, faultCodesEnabled]
- )
- const settingsItems = useSettingsSearchItems({ isCloud, supportEnabled, features })
- // Everything searchable without the server, titled in the workshop's
- // language and prepared once; typing only ranks it.
- const localItems = useMemo<PreparedLocal[]>(() => {
- const out: PreparedLocal[] = []
- const settingsWord = tNav('settings')
- for (const page of visiblePageCommands(ctx)) {
- const title = tNav(isMarine && page.marineTitleKey ? page.marineTitleKey : page.titleKey)
- const item: LocalItem = {
- kind: 'page',
- id: page.id,
- href: page.href,
- title,
- icon: isMarine && page.marineIcon ? page.marineIcon : page.icon,
- }
- out.push({ item, prepared: prepareItem({ title, keywords: page.keywords }) })
- }
- for (const action of visibleActionCommands(ctx)) {
- const title = t(`actions.${action.titleKey}`)
- const item: LocalItem = {
- kind: 'action',
- id: action.id,
- href: action.href,
- title,
- icon: action.icon,
- }
- out.push({ item, prepared: prepareItem({ title, keywords: action.keywords }) })
- }
- if (plateLookup.available) {
- const title = tPlate('open')
- out.push({
- item: {
- kind: 'action',
- id: 'action:plate-lookup',
- href: '#',
- title,
- subtitle: plateLookup.registryName ?? undefined,
- icon: ScanLine,
- run: () => openPlateLookup(),
- },
- prepared: prepareItem({
- title,
- keywords: ['plate', 'registry', 'lookup', 'license plate', 'vin', 'registration'],
- }),
- })
- }
- {
- const title = tShortcuts('help.title')
- out.push({
- item: {
- kind: 'action',
- id: SHORTCUTS_HELP_COMMAND,
- href: '#',
- title,
- icon: Keyboard,
- run: openShortcutsHelp,
- },
- prepared: prepareItem({
- title,
- keywords: ['hotkeys', 'keys', 'shortcut', 'keyboard', 'key bindings', 'cheat sheet'],
- }),
- })
- }
- if (canSettings) {
- for (const { item, prepared } of settingsItems) {
- const subtitle = item.description
- ? `${item.breadcrumb} · ${item.description}`
- : item.breadcrumb
- out.push({
- item: {
- kind: item.kind,
- id: item.id,
- href: item.href,
- title: item.title,
- subtitle: `${settingsWord}${CRUMB}${subtitle}`,
- icon: item.icon,
- locked: item.locked,
- anchor: item.anchor,
- },
- prepared,
- })
- }
- const integrationsWord = tSettings('nav.items.integrations.title')
- for (const c of connectors) {
- const category = tIntegrations(`categories.${c.category}`)
- out.push({
- item: {
- kind: 'connector',
- id: `connector:${c.id}`,
- href: `/settings/integrations/${c.id}`,
- title: c.name,
- subtitle: `${settingsWord}${CRUMB}${integrationsWord}${CRUMB}${category}`,
- icon: Plug,
- logo: c.logo,
- },
- prepared: prepareItem({
- title: c.name,
- keywords: [c.id, c.category, 'integration', 'connect', 'connector'],
- context: category,
- }),
- })
- }
- }
- return out
- }, [
- ctx,
- isMarine,
- canSettings,
- settingsItems,
- connectors,
- plateLookup.available,
- plateLookup.registryName,
- t,
- tNav,
- tSettings,
- tIntegrations,
- tPlate,
- tShortcuts,
- openShortcutsHelp,
- ])
- const preparedQuery = useMemo(() => prepareQuery(query), [query])
- const hasQuery = preparedQuery.tokens.length > 0
- const ranked = useMemo(
- () => (hasQuery ? rank(preparedQuery, localItems) : []),
- [hasQuery, preparedQuery, localItems]
- )
- // Direct hits go above the workshop's records; loose ones only show when
- // nothing else does, so "ola" never buries a customer under Localization.
- const { strongGroups, weakGroups } = useMemo(() => {
- type Group = { kind: LocalItem['kind']; rows: typeof ranked; best: number }
- const build = (rows: typeof ranked) => {
- const byKind = new Map<LocalItem['kind'], Group>()
- for (const row of rows) {
- const kind = row.item.kind
- let group = byKind.get(kind)
- if (!group) {
- group = { kind, rows: [], best: row.match.score }
- byKind.set(kind, group)
- }
- if (group.rows.length < LIMITS[kind]) group.rows.push(row)
- }
- return [...byKind.values()].sort((a, b) => b.best - a.best)
- }
- return {
- strongGroups: build(ranked.filter((r) => r.match.strong)),
- weakGroups: build(ranked.filter((r) => !r.match.strong)),
- }
- }, [ranked])
- // Opened by its shortcut, and by the header's search box, which has no
- // other way to reach this component.
- useShortcut('general.search', () => setOpen((prev) => !prev))
- useEffect(() => {
- const onOpen = () => setOpen(true)
- document.addEventListener(SEARCH_OPEN_EVENT, onOpen)
- return () => document.removeEventListener(SEARCH_OPEN_EVENT, onOpen)
- }, [])
- // On opening: forget what the server said last time (records change), read
- // what this person opened lately, and fetch the customers worked on last.
- useEffect(() => {
- if (!open) return
- cacheRef.current.clear()
- faultCacheRef.current.clear()
- // eslint-disable-next-line react-hooks/set-state-in-effect -- reading the store on open is the point
- setRecent(readRecent())
- let cancelled = false
- getRecentCustomers().then((res) => {
- if (!cancelled && res.success && res.data) setRecentCustomers(res.data)
- })
- return () => {
- cancelled = true
- }
- }, [open])
- // The server search: debounced a little, cached for the life of the
- // dialog, and never allowed to overwrite a newer answer with an older one.
- // Previous results stay on screen while the next ones load, so the list
- // never blinks empty between keystrokes.
- useEffect(() => {
- const term = query.trim()
- // Every change of query, cached or not, makes any answer still on its way stale.
- const seq = ++seqRef.current
- if (term.length < 2) {
- // eslint-disable-next-line react-hooks/set-state-in-effect -- clearing results on a short query is intentional
- setResults(EMPTY)
- setLoading(false)
- return
- }
- const key = term.toLowerCase()
- const cached = cacheRef.current.get(key)
- if (cached) {
- setResults(cached)
- setLoading(false)
- return
- }
- setLoading(true)
- const timer = setTimeout(() => {
- globalSearch(term).then((res) => {
- if (seq !== seqRef.current) return
- if (res.success && res.data) {
- cacheRef.current.set(key, res.data)
- setResults(res.data)
- }
- setLoading(false)
- })
- }, REMOTE_DEBOUNCE_MS)
- return () => clearTimeout(timer)
- }, [query])
- // Fault codes, asked for on their own so the code service can never hold
- // up vehicles, customers or jobs. Only a query that looks like a code goes
- // out; the jobs where a whole code was read come back with or without the
- // service, since they are the workshop's own.
- useEffect(() => {
- const term = query.trim()
- const seq = ++faultSeqRef.current
- if (!looksLikeFaultCode(term) && !looksLikeOemCode(term)) {
- // eslint-disable-next-line react-hooks/set-state-in-effect -- clearing results on a query that is not a code is intentional
- setFaultResults(NO_FAULT_CODES)
- return
- }
- const key = term.toUpperCase()
- const cached = faultCacheRef.current.get(key)
- if (cached) {
- setFaultResults(cached)
- return
- }
- const timer = setTimeout(() => {
- searchFaultCodes(term).then((res) => {
- if (seq !== faultSeqRef.current) return
- if (res.success && res.data) {
- const next = {
- query: key,
- available: res.data.available,
- codes: res.data.codes,
- oem: res.data.oem,
- jobs: res.data.jobs,
- }
- faultCacheRef.current.set(key, next)
- setFaultResults(next)
- }
- })
- }, REMOTE_DEBOUNCE_MS)
- return () => clearTimeout(timer)
- }, [query])
- const close = useCallback(() => {
- setOpen(false)
- setQuery('')
- }, [])
- const pickLocal = useCallback(
- (item: LocalItem) => {
- close()
- pushRecent({
- id: item.id,
- href: item.href,
- title: item.title,
- subtitle: item.subtitle,
- kind: 'local',
- })
- if (item.run) {
- item.run()
- return
- }
- go(item.href)
- if (item.anchor) requestSpotlight(item.anchor)
- },
- [close, go]
- )
- const pickRecord = useCallback(
- (href: string, title: string, subtitle?: string) => {
- close()
- pushRecent({ id: href, href, title, subtitle, kind: 'record' })
- go(href)
- },
- [close, go]
- )
- const hasRecords =
- results.vehicles.length > 0 ||
- results.customers.length > 0 ||
- results.services.length > 0 ||
- results.parts.length > 0 ||
- results.quotes.length > 0 ||
- results.reminders.length > 0 ||
- results.inspections.length > 0 ||
- results.tireSets.length > 0
- // The palette's fault code rows: the service's codes for what was typed, and
- // the whole code itself when the service has nothing for it, so Enter still
- // opens its page.
- const faultQuery = query.trim().toUpperCase()
- const faultCurrent = faultResults.query === faultQuery ? faultResults : NO_FAULT_CODES
- const typedCode = normalizeFaultCode(faultQuery)
- const faultCodeRows = faultCodesEnabled
- ? typedCode && !faultCurrent.codes.some((c) => c.code === typedCode)
- ? [{ code: typedCode, title: '' }, ...faultCurrent.codes]
- : faultCurrent.codes
- : []
- const faultJobs = faultCurrent.jobs
- const searchFaultWords =
- faultCodesEnabled && !looksLikeFaultCode(query) && query.trim().length >= 3
- const faultOemRows = faultCodesEnabled ? faultCurrent.oem : []
- const hasFaultRows = faultCodeRows.length > 0 || faultOemRows.length > 0 || faultJobs.length > 0
- const showWeak = !hasRecords && !hasFaultRows && strongGroups.length === 0
- const nothing =
- hasQuery &&
- !loading &&
- !hasRecords &&
- !hasFaultRows &&
- strongGroups.length === 0 &&
- weakGroups.length === 0
- const groupHeading = (kind: LocalItem['kind']) => {
- switch (kind) {
- case 'setting':
- case 'settingsPage':
- return t('groups.settings')
- case 'connector':
- return t('groups.integrations')
- case 'page':
- return t('groups.pages')
- case 'action':
- return t('groups.actions')
- }
- }
- // Settings pages and settings share a heading; cmdk needs one group per
- // heading or it draws it twice, so the two kinds are merged here.
- const mergeSettings = (groups: typeof strongGroups) => {
- const merged: typeof strongGroups = []
- for (const g of groups) {
- const heading = groupHeading(g.kind)
- const existing = merged.find((m) => groupHeading(m.kind) === heading)
- if (existing) {
- existing.rows = [...existing.rows, ...g.rows]
- .sort((a, b) => b.match.score - a.match.score)
- .slice(0, LIMITS.setting)
- existing.best = Math.max(existing.best, g.best)
- } else merged.push({ ...g, rows: [...g.rows] })
- }
- return merged.sort((a, b) => b.best - a.best)
- }
- const renderLocalGroups = (groups: typeof strongGroups) =>
- mergeSettings(groups).map((group) => (
- <CommandGroup key={group.kind} heading={groupHeading(group.kind)}>
- {group.rows.map(({ item, match }) => (
- <Row
- key={item.id}
- value={item.id}
- onSelect={() => pickLocal(item)}
- icon={item.icon}
- logo={item.logo}
- title={<Highlight text={item.title} ranges={match.ranges} />}
- subtitle={item.subtitle}
- locked={item.locked}
- proLabel={t('pro')}
- trailing={<LocalShortcut id={item.id} />}
- />
- ))}
- </CommandGroup>
- ))
- // Recent picks: local ones are looked up again so a renamed page reads
- // right and a vanished one drops out; records keep the words they had.
- const recentRows = useMemo(() => {
- const byId = new Map(localItems.map(({ item }) => [item.id, item]))
- return recent
- .map((pick) => {
- if (pick.kind === 'local') {
- const item = byId.get(pick.id)
- return item ? { pick, item } : null
- }
- return { pick, item: null }
- })
- .filter((r): r is { pick: RecentPick; item: LocalItem | null } => r !== null)
- .slice(0, 6)
- }, [recent, localItems])
- return (
- <CommandDialog
- open={open}
- onOpenChange={(next) => (next ? setOpen(true) : close())}
- title={t('title')}
- description={t('description')}
- className="top-[6vh] translate-y-0 gap-0 rounded-xl border-border/60 shadow-2xl sm:top-[12vh] sm:max-w-2xl"
- showCloseButton={false}
- shouldFilter={false}
- >
- <div className="relative">
- <CommandInput
- placeholder={t('placeholder')}
- value={query}
- onValueChange={setQuery}
- autoFocus
- />
- {loading && <div className="search-progress" aria-hidden />}
- </div>
- <CommandList className="max-h-[min(62vh,560px)] min-h-[220px] scroll-py-2">
- {nothing && (
- <div className="flex flex-col items-center gap-1 px-6 py-12 text-center">
- <Search className="mb-2 h-5 w-5 text-muted-foreground/60" />
- <p className="text-sm font-medium">{t('noResultsFor', { query: query.trim() })}</p>
- <p className="text-xs text-muted-foreground">{t('noResultsHint')}</p>
- </div>
- )}
- {!hasQuery && recentRows.length > 0 && (
- <CommandGroup heading={t('groups.recent')}>
- {recentRows.map(({ pick, item }) =>
- item ? (
- <Row
- key={pick.id}
- value={`recent:${pick.id}`}
- onSelect={() => pickLocal(item)}
- icon={item.icon}
- logo={item.logo}
- title={item.title}
- subtitle={item.subtitle}
- locked={item.locked}
- proLabel={t('pro')}
- trailing={<LocalShortcut id={item.id} />}
- />
- ) : (
- <Row
- key={pick.id}
- value={`recent:${pick.id}`}
- onSelect={() => pickRecord(pick.href, pick.title, pick.subtitle)}
- icon={iconForHref(pick.href)}
- title={pick.title}
- subtitle={pick.subtitle}
- />
- )
- )}
- </CommandGroup>
- )}
- {!hasQuery && recentCustomers.length > 0 && (
- <CommandGroup heading={t('recentCustomers')}>
- {recentCustomers.slice(0, RECENT_CUSTOMERS_SHOWN).map((c) => (
- <Row
- key={c.id}
- value={`recent-customer:${c.id}`}
- onSelect={() =>
- pickRecord(
- `/customers/${c.id}`,
- c.name,
- [c.company, c.email, c.phone].filter(Boolean).join(' · ') || undefined
- )
- }
- icon={Users}
- title={c.name}
- subtitle={[c.company, c.email, c.phone].filter(Boolean).join(' · ')}
- />
- ))}
- </CommandGroup>
- )}
- {!hasQuery && (
- <CommandGroup heading={t('groups.actions')}>
- {localItems
- .filter(({ item }) => item.kind === 'action')
- .map(({ item }) => (
- <Row
- key={item.id}
- value={item.id}
- onSelect={() => pickLocal(item)}
- icon={item.icon}
- title={item.title}
- subtitle={item.subtitle}
- trailing={<LocalShortcut id={item.id} />}
- />
- ))}
- </CommandGroup>
- )}
- {hasQuery && (faultCodeRows.length > 0 || faultOemRows.length > 0) && (
- <CommandGroup heading={tFault('group')}>
- {faultOemRows.map((c) => (
- <Row
- key={`${c.make}:${c.code}`}
- value={`fault-oem:${c.make}:${c.code}`}
- onSelect={() =>
- pickRecord(`/fault-codes/oem/${c.make}/${c.code}`, c.code, c.title || undefined)
- }
- icon={ScanLine}
- title={
- <>
- <span className="mr-2 font-mono font-semibold">{c.code}</span>
- <span className="mr-2 rounded-full border border-primary/30 bg-primary/10 px-1.5 py-px text-[10px] font-medium text-primary">
- {tCodes('oem.badge', { make: c.makeName })}
- </span>
- {c.title}
- </>
- }
- subtitle={
- c.meanings > 1
- ? tCodes('oem.otherMeanings', { count: c.meanings - 1 })
- : undefined
- }
- />
- ))}
- {faultCodeRows.map((c) => (
- <Row
- key={c.code}
- value={`fault-code:${c.code}`}
- onSelect={() => pickRecord(`/fault-codes/${c.code}`, c.code, c.title || undefined)}
- icon={ScanLine}
- title={
- <>
- <span className="mr-2 font-mono font-semibold">{c.code}</span>
- {c.title}
- </>
- }
- subtitle={
- // A whole code the service has no meaning for: say why, when we know.
- c.title
- ? undefined
- : isManufacturerCode(c.code)
- ? tCodes('manufacturerUnknown')
- : faultCurrent.available && faultCurrent.query === c.code
- ? tCodes('noMeaning')
- : undefined
- }
- />
- ))}
- </CommandGroup>
- )}
- {hasQuery && faultJobs.length > 0 && (
- <CommandGroup heading={tFault('jobsWith', { code: faultCurrent.query })}>
- {faultJobs.map((job) => {
- const href = job.serviceRecordId
- ? `/vehicles/${job.vehicleId}/service/${job.serviceRecordId}`
- : `/vehicles/${job.vehicleId}`
- const title = job.title ?? job.vehicle
- const subtitle = [job.title ? job.vehicle : null, job.licensePlate]
- .filter(Boolean)
- .join(' · ')
- return (
- <Row
- key={job.findingId}
- value={`fault-job:${job.findingId}`}
- onSelect={() => pickRecord(href, title, subtitle || undefined)}
- icon={Wrench}
- title={title}
- subtitle={subtitle}
- trailing={job.licensePlate}
- />
- )
- })}
- </CommandGroup>
- )}
- {hasQuery && renderLocalGroups(strongGroups)}
- {hasQuery && results.customers.length > 0 && (
- <CommandGroup heading={tNav('customers')}>
- {results.customers.map((c) => (
- <Fragment key={c.id}>
- <Row
- value={`customer:${c.id}`}
- onSelect={() =>
- pickRecord(
- `/customers/${c.id}`,
- c.name,
- [c.company, c.email, c.phone].filter(Boolean).join(' · ') || undefined
- )
- }
- icon={Users}
- title={c.name}
- subtitle={[c.company, c.email, c.phone].filter(Boolean).join(' · ')}
- />
- {c.vehicles.map((v) => (
- <Row
- key={v.id}
- value={`customer-vehicle:${v.id}`}
- onSelect={() => pickRecord(`/vehicles/${v.id}`, vehicleLine(v), c.name)}
- icon={Car}
- title={vehicleLine(v)}
- subtitle={c.name}
- trailing={v.licensePlate}
- className="ml-10"
- />
- ))}
- </Fragment>
- ))}
- </CommandGroup>
- )}
- {hasQuery && results.vehicles.length > 0 && (
- <CommandGroup heading={tNav(isMarine ? 'vessels' : 'vehicles')}>
- {results.vehicles.map((v) => (
- <Row
- key={v.id}
- value={`vehicle:${v.id}`}
- onSelect={() =>
- pickRecord(`/vehicles/${v.id}`, vehicleLine(v), v.licensePlate ?? undefined)
- }
- icon={Car}
- title={vehicleLine(v)}
- subtitle={
- typeKeyEnabled && (v.hsn || v.tsn) ? formatTypeKey(v.hsn, v.tsn) : undefined
- }
- trailing={v.licensePlate}
- />
- ))}
- </CommandGroup>
- )}
- {hasQuery && results.services.length > 0 && (
- <CommandGroup heading={t('services')}>
- {results.services.map((s) => {
- const href = s.vehicle
- ? `/vehicles/${s.vehicle.id}/service/${s.id}`
- : `/sales/${s.id}`
- const subtitle = [s.vehicle ? vehicleLine(s.vehicle) : null, s.vehicle?.licensePlate]
- .filter(Boolean)
- .join(' · ')
- return (
- <Row
- key={s.id}
- value={`service:${s.id}`}
- onSelect={() => pickRecord(href, s.title, subtitle || undefined)}
- icon={Wrench}
- title={s.title}
- subtitle={subtitle}
- trailing={s.invoiceNumber}
- />
- )
- })}
- </CommandGroup>
- )}
- {hasQuery && results.quotes.length > 0 && (
- <CommandGroup heading={tNav('quotes')}>
- {results.quotes.map((q) => (
- <Row
- key={q.id}
- value={`quote:${q.id}`}
- onSelect={() => pickRecord(`/quotes/${q.id}`, q.title, q.quoteNumber ?? undefined)}
- icon={FileText}
- title={q.title}
- subtitle={q.status}
- trailing={q.quoteNumber}
- />
- ))}
- </CommandGroup>
- )}
- {hasQuery && results.parts.length > 0 && (
- <CommandGroup heading={tNav('inventory')}>
- {results.parts.map((p) => (
- <Row
- key={p.id}
- value={`part:${p.id}`}
- onSelect={() => pickRecord('/inventory', p.name, p.partNumber ?? undefined)}
- icon={Package}
- title={p.name}
- subtitle={
- p.trackStock === false
- ? undefined
- : `${formatQuantity(p.quantity, p.unit)} in stock`
- }
- trailing={p.partNumber}
- />
- ))}
- </CommandGroup>
- )}
- {hasQuery && results.reminders.length > 0 && (
- <CommandGroup heading={tNav('reminders')}>
- {results.reminders.map((r) => {
- const href = r.vehicle ? `/vehicles/${r.vehicle.id}?tab=reminders` : '/reminders'
- const subtitle = r.vehicle
- ? `${vehicleLine(r.vehicle)}${r.vehicle.licensePlate ? ` · ${r.vehicle.licensePlate}` : ''}`
- : (r.customer?.name ?? '')
- return (
- <Row
- key={r.id}
- value={`reminder:${r.id}`}
- onSelect={() => pickRecord(href, r.title, subtitle || undefined)}
- icon={Bell}
- title={
- <span className={cn(r.isCompleted && 'text-muted-foreground line-through')}>
- {r.title}
- </span>
- }
- subtitle={subtitle}
- />
- )
- })}
- </CommandGroup>
- )}
- {/* Shelf code leads: the desk asks "where are these tires", and the
- code is the answer they are about to read out loud. */}
- {hasQuery && results.tireSets.length > 0 && (
- <CommandGroup heading={tNav('tireHotel')}>
- {results.tireSets.map((set) => {
- const title = set.location?.code ?? tTire(`statuses.${set.status}`)
- const subtitle = [
- set.customer?.name,
- set.vehicle ? `${set.vehicle.make} ${set.vehicle.model}` : null,
- set.size,
- tTire('list.tireCount', { count: set.quantity }),
- ]
- .filter(Boolean)
- .join(' · ')
- return (
- <Row
- key={set.id}
- value={`tire:${set.id}`}
- onSelect={() => pickRecord(`/tire-hotel/${set.id}`, title, subtitle)}
- icon={Disc3}
- title={<span className={cn(set.location?.code && 'font-mono')}>{title}</span>}
- subtitle={subtitle}
- trailing={set.vehicle?.licensePlate}
- />
- )
- })}
- </CommandGroup>
- )}
- {hasQuery && results.inspections.length > 0 && (
- <CommandGroup heading={tNav('inspections')}>
- {results.inspections.map((insp) => (
- <Row
- key={insp.id}
- value={`inspection:${insp.id}`}
- onSelect={() =>
- pickRecord(
- `/inspections/${insp.id}`,
- insp.template.name,
- vehicleLine(insp.vehicle)
- )
- }
- icon={ClipboardCheck}
- title={insp.template.name}
- subtitle={`${vehicleLine(insp.vehicle)} · ${insp.status}`}
- trailing={insp.vehicle.licensePlate}
- />
- ))}
- </CommandGroup>
- )}
- {hasQuery && showWeak && renderLocalGroups(weakGroups)}
- {searchFaultWords && (
- <CommandGroup heading={tFault('group')}>
- <Row
- value="fault-code-words"
- onSelect={() =>
- pickRecord(
- `/fault-codes?q=${encodeURIComponent(query.trim())}`,
- tFault('searchFor', { query: query.trim() })
- )
- }
- icon={ScanLine}
- title={tFault('searchFor', { query: query.trim() })}
- />
- </CommandGroup>
- )}
- </CommandList>
- <div className="flex items-center justify-between gap-4 border-t px-3 py-2 text-[11px] text-muted-foreground">
- <div className="hidden items-center gap-3 sm:flex">
- <span className="flex items-center gap-1">
- <Kbd>↑</Kbd>
- <Kbd>↓</Kbd>
- <span className="ml-1">{t('hints.navigate')}</span>
- </span>
- <span className="flex items-center gap-1">
- <Kbd>↵</Kbd>
- <span className="ml-1">{t('hints.open')}</span>
- </span>
- <span className="flex items-center gap-1">
- <Kbd>esc</Kbd>
- <span className="ml-1">{t('hints.close')}</span>
- </span>
- </div>
- <div className="min-w-0 truncate">
- {loading ? t('hints.searching') : !hasQuery ? t('hints.start') : null}
- </div>
- </div>
- </CommandDialog>
- )
- }
|