Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.11] Enhanced UI for rendering multiple documents in finding details flyout #1016

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cypress/integration/3_alerts.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe('Alerts', () => {
// and matches each entry with the corresponding element line.
const document = JSON.stringify(JSON.parse('{"winlog.event_id": 2003}'), null, 2);
const documentLines = document.split('\n');
cy.get('[data-test-subj="finding-details-flyout-document-toggle-0"]').click({ force: true });

cy.get('[data-test-subj="finding-details-flyout-rule-document-0"]')
.get('[class="euiCodeBlock__line"]')
.each((lineElement, lineIndex) => {
Expand Down
71 changes: 46 additions & 25 deletions public/pages/Findings/components/FindingDetailsFlyout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export default class FindingDetailsFlyout extends Component<
> {
constructor(props: FindingDetailsFlyoutProps) {
super(props);
const relatedDocuments: FindingDocumentItem[] = this.getRelatedDocuments();
const docIdToExpandedRowMap: FindingDetailsFlyoutState['docIdToExpandedRowMap'] = {};

if (relatedDocuments.length === 1) {
docIdToExpandedRowMap[relatedDocuments[0].id] = this.createDocumentBlock(relatedDocuments[0]);
}

this.state = {
loading: false,
ruleViewerFlyoutData: null,
Expand All @@ -102,7 +109,7 @@ export default class FindingDetailsFlyout extends Component<
loadingIndexPatternId: true,
areCorrelationsLoading: true,
allRules: {},
docIdToExpandedRowMap: {},
docIdToExpandedRowMap,
};
}

Expand Down Expand Up @@ -316,46 +323,60 @@ export default class FindingDetailsFlyout extends Component<
return patternId;
};

toggleDocumentDetails(item: FindingDocumentItem) {
const docIdToExpandedRowMapValues = { ...this.state.docIdToExpandedRowMap };
createDocumentBlock = (item: FindingDocumentItem) => {
let formattedDocument = '';
try {
formattedDocument = document ? JSON.stringify(JSON.parse(item.document), null, 2) : '';
} catch {
// no-op
}

return (
<EuiFormRow fullWidth={true} style={{ width: '100%' }}>
<EuiCodeBlock
language="json"
isCopyable
data-test-subj={`finding-details-flyout-rule-document-${item.itemIdx}`}
>
{formattedDocument}
</EuiCodeBlock>
</EuiFormRow>
);
};

getRelatedDocuments() {
const {
finding: { document_list, related_doc_ids },
} = this.props;
const relatedDocIdsSet = new Set(related_doc_ids);
const relatedDocuments: FindingDocumentItem[] = [];
document_list.forEach((documentInfo) => {
if (documentInfo.found && relatedDocIdsSet.has(documentInfo.id)) {
relatedDocuments.push({ ...documentInfo, itemIdx: relatedDocuments.length });
}
});

return relatedDocuments;
}

toggleDocumentDetails(item: FindingDocumentItem) {
const docIdToExpandedRowMapValues = { ...this.state.docIdToExpandedRowMap };

if (docIdToExpandedRowMapValues[item.id]) {
delete docIdToExpandedRowMapValues[item.id];
} else {
docIdToExpandedRowMapValues[item.id] = (
<EuiFormRow fullWidth={true}>
<EuiCodeBlock
language="json"
isCopyable
data-test-subj={`finding-details-flyout-rule-document-${item.itemIdx}`}
>
{formattedDocument}
</EuiCodeBlock>
</EuiFormRow>
);
docIdToExpandedRowMapValues[item.id] = this.createDocumentBlock(item);
}

this.setState({ docIdToExpandedRowMap: docIdToExpandedRowMapValues });
}

renderFindingDocuments(loadingIndexPatternId: boolean) {
const {
finding: { index, document_list, related_doc_ids },
finding: { index },
} = this.props;
const { indexPatternId, docIdToExpandedRowMap } = this.state;
const relatedDocIdsSet = new Set(related_doc_ids);
const relatedDocuments: FindingDocumentItem[] = [];
document_list.forEach((documentInfo) => {
if (documentInfo.found && relatedDocIdsSet.has(documentInfo.id)) {
relatedDocuments.push({ ...documentInfo, itemIdx: relatedDocuments.length });
}
});
const relatedDocuments: FindingDocumentItem[] = this.getRelatedDocuments();

if (relatedDocuments.length === 0) {
return (
Expand All @@ -377,10 +398,10 @@ export default class FindingDetailsFlyout extends Component<
const actions = [
{
render: ({ id }: FindingDocumentItem) => (
<EuiToolTip title="View surrounding documents">
<EuiToolTip content="View surrounding documents">
<EuiButtonIcon
disabled={loadingIndexPatternId}
iconType={'popout'}
iconType={'inspect'}
data-test-subj={'finding-details-flyout-view-surrounding-documents'}
onClick={() => {
if (indexPatternId) {
Expand Down Expand Up @@ -422,7 +443,7 @@ export default class FindingDetailsFlyout extends Component<
return (
<>
<EuiTitle size={'s'}>
<h3>Documents</h3>
<h3>Documents ({relatedDocuments.length})</h3>
</EuiTitle>
<EuiSpacer />
<EuiFormRow label={'Index'} data-test-subj={`finding-details-flyout-rule-document-index`}>
Expand Down
Loading