From d007d7d2e119608237a5dc38df6fa61596ef0076 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Wed, 18 May 2022 21:14:31 +0800 Subject: [PATCH] client: update locale-time --- .../src/components/locale-time/index.tsx | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/packages/client/src/components/locale-time/index.tsx b/packages/client/src/components/locale-time/index.tsx index 16b4045..e8c619e 100644 --- a/packages/client/src/components/locale-time/index.tsx +++ b/packages/client/src/components/locale-time/index.tsx @@ -1,7 +1,9 @@ -import dateFormat from 'date-fns/format'; -import distanceInWords from 'date-fns/formatDistance'; -import zh from 'date-fns/locale/zh-CN'; -import React, { useEffect, useRef, useState } from 'react'; +import React, { useEffect, useMemo, useRef, useState } from 'react'; +import * as timeagojs from 'timeago.js'; + +type Props = { + date: string | number | Date; +}; let callbacks: Array<() => void> = []; @@ -17,26 +19,14 @@ function eachMinute(fn: () => void) { }; } -type Props = { - date: string | number | Date; - format?: string; - timeago?: boolean; -}; - const getTimeago = (date: number | string | Date) => { - let content = distanceInWords(new Date(date), new Date(), { - addSuffix: true, - locale: zh, - }); - - content = content.replace('about', '').replace('less than a minute ago', 'just now').replace('minute', 'min'); - - return content; + return timeagojs.format(date, 'zh_CN'); }; -export const LocaleTime: React.FC = ({ date, timeago, format = 'yyyy-MM-dd HH:mm:ss' }) => { +export const LocaleTime: React.FC = ({ date }) => { const [, setMinutesMounted] = useState(0); const callback = useRef<() => void>(); + const formatDate = useMemo(() => new Date(date).toLocaleDateString(), [date]); useEffect(() => { callback.current = eachMinute(() => { @@ -50,7 +40,5 @@ export const LocaleTime: React.FC = ({ date, timeago, format = 'yyyy-MM-d }; }, []); - const formated = dateFormat(new Date(date), format); - - return ; + return ; };