|
| 1 | +import { Divider, H4, H5, Icon } from '@blueprintjs/core' |
| 2 | + |
| 3 | +import clsx from 'clsx' |
| 4 | +import { languageAtom, useTranslation } from 'i18n/i18n' |
| 5 | +import { useAtomValue } from 'jotai' |
| 6 | +import { |
| 7 | + FC, |
| 8 | + HTMLAttributes, |
| 9 | + ImgHTMLAttributes, |
| 10 | + useEffect, |
| 11 | + useMemo, |
| 12 | + useRef, |
| 13 | + useState, |
| 14 | +} from 'react' |
| 15 | + |
| 16 | +import { |
| 17 | + DEFAULTPROFID, |
| 18 | + DEFAULTSUBPROFID, |
| 19 | + useOperatorFilterProvider, |
| 20 | +} from 'components/editor/operator/sheet/sheetOperator/SheetOperatorFilterProvider' |
| 21 | +import { PROFESSIONS } from 'models/operator' |
| 22 | + |
| 23 | +export interface ProfClassification {} |
| 24 | + |
| 25 | +export const ProfClassification: FC<ProfClassification> = () => { |
| 26 | + const t = useTranslation() |
| 27 | + const language = useAtomValue(languageAtom) |
| 28 | + const { |
| 29 | + useProfFilterState: [{ selectedProf }, setProfFilter], |
| 30 | + } = useOperatorFilterProvider() |
| 31 | + |
| 32 | + const formattedProfessions = useMemo( |
| 33 | + () => [ |
| 34 | + { |
| 35 | + id: DEFAULTPROFID.ALL, |
| 36 | + name: t.components.editor.operator.sheet.sheetOperator |
| 37 | + .ProfClassificationWithFilters.all, |
| 38 | + name_en: |
| 39 | + t.components.editor.operator.sheet.sheetOperator |
| 40 | + .ProfClassificationWithFilters.all, |
| 41 | + sub: [], |
| 42 | + }, |
| 43 | + { |
| 44 | + id: DEFAULTPROFID.FAV, |
| 45 | + name: t.components.editor.operator.sheet.sheetOperator |
| 46 | + .ProfClassificationWithFilters.favorites, |
| 47 | + name_en: |
| 48 | + t.components.editor.operator.sheet.sheetOperator |
| 49 | + .ProfClassificationWithFilters.favorites, |
| 50 | + sub: [], |
| 51 | + }, |
| 52 | + ...PROFESSIONS.map((profession) => ({ |
| 53 | + ...profession, |
| 54 | + name_en: profession.name_en || profession.name, |
| 55 | + })), |
| 56 | + { |
| 57 | + id: DEFAULTPROFID.OTHERS, |
| 58 | + name: t.components.editor.operator.sheet.sheetOperator |
| 59 | + .ProfClassificationWithFilters.others, |
| 60 | + name_en: |
| 61 | + t.components.editor.operator.sheet.sheetOperator |
| 62 | + .ProfClassificationWithFilters.others, |
| 63 | + sub: [], |
| 64 | + }, |
| 65 | + ], |
| 66 | + [t], |
| 67 | + ) |
| 68 | + const subProfs = useMemo(() => { |
| 69 | + return [ |
| 70 | + { |
| 71 | + id: DEFAULTSUBPROFID.ALL, |
| 72 | + name: t.components.editor.operator.sheet.sheetOperator |
| 73 | + .ProfClassificationWithFilters.all, |
| 74 | + name_en: |
| 75 | + t.components.editor.operator.sheet.sheetOperator |
| 76 | + .ProfClassificationWithFilters.all, |
| 77 | + }, |
| 78 | + { |
| 79 | + id: DEFAULTSUBPROFID.SELECTED, |
| 80 | + name: t.components.editor.operator.sheet.sheetOperator |
| 81 | + .ProfClassificationWithFilters.selected, |
| 82 | + name_en: |
| 83 | + t.components.editor.operator.sheet.sheetOperator |
| 84 | + .ProfClassificationWithFilters.selected, |
| 85 | + }, |
| 86 | + ...(formattedProfessions.find(({ id }) => id === selectedProf[0])?.sub || |
| 87 | + []), |
| 88 | + ] |
| 89 | + }, [selectedProf, formattedProfessions, t]) |
| 90 | + |
| 91 | + return ( |
| 92 | + <div> |
| 93 | + <UlWithArrow className="px-4 py-2 gap-4 " key={selectedProf[0]}> |
| 94 | + {subProfs.map((subProf) => ( |
| 95 | + <li key={subProf.id} className="flex items-center justify-center"> |
| 96 | + <H4 |
| 97 | + className={clsx( |
| 98 | + 'truncate cursor-pointer opacity-50 hover:underline hover:opacity-90 m-0', |
| 99 | + selectedProf.includes(subProf.id) && '!opacity-100 underline', |
| 100 | + )} |
| 101 | + onClick={() => |
| 102 | + setProfFilter(({ selectedProf, ...rest }) => ({ |
| 103 | + selectedProf: [selectedProf[0], subProf.id], |
| 104 | + ...rest, |
| 105 | + })) |
| 106 | + } |
| 107 | + > |
| 108 | + {language === 'en' && subProf.name_en |
| 109 | + ? subProf.name_en |
| 110 | + : subProf.name} |
| 111 | + </H4> |
| 112 | + </li> |
| 113 | + ))} |
| 114 | + </UlWithArrow> |
| 115 | + <Divider className="m-0" /> |
| 116 | + <UlWithArrow className="py-1"> |
| 117 | + {formattedProfessions.map((prof) => ( |
| 118 | + <ProfIcon |
| 119 | + key={prof.id} |
| 120 | + profId={prof.id} |
| 121 | + name={language === 'en' && prof.name_en ? prof.name_en : prof.name} |
| 122 | + selected={selectedProf.includes(prof.id)} |
| 123 | + onProfClick={() => |
| 124 | + setProfFilter((prev) => ({ |
| 125 | + ...prev, |
| 126 | + selectedProf: [prof.id, DEFAULTSUBPROFID.ALL], |
| 127 | + })) |
| 128 | + } |
| 129 | + /> |
| 130 | + ))} |
| 131 | + </UlWithArrow> |
| 132 | + </div> |
| 133 | + ) |
| 134 | +} |
| 135 | + |
| 136 | +interface ProfIconProp extends ImgHTMLAttributes<HTMLImageElement> { |
| 137 | + name: string |
| 138 | + profId: string |
| 139 | + selected: boolean |
| 140 | + onProfClick: () => void |
| 141 | +} |
| 142 | + |
| 143 | +const ProfIcon: FC<ProfIconProp> = ({ |
| 144 | + name, |
| 145 | + profId, |
| 146 | + selected, |
| 147 | + onProfClick, |
| 148 | + ...restImgProps |
| 149 | +}) => { |
| 150 | + return ( |
| 151 | + <li |
| 152 | + className="grow cursor-pointer relative flex justify-center items-center p-2 min-w-[48px]" |
| 153 | + title={name} |
| 154 | + role="presentation" |
| 155 | + onClick={onProfClick} |
| 156 | + > |
| 157 | + {selected && ( |
| 158 | + <div className="w-full h-1 bg-black dark:bg-white absolute bottom-full left-0 rounded" /> |
| 159 | + )} |
| 160 | + {(Object.values(DEFAULTPROFID) as string[]).includes(profId) ? ( |
| 161 | + <H5 className="!text-xs sm:!text-base truncate m-0">{name}</H5> |
| 162 | + ) : ( |
| 163 | + <img |
| 164 | + {...restImgProps} |
| 165 | + className="invert dark:invert-0" |
| 166 | + src={'/assets/prof-icons/' + profId + '.png'} |
| 167 | + alt="" |
| 168 | + title={name} |
| 169 | + /> |
| 170 | + )} |
| 171 | + </li> |
| 172 | + ) |
| 173 | +} |
| 174 | + |
| 175 | +interface UlWithArrowProp extends HTMLAttributes<HTMLUListElement> {} |
| 176 | + |
| 177 | +const UlWithArrow: FC<UlWithArrowProp> = ({ className, ...ulProps }) => { |
| 178 | + const containerRef = useRef<HTMLUListElement>(null) |
| 179 | + const [showLeft, setShowLeft] = useState(false) |
| 180 | + const [showRight, setShowRight] = useState(false) |
| 181 | + |
| 182 | + const isOverflow = useMemo(() => showLeft || showRight, [showLeft, showRight]) |
| 183 | + |
| 184 | + useEffect(() => { |
| 185 | + const checkScroll = () => { |
| 186 | + const el = containerRef.current |
| 187 | + if (!el) return |
| 188 | + const { scrollLeft, scrollWidth, clientWidth } = el |
| 189 | + |
| 190 | + setShowLeft(scrollLeft > 0) |
| 191 | + setShowRight(scrollLeft + clientWidth < scrollWidth - 1) |
| 192 | + } |
| 193 | + |
| 194 | + const el = containerRef.current |
| 195 | + if (!el) return |
| 196 | + |
| 197 | + checkScroll() |
| 198 | + el.addEventListener('scroll', checkScroll) |
| 199 | + |
| 200 | + const resizeObserver = new ResizeObserver(checkScroll) |
| 201 | + resizeObserver.observe(el) |
| 202 | + resizeObserver.observe(el.firstElementChild as Element) |
| 203 | + |
| 204 | + return () => { |
| 205 | + el.removeEventListener('scroll', checkScroll) |
| 206 | + resizeObserver.disconnect() |
| 207 | + } |
| 208 | + }, []) |
| 209 | + |
| 210 | + return ( |
| 211 | + <div className="flex items-center relative w-full"> |
| 212 | + {showLeft && ( |
| 213 | + <Icon |
| 214 | + icon="chevron-left" |
| 215 | + className="absolute left-0 top-1/2 -translate-y-1/2 invert z-10" |
| 216 | + /> |
| 217 | + )} |
| 218 | + <ul |
| 219 | + {...ulProps} |
| 220 | + ref={containerRef} |
| 221 | + className={clsx( |
| 222 | + 'flex overflow-auto items-center w-full', |
| 223 | + !isOverflow && 'justify-center', |
| 224 | + className, |
| 225 | + )} |
| 226 | + /> |
| 227 | + {showRight && ( |
| 228 | + <Icon |
| 229 | + icon="chevron-right" |
| 230 | + className="absolute right-0 top-1/2 -translate-y-1/2 invert z-10" |
| 231 | + /> |
| 232 | + )} |
| 233 | + </div> |
| 234 | + ) |
| 235 | +} |
0 commit comments