diff --git a/x-pack/plugins/cases/public/client/ui/get_cases.tsx b/x-pack/plugins/cases/public/client/ui/get_cases.tsx
index 36556523fc3a3..3274bc67d2a47 100644
--- a/x-pack/plugins/cases/public/client/ui/get_cases.tsx
+++ b/x-pack/plugins/cases/public/client/ui/get_cases.tsx
@@ -28,7 +28,6 @@ export const getCasesLazy = ({
owner,
permissions,
basePath,
- onComponentInitialized,
actionsNavigation,
ruleDetailsNavigation,
showAlertDetails,
@@ -52,7 +51,6 @@ export const getCasesLazy = ({
>
}>
{
);
expect(mockAddLabels).toHaveBeenCalledWith({ alert_count: 3 });
});
+
+ it('should not start any transactions if the app ID is not defined', () => {
+ const { result } = renderUseCreateCaseWithAttachmentsTransaction();
+
+ result.current.startTransaction();
+
+ expect(mockStartTransaction).not.toHaveBeenCalled();
+ expect(mockAddLabels).not.toHaveBeenCalled();
+ });
});
describe('useAddAttachmentToExistingCaseTransaction', () => {
@@ -104,5 +113,14 @@ describe('cases transactions', () => {
);
expect(mockAddLabels).toHaveBeenCalledWith({ alert_count: 3 });
});
+
+ it('should not start any transactions if the app ID is not defined', () => {
+ const { result } = renderUseAddAttachmentToExistingCaseTransaction();
+
+ result.current.startTransaction({ attachments: bulkAttachments });
+
+ expect(mockStartTransaction).not.toHaveBeenCalled();
+ expect(mockAddLabels).not.toHaveBeenCalled();
+ });
});
});
diff --git a/x-pack/plugins/cases/public/common/apm/use_cases_transactions.ts b/x-pack/plugins/cases/public/common/apm/use_cases_transactions.ts
index 891726322cb8e..400f1aa2d956c 100644
--- a/x-pack/plugins/cases/public/common/apm/use_cases_transactions.ts
+++ b/x-pack/plugins/cases/public/common/apm/use_cases_transactions.ts
@@ -17,8 +17,8 @@ const BULK_ADD_ATTACHMENT_TO_NEW_CASE = 'bulkAddAttachmentsToNewCase' as const;
const ADD_ATTACHMENT_TO_EXISTING_CASE = 'addAttachmentToExistingCase' as const;
const BULK_ADD_ATTACHMENT_TO_EXISTING_CASE = 'bulkAddAttachmentsToExistingCase' as const;
-export type StartCreateCaseWithAttachmentsTransaction = (param: {
- appId: string;
+export type StartCreateCaseWithAttachmentsTransaction = (param?: {
+ appId?: string;
attachments?: CaseAttachmentsWithoutOwner;
}) => Transaction | undefined;
@@ -28,11 +28,17 @@ export const useCreateCaseWithAttachmentsTransaction = () => {
const startCreateCaseWithAttachmentsTransaction =
useCallback(
- ({ appId, attachments }) => {
+ ({ appId, attachments } = {}) => {
+ if (!appId) {
+ return;
+ }
+
if (!attachments) {
return startTransaction(`Cases [${appId}] ${CREATE_CASE}`);
}
+
const alertCount = getAlertCount(attachments);
+
if (alertCount <= 1) {
return startTransaction(`Cases [${appId}] ${ADD_ATTACHMENT_TO_NEW_CASE}`);
}
@@ -48,7 +54,7 @@ export const useCreateCaseWithAttachmentsTransaction = () => {
};
export type StartAddAttachmentToExistingCaseTransaction = (param: {
- appId: string;
+ appId?: string;
attachments: CaseAttachmentsWithoutOwner;
}) => Transaction | undefined;
@@ -59,13 +65,20 @@ export const useAddAttachmentToExistingCaseTransaction = () => {
const startAddAttachmentToExistingCaseTransaction =
useCallback(
({ appId, attachments }) => {
+ if (!appId) {
+ return;
+ }
+
const alertCount = getAlertCount(attachments);
+
if (alertCount <= 1) {
return startTransaction(`Cases [${appId}] ${ADD_ATTACHMENT_TO_EXISTING_CASE}`);
}
+
const transaction = startTransaction(
`Cases [${appId}] ${BULK_ADD_ATTACHMENT_TO_EXISTING_CASE}`
);
+
transaction?.addLabels({ alert_count: alertCount });
return transaction;
},
diff --git a/x-pack/plugins/cases/public/common/hooks.test.tsx b/x-pack/plugins/cases/public/common/hooks.test.tsx
index 80602f23012f5..42ca1c20d578e 100644
--- a/x-pack/plugins/cases/public/common/hooks.test.tsx
+++ b/x-pack/plugins/cases/public/common/hooks.test.tsx
@@ -10,9 +10,9 @@ import { renderHook } from '@testing-library/react-hooks';
import { TestProviders } from './mock';
import { useIsMainApplication } from './hooks';
-import { useApplication } from '../components/cases_context/use_application';
+import { useApplication } from './lib/kibana/use_application';
-jest.mock('../components/cases_context/use_application');
+jest.mock('./lib/kibana/use_application');
const useApplicationMock = useApplication as jest.Mock;
diff --git a/x-pack/plugins/cases/public/common/hooks.ts b/x-pack/plugins/cases/public/common/hooks.ts
index f65b56fecfd84..a3837d4ebb16c 100644
--- a/x-pack/plugins/cases/public/common/hooks.ts
+++ b/x-pack/plugins/cases/public/common/hooks.ts
@@ -6,10 +6,10 @@
*/
import { STACK_APP_ID } from '../../common/constants';
-import { useCasesContext } from '../components/cases_context/use_cases_context';
+import { useApplication } from './lib/kibana/use_application';
export const useIsMainApplication = () => {
- const { appId } = useCasesContext();
+ const { appId } = useApplication();
return appId === STACK_APP_ID;
};
diff --git a/x-pack/plugins/cases/public/common/lib/kibana/__mocks__/index.ts b/x-pack/plugins/cases/public/common/lib/kibana/__mocks__/index.ts
index 3aa4c02457ef7..7bf4e71e0717a 100644
--- a/x-pack/plugins/cases/public/common/lib/kibana/__mocks__/index.ts
+++ b/x-pack/plugins/cases/public/common/lib/kibana/__mocks__/index.ts
@@ -25,7 +25,6 @@ export const useKibana = jest.fn().mockReturnValue({
export const useHttp = jest.fn().mockReturnValue(createStartServicesMock().http);
export const useTimeZone = jest.fn();
export const useDateFormat = jest.fn();
-export const useBasePath = jest.fn(() => '/test/base/path');
export const useToasts = jest
.fn()
.mockReturnValue(notificationServiceMock.createStartContract().toasts);
diff --git a/x-pack/plugins/cases/public/common/lib/kibana/__mocks__/use_application.tsx b/x-pack/plugins/cases/public/common/lib/kibana/__mocks__/use_application.tsx
new file mode 100644
index 0000000000000..2a4945436e184
--- /dev/null
+++ b/x-pack/plugins/cases/public/common/lib/kibana/__mocks__/use_application.tsx
@@ -0,0 +1,10 @@
+/*
+ * 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.
+ */
+
+export const useApplication = jest
+ .fn()
+ .mockReturnValue({ appId: 'testAppId', appTitle: 'test-title' });
diff --git a/x-pack/plugins/cases/public/common/lib/kibana/hooks.ts b/x-pack/plugins/cases/public/common/lib/kibana/hooks.ts
index 39b4d3d1edc76..3d72e5ca552b9 100644
--- a/x-pack/plugins/cases/public/common/lib/kibana/hooks.ts
+++ b/x-pack/plugins/cases/public/common/lib/kibana/hooks.ts
@@ -30,8 +30,6 @@ export const useTimeZone = (): string => {
return timeZone === 'Browser' ? moment.tz.guess() : timeZone;
};
-export const useBasePath = (): string => useKibana().services.http.basePath.get();
-
export const useToasts = (): StartServices['notifications']['toasts'] =>
useKibana().services.notifications.toasts;
@@ -116,12 +114,12 @@ export const useCurrentUser = (): AuthenticatedElasticUser | null => {
* Returns a full URL to the provided page path by using
* kibana's `getUrlForApp()`
*/
-export const useAppUrl = (appId: string) => {
+export const useAppUrl = (appId?: string) => {
const { getUrlForApp } = useKibana().services.application;
const getAppUrl = useCallback(
(options?: { deepLinkId?: string; path?: string; absolute?: boolean }) =>
- getUrlForApp(appId, options),
+ getUrlForApp(appId ?? '', options),
[appId, getUrlForApp]
);
return { getAppUrl };
@@ -131,7 +129,7 @@ export const useAppUrl = (appId: string) => {
* Navigate to any app using kibana's `navigateToApp()`
* or by url using `navigateToUrl()`
*/
-export const useNavigateTo = (appId: string) => {
+export const useNavigateTo = (appId?: string) => {
const { navigateToApp, navigateToUrl } = useKibana().services.application;
const navigateTo = useCallback(
@@ -144,7 +142,7 @@ export const useNavigateTo = (appId: string) => {
if (url) {
navigateToUrl(url);
} else {
- navigateToApp(appId, options);
+ navigateToApp(appId ?? '', options);
}
},
[appId, navigateToApp, navigateToUrl]
@@ -156,7 +154,7 @@ export const useNavigateTo = (appId: string) => {
* Returns navigateTo and getAppUrl navigation hooks
*
*/
-export const useNavigation = (appId: string) => {
+export const useNavigation = (appId?: string) => {
const { navigateTo } = useNavigateTo(appId);
const { getAppUrl } = useAppUrl(appId);
return { navigateTo, getAppUrl };
diff --git a/x-pack/plugins/cases/public/common/lib/kibana/kibana_react.mock.tsx b/x-pack/plugins/cases/public/common/lib/kibana/kibana_react.mock.tsx
index 195c1f433a8e7..0223e4648ac93 100644
--- a/x-pack/plugins/cases/public/common/lib/kibana/kibana_react.mock.tsx
+++ b/x-pack/plugins/cases/public/common/lib/kibana/kibana_react.mock.tsx
@@ -9,6 +9,7 @@ import React from 'react';
import { BehaviorSubject } from 'rxjs';
import type { PublicAppInfo } from '@kbn/core/public';
+import { AppStatus } from '@kbn/core/public';
import type { RecursivePartial } from '@elastic/eui/src/components/common';
import { coreMock } from '@kbn/core/public/mocks';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
@@ -52,7 +53,21 @@ export const createStartServicesMock = ({ license }: StartServiceArgs = {}): Sta
services.application.currentAppId$ = new BehaviorSubject('testAppId');
services.application.applications$ = new BehaviorSubject