|
1 | | -import {Component} from 'react'; |
2 | 1 | import moment from 'moment'; |
3 | 2 | import momentTimezone from 'moment-timezone'; |
4 | 3 |
|
5 | 4 | import ConfigStore from 'sentry/stores/configStore'; |
6 | 5 |
|
7 | | -type DefaultProps = { |
8 | | - seconds: boolean; |
9 | | -}; |
10 | | - |
11 | | -type Props = DefaultProps & { |
| 6 | +interface Props extends React.HTMLAttributes<HTMLTimeElement> { |
12 | 7 | date: moment.MomentInput | momentTimezone.MomentInput; |
13 | 8 | dateOnly?: boolean; |
14 | 9 | format?: string; |
| 10 | + seconds?: boolean; |
15 | 11 | shortDate?: boolean; |
16 | 12 | timeAndDate?: boolean; |
17 | 13 | timeOnly?: boolean; |
18 | 14 | utc?: boolean; |
19 | | -}; |
20 | | - |
21 | | -class DateTime extends Component<Props> { |
22 | | - static defaultProps: DefaultProps = { |
23 | | - seconds: true, |
24 | | - }; |
25 | | - |
26 | | - getFormat = ({clock24Hours}: {clock24Hours: boolean}): string => { |
27 | | - const {dateOnly, timeOnly, seconds, shortDate, timeAndDate, format} = this.props; |
| 15 | +} |
28 | 16 |
|
| 17 | +function DateTime({ |
| 18 | + format, |
| 19 | + date, |
| 20 | + utc, |
| 21 | + shortDate, |
| 22 | + dateOnly, |
| 23 | + timeOnly, |
| 24 | + timeAndDate, |
| 25 | + seconds = true, |
| 26 | + ...props |
| 27 | +}: Props) { |
| 28 | + function getFormat({clock24Hours}: {clock24Hours: boolean}): string { |
29 | 29 | if (format) { |
30 | 30 | return format; |
31 | 31 | } |
@@ -74,31 +74,19 @@ class DateTime extends Component<Props> { |
74 | 74 |
|
75 | 75 | // Default is Oct 26, 2017 11:30 AM |
76 | 76 | return 'lll'; |
77 | | - }; |
78 | | - |
79 | | - render() { |
80 | | - const { |
81 | | - date, |
82 | | - utc, |
83 | | - seconds: _seconds, |
84 | | - shortDate: _shortDate, |
85 | | - dateOnly: _dateOnly, |
86 | | - timeOnly: _timeOnly, |
87 | | - timeAndDate: _timeAndDate, |
88 | | - ...carriedProps |
89 | | - } = this.props; |
90 | | - const user = ConfigStore.get('user'); |
91 | | - const options = user?.options; |
92 | | - const format = this.getFormat(options); |
93 | | - |
94 | | - return ( |
95 | | - <time {...carriedProps}> |
96 | | - {utc |
97 | | - ? moment.utc(date as moment.MomentInput).format(format) |
98 | | - : momentTimezone.tz(date, options?.timezone ?? '').format(format)} |
99 | | - </time> |
100 | | - ); |
101 | 77 | } |
| 78 | + |
| 79 | + const user = ConfigStore.get('user'); |
| 80 | + const options = user?.options; |
| 81 | + const formatString = getFormat(options); |
| 82 | + |
| 83 | + return ( |
| 84 | + <time {...props}> |
| 85 | + {utc |
| 86 | + ? moment.utc(date as moment.MomentInput).format(formatString) |
| 87 | + : momentTimezone.tz(date, options?.timezone ?? '').format(formatString)} |
| 88 | + </time> |
| 89 | + ); |
102 | 90 | } |
103 | 91 |
|
104 | 92 | export default DateTime; |
0 commit comments