11import * as React from 'react' ;
2- import { format , cancel , render } from 'timeago.js' ;
3- import { Opts , TDate } from 'timeago.js/lib/interface' ;
4- export { Opts , TDate } ;
5-
6- /**
7- * Convert input to a valid datetime string of <time> tag
8- * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/time
9- * @param input
10- * @returns datetime string
11- */
12- const toDateTime = ( input : TDate ) : string => {
13- // let date: Date = new Date();
14- // if (input instanceof Date) {
15- // date = input;
16- // //@ts -ignore
17- // } else if (!isNaN(input) || /^\d+$/.test(input)) {
18- // //@ts -ignore
19- // date = new Date(parseInt(input));
20- // } else {
21- // date = new Date(input);
22- // }
23-
24- // try {
25- // return date.toISOString();
26- // } catch (e) {
27- // console.error('invalid datetime');
28- // return '';
29- // }
2+ import timeAgo from 'native-time-ago' ;
303
4+ const toDateTime = ( input : any ) : string => {
315 return '' + ( input instanceof Date ? input . getTime ( ) : input ) ;
326} ;
337
34- // These ts-ignores are to import types from different React versions
35- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
36- // @ts -ignore
37- export interface TimeAgoProps
38- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
39- // @ts -ignore
40- extends React . ComponentProps < 'time' > ,
41- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
42- // @ts -ignore
43- React . DetailedHTMLProps < React . HTMLAttributes < HTMLElement > , HTMLElement > ,
44- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
45- // @ts -ignore
46- React . HTMLProps < HTMLTimeElement > {
47- readonly datetime : TDate ; // date to be formatted
48- readonly live ?: boolean ; // real time render.
49- readonly opts ?: Opts ;
50- readonly locale ?: string ; // locale lang
8+ export interface TimeAgoProps extends React . HTMLAttributes < HTMLTimeElement > {
9+ readonly datetime : any ;
10+ readonly live ?: boolean ;
11+ readonly opts ?: any ; // Not supported in native-time-ago
12+ readonly locale ?: string ;
5113}
5214
5315export default class TimeAgo extends React . PureComponent < TimeAgoProps , unknown > {
5416 static defaultProps = {
55- live : true ,
17+ live : false ,
5618 className : '' ,
5719 } ;
5820
5921 dom : HTMLTimeElement = null ;
22+ intervalId : ReturnType < typeof setInterval > = null ;
6023
6124 componentDidMount ( ) : void {
62- // fixed #6 https://github.com/hustcc/timeago-react/issues/6
63- // to reduce the file size.
64- // const { locale } = this.props;
65- // if (locale !== 'en' && locale !== 'zh_CN') {
66- // timeago.register(locale, require('timeago.js/locales/' + locale));
67- // }
68- // render it.
6925 this . renderTimeAgo ( ) ;
7026 }
7127
@@ -74,26 +30,31 @@ export default class TimeAgo extends React.PureComponent<TimeAgoProps, unknown>
7430 }
7531
7632 renderTimeAgo ( ) : void {
77- const { live, datetime, locale, opts } = this . props ;
78- // cancel all the interval
79- cancel ( this . dom ) ;
80- // if is live
33+ const { live, datetime, locale } = this . props ;
34+ if ( this . intervalId ) {
35+ clearInterval ( this . intervalId ) ;
36+ this . intervalId = null ;
37+ }
8138 if ( live !== false ) {
82- // live render
8339 this . dom . setAttribute ( 'datetime' , toDateTime ( datetime ) ) ;
84-
85- render ( this . dom , locale , opts ) ;
40+ const update = ( ) => {
41+ if ( this . dom ) {
42+ this . dom . textContent = timeAgo ( datetime , locale || 'en' ) ;
43+ }
44+ } ;
45+ update ( ) ;
46+ this . intervalId = setInterval ( update , 60000 ) ;
8647 }
8748 }
8849
89- // remove
9050 componentWillUnmount ( ) : void {
91- cancel ( this . dom ) ;
51+ if ( this . intervalId ) {
52+ clearInterval ( this . intervalId ) ;
53+ this . intervalId = null ;
54+ }
9255 }
9356
94- // for render
9557 render ( ) : React . JSX . Element {
96- // eslint-disable-next-line @typescript-eslint/no-unused-vars
9758 const { datetime, live, locale, opts, ...others } = this . props ;
9859 return (
9960 < time
@@ -102,7 +63,7 @@ export default class TimeAgo extends React.PureComponent<TimeAgoProps, unknown>
10263 } }
10364 { ...others }
10465 >
105- { format ( datetime , locale , opts ) }
66+ { timeAgo ( datetime , locale || 'en' ) }
10667 </ time >
10768 ) ;
10869 }
0 commit comments