@@ -61,6 +61,7 @@ const getPostMessage = async (postId: string) => {
6161 . innerJoin ( 'users' , 'users.snowflakeId' , 'messages.userId' )
6262 . select ( [
6363 'messages.id' ,
64+ 'messages.snowflakeId' ,
6465 'messages.content' ,
6566 'messages.createdAt' ,
6667 'users.id as authorId' ,
@@ -97,6 +98,7 @@ const getMessages = async (postId: string) => {
9798 'messages.snowflakeId' ,
9899 'messages.content' ,
99100 'messages.createdAt' ,
101+ 'messages.replyToMessageId' ,
100102 'users.id as authorId' ,
101103 'users.avatarUrl as authorAvatarUrl' ,
102104 'users.username as authorUsername' ,
@@ -179,6 +181,10 @@ const Post = async ({ params }: PostProps) => {
179181 const messages = await getMessages ( params . id )
180182 const postMessage = await getPostMessage ( params . id )
181183 const answerMessage = messages . find ( ( m ) => m . snowflakeId === post . answerId )
184+ // For replies. in `messages`, the post message is not included.
185+ // Incase The user has replied to the post message, we are creating this to search through allMessages
186+ const allMessages = [ ...messages , ...( postMessage ? [ postMessage ] : [ ] ) ]
187+ // If a user has sent multiple messages in a row, group them
182188 const groupedMessages = groupMessagesByUser ( messages , post . answerId )
183189 const hasAnswer =
184190 post . answerId && messages . some ( ( m ) => m . snowflakeId === post . answerId )
@@ -241,23 +247,23 @@ const Post = async ({ params }: PostProps) => {
241247 />
242248 < LayoutWithSidebar className = "mt-4" >
243249 < div >
244- < h1 className = "mb-4 font-semibold text-3xl" > { post . title } </ h1 >
250+ < h1 className = "mb-4 text-3xl font-semibold " > { post . title } </ h1 >
245251
246- < div className = "flex flex-col sm:flex-row gap-2 sm:items-center justify-between " >
252+ < div className = "flex flex-col justify-between gap-2 sm:flex-row sm:items-center " >
247253 < div className = "flex flex-wrap items-center gap-2" >
248254 { hasAnswer ? (
249- < div className = "px-2.5 py-1 border border-green-400 text-green-400 rounded-full opacity-60" >
255+ < div className = "rounded-full border border-green-400 px-2.5 py-1 text-green-400 opacity-60" >
250256 Answered
251257 </ div >
252258 ) : (
253- < div className = "px-2.5 py-1 border rounded-full opacity-50" >
259+ < div className = "rounded-full border px-2.5 py-1 opacity-50" >
254260 Unanswered
255261 </ div >
256262 ) }
257263 < div >
258264 { post . userIsPublic ? (
259265 < Link
260- className = " text-white opacity-90"
266+ className = "text-white opacity-90"
261267 href = { `/user/${ post . userID } ` }
262268 >
263269 { truncatedName }
@@ -267,14 +273,14 @@ const Post = async ({ params }: PostProps) => {
267273 ) } { ' ' }
268274 < span className = "opacity-50" >
269275 posted this in{ ' ' }
270- < span className = " font-semibold" > #{ post . channelName } </ span >
276+ < span className = "font-semibold" > #{ post . channelName } </ span >
271277 </ span >
272278 </ div >
273279 </ div >
274280
275281 < a
276282 href = { `https://discord.com/channels/752553802359505017/${ post . snowflakeId } /${ post . snowflakeId } ` }
277- className = "shrink-0 w-fit px-4 py-1.5 font-semibold text-white border-neutral-700 border rounded hover:bg-neutral-700 hover:no-underline transition-colors "
283+ className = "w-fit shrink-0 rounded border border-neutral-700 px-4 py-1.5 font-semibold text-white transition-colors hover:bg-neutral-700 hover:no-underline"
278284 target = "_blank"
279285 rel = "noopener noreferrer"
280286 >
@@ -309,8 +315,8 @@ const Post = async ({ params }: PostProps) => {
309315 </ MessageGroup >
310316
311317 { answerMessage && (
312- < div className = "p-2 sm:p-3 space-y-1.5 border border-green-400 rounded " >
313- < div className = "flex space-x-2 items-center text-green-400" >
318+ < div className = "space-y-1.5 rounded border border-green-400 p-2 sm:p-3 " >
319+ < div className = "flex items-center space-x-2 text-green-400" >
314320 < CheckCircleSolidIcon />
315321 < div className = "text-sm" >
316322 Answered by{ ' ' }
@@ -336,7 +342,7 @@ const Post = async ({ params }: PostProps) => {
336342
337343 < a
338344 href = { `#message-${ answerMessage . snowflakeId } ` }
339- className = "mt-2 opacity-80 font-semibold text-sm space-x-1 "
345+ className = "mt-2 space-x-1 text-sm font-semibold opacity-80 "
340346 >
341347 < span > View full answer</ span >
342348 < ArrowDownIcon size = { 4 } />
@@ -357,26 +363,51 @@ const Post = async ({ params }: PostProps) => {
357363 ( m ) => m . snowflakeId === post . answerId ,
358364 ) }
359365 >
360- { group . messages . map ( ( message , i ) => (
361- < Message
362- key = { message . id . toString ( ) }
363- snowflakeId = { message . snowflakeId }
364- createdAt = { message . createdAt }
365- content = { message . content }
366- isFirstRow = { i === 0 }
367- author = { {
368- username : message . authorUsername ,
369- avatarUrl : message . authorAvatarUrl ,
370- isPublic : message . userIsPublic ,
371- isOP : postMessage
372- ? message . authorId === postMessage . authorId
373- : false ,
374- isModerator : message . userIsModerator ,
375- userID : message . userID ,
376- } }
377- attachments = { message . attachments }
378- />
379- ) ) }
366+ { group . messages . map ( ( message , i ) => {
367+ const hasReply = message . replyToMessageId !== null
368+ const replyMessage = hasReply
369+ ? allMessages . find (
370+ ( m ) => m . snowflakeId === message . replyToMessageId ,
371+ )
372+ : 'deleted'
373+ return (
374+ < Message
375+ key = { message . id . toString ( ) }
376+ snowflakeId = { message . snowflakeId }
377+ createdAt = { message . createdAt }
378+ content = { message . content }
379+ reply = {
380+ hasReply &&
381+ replyMessage &&
382+ typeof replyMessage !== 'string'
383+ ? {
384+ author : {
385+ username : replyMessage . authorUsername ,
386+ avatarUrl : replyMessage . authorAvatarUrl ,
387+ } ,
388+ messageID : replyMessage . snowflakeId ,
389+ content : replyMessage . content ,
390+ attachments : replyMessage . attachments ,
391+ }
392+ : hasReply && ! replyMessage
393+ ? 'deleted'
394+ : undefined
395+ }
396+ isFirstRow = { i === 0 || hasReply }
397+ author = { {
398+ username : message . authorUsername ,
399+ avatarUrl : message . authorAvatarUrl ,
400+ isPublic : message . userIsPublic ,
401+ isOP : postMessage
402+ ? message . authorId === postMessage . authorId
403+ : false ,
404+ isModerator : message . userIsModerator ,
405+ userID : message . userID ,
406+ } }
407+ attachments = { message . attachments }
408+ />
409+ )
410+ } ) }
380411 </ MessageGroup >
381412 ) ) }
382413 </ div >
0 commit comments