82 lines
2.6 KiB
TypeScript
82 lines
2.6 KiB
TypeScript
import React, { useMemo } from 'react';
|
|
import { View, Animated } from 'react-native';
|
|
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
|
import { MyText, MyTouchableOpacity, BottomDropdown, tw } from 'common-ui';
|
|
import { trpc } from '../src/trpc-client';
|
|
import { useRegion } from '../src/contexts/RegionContext';
|
|
import { useRegionSwitchContext } from '../src/hooks/useRegionSwitch';
|
|
|
|
interface RegionBarProps {
|
|
testID?: string;
|
|
/** When provided, the bar collapses as this scroll offset grows. */
|
|
scrollY?: Animated.Value;
|
|
}
|
|
|
|
const BAR_HEIGHT = 34;
|
|
|
|
const RegionBar: React.FC<RegionBarProps> = ({ testID = 'region-bar', scrollY }) => {
|
|
const { regionId, regionName } = useRegion();
|
|
const { switchRegion } = useRegionSwitchContext();
|
|
const { data: regionsData } = trpc.common.getRegions.useQuery(undefined, { staleTime: Infinity });
|
|
const regionOptions = useMemo(
|
|
() => (regionsData?.regions ?? []).map((r) => ({ label: r.name, value: r.id })),
|
|
[regionsData]
|
|
);
|
|
|
|
const bar = (
|
|
<View style={tw`px-4 h-[34px] justify-center`}>
|
|
<BottomDropdown
|
|
label="Region"
|
|
testID={`${testID}-dropdown`}
|
|
options={regionOptions}
|
|
value={regionId ?? ''}
|
|
placeholder={regionName ?? 'Detecting region…'}
|
|
onValueChange={(value) => {
|
|
const next = regionOptions.find((o) => o.value === value);
|
|
if (next && typeof next.value === 'number') {
|
|
void switchRegion(next.value, next.label);
|
|
}
|
|
}}
|
|
triggerComponent={({ onPress, displayText }) => (
|
|
<MyTouchableOpacity onPress={onPress} style={tw`flex-row items-center`}>
|
|
<MaterialIcons name="place" size={16} color="#6B7280" />
|
|
<MyText style={tw`text-gray-600 text-sm font-medium ml-1`} numberOfLines={1}>
|
|
{displayText}
|
|
</MyText>
|
|
<MaterialIcons name="keyboard-arrow-down" size={16} color="#6B7280" />
|
|
</MyTouchableOpacity>
|
|
)}
|
|
/>
|
|
</View>
|
|
);
|
|
|
|
if (!scrollY) {
|
|
return (
|
|
<View testID={testID} style={{ height: BAR_HEIGHT, backgroundColor: '#FFFFFF' }}>
|
|
{bar}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const height = scrollY.interpolate({
|
|
inputRange: [0, 48],
|
|
outputRange: [BAR_HEIGHT, 0],
|
|
extrapolate: 'clamp',
|
|
});
|
|
const opacity = scrollY.interpolate({
|
|
inputRange: [0, 32],
|
|
outputRange: [1, 0],
|
|
extrapolate: 'clamp',
|
|
});
|
|
|
|
return (
|
|
<Animated.View
|
|
testID={testID}
|
|
style={[{ height, opacity, overflow: 'hidden', backgroundColor: '#FFFFFF' }]}
|
|
>
|
|
{bar}
|
|
</Animated.View>
|
|
);
|
|
};
|
|
|
|
export default RegionBar;
|