[Integrations UI] Restore post-save integration policy redirect (#103780)

* Revert "Remove post-installation redirect for integrations (#103179)"

This reverts commit 96c4350289.

* Restore post-save redirects but only when user hasn't navigated away
This commit is contained in:
Jen Huang 2021-06-30 09:22:14 -07:00 committed by GitHub
parent 0f4319c43e
commit c63e74f024
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 28 deletions

View file

@ -6,7 +6,7 @@
*/
import type { ReactEventHandler } from 'react';
import React, { useState, useEffect, useMemo, useCallback } from 'react';
import React, { useState, useEffect, useMemo, useCallback, useRef } from 'react';
import { useRouteMatch, useHistory, useLocation } from 'react-router-dom';
import styled from 'styled-components';
import { i18n } from '@kbn/i18n';
@ -271,6 +271,14 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => {
setFormState('SUBMITTED');
return result;
}, [packagePolicy]);
const doOnSaveNavigation = useRef<boolean>(true);
// Detect if user left page
useEffect(() => {
return () => {
doOnSaveNavigation.current = false;
};
}, []);
const onSubmit = useCallback(async () => {
if (formState === 'VALID' && hasErrors) {
@ -283,18 +291,20 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => {
}
const { error, data } = await savePackagePolicy();
if (!error) {
if (routeState && routeState.onSaveNavigateTo) {
handleNavigateTo(
typeof routeState.onSaveNavigateTo === 'function'
? routeState.onSaveNavigateTo(data!.item)
: routeState.onSaveNavigateTo
);
} else {
history.push(
getPath('policy_details', {
policyId: agentPolicy?.id || (params as AddFromPolicyParams).policyId,
})
);
if (doOnSaveNavigation.current) {
if (routeState && routeState.onSaveNavigateTo) {
handleNavigateTo(
typeof routeState.onSaveNavigateTo === 'function'
? routeState.onSaveNavigateTo(data!.item)
: routeState.onSaveNavigateTo
);
} else {
history.push(
getPath('policy_details', {
policyId: agentPolicy?.id || (params as AddFromPolicyParams).policyId,
})
);
}
}
const fromPolicyWithoutAgentsAssigned = from === 'policy' && agentPolicy && agentCount === 0;
@ -361,21 +371,22 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => {
setFormState('VALID');
}
}, [
getHref,
from,
packageInfo,
agentCount,
agentPolicy,
formState,
getPath,
handleNavigateTo,
hasErrors,
history,
agentCount,
savePackagePolicy,
doOnSaveNavigation,
from,
agentPolicy,
packageInfo,
notifications.toasts,
packagePolicy.name,
params,
getHref,
routeState,
savePackagePolicy,
handleNavigateTo,
history,
getPath,
params,
]);
const integrationInfo = useMemo(

View file

@ -6,7 +6,7 @@
*/
import type { ReactEventHandler } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { Redirect, Route, Switch, useLocation, useParams } from 'react-router-dom';
import { Redirect, Route, Switch, useLocation, useParams, useHistory } from 'react-router-dom';
import styled from 'styled-components';
import {
EuiBetaBadge,
@ -31,7 +31,12 @@ import {
useBreadcrumbs,
useStartServices,
} from '../../../../hooks';
import { PLUGIN_ID, INTEGRATIONS_ROUTING_PATHS, pagePathGetters } from '../../../../constants';
import {
PLUGIN_ID,
INTEGRATIONS_PLUGIN_ID,
INTEGRATIONS_ROUTING_PATHS,
pagePathGetters,
} from '../../../../constants';
import {
useCapabilities,
useGetPackageInfoByKey,
@ -39,7 +44,11 @@ import {
useAgentPolicyContext,
} from '../../../../hooks';
import { pkgKeyFromPackageInfo } from '../../../../services';
import type { DetailViewPanelName, PackageInfo } from '../../../../types';
import type {
CreatePackagePolicyRouteState,
DetailViewPanelName,
PackageInfo,
} from '../../../../types';
import { InstallStatus } from '../../../../types';
import { Error, Loading } from '../../../../components';
import type { WithHeaderLayoutProps } from '../../../../layouts';
@ -80,7 +89,8 @@ export function Detail() {
const { pkgkey, panel } = useParams<DetailParams>();
const { getHref } = useLink();
const hasWriteCapabilites = useCapabilities().write;
const { search } = useLocation();
const history = useHistory();
const { pathname, search, hash } = useLocation();
const queryParams = useMemo(() => new URLSearchParams(search), [search]);
const integration = useMemo(() => queryParams.get('integration'), [queryParams]);
const services = useStartServices();
@ -202,19 +212,75 @@ export function Detail() {
(ev) => {
ev.preventDefault();
// The object below, given to `createHref` is explicitly accessing keys of `location` in order
// to ensure that dependencies to this `useCallback` is set correctly (because `location` is mutable)
const currentPath = history.createHref({
pathname,
search,
hash,
});
const path = pagePathGetters.add_integration_to_policy({
pkgkey,
...(integration ? { integration } : {}),
...(agentPolicyIdFromContext ? { agentPolicyId: agentPolicyIdFromContext } : {}),
})[1];
let redirectToPath: CreatePackagePolicyRouteState['onSaveNavigateTo'] &
CreatePackagePolicyRouteState['onCancelNavigateTo'];
if (agentPolicyIdFromContext) {
redirectToPath = [
PLUGIN_ID,
{
path: `#${
pagePathGetters.policy_details({
policyId: agentPolicyIdFromContext,
})[1]
}`,
},
];
} else {
redirectToPath = [
INTEGRATIONS_PLUGIN_ID,
{
path: `#${
pagePathGetters.integration_details_policies({
pkgkey,
})[1]
}`,
},
];
}
const redirectBackRouteState: CreatePackagePolicyRouteState = {
onSaveNavigateTo: redirectToPath,
onCancelNavigateTo: [
INTEGRATIONS_PLUGIN_ID,
{
path: currentPath,
},
],
onCancelUrl: currentPath,
};
services.application.navigateToApp(PLUGIN_ID, {
// Necessary because of Fleet's HashRouter. Can be changed when
// https://github.com/elastic/kibana/issues/96134 is resolved
path: `#${path}`,
state: redirectBackRouteState,
});
},
[pkgkey, integration, services.application, agentPolicyIdFromContext]
[
history,
hash,
pathname,
search,
pkgkey,
integration,
services.application,
agentPolicyIdFromContext,
]
);
const headerRightContent = useMemo(