Skip to content
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 @@ -24,8 +24,7 @@
"data",
"inspector",
"kibanaReact",
"kibanaUtils",
"observabilityAIAssistant"
"kibanaUtils"
],
"extraPublicDirs": [
"common"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ import type { CasesPublicStart } from '@kbn/cases-plugin/public';
import { mockCasesContract } from '@kbn/cases-plugin/public/mocks';
import { notificationServiceMock } from '@kbn/core-notifications-browser-mocks';
import { AddPageAttachmentToCaseModal } from './add_page_attachment_to_case_modal';
import * as usePageSummaryHook from '../../hooks/use_page_summary';

jest.mock('../../hooks/use_page_summary', () => ({
usePageSummary: jest.fn(() => ({
isObsAIAssistantEnabled: true,
generateSummary: jest.fn(),
isLoading: false,
summary: '',
errors: [],
abortController: { signal: new AbortController().signal, abort: jest.fn() },
screenContexts: [],
isComplete: false,
})),
}));

const mockCases: Partial<CasesPublicStart> = mockCasesContract();

Expand Down Expand Up @@ -127,17 +113,6 @@ describe('AddPageAttachmentToCaseModal', () => {
});

it('opens case modal when confirm button is clicked', () => {
jest.spyOn(usePageSummaryHook, 'usePageSummary').mockReturnValue({
isObsAIAssistantEnabled: true,
generateSummary: jest.fn(),
isLoading: false,
summary: '',
errors: [],
abortController: { signal: new AbortController().signal, abort: jest.fn() },
screenContexts: [],
isComplete: true,
});

const mockCasesModal = {
open: jest.fn(),
close: jest.fn(),
Expand All @@ -159,17 +134,6 @@ describe('AddPageAttachmentToCaseModal', () => {
});

it('passes correct getAttachments payload when case modal is opened', () => {
jest.spyOn(usePageSummaryHook, 'usePageSummary').mockReturnValue({
isObsAIAssistantEnabled: true,
generateSummary: jest.fn(),
isLoading: false,
summary: '',
errors: [],
abortController: { signal: new AbortController().signal, abort: jest.fn() },
screenContexts: [],
isComplete: true,
});

const mockCasesModal = {
open: jest.fn(),
close: jest.fn(),
Expand Down Expand Up @@ -200,7 +164,6 @@ describe('AddPageAttachmentToCaseModal', () => {
{
persistableStateAttachmentState: {
...pageAttachmentState,
screenContext: [],
summary: comment,
},
persistableStateAttachmentTypeId: '.page',
Expand All @@ -209,63 +172,7 @@ describe('AddPageAttachmentToCaseModal', () => {
]);
});

it('passes correct screenContexts to the case attachment', () => {
const mockCasesModal = {
open: jest.fn(),
close: jest.fn(),
};
mockCases.hooks!.useCasesAddToExistingCaseModal = jest.fn(() => mockCasesModal);
const screenContexts = [{ screenDescription: 'testContext' }];
jest.spyOn(usePageSummaryHook, 'usePageSummary').mockReturnValue({
isObsAIAssistantEnabled: true,
generateSummary: jest.fn(),
isLoading: false,
summary: '',
errors: [],
abortController: { signal: new AbortController().signal, abort: jest.fn() },
screenContexts,
isComplete: true,
});
render(
<IntlProvider locale="en">
<AddPageAttachmentToCaseModal
pageAttachmentState={pageAttachmentState}
cases={mockCases as CasesPublicStart}
onCloseModal={jest.fn()}
notifications={notifications}
/>
</IntlProvider>
);
fireEvent.click(screen.getByText('Confirm'));
expect(mockCasesModal!.open).toHaveBeenCalledWith({
getAttachments: expect.any(Function),
});
const attachments = mockCasesModal!.open.mock.calls[0][0].getAttachments();
expect(attachments).toEqual([
{
persistableStateAttachmentState: {
...pageAttachmentState,
screenContext: screenContexts,
summary: '',
},
persistableStateAttachmentTypeId: '.page',
type: 'persistableState',
},
]);
});

it('can update the summary comment', () => {
jest.spyOn(usePageSummaryHook, 'usePageSummary').mockReturnValue({
isObsAIAssistantEnabled: true,
generateSummary: jest.fn(),
isLoading: false,
summary: '',
errors: [],
abortController: { signal: new AbortController().signal, abort: jest.fn() },
screenContexts: [],
isComplete: true,
});

const mockCasesModal = {
open: jest.fn(),
close: jest.fn(),
Expand All @@ -292,7 +199,6 @@ describe('AddPageAttachmentToCaseModal', () => {
{
persistableStateAttachmentState: {
...pageAttachmentState,
screenContext: [],
summary: comment,
},
persistableStateAttachmentTypeId: '.page',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ import type { CasesPublicStart } from '@kbn/cases-plugin/public';
import { EuiConfirmModal, EuiModalHeader, EuiModalHeaderTitle, EuiModalBody } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { AttachmentType } from '@kbn/cases-plugin/common';
import type { ObservabilityAIAssistantPublicStart } from '@kbn/observability-ai-assistant-plugin/public';
import {
type PageAttachmentPersistedState,
PAGE_ATTACHMENT_TYPE,
} from '@kbn/page-attachment-schema';
import { type CasesPermissions } from '@kbn/cases-plugin/common';
import type { NotificationsStart } from '@kbn/core/public';
import { useChatService } from '../../hooks/use_chat_service';
import { usePageSummary } from '../../hooks/use_page_summary';
import { AddToCaseComment } from '../add_to_case_comment';

export interface AddPageAttachmentToCaseModalProps {
pageAttachmentState: PageAttachmentPersistedState;
observabilityAIAssistant?: ObservabilityAIAssistantPublicStart;
notifications: NotificationsStart;
cases: CasesPublicStart;
onCloseModal: () => void;
Expand All @@ -32,15 +28,11 @@ export interface AddPageAttachmentToCaseModalProps {
export function AddPageAttachmentToCaseModal({
pageAttachmentState,
cases,
observabilityAIAssistant,
notifications,
onCloseModal,
}: AddPageAttachmentToCaseModalProps) {
const getCasesContext = cases.ui.getCasesContext;
const canUseCases = cases.helpers.canUseCases;
const { ObservabilityAIAssistantChatServiceContext, chatService } = useChatService({
observabilityAIAssistant,
});

const casesPermissions: CasesPermissions = useMemo(() => {
if (!canUseCases) {
Expand Down Expand Up @@ -87,50 +79,25 @@ export function AddPageAttachmentToCaseModal({
owner={['observability']}
features={{ alerts: { sync: false } }}
>
{ObservabilityAIAssistantChatServiceContext && chatService ? (
<ObservabilityAIAssistantChatServiceContext.Provider value={chatService}>
<AddToCaseButtonContent
pageAttachmentState={pageAttachmentState}
cases={cases}
observabilityAIAssistant={observabilityAIAssistant}
notifications={notifications}
onCloseModal={onCloseModal}
/>
</ObservabilityAIAssistantChatServiceContext.Provider>
) : (
<AddToCaseButtonContent
pageAttachmentState={pageAttachmentState}
notifications={notifications}
cases={cases}
onCloseModal={onCloseModal}
/>
)}
<AddToCaseButtonContent
pageAttachmentState={pageAttachmentState}
cases={cases}
onCloseModal={onCloseModal}
/>
</CasesContext>
) : null;
}

function AddToCaseButtonContent({
pageAttachmentState,
cases,
observabilityAIAssistant,
notifications,
onCloseModal,
}: AddPageAttachmentToCaseModalProps) {
}: Omit<AddPageAttachmentToCaseModalProps, 'notifications'>) {
const [isCommentModalOpen, setIsCommentModalOpen] = useState(true);
const [isCommentLoading, setIsCommentLoading] = useState(true);
const [comment, setComment] = useState<string>('');
const useCasesAddToExistingCaseModal = cases.hooks.useCasesAddToExistingCaseModal!;
const { screenContexts } = usePageSummary({
observabilityAIAssistant,
appInstructions:
'When referring to Synthetics monitors, include the monitor name, test run timestamp, and test run location.',
});
const casesModal = useCasesAddToExistingCaseModal({
onClose: (_, isCreateCase) => {
if (!isCreateCase) {
onCloseModal();
}
},
onClose: onCloseModal,
});

const handleCloseModal = useCallback(() => {
Expand All @@ -146,14 +113,13 @@ function AddToCaseButtonContent({
persistableStateAttachmentState: {
...pageAttachmentState,
summary: comment,
screenContext: screenContexts || [],
},
persistableStateAttachmentTypeId: PAGE_ATTACHMENT_TYPE,
type: AttachmentType.persistableState,
},
],
});
}, [casesModal, comment, pageAttachmentState, screenContexts]);
}, [casesModal, comment, pageAttachmentState]);

return isCommentModalOpen ? (
<EuiConfirmModal
Expand All @@ -167,7 +133,6 @@ function AddToCaseButtonContent({
onConfirm={onCommentAdded}
data-test-subj="syntheticsAddToCaseCommentModal"
style={{ width: 800 }}
isLoading={isCommentLoading}
confirmButtonText={i18n.translate(
'xpack.observabilityShared.cases.addToPageAttachmentToCaseModal.confirmButtonText',
{
Expand All @@ -189,13 +154,7 @@ function AddToCaseButtonContent({
</EuiModalHeaderTitle>
</EuiModalHeader>
<EuiModalBody>
<AddToCaseComment
setComment={setComment}
comment={comment}
setIsLoading={setIsCommentLoading}
notifications={notifications}
observabilityAIAssistant={observabilityAIAssistant}
/>
<AddToCaseComment onCommentChange={(change) => setComment(change)} comment={comment} />
</EuiModalBody>
</EuiConfirmModal>
) : null;
Expand Down
Loading