Skip to content

Commit 11075e2

Browse files
ref(js): Convert DateTime to a FC (#34628)
1 parent d410155 commit 11075e2

File tree

1 file changed

+27
-39
lines changed

1 file changed

+27
-39
lines changed

static/app/components/dateTime.tsx

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import {Component} from 'react';
21
import moment from 'moment';
32
import momentTimezone from 'moment-timezone';
43

54
import ConfigStore from 'sentry/stores/configStore';
65

7-
type DefaultProps = {
8-
seconds: boolean;
9-
};
10-
11-
type Props = DefaultProps & {
6+
interface Props extends React.HTMLAttributes<HTMLTimeElement> {
127
date: moment.MomentInput | momentTimezone.MomentInput;
138
dateOnly?: boolean;
149
format?: string;
10+
seconds?: boolean;
1511
shortDate?: boolean;
1612
timeAndDate?: boolean;
1713
timeOnly?: boolean;
1814
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+
}
2816

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 {
2929
if (format) {
3030
return format;
3131
}
@@ -74,31 +74,19 @@ class DateTime extends Component<Props> {
7474

7575
// Default is Oct 26, 2017 11:30 AM
7676
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-
);
10177
}
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+
);
10290
}
10391

10492
export default DateTime;

0 commit comments

Comments
 (0)