-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
index.tsx
130 lines (113 loc) · 4.79 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* 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 { pick } from 'lodash/fp';
import { EuiProgress } from '@elastic/eui';
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { useDispatch } from 'react-redux';
import styled from 'styled-components';
import { timelineActions, timelineSelectors } from '../../store/timeline';
import { timelineDefaults } from '../../../timelines/store/timeline/defaults';
import { defaultHeaders } from './body/column_headers/default_headers';
import { isTab } from '../../../common/components/accessibility/helpers';
import { useSourcererScope } from '../../../common/containers/sourcerer';
import { SourcererScopeName } from '../../../common/store/sourcerer/model';
import { FlyoutHeader, FlyoutHeaderPanel } from '../flyout/header';
import { TimelineType, TimelineId } from '../../../../common/types/timeline';
import { useDeepEqualSelector, useShallowEqualSelector } from '../../../common/hooks/use_selector';
import { activeTimeline } from '../../containers/active_timeline_context';
import { EVENTS_COUNT_BUTTON_CLASS_NAME, onTimelineTabKeyPressed } from './helpers';
import * as i18n from './translations';
import { TabsContent } from './tabs_content';
import { HideShowContainer, TimelineContainer } from './styles';
import { useTimelineFullScreen } from '../../../common/containers/use_full_screen';
const TimelineTemplateBadge = styled.div`
background: ${({ theme }) => theme.eui.euiColorVis3_behindText};
color: #fff;
padding: 10px 15px;
font-size: 0.8em;
`;
export interface Props {
timelineId: TimelineId;
}
const TimelineSavingProgressComponent: React.FC<Props> = ({ timelineId }) => {
const getTimeline = useMemo(() => timelineSelectors.getTimelineByIdSelector(), []);
const isSaving = useShallowEqualSelector(
(state) => (getTimeline(state, timelineId) ?? timelineDefaults).isSaving
);
return isSaving ? <EuiProgress size="s" color="primary" position="absolute" /> : null;
};
const TimelineSavingProgress = React.memo(TimelineSavingProgressComponent);
const StatefulTimelineComponent: React.FC<Props> = ({ timelineId }) => {
const dispatch = useDispatch();
const containerElement = useRef<HTMLDivElement | null>(null);
const getTimeline = useMemo(() => timelineSelectors.getTimelineByIdSelector(), []);
const { selectedPatterns } = useSourcererScope(SourcererScopeName.timeline);
const { graphEventId, savedObjectId, timelineType } = useDeepEqualSelector((state) =>
pick(
['graphEventId', 'savedObjectId', 'timelineType'],
getTimeline(state, timelineId) ?? timelineDefaults
)
);
const { timelineFullScreen } = useTimelineFullScreen();
useEffect(() => {
if (!savedObjectId) {
dispatch(
timelineActions.createTimeline({
id: timelineId,
columns: defaultHeaders,
indexNames: selectedPatterns,
expandedDetail: activeTimeline.getExpandedDetail(),
show: false,
})
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const onSkipFocusBeforeEventsTable = useCallback(() => {
containerElement.current
?.querySelector<HTMLButtonElement>('.globalFilterBar__addButton')
?.focus();
}, [containerElement]);
const onSkipFocusAfterEventsTable = useCallback(() => {
containerElement.current
?.querySelector<HTMLButtonElement>(`.${EVENTS_COUNT_BUTTON_CLASS_NAME}`)
?.focus();
}, [containerElement]);
const onKeyDown = useCallback(
(keyboardEvent: React.KeyboardEvent) => {
if (isTab(keyboardEvent)) {
onTimelineTabKeyPressed({
containerElement: containerElement.current,
keyboardEvent,
onSkipFocusBeforeEventsTable,
onSkipFocusAfterEventsTable,
});
}
},
[containerElement, onSkipFocusBeforeEventsTable, onSkipFocusAfterEventsTable]
);
return (
<TimelineContainer
data-test-subj="timeline"
data-timeline-id={timelineId}
onKeyDown={onKeyDown}
ref={containerElement}
>
<TimelineSavingProgress timelineId={timelineId} />
{timelineType === TimelineType.template && (
<TimelineTemplateBadge>{i18n.TIMELINE_TEMPLATE}</TimelineTemplateBadge>
)}
<FlyoutHeaderPanel timelineId={timelineId} />
<HideShowContainer $isVisible={!timelineFullScreen}>
<FlyoutHeader timelineId={timelineId} />
</HideShowContainer>
<TabsContent graphEventId={graphEventId} timelineId={timelineId} />
</TimelineContainer>
);
};
StatefulTimelineComponent.displayName = 'StatefulTimelineComponent';
export const StatefulTimeline = React.memo(StatefulTimelineComponent);