57 lines
2 KiB
TypeScript
57 lines
2 KiB
TypeScript
import React from 'react'
|
|
import { MapPin, ChevronDown } from 'lucide-react'
|
|
import { trpc } from '../lib/trpc-client'
|
|
import { useRegion } from '../contexts/RegionContext'
|
|
import { useRegionSwitchContext } from '../hooks/useRegionSwitch'
|
|
|
|
interface RegionBarProps {
|
|
testID?: string
|
|
/** Current vertical scroll offset — the bar collapses as it grows (matches native scrollY). */
|
|
scrollTop?: number
|
|
}
|
|
|
|
const BAR_HEIGHT = 34
|
|
|
|
export const RegionBar: React.FC<RegionBarProps> = ({ testID = 'region-bar', scrollTop = 0 }) => {
|
|
const { regionId, regionName } = useRegion()
|
|
const { switchRegion } = useRegionSwitchContext()
|
|
const { data: regionsData } = trpc.common.getRegions.useQuery(undefined, { staleTime: Infinity })
|
|
const regions = regionsData?.regions ?? []
|
|
|
|
const height = Math.max(0, BAR_HEIGHT - (scrollTop / 48) * BAR_HEIGHT)
|
|
const opacity = Math.max(0, 1 - scrollTop / 32)
|
|
|
|
return (
|
|
<div
|
|
data-testid={testID}
|
|
className="overflow-hidden bg-white"
|
|
style={{ height, opacity }}
|
|
>
|
|
<div className="flex h-[34px] items-center px-4">
|
|
<MapPin className="h-4 w-4 shrink-0 text-gray-500" />
|
|
<select
|
|
data-testid={`${testID}-dropdown`}
|
|
aria-label="Region"
|
|
value={regionId ?? ''}
|
|
onChange={(e) => {
|
|
const next = regions.find((r: any) => String(r.id) === e.target.value)
|
|
if (next) void switchRegion(next.id, next.name)
|
|
}}
|
|
className="ml-1 min-w-0 flex-1 cursor-pointer appearance-none truncate bg-transparent text-sm font-medium text-gray-600 focus:outline-none"
|
|
>
|
|
<option value="" disabled>
|
|
{regionName ?? 'Detecting region…'}
|
|
</option>
|
|
{regions.map((r: any) => (
|
|
<option key={r.id} value={r.id}>
|
|
{r.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
<ChevronDown className="h-4 w-4 shrink-0 text-gray-500" />
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default RegionBar
|