diff --git a/src/pages/workspace/utils/webhooks/responder_requests_table.tsx b/src/pages/workspace/utils/webhooks/responder_requests_table.tsx index a6e6fae..10533c3 100644 --- a/src/pages/workspace/utils/webhooks/responder_requests_table.tsx +++ b/src/pages/workspace/utils/webhooks/responder_requests_table.tsx @@ -1,7 +1,19 @@ import { useCallback, useEffect, useState } from 'react'; import type { EuiDataGridCellValueElementProps, EuiDataGridColumn, Pagination } from '@elastic/eui'; -import { EuiCodeBlock, EuiDataGrid, EuiEmptyPrompt, EuiFlexGroup, EuiFlexItem, EuiIcon } from '@elastic/eui'; +import { + EuiButton, + EuiCodeBlock, + EuiConfirmModal, + EuiDataGrid, + EuiEmptyPrompt, + EuiFlexGroup, + EuiFlexItem, + EuiFormRow, + EuiIcon, + EuiPanel, + EuiSpacer, +} from '@elastic/eui'; import axios from 'axios'; import { unix } from 'moment'; @@ -36,7 +48,7 @@ function guessBodyContentType(request: ResponderRequest) { } export function ResponderRequestsTable({ responder }: ResponderRequestsTableProps) { - const { uiState } = useWorkspaceContext(); + const { uiState, addToast } = useWorkspaceContext(); const [requests, setRequests] = useState>( responder.settings.requestsToTrack > 0 ? { status: 'pending' } : { status: 'succeeded', data: [] }, @@ -109,6 +121,11 @@ export function ResponderRequestsTable({ responder }: ResponderRequestsTableProp [setPagination, pagination], ); + const [clearHistoryStatus, setClearHistoryStatus] = useState<{ isModalVisible: boolean; isInProgress: boolean }>({ + isInProgress: false, + isModalVisible: false, + }); + const renderCellValue = useCallback( ({ rowIndex, columnId, isDetails }: EuiDataGridCellValueElementProps) => { if (requests.status !== 'succeeded' || rowIndex >= requests.data.length) { @@ -210,26 +227,100 @@ export function ResponderRequestsTable({ responder }: ResponderRequestsTableProp ); } + const clearConfirmModal = clearHistoryStatus.isModalVisible ? ( + setClearHistoryStatus({ isModalVisible: false, isInProgress: false })} + isLoading={clearHistoryStatus.isInProgress} + onConfirm={() => { + setClearHistoryStatus((currentStatus) => ({ ...currentStatus, isInProgress: true })); + + axios + .post( + getApiUrl(`/api/utils/webhooks/responders/${encodeURIComponent(responder.id)}/clear`), + undefined, + getApiRequestConfig(), + ) + .then( + () => { + setRequests({ status: 'succeeded', data: [] }); + + addToast({ + id: `success-clear-responder-history-${responder.name}`, + iconType: 'check', + color: 'success', + title: `Successfully cleared request history for "${responder.name}" responder`, + }); + + setClearHistoryStatus({ isModalVisible: false, isInProgress: false }); + }, + () => { + addToast({ + id: `failed-clear-responder-history-${responder.name}`, + iconType: 'warning', + color: 'danger', + title: `Unable to clear request history for "${responder.name}" responder, please try again later`, + }); + setClearHistoryStatus((currentStatus) => ({ ...currentStatus, isInProgress: false })); + }, + ); + }} + cancelButtonText="Cancel" + confirmButtonText="Clear" + buttonColor="danger" + > + The request history for{' '} + + {responder.name} ({responder.path}) + {' '} + will be cleared. Are you sure you want to proceed? + + ) : null; + + const shouldDisplayControlPanel = requests.status === 'succeeded' && requests.data.length > 0; + const controlPanel = shouldDisplayControlPanel ? ( + + + + + + setClearHistoryStatus({ isModalVisible: true, isInProgress: false })} + > + Clear + + + + + + ) : null; + return ( + {controlPanel} - + + + + {clearConfirmModal} );