From 2cb240eaa56d5b3caff9cf5f8522f15575bb4523 Mon Sep 17 00:00:00 2001 From: Tristan Yang Date: Mon, 13 Feb 2023 07:36:11 +0800 Subject: [PATCH] refactor: fromNowTime --- src/common/component/FileBox/index.tsx | 5 ++--- src/common/component/FileMessage/index.tsx | 5 ++--- src/common/utils.tsx | 7 +++++++ src/routes/chat/GuestSessionList/Session.tsx | 4 ++-- src/routes/chat/SessionList/Session.tsx | 6 +++--- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/common/component/FileBox/index.tsx b/src/common/component/FileBox/index.tsx index 2c0b8953..8ad2d231 100644 --- a/src/common/component/FileBox/index.tsx +++ b/src/common/component/FileBox/index.tsx @@ -1,5 +1,4 @@ import { FC, ReactElement } from "react"; -import dayjs from "dayjs"; import clsx from "clsx"; import { VideoPreview, @@ -9,7 +8,7 @@ import { CodePreview, DocPreview } from "./preview"; -import { getFileIcon, formatBytes } from "../../utils"; +import { getFileIcon, formatBytes, fromNowTime } from "../../utils"; import IconDownload from "../../../assets/icons/download.svg"; import { useAppSelector } from "../../../app/store"; @@ -95,7 +94,7 @@ const FileBox: FC = ({ {name} {formatBytes(size)} - {dayjs(created_at).fromNow()} + {fromNowTime(created_at)} by {fromUser.name} diff --git a/src/common/component/FileMessage/index.tsx b/src/common/component/FileMessage/index.tsx index d01a448f..03392ae6 100644 --- a/src/common/component/FileMessage/index.tsx +++ b/src/common/component/FileMessage/index.tsx @@ -1,11 +1,10 @@ import { FC, useEffect, useState } from "react"; -import dayjs from "dayjs"; import ImageMessage from "./ImageMessage"; import useRemoveLocalMessage from "../../hook/useRemoveLocalMessage"; import useUploadFile from "../../hook/useUploadFile"; import useSendMessage from "../../hook/useSendMessage"; import Progress from "./Progress"; -import { getFileIcon, formatBytes, isImage, getImageSize } from "../../utils"; +import { getFileIcon, formatBytes, isImage, getImageSize, fromNowTime } from "../../utils"; import { useAppSelector } from "../../../app/store"; import IconDownload from "../../../assets/icons/download.svg"; import IconClose from "../../../assets/icons/close.circle.svg"; @@ -167,7 +166,7 @@ const FileMessage: FC = ({ ) : ( <> {formatBytes(size)} - {dayjs(created_at).fromNow()} + {fromNowTime(created_at)} {fromUser && ( by {fromUser.name} diff --git a/src/common/utils.tsx b/src/common/utils.tsx index 685cdf39..3546c2c8 100644 --- a/src/common/utils.tsx +++ b/src/common/utils.tsx @@ -1,3 +1,4 @@ +import dayjs from "dayjs"; import BASE_URL, { FILE_IMAGE_SIZE, ContentTypes, KEY_TOKEN, KEY_REFRESH_TOKEN, KEY_EXPIRE } from "../app/config"; import IconPdf from "../assets/icons/file.pdf.svg"; import IconAudio from "../assets/icons/file.audio.svg"; @@ -383,4 +384,10 @@ export const isDarkMode = () => { return isDarkMode || (!isLightMode && window.matchMedia('(prefers-color-scheme: dark)').matches); +}; + +export const fromNowTime = (ts?: number) => { + if (!ts) return null; + const currTS = + new Date(); + return dayjs(ts > currTS ? currTS : ts).fromNow(); }; \ No newline at end of file diff --git a/src/routes/chat/GuestSessionList/Session.tsx b/src/routes/chat/GuestSessionList/Session.tsx index d5b3036f..780a223f 100644 --- a/src/routes/chat/GuestSessionList/Session.tsx +++ b/src/routes/chat/GuestSessionList/Session.tsx @@ -1,11 +1,11 @@ // @ts-nocheck import { useState, useEffect, FC } from "react"; -import dayjs from "dayjs"; import clsx from "clsx"; import { renderPreviewMessage } from "../../chat/utils"; import Avatar from "../../../common/component/Avatar"; import { NavLink } from "react-router-dom"; import { useAppSelector } from "../../../app/store"; +import { fromNowTime } from "../../../common/utils"; interface IProps { id: number; @@ -49,7 +49,7 @@ const Session: FC = ({ id, mid }) => { {name} - {previewMsg.created_at ? dayjs(previewMsg.created_at).fromNow() : null} + {fromNowTime(previewMsg.created_at)}
diff --git a/src/routes/chat/SessionList/Session.tsx b/src/routes/chat/SessionList/Session.tsx index 7d900b2b..1436c212 100644 --- a/src/routes/chat/SessionList/Session.tsx +++ b/src/routes/chat/SessionList/Session.tsx @@ -1,7 +1,6 @@ // @ts-nocheck import { useState, useEffect, FC } from "react"; import clsx from "clsx"; -import dayjs from "dayjs"; import { useDrop } from "react-dnd"; import { NativeTypes } from "react-dnd-html5-backend"; import { useNavigate, NavLink } from "react-router-dom"; @@ -13,6 +12,7 @@ import IconLock from "../../../assets/icons/lock.svg"; import useContextMenu from "../../../common/hook/useContextMenu"; import useUploadFile from "../../../common/hook/useUploadFile"; import { useAppSelector } from "../../../app/store"; +import { fromNowTime } from "../../../common/utils"; interface IProps { type?: "user" | "channel"; @@ -135,8 +135,8 @@ const Session: FC = ({ {!is_public && } - - {previewMsg.created_at ? dayjs(previewMsg.created_at).fromNow() : null} + + {fromNowTime(previewMsg.created_at)}