-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcall-sidebar.tsx
More file actions
198 lines (181 loc) · 7.38 KB
/
call-sidebar.tsx
File metadata and controls
198 lines (181 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { useQuery } from '@tanstack/react-query';
import { router } from 'expo-router';
import { Check, CircleX, Eye, MapPin } from 'lucide-react-native';
import { useColorScheme } from 'nativewind';
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import { Alert, Platform, Pressable, ScrollView } from 'react-native';
import { CustomBottomSheet } from '@/components/ui/bottom-sheet';
import { Text } from '@/components/ui/text';
import { VStack } from '@/components/ui/vstack';
import { openMapsWithAddress, openMapsWithDirections } from '@/lib/navigation';
import { useCoreStore } from '@/stores/app/core-store';
import { useCallsStore } from '@/stores/calls/store';
import { CallCard } from '../calls/call-card';
import { Button, ButtonIcon } from '../ui/button';
import { Card } from '../ui/card';
import { HStack } from '../ui/hstack';
export const SidebarCallCard = () => {
const { colorScheme } = useColorScheme();
const activeCall = useCoreStore((state) => state.activeCall);
const activePriority = useCoreStore((state) => state.activePriority);
const setActiveCall = useCoreStore((state) => state.setActiveCall);
const [isBottomSheetOpen, setIsBottomSheetOpen] = React.useState(false);
const { t } = useTranslation();
// Fetch calls data when bottom sheet opens
const { data: openCallsData, isLoading } = useQuery({
queryKey: ['calls', 'open'],
queryFn: async () => {
// Only fetch when bottom sheet is open
if (!isBottomSheetOpen) return [];
await useCallsStore.getState().fetchCalls();
return useCallsStore.getState().calls;
},
enabled: isBottomSheetOpen, // Only run query when bottom sheet is open
});
const handleDeselect = () => {
if (Platform.OS === 'web') {
const confirmed = window.confirm(`${t('calls.confirm_deselect_title')}\n${t('calls.confirm_deselect_message')}`);
if (confirmed) {
setActiveCall(null);
}
return;
}
Alert.alert(
t('calls.confirm_deselect_title'),
t('calls.confirm_deselect_message'),
[
{
text: t('common.cancel'),
style: 'cancel',
},
{
text: t('common.confirm'),
onPress: () => setActiveCall(null),
style: 'destructive',
},
],
{ cancelable: true }
);
};
// Check if location data exists (either coordinates or address)
const hasLocationData = (call: typeof activeCall) => {
if (!call) return false;
const hasCoordinates = call.Latitude && call.Longitude;
const hasAddress = call.Address && call.Address.trim() !== '';
return hasCoordinates || hasAddress;
};
const showLocationAlert = () => {
if (Platform.OS === 'web') {
window.alert(`${t('calls.no_location_title')}\n${t('calls.no_location_message')}`);
} else {
Alert.alert(t('calls.no_location_title'), t('calls.no_location_message'), [{ text: t('common.ok') }]);
}
};
const handleDirections = async () => {
if (!activeCall) return;
const latitude = activeCall.Latitude;
const longitude = activeCall.Longitude;
const address = activeCall.Address;
// Check if we have coordinates
if (latitude && longitude) {
try {
await openMapsWithDirections(latitude, longitude, address);
} catch {
showLocationAlert();
}
} else if (address && address.trim() !== '') {
// Fall back to address if no coordinates
try {
await openMapsWithAddress(address);
} catch {
showLocationAlert();
}
} else {
// No location data available
showLocationAlert();
}
};
return (
<>
<Pressable onPress={() => setIsBottomSheetOpen(true)} className="w-full" testID="call-selection-trigger">
{activeCall && activePriority ? (
<CallCard call={activeCall} priority={activePriority} />
) : (
<Card className="w-full bg-background-50">
<Text className="font-medium">{t('calls.no_call_selected')}</Text>
<Text className="text-sm text-gray-500">{t('calls.no_call_selected_info')}</Text>
</Card>
)}
</Pressable>
{activeCall && (
<HStack className="w-full">
<Button
variant="outline"
className="flex-1"
size="sm"
action="primary"
onPress={() => {
router.push(`/call/${activeCall.CallId}`);
}}
>
<ButtonIcon as={Eye} />
</Button>
{hasLocationData(activeCall) && (
<Button variant="outline" className="flex-1" size="sm" action="primary" onPress={handleDirections}>
<ButtonIcon as={MapPin} />
</Button>
)}
<Button variant="outline" className="flex-1" size="sm" action="primary" onPress={handleDeselect}>
<ButtonIcon as={CircleX} />
</Button>
</HStack>
)}
<CustomBottomSheet isOpen={isBottomSheetOpen} onClose={() => setIsBottomSheetOpen(false)} isLoading={isLoading} loadingText={t('common.loading')} snapPoints={[60]} testID="call-selection-bottom-sheet">
<VStack space="md" className="w-full flex-1">
<Text className="text-lg font-bold">{t('calls.select_active_call')}</Text>
<ScrollView className="w-full flex-1" showsVerticalScrollIndicator={false}>
<VStack space="md" className="w-full">
{openCallsData?.map((call) => (
<Pressable
key={call.CallId}
onPress={() => {
const handleCallSelect = async () => {
try {
await setActiveCall(call.CallId);
setIsBottomSheetOpen(false);
} catch (error) {
console.error('Failed to set active call:', error);
}
};
handleCallSelect().catch((error) => {
console.error('Failed to handle call selection:', error);
});
}}
className={`rounded-lg border p-4 ${colorScheme === 'dark' ? 'border-neutral-800 bg-neutral-800' : 'border-neutral-200 bg-neutral-50'} ${activeCall?.CallId === call.CallId ? (colorScheme === 'dark' ? 'bg-primary-900' : 'bg-primary-50') : ''
}`}
testID={`call-item-${call.CallId}`}
>
<HStack space="md" className="items-center justify-between">
<VStack className="flex-1">
<Text className={`font-medium ${colorScheme === 'dark' ? 'text-neutral-200' : 'text-neutral-700'}`}>{call.Name}</Text>
<Text size="sm" className={colorScheme === 'dark' ? 'text-neutral-400' : 'text-neutral-500'}>
{call.Type}
</Text>
</VStack>
{activeCall?.CallId === call.CallId && <Check size={20} color={colorScheme === 'dark' ? '#60a5fa' : '#2563eb'} />}
</HStack>
</Pressable>
))}
{!isLoading && openCallsData?.length === 0 && (
<Text className="py-8 text-center text-gray-500" testID="no-calls-message">
{t('calls.no_open_calls')}
</Text>
)}
</VStack>
</ScrollView>
</VStack>
</CustomBottomSheet>
</>
);
};