diff --git a/app/(app)/my-posts/_client.tsx b/app/(app)/my-posts/_client.tsx index 49389e0d..0f50593d 100644 --- a/app/(app)/my-posts/_client.tsx +++ b/app/(app)/my-posts/_client.tsx @@ -24,6 +24,17 @@ function classNames(...classes: string[]) { return classes.filter(Boolean).join(" "); } +const renderDate = (label: string, date: string | Date) => ( + + {label} {new Date(date).toLocaleDateString()} at{" "} + {new Date(date).toLocaleTimeString(navigator.language, { + hour: "2-digit", + minute: "2-digit", + hour12: false, + })} + +); + const MyPosts = () => { const searchParams = useSearchParams(); @@ -113,7 +124,15 @@ const MyPosts = () => { {selectedTabData.status === "success" && selectedTabData.data?.map( - ({ id, title, excerpt, readTimeMins, slug, published }) => { + ({ + id, + title, + excerpt, + readTimeMins, + slug, + published, + updatedAt, + }) => { const postStatus = published ? getPostStatus(new Date(published)) : PostStatus.DRAFT; @@ -140,31 +159,23 @@ const MyPosts = () => {
{published && postStatus === PostStatus.SCHEDULED ? ( - - Scheduled to publish on{" "} - {new Date(published).toLocaleDateString()} at{" "} - {new Date(published).toLocaleTimeString( - navigator.language, - { - hour: "2-digit", - minute: "2-digit", - hour12: false, - }, - )} - + <> + {renderDate("Scheduled to publish on ", published)} + ) : published && postStatus === PostStatus.PUBLISHED ? ( - - Published on{" "} - {new Date(published).toLocaleDateString()} at{" "} - {new Date(published).toLocaleTimeString( - navigator.language, - { - hour: "2-digit", - minute: "2-digit", - hour12: false, - }, + <> + {/*If updatedAt is greater than published by more than on minutes show updated at else show published + as on updating published updatedAt is automatically updated and is greater than published*/} + {new Date(updatedAt).getTime() - + new Date(published).getTime() >= + 60000 ? ( + <>{renderDate("Last updated on ", updatedAt)} + ) : ( + <>{renderDate("Published on ", published)} )} - + + ) : postStatus === PostStatus.DRAFT ? ( + <>{renderDate("Last updated on ", updatedAt)} ) : null}
diff --git a/server/api/router/post.ts b/server/api/router/post.ts index 449cc46e..b2792a77 100644 --- a/server/api/router/post.ts +++ b/server/api/router/post.ts @@ -395,7 +395,9 @@ export const postRouter = createTRPCRouter({ lte(posts.published, new Date().toISOString()), eq(posts.userId, ctx?.session?.user?.id), ), - orderBy: (posts, { desc }) => [desc(posts.published)], + orderBy: (posts, { desc, sql }) => [ + desc(sql`GREATEST(${posts.updatedAt}, ${posts.published})`), + ], }); }), myScheduled: protectedProcedure.query(async ({ ctx }) => { @@ -413,6 +415,7 @@ export const postRouter = createTRPCRouter({ return ctx.db.query.post.findMany({ where: (posts, { eq }) => and(eq(posts.userId, ctx.session.user.id), isNull(posts.published)), + orderBy: (posts, { desc }) => [desc(posts.updatedAt)], }); }), editDraft: protectedProcedure