Ensure threads group together posts by op

zio/stable
Paul Frazee 2022-11-23 13:45:50 -06:00
parent d09bf31431
commit a993499890
1 changed files with 19 additions and 0 deletions

View File

@ -297,6 +297,7 @@ export class PostThreadViewModel {
private _replaceAll(res: GetPostThread.Response) { private _replaceAll(res: GetPostThread.Response) {
// TODO: validate .record // TODO: validate .record
sortThread(res.data.thread)
const keyGen = reactKeyGenerator() const keyGen = reactKeyGenerator()
const thread = new PostThreadViewPostModel( const thread = new PostThreadViewPostModel(
this.rootStore, this.rootStore,
@ -308,3 +309,21 @@ export class PostThreadViewModel {
this.thread = thread 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))
}
}