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

[Rules migration] Add possibility to navigate to a specific migration (#11264) #201597

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import React from 'react';
import { Routes, Route } from '@kbn/shared-ux-router';

import type { SecuritySubPluginRoutes } from '../app/types';
import { SIEM_MIGRATIONS_RULES_PATH, SecurityPageName } from '../../common/constants';
Expand All @@ -17,7 +18,9 @@ export const RulesRoutes = () => {
return (
<PluginTemplateWrapper>
<SecurityRoutePageWrapper pageName={SecurityPageName.siemMigrationsRules}>
<RulesPage />
<Routes>
<Route path={`${SIEM_MIGRATIONS_RULES_PATH}/:migrationId?`} component={RulesPage} />
</Routes>
</SecurityRoutePageWrapper>
</PluginTemplateWrapper>
);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import { EuiEmptyPrompt, EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import * as i18n from './translations';

const UnknownMigrationComponent = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we export the memo component directly everywhere? It's shorter and it makes it easier to find the references in the IDE.

export const UnknownMigration = React.memo(() => {
// [...]
});
UnknownMigration.displayName = 'UnknownMigration';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, will update our components in one of the next PRs.

return (
<EuiFlexGroup
alignItems="center"
gutterSize="s"
responsive={false}
direction="column"
wrap={true}
>
<EuiFlexItem grow={false}>
<EuiEmptyPrompt
title={<h2>{i18n.UNKNOWN_MIGRATION}</h2>}
titleSize="s"
body={i18n.UNKNOWN_MIGRATION_BODY}
data-test-subj="noMigrationsAvailable"
/>
</EuiFlexItem>
</EuiFlexGroup>
);
};

export const UnknownMigration = React.memo(UnknownMigrationComponent);
UnknownMigration.displayName = 'UnknownMigration';
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';

export const UNKNOWN_MIGRATION = i18n.translate(
'xpack.securitySolution.siemMigrations.rules.unknownMigrationTitle',
{
defaultMessage: 'Unknown migration',
}
);

export const UNKNOWN_MIGRATION_BODY = i18n.translate(
'xpack.securitySolution.siemMigrations.rules.unknownMigrationBodyTitle',
{
defaultMessage:
'Selected migration does not exist. Please select one of the available migraitons',
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,113 @@
* 2.0.
*/

import React, { useCallback, useEffect, useMemo, useState } from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';

import { EuiSkeletonLoading, EuiSkeletonText, EuiSkeletonTitle } from '@elastic/eui';
import type { RouteComponentProps } from 'react-router-dom';
import { useNavigation } from '../../../common/lib/kibana';
import type { RuleMigration } from '../../../../common/siem_migrations/model/rule_migration.gen';
import { HeaderPage } from '../../../common/components/header_page';
import { SecuritySolutionPageWrapper } from '../../../common/components/page_wrapper';
import { SecurityPageName } from '../../../app/types';

import * as i18n from './translations';
import { RulesTable } from '../components/rules_table';
import { NeedAdminForUpdateRulesCallOut } from '../../../detections/components/callouts/need_admin_for_update_callout';
import { MissingPrivilegesCallOut } from '../../../detections/components/callouts/missing_privileges_callout';
import { HeaderButtons } from '../components/header_buttons';
import { useRulePreviewFlyout } from '../hooks/use_rule_preview_flyout';
import { NoMigrations } from '../components/no_migrations';
import { UnknownMigration } from '../components/unknown_migration';
import { useLatestStats } from '../hooks/use_latest_stats';

export const RulesPage = React.memo(() => {
const { data: ruleMigrationsStatsAll, isLoading: isLoadingMigrationsStats } = useLatestStats();

const migrationsIds = useMemo(() => {
if (isLoadingMigrationsStats || !ruleMigrationsStatsAll?.length) {
return [];
}
return ruleMigrationsStatsAll
.filter((migration) => migration.status === 'finished')
.map((migration) => migration.id);
}, [isLoadingMigrationsStats, ruleMigrationsStatsAll]);

const [selectedMigrationId, setSelectedMigrationId] = useState<string | undefined>();
const onMigrationIdChange = (selectedId?: string) => {
setSelectedMigrationId(selectedId);
};

useEffect(() => {
if (!migrationsIds.length) {
return;
}
const index = migrationsIds.findIndex((id) => id === selectedMigrationId);
if (index === -1) {
setSelectedMigrationId(migrationsIds[0]);
}
}, [migrationsIds, selectedMigrationId]);

const ruleActionsFactory = useCallback(
(ruleMigration: RuleMigration, closeRulePreview: () => void) => {
// TODO: Add flyout action buttons
return null;
type RulesMigrationPageProps = RouteComponentProps<{ migrationId?: string }>;

export const RulesPage: React.FC<RulesMigrationPageProps> = React.memo(
({
match: {
params: { migrationId },
},
[]
);

const { rulePreviewFlyout, openRulePreview } = useRulePreviewFlyout({
ruleActionsFactory,
});

return (
<>
<NeedAdminForUpdateRulesCallOut />
<MissingPrivilegesCallOut />

<SecuritySolutionPageWrapper>
<HeaderPage title={i18n.PAGE_TITLE}>
<HeaderButtons
migrationsIds={migrationsIds}
selectedMigrationId={selectedMigrationId}
onMigrationIdChange={onMigrationIdChange}
}) => {
const { navigateTo } = useNavigation();

const { data: ruleMigrationsStatsAll, isLoading: isLoadingMigrationsStats } = useLatestStats();

const migrationsIds = useMemo(() => {
if (isLoadingMigrationsStats || !ruleMigrationsStatsAll?.length) {
return [];
}
return ruleMigrationsStatsAll
.filter((migration) => migration.status === 'finished')
.map((migration) => migration.id);
}, [isLoadingMigrationsStats, ruleMigrationsStatsAll]);

useEffect(() => {
if (isLoadingMigrationsStats) {
return;
}

// Navigate to landing page if there are no migrations
if (!migrationsIds.length) {
navigateTo({ deepLinkId: SecurityPageName.landing, path: 'siem_migrations' });
return;
}

// Navigate to the most recent migration if none is selected
if (!migrationId) {
navigateTo({ deepLinkId: SecurityPageName.siemMigrationsRules, path: migrationsIds[0] });
}
}, [isLoadingMigrationsStats, migrationId, migrationsIds, navigateTo]);

const onMigrationIdChange = (selectedId?: string) => {
navigateTo({ deepLinkId: SecurityPageName.siemMigrationsRules, path: selectedId });
};

const ruleActionsFactory = useCallback(
(ruleMigration: RuleMigration, closeRulePreview: () => void) => {
// TODO: Add flyout action buttons
return null;
},
[]
);

const { rulePreviewFlyout, openRulePreview } = useRulePreviewFlyout({
ruleActionsFactory,
});

const content = useMemo(() => {
if (!migrationId || !migrationsIds.includes(migrationId)) {
return <UnknownMigration />;
}
return <RulesTable migrationId={migrationId} openRulePreview={openRulePreview} />;
}, [migrationId, migrationsIds, openRulePreview]);

return (
<>
<NeedAdminForUpdateRulesCallOut />
<MissingPrivilegesCallOut />

<SecuritySolutionPageWrapper>
<HeaderPage title={i18n.PAGE_TITLE}>
<HeaderButtons
migrationsIds={migrationsIds}
selectedMigrationId={migrationId}
onMigrationIdChange={onMigrationIdChange}
/>
</HeaderPage>
<EuiSkeletonLoading
isLoading={isLoadingMigrationsStats}
loadingContent={
<>
<EuiSkeletonTitle />
<EuiSkeletonText />
</>
}
loadedContent={content}
/>
</HeaderPage>
<EuiSkeletonLoading
isLoading={isLoadingMigrationsStats}
loadingContent={
<>
<EuiSkeletonTitle />
<EuiSkeletonText />
</>
}
loadedContent={
selectedMigrationId ? (
<RulesTable migrationId={selectedMigrationId} openRulePreview={openRulePreview} />
) : (
<NoMigrations />
)
}
/>
{rulePreviewFlyout}
</SecuritySolutionPageWrapper>
</>
);
});
{rulePreviewFlyout}
</SecuritySolutionPageWrapper>
</>
);
}
);
RulesPage.displayName = 'RulesPage';