Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'AutoUpdateTime.js' component to TypeScript #31521

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,35 @@
* Displays the user's local time and updates it every minute.
* The time auto-update logic is extracted to this component to avoid re-rendering a more complex component, e.g. DetailsPage.
*/
import PropTypes from 'prop-types';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
import useThemeStyles from '@hooks/useThemeStyles';
import DateUtils from '@libs/DateUtils';
import {Timezone} from '@src/types/onyx/PersonalDetails';
import Text from './Text';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import withLocalize, {WithLocalizeProps} from './withLocalize';

const propTypes = {
type AutoUpdateTimeProps = WithLocalizeProps & {
/** Timezone of the user from their personal details */
timezone: PropTypes.shape({
/** Value of selected timezone */
selected: PropTypes.string,

/** Whether timezone is automatically set */
automatic: PropTypes.bool,
}).isRequired,
...withLocalizePropTypes,
timezone: Timezone;
};

function AutoUpdateTime(props) {
function AutoUpdateTime({timezone, preferredLocale, translate}: AutoUpdateTimeProps) {
const styles = useThemeStyles();
/**
* @returns {Date} Returns the locale Date object
*/
const getCurrentUserLocalTime = useCallback(
() => DateUtils.getLocalDateFromDatetime(props.preferredLocale, null, props.timezone.selected),
[props.preferredLocale, props.timezone.selected],
);
/** @returns Returns the locale Date object */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** @returns Returns the locale Date object */
/** @returns Returns the locale Date object */

NAB

const getCurrentUserLocalTime = useCallback(() => DateUtils.getLocalDateFromDatetime(preferredLocale, undefined, timezone.selected), [preferredLocale, timezone.selected]);

const [currentUserLocalTime, setCurrentUserLocalTime] = useState(getCurrentUserLocalTime);
const minuteRef = useRef(new Date().getMinutes());
const timezoneName = useMemo(() => DateUtils.getZoneAbbreviation(currentUserLocalTime, props.timezone.selected), [currentUserLocalTime, props.timezone.selected]);
const timezoneName = useMemo(() => {
if (timezone.selected) {
return DateUtils.getZoneAbbreviation(currentUserLocalTime, timezone.selected);
}
return '';
Comment on lines +26 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change inspired by a typing problem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as getZoneAbbreviation expects that timezone is defined

}, [currentUserLocalTime, timezone.selected]);

useEffect(() => {
// If the any of the props that getCurrentUserLocalTime depends on change, we want to update the displayed time immediately
// If any of the props that getCurrentUserLocalTime depends on change, we want to update the displayed time immediately
setCurrentUserLocalTime(getCurrentUserLocalTime());

// Also, if the user leaves this page open, we want to make sure the displayed time is updated every minute when the clock changes
Expand All @@ -58,7 +51,7 @@ function AutoUpdateTime(props) {
style={[styles.textLabelSupporting, styles.mb1]}
numberOfLines={1}
>
{props.translate('detailsPage.localTime')}
{translate('detailsPage.localTime')}
</Text>
<Text numberOfLines={1}>
{DateUtils.formatToLocalTime(currentUserLocalTime)} {timezoneName}
Expand All @@ -67,6 +60,5 @@ function AutoUpdateTime(props) {
);
}

AutoUpdateTime.propTypes = propTypes;
AutoUpdateTime.displayName = 'AutoUpdateTime';
export default withLocalize(AutoUpdateTime);
6 changes: 3 additions & 3 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function setLocale(localeString: Locale) {
* Gets the user's stored time zone NVP and returns a localized
* Date object for the given ISO-formatted datetime string
*/
function getLocalDateFromDatetime(locale: Locale, datetime: string, currentSelectedTimezone: SelectedTimezone = timezone.selected): Date {
function getLocalDateFromDatetime(locale: Locale, datetime?: string, currentSelectedTimezone: SelectedTimezone = timezone.selected): Date {
setLocale(locale);
if (!datetime) {
return utcToZonedTime(new Date(), currentSelectedTimezone);
Expand Down Expand Up @@ -209,7 +209,7 @@ function datetimeToRelative(locale: Locale, datetime: string): string {
* @param selectedTimezone
* @returns
*/
function getZoneAbbreviation(datetime: string, selectedTimezone: SelectedTimezone): string {
function getZoneAbbreviation(datetime: string | Date, selectedTimezone: SelectedTimezone): string {
return formatInTimeZone(datetime, selectedTimezone, 'zzz');
}

Expand All @@ -236,7 +236,7 @@ function formatToDayOfWeek(datetime: string): string {
*
* @returns 2:30 PM
*/
function formatToLocalTime(datetime: string): string {
function formatToLocalTime(datetime: string | Date): string {
return format(new Date(datetime), CONST.DATE.LOCAL_TIME_FORMAT);
}

Expand Down