|
| 1 | +import { Box, Chip, Divider, Typography } from '@mui/material' |
| 2 | +import { Description, Opacity, Person } from '@mui/icons-material' |
| 3 | +import { GroupType } from '@/constants' |
| 4 | +import { ContactResult, SearchResult, WellResult } from '@/interfaces/ocotillo' |
| 5 | +import { highlight } from '@/utils' |
| 6 | + |
| 7 | +const TypeIcon = ({ group }: { group: GroupType }) => { |
| 8 | + const sx = { fontSize: 18, color: 'text.secondary', flexShrink: 0, mt: '2px' } |
| 9 | + |
| 10 | + switch (group) { |
| 11 | + case GroupType.Wells: |
| 12 | + case GroupType.Springs: |
| 13 | + return <Opacity sx={sx} /> |
| 14 | + case GroupType.Contacts: |
| 15 | + return <Person sx={sx} /> |
| 16 | + case GroupType.Assets: |
| 17 | + return <Description sx={sx} /> |
| 18 | + default: |
| 19 | + return null |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +const buildSubtitle = (option: SearchResult): string | null => { |
| 24 | + if (option.group === GroupType.Wells || option.group === GroupType.Springs) { |
| 25 | + const properties = (option as WellResult).properties |
| 26 | + const parts: string[] = [] |
| 27 | + |
| 28 | + if (properties.owner_name) parts.push(`Owner: ${properties.owner_name}`) |
| 29 | + if (properties.county) parts.push(properties.county) |
| 30 | + if (properties.site_name) parts.push(properties.site_name) |
| 31 | + if (properties.thing_type) parts.push(properties.thing_type) |
| 32 | + if (properties.well_depth) |
| 33 | + parts.push(`${properties.well_depth.toFixed(0)} ft`) |
| 34 | + if (properties.hole_depth) { |
| 35 | + parts.push(`hole ${properties.hole_depth.toFixed(0)} ft`) |
| 36 | + } |
| 37 | + if (properties.well_purposes?.length) |
| 38 | + parts.push(...properties.well_purposes) |
| 39 | + |
| 40 | + return parts.length ? parts.join(' · ') : null |
| 41 | + } |
| 42 | + |
| 43 | + if (option.group === GroupType.Contacts) { |
| 44 | + const properties = (option as ContactResult).properties |
| 45 | + const parts: string[] = [] |
| 46 | + |
| 47 | + if (properties.phone?.length) parts.push(properties.phone[0]) |
| 48 | + if (properties.address?.length) parts.push(properties.address[0]) |
| 49 | + |
| 50 | + return parts.length ? parts.join(' · ') : null |
| 51 | + } |
| 52 | + |
| 53 | + return null |
| 54 | +} |
| 55 | + |
| 56 | +const RelatedThings = ({ |
| 57 | + things, |
| 58 | + query, |
| 59 | +}: { |
| 60 | + things: { id: number; label: string; thing_type: string }[] |
| 61 | + query: string |
| 62 | +}) => { |
| 63 | + if (!things?.length) return null |
| 64 | + |
| 65 | + return ( |
| 66 | + <Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5, mt: 0.5 }}> |
| 67 | + {things.slice(0, 4).map((thing) => ( |
| 68 | + <Chip |
| 69 | + key={thing.id} |
| 70 | + size="small" |
| 71 | + icon={<Opacity sx={{ fontSize: '12px !important' }} />} |
| 72 | + label={highlight(thing.label, query)} |
| 73 | + variant="outlined" |
| 74 | + sx={{ fontSize: 11 }} |
| 75 | + /> |
| 76 | + ))} |
| 77 | + {things.length > 4 && ( |
| 78 | + <Chip |
| 79 | + size="small" |
| 80 | + label={`+${things.length - 4} more`} |
| 81 | + variant="outlined" |
| 82 | + sx={{ fontSize: 11 }} |
| 83 | + /> |
| 84 | + )} |
| 85 | + </Box> |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +const ResultRow = ({ |
| 90 | + option, |
| 91 | + query, |
| 92 | + onClick, |
| 93 | +}: { |
| 94 | + option: SearchResult |
| 95 | + query: string |
| 96 | + onClick: () => void |
| 97 | +}) => { |
| 98 | + const subtitle = buildSubtitle(option) |
| 99 | + const relatedThings = |
| 100 | + option.group === GroupType.Contacts |
| 101 | + ? (option as ContactResult).properties.things |
| 102 | + : option.group === GroupType.Assets |
| 103 | + ? (option as any).properties?.things |
| 104 | + : null |
| 105 | + |
| 106 | + return ( |
| 107 | + <Box |
| 108 | + onClick={onClick} |
| 109 | + sx={{ |
| 110 | + display: 'flex', |
| 111 | + alignItems: 'flex-start', |
| 112 | + gap: 1.5, |
| 113 | + px: 1.5, |
| 114 | + py: 1, |
| 115 | + borderRadius: 1, |
| 116 | + cursor: 'pointer', |
| 117 | + '&:hover': { bgcolor: 'action.hover' }, |
| 118 | + }} |
| 119 | + > |
| 120 | + <TypeIcon group={option.group} /> |
| 121 | + <Box sx={{ minWidth: 0, flex: 1 }}> |
| 122 | + <Typography variant="body2" fontWeight={600} sx={{ lineHeight: 1.4 }}> |
| 123 | + {highlight(option.label, query)} |
| 124 | + </Typography> |
| 125 | + {subtitle && ( |
| 126 | + <Typography |
| 127 | + variant="caption" |
| 128 | + color="text.secondary" |
| 129 | + sx={{ lineHeight: 1.4, display: 'block' }} |
| 130 | + > |
| 131 | + {subtitle} |
| 132 | + </Typography> |
| 133 | + )} |
| 134 | + {relatedThings && ( |
| 135 | + <RelatedThings things={relatedThings} query={query} /> |
| 136 | + )} |
| 137 | + </Box> |
| 138 | + </Box> |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +type DefaultResultsProps = { |
| 143 | + grouped: Map<GroupType, SearchResult[]> |
| 144 | + query: string |
| 145 | + onSelect: (result: SearchResult) => void |
| 146 | +} |
| 147 | + |
| 148 | +export const DefaultResults = ({ |
| 149 | + grouped, |
| 150 | + query, |
| 151 | + onSelect, |
| 152 | +}: DefaultResultsProps) => ( |
| 153 | + <Box sx={{ py: 0.5 }}> |
| 154 | + {Array.from(grouped.entries()).map(([group, items], groupIndex) => ( |
| 155 | + <Box key={group}> |
| 156 | + {groupIndex > 0 && <Divider sx={{ my: 0.5 }} />} |
| 157 | + {items.map((option, index) => ( |
| 158 | + <ResultRow |
| 159 | + key={`${option.group}-${(option as any).properties?.id ?? index}`} |
| 160 | + option={option} |
| 161 | + query={query} |
| 162 | + onClick={() => onSelect(option)} |
| 163 | + /> |
| 164 | + ))} |
| 165 | + </Box> |
| 166 | + ))} |
| 167 | + </Box> |
| 168 | +) |
0 commit comments