Skip to content

Commit ca2b84d

Browse files
committed
[fix] Post Sorting and Timestamp Display in Your Posts Section
1 parent cb1070e commit ca2b84d

File tree

1 file changed

+33
-48
lines changed

1 file changed

+33
-48
lines changed

app/(app)/my-posts/_client.tsx

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ function classNames(...classes: string[]) {
2424
return classes.filter(Boolean).join(" ");
2525
}
2626

27+
const renderDate = (label: string, date: string | Date) => (
28+
<small>
29+
{label} {new Date(date).toLocaleDateString()} at{" "}
30+
{new Date(date).toLocaleTimeString(navigator.language, {
31+
hour: "2-digit",
32+
minute: "2-digit",
33+
hour12: false,
34+
})}
35+
</small>
36+
);
37+
2738
const MyPosts = () => {
2839
const searchParams = useSearchParams();
2940

@@ -113,7 +124,15 @@ const MyPosts = () => {
113124

114125
{selectedTabData.status === "success" &&
115126
selectedTabData.data?.map(
116-
({ id, title, excerpt, readTimeMins, slug, published, updatedAt }) => {
127+
({
128+
id,
129+
title,
130+
excerpt,
131+
readTimeMins,
132+
slug,
133+
published,
134+
updatedAt,
135+
}) => {
117136
const postStatus = published
118137
? getPostStatus(new Date(published))
119138
: PostStatus.DRAFT;
@@ -140,58 +159,24 @@ const MyPosts = () => {
140159
<div className="flex items-center">
141160
<div className="flex-grow">
142161
{published && postStatus === PostStatus.SCHEDULED ? (
143-
<small>
144-
Scheduled to publish on{" "}
145-
{new Date(published).toLocaleDateString()} at{" "}
146-
{new Date(published).toLocaleTimeString(
147-
navigator.language,
148-
{
149-
hour: "2-digit",
150-
minute: "2-digit",
151-
hour12: false,
152-
},
153-
)}
154-
</small>
162+
<>
163+
{renderDate("Scheduled to publish on ", published)}
164+
</>
155165
) : published && postStatus === PostStatus.PUBLISHED ? (
156-
<small>
166+
<>
157167
{/*If updatedAt is greater than published by more than on minutes show updated at else show published
158168
as on updating published updatedAt is automatically updated and is greater than published*/}
159-
{(new Date(updatedAt).getTime() - new Date(published).getTime()) >= 60000 ? (
160-
<>
161-
{"Last updated on "}
162-
{new Date(updatedAt).toLocaleDateString()} at{" "}
163-
{new Date(updatedAt).toLocaleTimeString(navigator.language, {
164-
hour: "2-digit",
165-
minute: "2-digit",
166-
hour12: false,
167-
})}
168-
</>
169+
{new Date(updatedAt).getTime() -
170+
new Date(published).getTime() >=
171+
60000 ? (
172+
<>{renderDate("Last updated on ", updatedAt)}</>
169173
) : (
170-
<>
171-
{"Published on "}
172-
{new Date(published).toLocaleDateString()} at{" "}
173-
{new Date(published).toLocaleTimeString(navigator.language, {
174-
hour: "2-digit",
175-
minute: "2-digit",
176-
hour12: false,
177-
})}
178-
</>
174+
<>{renderDate("Published on ", published)}</>
179175
)}
180-
</small>
181-
): postStatus === PostStatus.DRAFT ? (
182-
<small>
183-
Last Updated on {" "}
184-
{new Date(updatedAt).toLocaleDateString()} at{" "}
185-
{new Date(updatedAt).toLocaleTimeString(
186-
navigator.language,
187-
{
188-
hour: "2-digit",
189-
minute: "2-digit",
190-
hour12: false,
191-
},
192-
)}
193-
</small>
194-
): null}
176+
</>
177+
) : postStatus === PostStatus.DRAFT ? (
178+
<>{renderDate("Last updated on ", updatedAt)}</>
179+
) : null}
195180
</div>
196181

197182
<Menu

0 commit comments

Comments
 (0)