This commit is contained in:
Zhiyuan Zheng 2022-06-14 23:43:11 +02:00
parent 04813cbc78
commit 7c0bcbeb06
5 changed files with 12 additions and 15 deletions

View File

@ -3,11 +3,10 @@ import { FormattedRelativeTime } from 'react-intl'
import { AppState } from 'react-native'
export interface Props {
type: 'past' | 'future'
time: string | number
}
const RelativeTime: React.FC<Props> = ({ type, time }) => {
const RelativeTime: React.FC<Props> = ({ time }) => {
const [now, setNow] = useState(new Date().getTime())
useEffect(() => {
const appStateListener = AppState.addEventListener('change', state => {
@ -21,9 +20,7 @@ const RelativeTime: React.FC<Props> = ({ type, time }) => {
return (
<FormattedRelativeTime
value={
((type === 'past' ? -1 : 1) * (now - new Date(time).getTime())) / 1000
}
value={(new Date(time).getTime() - now) / 1000}
updateIntervalInSeconds={1}
/>
)

View File

@ -32,7 +32,7 @@ const HeaderSharedCreated = React.memo(
/>
</>
) : (
<RelativeTime type='past' time={actualTime} />
<RelativeTime time={actualTime} />
)}
</CustomText>
{edited_at ? (

View File

@ -269,7 +269,7 @@ const TimelinePoll: React.FC<Props> = ({
))
}, [theme, allOptions])
const pollVoteCounts = useMemo(() => {
const pollVoteCounts = () => {
if (poll.voters_count !== null) {
return (
t('shared.poll.meta.count.voters', { count: poll.voters_count }) + ' • '
@ -279,9 +279,9 @@ const TimelinePoll: React.FC<Props> = ({
t('shared.poll.meta.count.votes', { count: poll.votes_count }) + ' • '
)
}
}, [poll.voters_count, poll.votes_count])
}
const pollExpiration = useMemo(() => {
const pollExpiration = () => {
if (poll.expired) {
return t('shared.poll.meta.expiration.expired')
} else {
@ -289,12 +289,12 @@ const TimelinePoll: React.FC<Props> = ({
return (
<Trans
i18nKey='componentTimeline:shared.poll.meta.expiration.until'
components={[<RelativeTime type='future' time={poll.expires_at} />]}
components={[<RelativeTime time={poll.expires_at} />]}
/>
)
}
}
}, [theme, i18n.language, poll.expired, poll.expires_at])
}
return (
<View style={{ marginTop: StyleConstants.Spacing.M }}>
@ -312,8 +312,8 @@ const TimelinePoll: React.FC<Props> = ({
fontStyle='S'
style={{ flexShrink: 1, color: colors.secondary }}
>
{pollVoteCounts}
{pollExpiration}
{pollVoteCounts()}
{pollExpiration()}
</CustomText>
</View>
</View>

View File

@ -139,7 +139,7 @@
},
"expiration": {
"expired": "Vote expired",
"until": "Expires in <0 />"
"until": "Expires <0 />"
}
}
}

View File

@ -93,7 +93,7 @@ const ScreenAnnouncements: React.FC<
<Trans
i18nKey='screenAnnouncements:content.published'
components={[
<RelativeTime type='past' time={item.published_at} />
<RelativeTime time={item.published_at} />
]}
/>
</CustomText>