import { Button, ButtonGroup, Icon, Skeleton, Box, Accordion, Field, FieldGroup, Pagination } from '@rocket.chat/fuselage'; import { useMutableCallback } from '@rocket.chat/fuselage-hooks'; import React, { useMemo, useCallback, useState, useEffect } from 'react'; import Page from '../../../../components/Page'; import { useTranslation } from '../../../../contexts/TranslationContext'; import { useHighlightedCode } from '../../../../hooks/useHighlightedCode'; import { integrations as eventList } from '../../../../../app/integrations/lib/rocketchat'; import { useMethod } from '../../../../contexts/ServerContext'; import { useRoute, useRouteParameter } from '../../../../contexts/RouterContext'; import { useFormatDateAndTime } from '../../../../hooks/useFormatDateAndTime'; import { useToastMessageDispatch } from '../../../../contexts/ToastMessagesContext'; import { integrationHistoryStreamer } from '../../../../../app/integrations/client/streamer'; import { useEndpointData } from '../../../../hooks/useEndpointData'; import { AsyncStatePhase } from '../../../../hooks/useAsyncState'; function HistoryItem({ data, ...props }) { const t = useTranslation(); const replayOutgoingIntegration = useMethod('replayOutgoingIntegration'); const { _id, _createdAt, _updatedAt, httpResult, event, step, httpCallData, data: dataSentToTrigger, prepareSentMessage, processSentMessage, url, httpError, errorStack, error, integration: { _id: integrationId }, } = data; const createdAt = typeof _createdAt === 'string' ? _createdAt : _createdAt.toISOString(); const updatedAt = typeof _updatedAt === 'string' ? _updatedAt : _updatedAt.toISOString(); const handleClickReplay = useMutableCallback((e) => { e.stopPropagation(); replayOutgoingIntegration({ integrationId, historyId: _id }); }); const formatDateAndTime = useFormatDateAndTime(); const dataSentToTriggerCode = useHighlightedCode('json', JSON.stringify(dataSentToTrigger || '', null, 2)); const prepareSentMessageCode = useHighlightedCode('json', JSON.stringify(prepareSentMessage || '', null, 2)); const processSentMessageCode = useHighlightedCode('json', JSON.stringify(processSentMessage || '', null, 2)); const httpCallDataCode = useHighlightedCode('json', JSON.stringify(httpCallData || '', null, 2)); const httpErrorCode = useHighlightedCode('json', JSON.stringify(httpError || '', null, 2)); const httpResultCode = useHighlightedCode('json', JSON.stringify(httpResult || '', null, 2)); const errorStackCode = useHighlightedCode('json', JSON.stringify(errorStack || '', null, 2)); return {formatDateAndTime(_createdAt)} } {...props} > {t('Status')} {error ? t('Failure') : t('Success')} {t('Integration_Outgoing_WebHook_History_Time_Triggered')} {createdAt} {t('Integration_Outgoing_WebHook_History_Time_Ended_Or_Error')} {updatedAt} {t('Event_Trigger')} {t(eventList.outgoingEvents[event].label)} {t('Integration_Outgoing_WebHook_History_Trigger_Step')} {step} {dataSentToTrigger && {t('Integration_Outgoing_WebHook_History_Data_Passed_To_Trigger')}
} {prepareSentMessage && {t('Integration_Outgoing_WebHook_History_Messages_Sent_From_Prepare_Script')}
} {processSentMessage && {t('Integration_Outgoing_WebHook_History_Messages_Sent_From_Process_Script')}
} {url && {t('URL')} {url} } {httpCallData && {t('Integration_Outgoing_WebHook_History_Data_Passed_To_URL')}
} {httpError && {t('Integration_Outgoing_WebHook_History_Http_Response_Error')}
} {httpResult && {t('Integration_Outgoing_WebHook_History_Http_Response')}
} {errorStack && {t('Integration_Outgoing_WebHook_History_Error_Stacktrace')}
}
; } function HistoryContent({ data, state, onChange, ...props }) { const t = useTranslation(); if (!data || state === AsyncStatePhase.LOADING) { return ; } if (data.length < 1) { return {t('Integration_Outgoing_WebHook_No_History')}; } return <> {data.map((current) => )} ; } function OutgoingWebhookHistoryPage(props) { const dispatchToastMessage = useToastMessageDispatch(); const t = useTranslation(); const [currentData, setCurrentData] = useState(); const [current, setCurrent] = useState(); const [itemsPerPage, setItemsPerPage] = useState(); const [mounted, setMounted] = useState(false); const [total, setTotal] = useState(0); const router = useRoute('admin-integrations'); const clearHistory = useMethod('clearIntegrationHistory'); const id = useRouteParameter('id'); const query = useMemo(() => ({ id, count: itemsPerPage, offset: current, }), [id, itemsPerPage, current]); const { value: data, phase: state, reload } = useEndpointData('integrations.history', query); const handleClearHistory = async () => { try { await clearHistory(); dispatchToastMessage({ type: 'success', message: t('Integration_History_Cleared') }); reload(); setMounted(false); } catch (e) { dispatchToastMessage({ type: 'error', message: e }); } }; const handleClickReturn = () => { router.push({ }); }; const handleDataChange = useMutableCallback(({ type, id, diff, data }) => { if (type === 'inserted') { setTotal((total) => total + 1); setCurrentData((state) => [data].concat(state)); return; } if (type === 'updated') { setCurrentData((state) => { const index = state.findIndex(({ _id }) => _id === id); Object.assign(state[index], diff); return state; }); return; } if (type === 'removed') { setCurrentData([]); } }); useEffect(() => { if (state === AsyncStatePhase.RESOLVED && !mounted) { setCurrentData(data.history); setTotal(data.total); setMounted(true); } }, [data, mounted, state]); useEffect(() => { if (mounted) { integrationHistoryStreamer.on(id, handleDataChange); } return () => integrationHistoryStreamer.removeListener(id, handleDataChange); }, [handleDataChange, id, mounted]); const showingResultsLabel = useCallback(({ count, current, itemsPerPage }) => t('Showing_results_of', current + 1, Math.min(current + itemsPerPage, count), count), [t]); return ; } export default OutgoingWebhookHistoryPage;