-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
deetergp
merged 12 commits into
Expensify:main
from
software-mansion-labs:ts-migration/auto-update-time-component
Dec 14, 2023
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dda39ec
Rename AutoUpdateTime
BartoszGrajdek 59ae8c5
Merge remote-tracking branch 'origin/main' into ts-migration/auto-upd…
BartoszGrajdek 01e00ec
[TS migration] Migrate 'AutoUpdateTime.js' component to TypeScript
BartoszGrajdek f1b3339
fix: prettier
BartoszGrajdek 2522433
Merge remote-tracking branch 'origin/main' into ts-migration/auto-upd…
BartoszGrajdek 7770c89
fix: remove required<> from types
BartoszGrajdek bb6a916
Destructure props
BartoszGrajdek 2ba4c7d
fix: remove type from JSDoc
BartoszGrajdek 9254f76
fix: spelling mistake
BartoszGrajdek ca41e60
Merge remote-tracking branch 'origin/main' into ts-migration/auto-upd…
BartoszGrajdek 3e8dd77
Merge branch 'main' into ts-migration/auto-update-time-component
blazejkustra 4a78732
Merge branch 'main' into ts-migration/auto-update-time-component
blazejkustra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 DateUtils from '@libs/DateUtils'; | ||
import useThemeStyles from '@styles/useThemeStyles'; | ||
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 */ | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this change inspired by a typing problem? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, as |
||
}, [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 | ||
|
@@ -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} | ||
|
@@ -67,6 +60,5 @@ function AutoUpdateTime(props) { | |
); | ||
} | ||
|
||
AutoUpdateTime.propTypes = propTypes; | ||
AutoUpdateTime.displayName = 'AutoUpdateTime'; | ||
export default withLocalize(AutoUpdateTime); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NAB