diff --git a/src/routes/blog/[title]/index.tsx b/src/routes/blog/[title]/index.tsx index 88f266e..0772821 100644 --- a/src/routes/blog/[title]/index.tsx +++ b/src/routes/blog/[title]/index.tsx @@ -35,16 +35,52 @@ const getPostByTitle = query( const userID = await getUserID(event.nativeEvent); const conn = ConnectionFactory(); - let query; - //TODO: get id from url param instead of title param - // - if (title == "by-id") { - const [searchParams, setSearchParams] = useSearchParams(); + // Handle by-id route: lookup post by ID and redirect to title-based URL + if (title === "by-id") { + const url = new URL(event.request.url); + const id = url.searchParams.get("id"); - query = "SELECT * FROM Post WHERE id = ?"; - } else { - query = "SELECT * FROM Post WHERE title = ?"; + if (!id) { + return { + post: null, + exists: false, + comments: [], + likes: [], + tags: [], + userCommentArray: [], + reactionArray: [], + privilegeLevel: "anonymous" as const, + userID: null + }; + } + + const idQuery = "SELECT title FROM Post WHERE id = ?"; + const idResult = await conn.execute({ + sql: idQuery, + args: [id] + }); + + const postData = idResult.rows[0] as any; + if (postData?.title) { + return { + redirect: `/blog/${encodeURIComponent(postData.title)}${sortBy !== "newest" ? `?sortBy=${sortBy}` : ""}` + }; + } + + return { + post: null, + exists: false, + comments: [], + likes: [], + tags: [], + userCommentArray: [], + reactionArray: [], + privilegeLevel: "anonymous" as const, + userID: null + }; } + + let query = "SELECT * FROM Post WHERE title = ?"; if (privilegeLevel !== "admin") { query += ` AND published = TRUE`; } @@ -264,177 +300,184 @@ export default function PostPage() { return ( <> }> - {(loadedData) => ( - }> - {(p) => { - const postData = loadedData(); + {(loadedData) => { + // Handle redirect for by-id route + if ("redirect" in loadedData()) { + return ; + } - // Convert arrays back to Maps for component - const userCommentMap = new Map( - postData.userCommentArray || [] - ); - const reactionMap = new Map( - postData.reactionArray || [] - ); + return ( + }> + {(p) => { + const postData = loadedData(); - return ( - <> - - {p().title.replaceAll("_", " ")} | Michael Freno - - + // Convert arrays back to Maps for component + const userCommentMap = new Map( + postData.userCommentArray || [] + ); + const reactionMap = new Map( + postData.reactionArray || [] + ); - - {/* Fixed banner image background */} - - - - - {p().title.replaceAll("_", " ")} - - {p().subtitle} - - - - + return ( + <> + + {p().title.replaceAll("_", " ")} | Michael Freno + + - - {/* Content that slides over the fixed image */} - - - - - - Written {new Date(p().date).toDateString()} - - By Michael Freno - - - - - {(tag) => { - const tagValue = tag.value; - return tagValue ? ( - - {tagValue} - - ) : null; - }} - - - - - - - - - - - - {postData.reads || 0}{" "} - {postData.reads === 1 ? "Hit" : "Hits"} - - - - - - { - document - .getElementById("comments") - ?.scrollIntoView({ behavior: "smooth" }); - }} - class="tooltip flex flex-col" - > - - - - - {postData.comments.length}{" "} - {postData.comments.length === 1 - ? "Comment" - : "Comments"} - - - - - - - - - - - {/* Post body */} - + {/* Fixed banner image background */} + + - - - - - Edit - + + + {p().title.replaceAll("_", " ")} + + {p().subtitle} + - + + - {/* Comments section */} - - + {/* Content that slides over the fixed image */} + + + + + + Written {new Date(p().date).toDateString()} + + By Michael Freno + + + + + {(tag) => { + const tagValue = tag.value; + return tagValue ? ( + + {tagValue} + + ) : null; + }} + + + + + + + + + + + + {postData.reads || 0}{" "} + {postData.reads === 1 ? "Hit" : "Hits"} + + + + + + { + document + .getElementById("comments") + ?.scrollIntoView({ behavior: "smooth" }); + }} + class="tooltip flex flex-col" + > + + + + + {postData.comments.length}{" "} + {postData.comments.length === 1 + ? "Comment" + : "Comments"} + + + + + + + + + + + {/* Post body */} + + + + + + Edit + + + + + {/* Comments section */} + + + - - > - ); - }} - - )} + > + ); + }} + + ); + }} > );
+ By Michael Freno +