1
1
import { TFunction } from 'i18next' ;
2
- import React , { useContext } from 'react' ;
2
+ import React , { useContext , useState } from 'react' ;
3
3
import { useTranslation } from 'react-i18next' ;
4
4
import spacetime from 'spacetime' ;
5
5
import styled from 'styled-components' ;
6
6
import Filter from '@components/FilterInput/Filter' ;
7
- import {
8
- FilterClickableRow ,
9
- FilterPopupTrailing ,
10
- FilterSeparator ,
11
- FilterTextRow ,
12
- } from '@components/FilterInput/FilterRows' ;
7
+ import { FilterClickableRow , FilterSeparator , FilterText } from '@components/FilterInput/FilterRows' ;
13
8
import DateInput from '@components/Form/DateInput' ;
9
+ import HeightAnimatedContainer from '@components/HeightAnimatedContainer' ;
10
+ import Icon from '@components/Icon' ;
14
11
import { TimezoneContext } from '@components/TimezoneProvider' ;
15
- import { getDateTimeLocalString , getTimeFromPastByDays , getTimeRangeString } from '@utils/date' ;
12
+ import { getDateTimeLocalString , getTimeFromPastByDays } from '@utils/date' ;
16
13
17
14
export type TimerangeValues = { start : number | null ; end : number | null } ;
18
15
type Props = {
@@ -30,6 +27,7 @@ const TimerangeSelection: React.FC<Props> = ({ value, onChange }) => {
30
27
< Filter
31
28
label = "Time frame"
32
29
value = { labelValue }
30
+ onClear = { ( ) => onChange ( { start : null , end : null } ) }
33
31
content = { ( { onClose } ) => (
34
32
< TimeRangePopup label = { labelValue } value = { value } onChange = { onChange } onClose = { onClose } />
35
33
) }
@@ -43,9 +41,11 @@ const TimeRangePopup: React.FC<{
43
41
label : string | null ;
44
42
onChange : ( args : TimerangeValues ) => void ;
45
43
onClose : ( ) => void ;
46
- } > = ( { value, onChange , onClose } ) => {
44
+ } > = ( { value, label , onChange } ) => {
47
45
const { t } = useTranslation ( ) ;
48
46
const { timezone } = useContext ( TimezoneContext ) ;
47
+ const [ customOpen , setCustomOpen ] = useState ( label === t ( 'date.custom' ) ) ;
48
+
49
49
const presets = [
50
50
{ label : t ( 'date.today' ) , start : getTimeFromPastByDays ( 0 , timezone ) , end : null } ,
51
51
{ label : t ( 'date.yesterday' ) , start : getTimeFromPastByDays ( 1 , timezone ) , end : getTimeFromPastByDays ( 0 , timezone ) } ,
@@ -61,31 +61,34 @@ const TimeRangePopup: React.FC<{
61
61
</ FilterClickableRow >
62
62
) ) }
63
63
< FilterSeparator />
64
- < FilterTextRow > { t ( 'filters.custom' ) } </ FilterTextRow >
65
-
66
- < TimerangeFields >
67
- < DateInput
68
- inputType = "datetime-local"
69
- onChange = { ( newValue ) => onChange ( { ...value , start : newValue ? spacetime ( newValue , timezone ) . epoch : null } ) }
70
- initialValue = { value . start ? getDateTimeLocalString ( new Date ( value . start ) , timezone ) : undefined }
71
- />
64
+ < FilterClickableRow onClick = { ( ) => setCustomOpen ( ! customOpen ) } >
65
+ < CollapseRow >
66
+ < FilterText > { t ( 'filters.custom' ) } </ FilterText >
67
+ < Icon name = "chevron" size = "xs" rotate = { customOpen ? - 180 : 0 } />
68
+ </ CollapseRow >
69
+ </ FilterClickableRow >
72
70
73
- < DateInput
74
- inputType = "datetime-local"
75
- onChange = { ( newValue ) => onChange ( { ...value , end : newValue ? spacetime ( newValue , timezone ) . epoch : null } ) }
76
- initialValue = { value . end ? getDateTimeLocalString ( new Date ( value . end ) , timezone ) : undefined }
77
- />
78
- </ TimerangeFields >
71
+ < HeightAnimatedContainer >
72
+ { customOpen && (
73
+ < TimerangeFields >
74
+ < DateInput
75
+ inputType = "datetime-local"
76
+ onChange = { ( newValue ) =>
77
+ onChange ( { ...value , start : newValue ? spacetime ( newValue , timezone ) . epoch : null } )
78
+ }
79
+ initialValue = { value . start ? getDateTimeLocalString ( new Date ( value . start ) , timezone ) : undefined }
80
+ />
79
81
80
- < FilterPopupTrailing
81
- clear = { {
82
- onClick : ( ) => {
83
- onChange ( { start : null , end : null } ) ;
84
- onClose ( ) ;
85
- } ,
86
- disabled : value . start === null && value . end === null ,
87
- } }
88
- />
82
+ < DateInput
83
+ inputType = "datetime-local"
84
+ onChange = { ( newValue ) =>
85
+ onChange ( { ...value , end : newValue ? spacetime ( newValue , timezone ) . epoch : null } )
86
+ }
87
+ initialValue = { value . end ? getDateTimeLocalString ( new Date ( value . end ) , timezone ) : undefined }
88
+ />
89
+ </ TimerangeFields >
90
+ ) }
91
+ </ HeightAnimatedContainer >
89
92
</ div >
90
93
) ;
91
94
} ;
@@ -111,19 +114,21 @@ function formatString(value: TimerangeValues, timezone: string, t: TFunction): s
111
114
return t ( 'date.yesterday' ) ;
112
115
}
113
116
114
- return getRawDateString ( value , timezone ) ;
115
- }
116
-
117
- function getRawDateString ( value : TimerangeValues , timezone : string ) : string {
118
- return `${ value . start ? getTimeRangeString ( new Date ( value . start ) , timezone ) : '' } - ${
119
- value . end ? getTimeRangeString ( new Date ( value . end ) , timezone ) : ''
120
- } `;
117
+ return t ( 'date.custom' ) ;
121
118
}
122
119
123
120
const TimerangeFields = styled . div `
124
121
display: flex;
125
122
flex-direction: column;
126
123
gap: 0.5rem;
124
+ margin-top: 0.5rem;
125
+ ` ;
126
+
127
+ const CollapseRow = styled . div `
128
+ width: 100%;
129
+ display: flex;
130
+ justify-content: space-between;
131
+ align-items: center;
127
132
` ;
128
133
129
134
export default TimerangeSelection ;
0 commit comments