import { Button, ButtonGroup, Icon, Skeleton, Box, Accordion, Field, FieldGroup, Pagination } from '@rocket.chat/fuselage';
import React, { useMemo, useCallback, useState } from 'react';
import Page from '../../../components/basic/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 { useEndpointDataExperimental, ENDPOINT_STATES } from '../../../hooks/useEndpointDataExperimental';
import { useRoute, useRouteParameter } from '../../../contexts/RouterContext';
import { useFormatDateAndTime } from '../../../hooks/useFormatDateAndTime';
import { useToastMessageDispatch } from '../../../contexts/ToastMessagesContext';
function HistoryItem({ data, onChange, ...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 handleClickReplay = useCallback((e) => {
e.stopPropagation();
replayOutgoingIntegration({ integrationId, historyId: _id });
onChange();
}, [_id, integrationId, onChange, replayOutgoingIntegration]);
const formatDateAndTime = useFormatDateAndTime();
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}
{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 === ENDPOINT_STATES.LOADING) {
return
;
}
if (data.history.length < 1) {
return {t('Integration_Outgoing_WebHook_No_History')};
}
return <>
{data.history.map((current) => )}
>;
}
function OutgoingWebhookHistoryPage(props) {
const dispatchToastMessage = useToastMessageDispatch();
const t = useTranslation();
const [cache, setCache] = useState();
const [current, setCurrent] = useState();
const [itemsPerPage, setItemsPerPage] = useState();
const onChange = useCallback(() => {
setCache(new Date());
}, []);
const router = useRoute('admin-integrations');
const clearHistory = useMethod('clearIntegrationHistory');
const handleClearHistory = async () => {
try {
await clearHistory();
dispatchToastMessage({ type: 'success', message: t('Integration_History_Cleared') });
onChange();
} catch (e) {
dispatchToastMessage({ type: 'error', message: e });
}
};
const handleClickReturn = () => {
router.push({ });
};
const id = useRouteParameter('id');
const query = useMemo(() => ({
id,
count: itemsPerPage,
offset: current,
// TODO: remove cache. Is necessary for data validation
}), [id, itemsPerPage, current, cache]);
const { data, state } = useEndpointDataExperimental('integrations.history', query);
const showingResultsLabel = useCallback(({ count, current, itemsPerPage }) => t('Showing results %s - %s of %s', current + 1, Math.min(current + itemsPerPage, count), count), [t]);
return
;
}
export default OutgoingWebhookHistoryPage;