From 8872de4ad0e4041085580b39dcd7209f46cf839f Mon Sep 17 00:00:00 2001 From: Puyodead1 Date: Fri, 22 Sep 2023 16:48:13 -0400 Subject: [PATCH] timestamps --- src/components/Spoiler.tsx | 10 ++-- src/components/markdown/MarkdownRenderer.tsx | 9 ++-- src/components/markdown/Timestamp.tsx | 53 ++++++++++++++++++++ src/components/messaging/MessageBase.tsx | 7 ++- 4 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/components/markdown/Timestamp.tsx diff --git a/src/components/Spoiler.tsx b/src/components/Spoiler.tsx index 4b329ad..7ae548b 100644 --- a/src/components/Spoiler.tsx +++ b/src/components/Spoiler.tsx @@ -9,12 +9,12 @@ const Container = styled.div` transition: background-color 0.1s ease; &:hover { - background-color: hsl(var(--background-tertiary-hsl) / 0.5); + background-color: hsl(var(--background-tertiary-hsl) / 0.3); cursor: pointer; } &.visible { - background-color: hsl(var(--background-tertiary-hsl) / 0.5); + background-color: hsl(var(--background-tertiary-hsl) / 0.3); cursor: pointer; // target child span @@ -30,17 +30,17 @@ const Text = styled.span` `; interface Props { - content: string; + children: React.ReactNode; } -function Spoiler({ content }: Props) { +function Spoiler({ children }: Props) { const [shown, setShown] = React.useState(false); const show = () => setShown(true); return ( - {content} + {children} ); } diff --git a/src/components/markdown/MarkdownRenderer.tsx b/src/components/markdown/MarkdownRenderer.tsx index c57ed02..3901160 100644 --- a/src/components/markdown/MarkdownRenderer.tsx +++ b/src/components/markdown/MarkdownRenderer.tsx @@ -8,6 +8,7 @@ import CodeBlock from "../Codeblock"; import Link from "../Link"; import Spoiler from "../Spoiler"; import { MarkdownProps } from "./Markdown"; +import Timestamp from "./Timestamp"; const Container = styled.div` // remove the excessive left padding, and margin in lists @@ -129,11 +130,13 @@ const customRenderer: Partial = { if (spoilerRe.test(text)) { // get content inside spoiler tags const spoilerContent = text.match(spoilerRe)![0].slice(2, -2); - return ; + return ; } - if (FormattingPatterns.Timestamp.test(text)) { - return "timestamp baaaaby"; + const match = text.match(FormattingPatterns.Timestamp); + if (match) { + const { timestamp, style } = match.groups || {}; + return ; } return text; diff --git a/src/components/markdown/Timestamp.tsx b/src/components/markdown/Timestamp.tsx new file mode 100644 index 0000000..c60d01d --- /dev/null +++ b/src/components/markdown/Timestamp.tsx @@ -0,0 +1,53 @@ +import dayjs from "dayjs"; +import { memo } from "react"; +import styled from "styled-components"; +import Tooltip from "../Tooltip"; + +const Container = styled.div` + background-color: hsl(var(--background-tertiary-hsl) / 0.3); + padding: 2px; + border-radius: 4px; + width: fit-content; +`; + +interface Props { + timestamp: string; + style: string; +} + +function Timestamp({ timestamp, style }: Props) { + const date = dayjs.unix(Number(timestamp)); + + let value = ""; + switch (style) { + case "t": + value = date.format("hh:mm"); + break; + case "T": + value = date.format("hh:mm:ss"); + break; + case "R": + value = date.fromNow(); + break; + case "D": + value = date.format("DD MMMM YYYY"); + break; + case "F": + value = date.format("dddd, DD MMMM YYYY hh:mm"); + break; + case "f": + default: + value = date.format("DD MMMM YYYY hh:mm"); + break; + } + + return ( + + + {value} + + + ); +} + +export default memo(Timestamp); diff --git a/src/components/messaging/MessageBase.tsx b/src/components/messaging/MessageBase.tsx index 3980002..38a8add 100644 --- a/src/components/messaging/MessageBase.tsx +++ b/src/components/messaging/MessageBase.tsx @@ -94,13 +94,16 @@ export const MessageDetails = observer(({ message, position }: { message: Messag if (message instanceof Message && message.edited_timestamp) { return (
- + - + (edited)