mirror of
https://gitea.tendokyu.moe/ppc/amnet.git
synced 2026-09-27 00:50:06 +03:00
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import React, {useEffect, useState} from 'react';
|
|
import {StyleProp, TextStyle} from 'react-native';
|
|
import {formatDistance} from 'date-fns/formatDistance';
|
|
import {Paragraph} from "react-native-paper";
|
|
|
|
interface Props {
|
|
dateTo: Date;
|
|
updateInterval?: number;
|
|
dateFrom?: Date;
|
|
hideAgo?: boolean;
|
|
style?: StyleProp<TextStyle>;
|
|
}
|
|
|
|
const TimeAgo: React.FC<Props> = ({
|
|
dateTo,
|
|
dateFrom,
|
|
updateInterval = 60000,
|
|
hideAgo = false,
|
|
style,
|
|
}) => {
|
|
const [currentDate, setCurrentDate] = useState(new Date());
|
|
|
|
useEffect(() => {
|
|
const interval = setInterval(() => {
|
|
if (!dateFrom) setCurrentDate(new Date());
|
|
}, updateInterval);
|
|
return () => clearInterval(interval);
|
|
}, [dateFrom, updateInterval]);
|
|
|
|
return (
|
|
<Paragraph {...{style}}>
|
|
{formatDistance(dateTo, dateFrom || currentDate, {
|
|
addSuffix: !hideAgo,
|
|
})}
|
|
</Paragraph>
|
|
);
|
|
};
|
|
|
|
export default TimeAgo;
|