11import { format } from 'date-fns' ;
22import { Stack , useLocalSearchParams , useRouter } from 'expo-router' ;
3- import { ClockIcon , FileTextIcon , ImageIcon , InfoIcon , PaperclipIcon , RouteIcon , UserIcon , UsersIcon } from 'lucide-react-native' ;
3+ import { ClockIcon , FileTextIcon , ImageIcon , InfoIcon , LoaderIcon , PaperclipIcon , RouteIcon , UserIcon , UsersIcon } from 'lucide-react-native' ;
44import { useColorScheme } from 'nativewind' ;
55import React , { useEffect , useState } from 'react' ;
66import { useTranslation } from 'react-i18next' ;
@@ -22,15 +22,18 @@ import { VStack } from '@/components/ui/vstack';
2222import { useAnalytics } from '@/hooks/use-analytics' ;
2323import { logger } from '@/lib/logging' ;
2424import { openMapsWithDirections } from '@/lib/navigation' ;
25+ import { useCoreStore } from '@/stores/app/core-store' ;
2526import { useLocationStore } from '@/stores/app/location-store' ;
2627import { useCallDetailStore } from '@/stores/calls/detail-store' ;
28+ import { useStatusBottomSheetStore } from '@/stores/status/store' ;
2729import { useToastStore } from '@/stores/toast/store' ;
2830
2931import { useCallDetailMenu } from '../../components/calls/call-detail-menu' ;
3032import CallFilesModal from '../../components/calls/call-files-modal' ;
3133import CallImagesModal from '../../components/calls/call-images-modal' ;
3234import CallNotesModal from '../../components/calls/call-notes-modal' ;
3335import { CloseCallBottomSheet } from '../../components/calls/close-call-bottom-sheet' ;
36+ import { StatusBottomSheet } from '../../components/status/status-bottom-sheet' ;
3437
3538export default function CallDetail ( ) {
3639 const { id } = useLocalSearchParams ( ) ;
@@ -48,10 +51,13 @@ export default function CallDetail() {
4851 longitude : null ,
4952 } ) ;
5053 const { call, callExtraData, callPriority, isLoading, error, fetchCallDetail, reset } = useCallDetailStore ( ) ;
54+ const { activeCall, activeStatuses } = useCoreStore ( ) ;
55+ const { setIsOpen : setStatusBottomSheetOpen , setSelectedCall } = useStatusBottomSheetStore ( ) ;
5156 const [ isNotesModalOpen , setIsNotesModalOpen ] = useState ( false ) ;
5257 const [ isImagesModalOpen , setIsImagesModalOpen ] = useState ( false ) ;
5358 const [ isFilesModalOpen , setIsFilesModalOpen ] = useState ( false ) ;
5459 const [ isCloseCallModalOpen , setIsCloseCallModalOpen ] = useState ( false ) ;
60+ const [ isSettingActive , setIsSettingActive ] = useState ( false ) ;
5561 const showToast = useToastStore ( ( state ) => state . showToast ) ;
5662
5763 const { colorScheme } = useColorScheme ( ) ;
@@ -88,6 +94,37 @@ export default function CallDetail() {
8894 setIsCloseCallModalOpen ( true ) ;
8995 } ;
9096
97+ const handleSetActive = async ( ) => {
98+ if ( ! call ) return ;
99+
100+ setIsSettingActive ( true ) ;
101+
102+ try {
103+ // Set this call as the active call in the core store
104+ await useCoreStore . getState ( ) . setActiveCall ( call . CallId ) ;
105+
106+ // Find a "Responding" or "En Route" status from the available statuses
107+ const respondingStatus = activeStatuses ?. Statuses . find (
108+ ( status ) => status . Text . toLowerCase ( ) . includes ( 'responding' ) || status . Text . toLowerCase ( ) . includes ( 'en route' ) || status . Text . toLowerCase ( ) . includes ( 'enroute' )
109+ ) ;
110+
111+ // Pre-select the current call and open the status bottom sheet
112+ setSelectedCall ( call ) ;
113+ setStatusBottomSheetOpen ( true , respondingStatus || activeStatuses ?. Statuses [ 0 ] ) ;
114+
115+ // Show success message
116+ showToast ( 'success' , t ( 'call_detail.set_active_success' ) ) ;
117+ } catch ( error ) {
118+ logger . error ( {
119+ message : 'Failed to set call as active' ,
120+ context : { error, callId : call . CallId } ,
121+ } ) ;
122+ showToast ( 'error' , t ( 'call_detail.set_active_error' ) ) ;
123+ } finally {
124+ setIsSettingActive ( false ) ;
125+ }
126+ } ;
127+
91128 // Initialize the call detail menu hook
92129 const { HeaderRightMenu, CallDetailActionSheet } = useCallDetailMenu ( {
93130 onEditCall : handleEditCall ,
@@ -446,10 +483,17 @@ export default function CallDetail() {
446483 < ScrollView className = { `size-full w-full flex-1 ${ colorScheme === 'dark' ? 'bg-neutral-950' : 'bg-neutral-50' } ` } >
447484 { /* Header */ }
448485 < Box className = { `p-4 shadow-sm ${ colorScheme === 'dark' ? 'bg-neutral-900' : 'bg-neutral-100' } ` } >
449- < HStack className = "mb-2 items-center" >
486+ < HStack className = "mb-2 items-center justify-between " >
450487 < Heading size = "md" >
451488 { call . Name } ({ call . Number } )
452489 </ Heading >
490+ { /* Show "Set Active" button if this call is not the active call */ }
491+ { activeCall ?. CallId !== call . CallId && (
492+ < Button variant = "solid" size = "sm" onPress = { handleSetActive } disabled = { isSettingActive } className = { `${ isSettingActive ? 'bg-primary-400 opacity-80' : 'bg-primary-500' } shadow-lg` } >
493+ { isSettingActive && < ButtonIcon as = { LoaderIcon } className = "mr-1 animate-spin text-white" /> }
494+ < ButtonText className = "font-medium text-white" > { isSettingActive ? t ( 'call_detail.setting_active' ) : t ( 'call_detail.set_active' ) } </ ButtonText >
495+ </ Button >
496+ ) }
453497 </ HStack >
454498 < VStack className = "space-y-1" >
455499 < Box style = { { height : 80 } } >
@@ -548,6 +592,9 @@ export default function CallDetail() {
548592 { /* Close Call Bottom Sheet */ }
549593 < CloseCallBottomSheet isOpen = { isCloseCallModalOpen } onClose = { ( ) => setIsCloseCallModalOpen ( false ) } callId = { callId } />
550594
595+ { /* Status Bottom Sheet */ }
596+ < StatusBottomSheet />
597+
551598 { /* Call Detail Menu ActionSheet */ }
552599 < CallDetailActionSheet />
553600 </ >
0 commit comments