From a9934998909b7d828f66e2b1b0b1e0aeb20adf6a Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Wed, 23 Nov 2022 13:45:50 -0600 Subject: [PATCH] Ensure threads group together posts by op --- src/state/models/post-thread-view.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/state/models/post-thread-view.ts b/src/state/models/post-thread-view.ts index 0334c844..860a8f6a 100644 --- a/src/state/models/post-thread-view.ts +++ b/src/state/models/post-thread-view.ts @@ -297,6 +297,7 @@ export class PostThreadViewModel { private _replaceAll(res: GetPostThread.Response) { // TODO: validate .record + sortThread(res.data.thread) const keyGen = reactKeyGenerator() const thread = new PostThreadViewPostModel( this.rootStore, @@ -308,3 +309,21 @@ export class PostThreadViewModel { this.thread = thread } } + +function sortThread(post: GetPostThread.Post) { + if (post.replies) { + post.replies.sort((a: GetPostThread.Post, b: GetPostThread.Post) => { + const aIsByOp = a.author.did === post.author.did + const bIsByOp = b.author.did === post.author.did + if (aIsByOp && bIsByOp) { + return a.indexedAt.localeCompare(b.indexedAt) // oldest + } else if (aIsByOp) { + return -1 // op's own reply + } else if (bIsByOp) { + return 1 // op's own reply + } + return b.indexedAt.localeCompare(a.indexedAt) // newest + }) + post.replies.forEach(reply => sortThread(reply)) + } +}