@@ -10,8 +10,8 @@ import type { FeatureArea } from '@/lib/config'
1010import type { GetArea } from '@/lib/const'
1111import type { Query } from '@/lib/data'
1212import { ucFirstStr } from '@/lib/utils'
13+ import { useMemo , useState } from 'react'
1314import { toast } from 'sonner'
14- import { useMapDashboard } from './hooks/useDashboard'
1515
1616function areaToOption < A extends FeatureArea > ( data : GetArea < A > ) : ComboboxOption {
1717 return {
@@ -23,7 +23,7 @@ function areaToOption<A extends FeatureArea>(data: GetArea<A>): ComboboxOption {
2323
2424export type ComboboxAreaProps < A extends FeatureArea > = Omit <
2525 ComboboxProps ,
26- 'autoClose' | 'fullWidth' | ' options' | 'onSelect' | 'selected'
26+ 'options' | 'onSelect' | 'selected'
2727> & {
2828 area : A
2929 query : Query < A >
@@ -36,36 +36,35 @@ export default function ComboboxArea<A extends FeatureArea>({
3636 onSelect,
3737 ...comboboxProps
3838} : ComboboxAreaProps < A > ) {
39- const { selectedArea } = useMapDashboard ( )
39+ const [ selectedArea , setSelectedArea ] = useState < GetArea < A > | undefined > ( )
4040 const { data : areas = [ ] , error } = useArea ( area , {
4141 ...query ,
4242 sortBy : 'name' ,
4343 } )
4444
45+ const options = useMemo ( ( ) => areas . map ( ( a ) => areaToOption < A > ( a ) ) , [ areas ] )
46+
4547 if ( error ) {
4648 toast . error ( `Failed to fetch ${ area } data` , {
4749 description : error ?. message ,
4850 closeButton : true ,
4951 } )
5052 }
5153
52- const areaData = selectedArea [ area ]
53-
5454 return (
5555 < Combobox
5656 { ...comboboxProps }
57- options = { areas . map ( ( a ) => areaToOption < A > ( a ) ) }
57+ options = { options }
5858 label = { comboboxProps . label || ucFirstStr ( area ) }
5959 placeholder = { comboboxProps . placeholder || `Search ${ ucFirstStr ( area ) } ` }
6060 onSelect = { ( opt ) => {
6161 const selectedArea = areas . find ( ( a ) => a . code === opt . key )
6262 if ( selectedArea ) {
63+ setSelectedArea ( selectedArea )
6364 onSelect ?.( selectedArea )
6465 }
6566 } }
66- selected = { areaData ? areaToOption ( areaData ) : undefined }
67- autoClose
68- fullWidth
67+ selected = { selectedArea ? areaToOption ( selectedArea ) : undefined }
6968 />
7069 )
7170}
0 commit comments