Skip to content

Commit

Permalink
[Security Solution][Notes] - fix attach to active timeline behavior (e…
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeOberti authored Jul 18, 2024
1 parent 1684d37 commit 6dade93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ export const AddNote = memo(({ eventId }: AddNewNoteProps) => {

// if the flyout is open from a timeline and that timeline is saved, we automatically check the checkbox to associate the note to it
const isTimelineFlyout = useIsTimelineFlyoutOpen();
const [checked, setChecked] = useState(isTimelineFlyout && activeTimeline.savedObjectId != null);

const [checked, setChecked] = useState<boolean>(true);
const onCheckboxChange = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => setChecked(e.target.checked),
[]
Expand Down Expand Up @@ -132,6 +133,11 @@ export const AddNote = memo(({ eventId }: AddNewNoteProps) => {
[editorValue, isMarkdownInvalid]
);

const initialCheckboxChecked = useMemo(
() => isTimelineFlyout && activeTimeline.savedObjectId != null,
[activeTimeline?.savedObjectId, isTimelineFlyout]
);

const checkBoxDisabled = useMemo(
() => !isTimelineFlyout || (isTimelineFlyout && activeTimeline?.savedObjectId == null),
[activeTimeline?.savedObjectId, isTimelineFlyout]
Expand Down Expand Up @@ -171,7 +177,7 @@ export const AddNote = memo(({ eventId }: AddNewNoteProps) => {
</>
}
disabled={checkBoxDisabled}
checked={checked}
checked={initialCheckboxChecked && checked}
onChange={(e) => onCheckboxChange(e)}
/>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ describe('useInvestigationGuide', () => {
expect(hookResult.result.current).toEqual(false);
});

it('should return false when timeline flyout is in url but params are empty preview', () => {
window.location.search =
'http://app/security/alerts&flyout=(right:(id:document-details-right))&timelineFlyout=(preview:!())';
const hookResult = renderHook(() => useIsTimelineFlyoutOpen());
expect(hookResult.result.current).toEqual(false);
});

it('should return true when timeline flyout is open', () => {
window.location.search =
'http://app/security/alerts&flyout=(right:(id:document-details-right))&timelineFlyout=(right:(id:document-details-right,params:(id:id,indexName:index,scopeId:scope)))';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { URL_PARAM_KEY } from '../../../../common/hooks/use_url_state';
*/
export const useIsTimelineFlyoutOpen = (): boolean => {
const query = new URLSearchParams(window.location.search);
return (
query.has(URL_PARAM_KEY.timelineFlyout) && query.get(URL_PARAM_KEY.timelineFlyout) !== '()'
);
const queryHasTimelineFlyout = query.has(URL_PARAM_KEY.timelineFlyout);
const timelineFlyoutHasValue =
query.get(URL_PARAM_KEY.timelineFlyout) !== '()' &&
query.get(URL_PARAM_KEY.timelineFlyout) !== '(preview:!())';

return queryHasTimelineFlyout && timelineFlyoutHasValue;
};

0 comments on commit 6dade93

Please sign in to comment.