File size: 10,892 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { defineStore } from 'pinia'
import type {
  EntitiesContact,
  EntitiesMessageThread,
  EntitiesMessage,
} from '~~/shared/types/api'

const PAGE_SIZE = 20

export const useThreadsStore = defineStore('threads', () => {
  const threads = ref<EntitiesMessageThread[]>([])
  const threadId = ref<string | null>(null)
  const loadingThreads = ref(true)
  const loadingMoreThreads = ref(false)
  const hasMoreThreads = ref(false)
  let pageCount = 1
  let pageContext = ''
  let loadMoreGeneration = 0
  const archivedThreads = ref(false)
  const unreadOnly = ref(false)
  const retainedThread = ref<EntitiesMessageThread | null>(null)
  const openedThreads = ref(new Map<string, EntitiesMessageThread>())
  const fetchedThreadIds = ref(new Set<string>())
  let threadRequestId = 0
  let stateGeneration = 0
  let readMutationGeneration = 0
  const { apiFetch } = useApi()
  const notificationsStore = useNotificationsStore()

  const currentThread = computed<EntitiesMessageThread | null>(() => {
    return (
      threads.value.find((x) => x.id === threadId.value) ??
      ((unreadOnly.value ||
        loadingThreads.value ||
        hasMoreThreads.value ||
        (threadId.value !== null &&
          fetchedThreadIds.value.has(threadId.value))) &&
      retainedThread.value?.id === threadId.value
        ? retainedThread.value
        : null)
    )
  })

  const visibleThreads = computed(() =>
    unreadOnly.value
      ? threads.value.filter((thread) => thread.unread_count > 0)
      : threads.value,
  )

  const hasThread = computed(
    () => currentThread.value !== null && !loadingThreads.value,
  )

  function hasThreadId(id: string): boolean {
    return (
      threads.value.some((thread) => thread.id === id) ||
      currentThread.value?.id === id
    )
  }

  function replaceThread(updatedThread: EntitiesMessageThread) {
    const index = threads.value.findIndex(
      (thread) => thread.id === updatedThread.id,
    )
    const existingThread =
      index !== -1 ? threads.value[index] : retainedThread.value
    const replacement = {
      ...updatedThread,
      // Mark-read PUT responses are not contact-enriched.
      contact_details:
        updatedThread.contact_details ??
        (updatedThread.contact === existingThread?.contact
          ? existingThread.contact_details
          : undefined),
    }
    if (index !== -1) {
      threads.value[index] = replacement
    }
    if (updatedThread.id === threadId.value) {
      retainedThread.value = replacement
    }
    if (
      openedThreads.value.has(updatedThread.id) ||
      updatedThread.id === threadId.value
    ) {
      openedThreads.value.set(updatedThread.id, replacement)
    }
  }

  async function loadThreads(savedContacts: EntitiesContact[] = []) {
    const requestId = ++threadRequestId
    const readGeneration = readMutationGeneration
    const selectedThread = currentThread.value
    if (selectedThread) {
      openedThreads.value.set(selectedThread.id, selectedThread)
    }
    for (const openedThread of openedThreads.value.values()) {
      for (const contact of savedContacts) {
        const matchesNumber = contact.phone_numbers.includes(
          openedThread.contact,
        )
        if (matchesNumber || openedThread.contact_details?.id === contact.id) {
          openedThread.contact_details = matchesNumber ? contact : undefined
        }
      }
    }
    const phonesStore = usePhonesStore()
    if (phonesStore.owner === null && phonesStore.phones.length === 0) {
      loadingThreads.value = false
      hasMoreThreads.value = false
      return
    }

    const owner = phonesStore.owner ?? phonesStore.phones[0]?.phone_number
    const archived = archivedThreads.value
    const unread = unreadOnly.value
    const context = JSON.stringify([owner, archived, unread])
    if (context !== pageContext) {
      pageContext = context
      pageCount = 1
      hasMoreThreads.value = false
      loadingMoreThreads.value = false
      loadMoreGeneration++
    }

    try {
      const responses = await Promise.all(
        Array.from({ length: pageCount }, (_, page) =>
          apiFetch<{ data: EntitiesMessageThread[] }>('/v1/message-threads', {
            params: {
              owner,
              limit: PAGE_SIZE,
              skip: page * PAGE_SIZE,
              is_archived: archived,
              unread_only: unread,
              contacts: true,
            },
          }),
        ),
      )
      if (requestId !== threadRequestId) return
      if (readGeneration !== readMutationGeneration) {
        await loadThreads()
        return
      }

      phonesStore.getHeartbeat().catch(console.error)
      retainedThread.value = currentThread.value
      const combined = new Map<string, EntitiesMessageThread>()
      let hasMore = false
      for (const response of responses) {
        hasMore = response.data.length === PAGE_SIZE
        for (const thread of response.data) {
          if (!combined.has(thread.id)) combined.set(thread.id, thread)
        }
      }
      threads.value = [...combined.values()]
      hasMoreThreads.value = hasMore
      for (const thread of threads.value) {
        if (openedThreads.value.has(thread.id)) {
          openedThreads.value.set(thread.id, thread)
        }
      }
    } finally {
      if (requestId === threadRequestId) {
        loadingThreads.value = false
      }
    }
  }

  async function loadMoreThreads() {
    if (
      loadingThreads.value ||
      loadingMoreThreads.value ||
      !hasMoreThreads.value
    )
      return

    const generation = ++loadMoreGeneration
    const previousPageCount = pageCount
    pageCount++
    loadingMoreThreads.value = true
    try {
      await loadThreads()
    } catch (error) {
      if (generation !== loadMoreGeneration) return
      pageCount = previousPageCount
      throw error
    } finally {
      if (generation === loadMoreGeneration) loadingMoreThreads.value = false
    }
  }

  async function fetchThread(id: string): Promise<EntitiesMessageThread> {
    const generation = stateGeneration
    const response = await apiFetch<{ data: EntitiesMessageThread }>(
      `/v1/message-threads/${id}`,
    )
    if (generation !== stateGeneration) {
      throw new DOMException('thread fetch was canceled', 'AbortError')
    }
    openedThreads.value.set(response.data.id, response.data)
    fetchedThreadIds.value.add(response.data.id)
    return response.data
  }

  async function loadThreadMessages(
    id: string | null,
  ): Promise<EntitiesMessage[]> {
    setThreadId(id)
    const thread = currentThread.value
    if (!thread) throw new Error(`Cannot find thread with id ${id}`)

    const response = await apiFetch<{ data: EntitiesMessage[] }>(
      '/v1/messages',
      {
        params: {
          contact: thread.contact,
          owner: thread.owner,
          limit: 50,
        },
      },
    )
    return response.data
  }

  function setThreadId(id: string | null) {
    const previousThread = currentThread.value
    if (previousThread) {
      openedThreads.value.set(previousThread.id, previousThread)
    }
    const cached = id === null ? undefined : openedThreads.value.get(id)
    const phonesStore = usePhonesStore()
    const owner = phonesStore.owner ?? phonesStore.phones[0]?.phone_number
    retainedThread.value =
      id === null
        ? null
        : (threads.value.find((thread) => thread.id === id) ??
          ((unreadOnly.value ||
            loadingThreads.value ||
            hasMoreThreads.value ||
            fetchedThreadIds.value.has(id)) &&
          cached &&
          cached.owner === owner &&
          cached.is_archived === archivedThreads.value
            ? cached
            : null))
    if (retainedThread.value) {
      openedThreads.value.set(retainedThread.value.id, retainedThread.value)
    }
    threadId.value = id
  }

  function toggleArchive() {
    archivedThreads.value = !archivedThreads.value
  }

  async function updateThread(payload: {
    threadId: string
    isArchived: boolean
  }) {
    await apiFetch(`/v1/message-threads/${payload.threadId}`, {
      method: 'PUT',
      body: { is_archived: payload.isArchived },
    })
    threads.value = threads.value.filter(
      (thread) => thread.id !== payload.threadId,
    )
    setThreadId(null)
    openedThreads.value.delete(payload.threadId)
    fetchedThreadIds.value.delete(payload.threadId)
    notificationsStore.addNotification({
      message: payload.isArchived ? 'Archived' : 'Unarchived',
      type: 'success',
    })
  }

  async function resetThreadUnreadCount(threadId: string, force = false) {
    const thread =
      threads.value.find((item) => item.id === threadId) ??
      (retainedThread.value?.id === threadId ? retainedThread.value : null)
    if (!thread) throw new Error(`Cannot find thread with id ${threadId}`)
    if (!force && thread.unread_count === 0) return

    try {
      const response = await apiFetch<{ data: EntitiesMessageThread }>(
        `/v1/message-threads/${threadId}`,
        {
          method: 'PUT',
          body: { unread_count: 0 },
        },
      )
      readMutationGeneration++
      replaceThread(response.data)
    } catch (error) {
      notificationsStore.addNotification({
        message: 'The message thread could not be marked as read',
        type: 'error',
      })
      try {
        await loadThreads()
      } catch (reloadError) {
        throw new AggregateError(
          [error, reloadError],
          'Could not mark the message thread as read or reload threads',
          { cause: reloadError },
        )
      }
      throw error
    }
  }

  async function deleteThread(id: string) {
    await apiFetch(`/v1/message-threads/${id}`, { method: 'DELETE' })
    setThreadId(null)
    openedThreads.value.delete(id)
    fetchedThreadIds.value.delete(id)
    notificationsStore.addNotification({
      message: 'The message thread has been deleted successfully',
      type: 'success',
    })
  }

  function resetState() {
    threadRequestId++
    stateGeneration++
    readMutationGeneration = 0
    loadMoreGeneration++
    pageCount = 1
    pageContext = ''
    hasMoreThreads.value = false
    loadingMoreThreads.value = false
    threads.value = []
    threadId.value = null
    retainedThread.value = null
    openedThreads.value.clear()
    fetchedThreadIds.value.clear()
    archivedThreads.value = false
    unreadOnly.value = false
    loadingThreads.value = true
  }

  return {
    threads,
    threadId,
    loadingThreads,
    loadingMoreThreads,
    hasMoreThreads,
    archivedThreads,
    unreadOnly,
    visibleThreads,
    currentThread,
    hasThread,
    hasThreadId,
    loadThreads,
    loadMoreThreads,
    fetchThread,
    loadThreadMessages,
    setThreadId,
    toggleArchive,
    updateThread,
    resetThreadUnreadCount,
    deleteThread,
    resetState,
  }
})