Skip to content

Commit 7bc0f84

Browse files
committed
fix: simpler merging logic, fix shared thread not showing up
1 parent f3d4081 commit 7bc0f84

1 file changed

Lines changed: 15 additions & 21 deletions

File tree

apps/frontend/app/components/chat/ChatSidebar.vue

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<script setup lang="ts">
22
import type { Doc } from 'backend-convex/convex/_generated/dataModel'
3+
import { keyBy } from '@namesmt/utils'
34
import { api } from 'backend-convex/convex/_generated/api'
45
import { useConvexQuery } from 'convex-vue'
56
import { computed, ref } from 'vue'
6-
77
import {
88
ContextMenu,
99
ContextMenuContent,
@@ -39,16 +39,7 @@ const isFetching = ref(false)
3939
// Subscribe to Convex to sync threads
4040
if ($auth.loggedIn) {
4141
const { data: threadsFromConvex, isLoading: fetchingFromConvex } = useConvexQuery(api.threads.listByUser)
42-
watch(threadsFromConvex, (tFC) => {
43-
// Keep threads that are not assigned to any users
44-
// Must reconstruct array or else it cant be cloned to IDB.
45-
threads.value = JSON.parse(JSON.stringify(
46-
[
47-
...threads.value.filter(t => !t.userId),
48-
...tFC,
49-
].sort((a, b) => b.timestamp - a.timestamp),
50-
))
51-
})
42+
watch(threadsFromConvex, _mergeToLocalThreads)
5243
watch(fetchingFromConvex, (fFC) => {
5344
isFetching.value = fFC
5445
})
@@ -71,21 +62,24 @@ if ($auth.loggedIn) {
7162
// For anonymous users, subscribe to threads via sessionId
7263
else {
7364
const { data: threadsFromConvex, isLoading: fetchingFromConvex } = useConvexQuery(api.threads.listBySessionId, { sessionId: $init.sessionId })
74-
watch(threadsFromConvex, (tFC) => {
75-
// Keep threads from other sessionIds, that are not assigned to any users
76-
// Must reconstruct array or else it cant be cloned to IDB.
77-
threads.value = JSON.parse(JSON.stringify(
78-
[
79-
...threads.value.filter(t => !t.userId && t.sessionId !== $init.sessionId),
80-
...tFC.map(t => ({ ...t, lockerKey: getLockerKey(t._id) })),
81-
].sort((a, b) => b.timestamp - a.timestamp),
82-
))
83-
})
65+
watch(threadsFromConvex, _mergeToLocalThreads)
8466
watch(fetchingFromConvex, (fFC) => {
8567
isFetching.value = fFC
8668
})
8769
}
8870
71+
function _mergeToLocalThreads(tFC: Doc<'threads'>[]) {
72+
const threadsMapped = keyBy(tFC, '_id')
73+
for (const thread of threads.value) {
74+
if (!threadsMapped[thread._id])
75+
threadsMapped[thread._id] = thread
76+
}
77+
threads.value = Object.values(threadsMapped).map((t) => {
78+
t.lockerKey = t.lockerKey || getLockerKey(t._id)
79+
return t
80+
}).sort((a, b) => b.timestamp - a.timestamp)
81+
}
82+
8983
// Fuck TS in these situations
9084
// [other, pinned]
9185
const threadsPartitioned = useArrayReduce(threads, (a, c) => {

0 commit comments

Comments
 (0)