|
| 1 | +import { Pagination } from '@rocket.chat/fuselage'; |
| 2 | +import { useDebouncedValue } from '@rocket.chat/fuselage-hooks'; |
| 3 | +import { useSort, Page, PageHeader, PageContent, usePagination, GenericTableLoadingRow } from '@rocket.chat/ui-client'; |
| 4 | +import { useEndpoint, useRouteParameter, useRouter } from '@rocket.chat/ui-contexts'; |
| 5 | +import { MediaCallHistoryTable, isCallHistoryUnknownContact, isCallHistoryTableInternalContact } from '@rocket.chat/ui-voip'; |
| 6 | +import type { CallHistoryTableInternalContact, CallHistoryUnknownContact, CallHistoryTableExternalContact } from '@rocket.chat/ui-voip'; |
| 7 | +import { useQuery } from '@tanstack/react-query'; |
| 8 | +import { useCallback, useMemo, useState } from 'react'; |
| 9 | +import { useTranslation } from 'react-i18next'; |
| 10 | + |
| 11 | +import CallHistoryPageFilters, { useCallHistoryPageFilters } from './CallHistoryPageFilters'; |
| 12 | +import CallHistoryRowExternalUser from './CallHistoryRowExternalUser'; |
| 13 | +import CallHistoryRowInternalUser from './CallHistoryRowInternalUser'; |
| 14 | +import CallHistoryRowUnknownUser from './CallHistoryRowUnknownUser'; |
| 15 | +import MediaCallHistoryContextualbar from './MediaCallHistoryContextualbar'; |
| 16 | +import GenericNoResults from '../../components/GenericNoResults'; |
| 17 | +import UserInfoWithData from '../room/contextualBar/UserInfo/UserInfoWithData'; |
| 18 | + |
| 19 | +const getSort = (sortBy: 'contact' | 'type' | 'status' | 'timestamp', sortDirection: 'asc' | 'desc') => { |
| 20 | + const sortDirectionValue = sortDirection === 'asc' ? 1 : -1; |
| 21 | + switch (sortBy) { |
| 22 | + case 'type': |
| 23 | + return { direction: sortDirectionValue, ts: -1 }; |
| 24 | + case 'status': |
| 25 | + return { state: sortDirectionValue, ts: -1 }; |
| 26 | + case 'timestamp': |
| 27 | + return { ts: sortDirectionValue }; |
| 28 | + case 'contact': |
| 29 | + return { contactName: sortDirectionValue, contactUsername: sortDirectionValue, contactExtension: sortDirectionValue, ts: -1 }; |
| 30 | + default: |
| 31 | + return { ts: -1 }; |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +const getStateFilter = <T extends string[]>(states: T): T | [...T, 'error'] | undefined => { |
| 36 | + if (states.length === 0) { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + if (states.includes('failed')) { |
| 40 | + return [...states, 'error']; |
| 41 | + } |
| 42 | + return states; |
| 43 | +}; |
| 44 | + |
| 45 | +type DetailsTab = { |
| 46 | + openTab: 'details'; |
| 47 | + rid: string; |
| 48 | +}; |
| 49 | + |
| 50 | +type UserInfoTab = { |
| 51 | + openTab: 'user-info'; |
| 52 | + rid: string; |
| 53 | + userId: string; |
| 54 | +}; |
| 55 | + |
| 56 | +type Tab = DetailsTab | UserInfoTab; |
| 57 | + |
| 58 | +const CallHistoryPage = () => { |
| 59 | + const { t } = useTranslation(); |
| 60 | + const [tab, setTab] = useState<Tab | null>(null); |
| 61 | + const sortProps = useSort<'contact' | 'type' | 'status' | 'timestamp'>('timestamp', 'desc'); |
| 62 | + |
| 63 | + const getCallHistory = useEndpoint('GET', '/v1/call-history.list'); |
| 64 | + const { setItemsPerPage, setCurrent, ...paginationProps } = usePagination(); |
| 65 | + |
| 66 | + const router = useRouter(); |
| 67 | + const historyId = useRouteParameter('historyId'); |
| 68 | + |
| 69 | + const filterProps = useCallHistoryPageFilters(); |
| 70 | + |
| 71 | + const { searchText, type, states } = filterProps; |
| 72 | + |
| 73 | + const debouncedSearchText = useDebouncedValue(searchText, 400); |
| 74 | + |
| 75 | + const onClickRow = useCallback( |
| 76 | + (rid: string, _id: string) => { |
| 77 | + router.navigate(`/call-history/details/${_id}`); |
| 78 | + setTab({ openTab: 'details', rid }); |
| 79 | + }, |
| 80 | + [router], |
| 81 | + ); |
| 82 | + |
| 83 | + const openUserInfo = useCallback( |
| 84 | + (userId: string, rid: string) => { |
| 85 | + setTab({ openTab: 'user-info', rid, userId }); |
| 86 | + }, |
| 87 | + [setTab], |
| 88 | + ); |
| 89 | + |
| 90 | + const closeTab = useCallback(() => { |
| 91 | + setTab(null); |
| 92 | + router.navigate('/call-history'); |
| 93 | + }, [router]); |
| 94 | + |
| 95 | + const handleBack = useCallback(() => { |
| 96 | + if (historyId && tab?.rid) { |
| 97 | + onClickRow(tab.rid, historyId); |
| 98 | + return; |
| 99 | + } |
| 100 | + setTab(null); |
| 101 | + }, [setTab, historyId, onClickRow, tab?.rid]); |
| 102 | + |
| 103 | + const { data, isPending, error, refetch } = useQuery({ |
| 104 | + queryKey: [ |
| 105 | + 'call-history', |
| 106 | + 'list', |
| 107 | + sortProps.sortBy, |
| 108 | + sortProps.sortDirection, |
| 109 | + paginationProps.current, |
| 110 | + paginationProps.itemsPerPage, |
| 111 | + type, |
| 112 | + states, |
| 113 | + debouncedSearchText, |
| 114 | + ], |
| 115 | + queryFn: () => { |
| 116 | + const sort = getSort(sortProps.sortBy, sortProps.sortDirection); |
| 117 | + const stateFilter = getStateFilter(states); |
| 118 | + |
| 119 | + return getCallHistory({ |
| 120 | + count: paginationProps.itemsPerPage, |
| 121 | + offset: paginationProps.current, |
| 122 | + sort: JSON.stringify(sort), |
| 123 | + ...(type !== 'all' && { direction: type }), |
| 124 | + ...(stateFilter && { state: stateFilter }), |
| 125 | + ...(debouncedSearchText && { filter: debouncedSearchText }), |
| 126 | + }); |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + const tableData = useMemo(() => { |
| 131 | + return data?.items.map((item) => { |
| 132 | + if (item.external) { |
| 133 | + return { |
| 134 | + _id: item._id, |
| 135 | + contact: item.contactExtension |
| 136 | + ? ({ number: item.contactExtension } as CallHistoryTableExternalContact) |
| 137 | + : ({ unknown: true } as CallHistoryUnknownContact), |
| 138 | + type: item.direction, |
| 139 | + status: item.state, |
| 140 | + timestamp: item.ts, |
| 141 | + duration: item.duration, |
| 142 | + }; |
| 143 | + } |
| 144 | + if (!item.contactUsername || !item.contactName) { |
| 145 | + return { |
| 146 | + _id: item._id, |
| 147 | + contact: { unknown: true } as CallHistoryUnknownContact, |
| 148 | + type: item.direction, |
| 149 | + status: item.state, |
| 150 | + timestamp: item.ts, |
| 151 | + duration: item.duration, |
| 152 | + }; |
| 153 | + } |
| 154 | + return { |
| 155 | + _id: item._id, |
| 156 | + rid: item.rid, |
| 157 | + contact: { _id: item.contactId, username: item.contactUsername, name: item.contactName } as CallHistoryTableInternalContact, |
| 158 | + messageId: item.messageId, |
| 159 | + type: item.direction, |
| 160 | + status: item.state, |
| 161 | + timestamp: item.ts, |
| 162 | + duration: item.duration, |
| 163 | + }; |
| 164 | + }); |
| 165 | + }, [data]); |
| 166 | + |
| 167 | + if (isPending) { |
| 168 | + return ( |
| 169 | + <PageContent> |
| 170 | + <MediaCallHistoryTable sort={sortProps}> |
| 171 | + <GenericTableLoadingRow cols={5} /> |
| 172 | + </MediaCallHistoryTable> |
| 173 | + </PageContent> |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + if (error) { |
| 178 | + return ( |
| 179 | + <Page> |
| 180 | + <PageHeader title={t('Call_history')} /> |
| 181 | + <PageContent> |
| 182 | + <GenericNoResults |
| 183 | + icon='warning' |
| 184 | + title={t('Something_went_wrong')} |
| 185 | + description={t('Please_try_again')} |
| 186 | + buttonTitle={t('Reload_page')} |
| 187 | + buttonAction={() => refetch()} |
| 188 | + /> |
| 189 | + </PageContent> |
| 190 | + </Page> |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + return ( |
| 195 | + <Page flexDirection='row'> |
| 196 | + <Page> |
| 197 | + <PageHeader title={t('Call_history')} /> |
| 198 | + <PageContent> |
| 199 | + <CallHistoryPageFilters {...filterProps} /> |
| 200 | + {!tableData || (tableData.length === 0 && <GenericNoResults />)} |
| 201 | + {tableData && tableData.length > 0 && ( |
| 202 | + <MediaCallHistoryTable sort={sortProps}> |
| 203 | + {tableData.map((item) => { |
| 204 | + if (isCallHistoryUnknownContact(item.contact)) { |
| 205 | + return ( |
| 206 | + <CallHistoryRowUnknownUser key={item._id} {...item} contact={item.contact} onClick={() => onClickRow('', item._id)} /> |
| 207 | + ); |
| 208 | + } |
| 209 | + if (isCallHistoryTableInternalContact(item.contact)) { |
| 210 | + return ( |
| 211 | + <CallHistoryRowInternalUser |
| 212 | + key={item._id} |
| 213 | + {...item} |
| 214 | + contact={item.contact} |
| 215 | + onClick={() => onClickRow(item.rid ?? '', item._id)} |
| 216 | + rid={item.rid ?? ''} |
| 217 | + onClickUserInfo={item.rid ? openUserInfo : undefined} |
| 218 | + /> |
| 219 | + ); |
| 220 | + } |
| 221 | + |
| 222 | + return ( |
| 223 | + <CallHistoryRowExternalUser key={item._id} {...item} contact={item.contact} onClick={() => onClickRow('', item._id)} /> |
| 224 | + ); |
| 225 | + })} |
| 226 | + </MediaCallHistoryTable> |
| 227 | + )} |
| 228 | + <Pagination divider count={data?.total || 0} onSetItemsPerPage={setItemsPerPage} onSetCurrent={setCurrent} {...paginationProps} /> |
| 229 | + </PageContent> |
| 230 | + </Page> |
| 231 | + {tab?.openTab === 'user-info' && ( |
| 232 | + <UserInfoWithData rid={tab.rid} uid={tab.userId} onClose={closeTab} onClickBack={historyId ? handleBack : undefined} /> |
| 233 | + )} |
| 234 | + {tab?.openTab === 'details' && historyId && ( |
| 235 | + <MediaCallHistoryContextualbar historyId={historyId} onClose={closeTab} messageRoomId={tab.rid} openUserInfo={openUserInfo} /> |
| 236 | + )} |
| 237 | + </Page> |
| 238 | + ); |
| 239 | +}; |
| 240 | + |
| 241 | +export default CallHistoryPage; |
0 commit comments