Skip to content

Commit dff6f30

Browse files
committed
refactor: extract dynamic time-ago logic into a standalone component
### CHANGES - Create PostTimeAgo component for dynamic relative time updates - Replace static date formatting in header with new component - Clean up unused imports and hooks in post components - Remove redundant Chakra UI components from the slug page - Implement interval to refresh relative post timestamps every minute
1 parent 96885ca commit dff6f30

4 files changed

Lines changed: 39 additions & 22 deletions

File tree

src/app/~/[slug]/page.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
import { Metadata } from "next";
22
import { notFound } from "next/navigation";
3-
import { getAllPosts, getPostByFilepath, getPostBySlug } from "@/lib/api";
4-
import { markdownToHtml } from "@/lib/markdown";
5-
import {
6-
Container,
7-
HStack,
8-
IconButton,
9-
Portal,
10-
VStack,
11-
} from "@chakra-ui/react";
3+
import { getAllPosts, getPostBySlug } from "@/lib/api";
4+
import { IconButton, Portal, VStack } from "@chakra-ui/react";
125
import { PostBody } from "@/components/posts/body";
136
import { PostHeader } from "@/components/posts/header";
14-
import { merriweather, merriweatherSans } from "@/lib/fonts";
7+
import { merriweather } from "@/lib/fonts";
158
import Link from "next/link";
169
import { FiArrowLeft } from "react-icons/fi";
1710
import { ReadingTimeProgressBar } from "@/components/reading-progress";

src/components/posts/body.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { merriweather, merriweatherSans, notoSerif } from "@/lib/fonts";
2-
import { Box, Heading, Text } from "@chakra-ui/react";
1+
import { merriweather, notoSerif } from "@/lib/fonts";
2+
import { Heading, Text } from "@chakra-ui/react";
33
import Markdown from "react-markdown";
44
import remarkGfm from "remark-gfm";
5-
import { useWindowScroll } from "react-use";
6-
import { useEffect } from "react";
75

86
type Props = {
97
content: string;

src/components/posts/header.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Author } from "@/interfaces/author";
21
import { Post } from "@/interfaces/post";
3-
import { Badge, Heading, HeadingProps, HStack, Text } from "@chakra-ui/react";
4-
import { format, formatDistance } from "date-fns";
2+
import { Badge, Heading, HeadingProps, HStack } from "@chakra-ui/react";
3+
import { format } from "date-fns";
4+
import { PostTimeAgo } from "../timeago";
55

66
type Props = Post & { _heading: HeadingProps };
77

@@ -21,11 +21,7 @@ export const PostHeader = ({
2121
<Badge size={"lg"}>{author}</Badge>
2222
<Badge size={"lg"}>{stats?.text}</Badge>
2323
<Badge size={"lg"}>{format(date, "dd MMMM yyy")}</Badge>
24-
<Badge size={"lg"}>
25-
{formatDistance(new Date(), new Date(date), {
26-
addSuffix: true,
27-
})}
28-
</Badge>
24+
<PostTimeAgo date={date} />
2925
</HStack>
3026
</>
3127
);

src/components/timeago.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use client";
2+
3+
import { Badge } from "@chakra-ui/react";
4+
import { format, formatDistance } from "date-fns";
5+
import { useEffect, useState } from "react";
6+
7+
type PostTimeAgoProps = {
8+
date: string | Date;
9+
};
10+
11+
export const PostTimeAgo = ({ date }: PostTimeAgoProps) => {
12+
const [timeAgo, setTimeAgo] = useState("");
13+
14+
useEffect(() => {
15+
const updateTime = () => {
16+
setTimeAgo(
17+
formatDistance(new Date(), new Date(date), {
18+
addSuffix: true,
19+
})
20+
);
21+
};
22+
23+
updateTime();
24+
const interval = setInterval(updateTime, 60000);
25+
26+
return () => clearInterval(interval);
27+
}, [date]);
28+
29+
return <Badge size={"lg"}>{timeAgo}</Badge>;
30+
};

0 commit comments

Comments
 (0)