Skip to content

Commit e249a07

Browse files
authored
[fix] Post Sorting and Timestamp Display in Your Posts Section (#1095)
* [fix] Post Sorting and Timestamp Display in Your Posts Section
1 parent d2cdf7c commit e249a07

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

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

Lines changed: 35 additions & 24 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 }) => {
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,31 +159,23 @@ 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>
157-
Published on{" "}
158-
{new Date(published).toLocaleDateString()} at{" "}
159-
{new Date(published).toLocaleTimeString(
160-
navigator.language,
161-
{
162-
hour: "2-digit",
163-
minute: "2-digit",
164-
hour12: false,
165-
},
166+
<>
167+
{/*If updatedAt is greater than published by more than on minutes show updated at else show published
168+
as on updating published updatedAt is automatically updated and is greater than published*/}
169+
{new Date(updatedAt).getTime() -
170+
new Date(published).getTime() >=
171+
60000 ? (
172+
<>{renderDate("Last updated on ", updatedAt)}</>
173+
) : (
174+
<>{renderDate("Published on ", published)}</>
166175
)}
167-
</small>
176+
</>
177+
) : postStatus === PostStatus.DRAFT ? (
178+
<>{renderDate("Last updated on ", updatedAt)}</>
168179
) : null}
169180
</div>
170181

server/api/router/post.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ export const postRouter = createTRPCRouter({
395395
lte(posts.published, new Date().toISOString()),
396396
eq(posts.userId, ctx?.session?.user?.id),
397397
),
398-
orderBy: (posts, { desc }) => [desc(posts.published)],
398+
orderBy: (posts, { desc, sql }) => [
399+
desc(sql`GREATEST(${posts.updatedAt}, ${posts.published})`),
400+
],
399401
});
400402
}),
401403
myScheduled: protectedProcedure.query(async ({ ctx }) => {
@@ -413,6 +415,7 @@ export const postRouter = createTRPCRouter({
413415
return ctx.db.query.post.findMany({
414416
where: (posts, { eq }) =>
415417
and(eq(posts.userId, ctx.session.user.id), isNull(posts.published)),
418+
orderBy: (posts, { desc }) => [desc(posts.updatedAt)],
416419
});
417420
}),
418421
editDraft: protectedProcedure

0 commit comments

Comments
 (0)