|
4 | 4 | -->
|
5 | 5 |
|
6 | 6 | <script setup lang="ts">
|
7 |
| -import { onMounted } from 'vue' |
| 7 | +import { onMounted, onUnmounted } from 'vue' |
8 | 8 | import { onTalkHashDirty, onTalkHashUpdate, openConversation, setTalkHash } from './talk.service.ts'
|
9 | 9 | import { registerTalkDesktopSettingsSection } from '../Settings/index.ts'
|
10 | 10 | import { subscribeBroadcast } from '../../../shared/broadcast.service.ts'
|
11 | 11 | import { appData } from '../../../app/AppData.js'
|
| 12 | +import unreadCountStore, { EVENTS } from '@talk/src/store/unreadCountStore' |
| 13 | +import { subscribe, emit as emitEvent } from '@nextcloud/event-bus' |
12 | 14 |
|
13 | 15 | const emit = defineEmits<{
|
14 | 16 | (event: 'ready'): void
|
15 | 17 | }>()
|
16 | 18 |
|
| 19 | +// Handler for unread count updates |
| 20 | +let unsubscribeUnreadCount: (() => void) | null = null |
| 21 | + |
| 22 | +const waitForTalkAndStore = async () => { |
| 23 | + console.debug('TalkWrapper: Waiting for Talk instance and store...') |
| 24 | + let attempts = 0 |
| 25 | + const maxAttempts = 50 |
| 26 | + const delay = 100 |
| 27 | + |
| 28 | + while (attempts < maxAttempts) { |
| 29 | + if (window.OCA?.Talk?.instance && window.store && window.TALK_DESKTOP) { |
| 30 | + console.debug('TalkWrapper: Talk instance, store, and TALK_DESKTOP found') |
| 31 | + return true |
| 32 | + } |
| 33 | + await new Promise(resolve => setTimeout(resolve, delay)) |
| 34 | + attempts++ |
| 35 | + } |
| 36 | + throw new Error('Talk instance, store, or TALK_DESKTOP not initialized after maximum attempts') |
| 37 | +} |
| 38 | + |
17 | 39 | onMounted(async () => {
|
18 |
| - // Importing the main Talk entry point mounts a Vue app to the #content |
19 |
| - await import('@talk/src/main.js') |
| 40 | + try { |
| 41 | + await import('@talk/src/main.js') |
| 42 | + |
| 43 | + registerTalkDesktopSettingsSection() |
| 44 | + subscribeBroadcast('talk:conversation:open', ({ token, directCall }) => openConversation(token, { directCall })) |
| 45 | + |
| 46 | + await waitForTalkAndStore() |
| 47 | + const store = window.store |
| 48 | + |
| 49 | + // Register store module if not already registered |
| 50 | + if (!store.hasModule('unreadCount')) { |
| 51 | + store.registerModule('unreadCount', unreadCountStore) |
| 52 | + } |
20 | 53 |
|
21 |
| - // Additional integrations |
22 |
| - registerTalkDesktopSettingsSection() |
23 |
| - subscribeBroadcast('talk:conversation:open', ({ token, directCall }) => openConversation(token, { directCall })) |
| 54 | + // Initialize badge handling |
| 55 | + window.TALK_DESKTOP.setBadgeCount(0) |
| 56 | + |
| 57 | + // Subscribe to unread count updates |
| 58 | + unsubscribeUnreadCount = subscribe(EVENTS.UNREAD_COUNT_UPDATED, ({ unreadMessages }) => { |
| 59 | + window.TALK_DESKTOP.setBadgeCount(unreadMessages || 0) |
| 60 | + }) |
| 61 | + |
| 62 | + // Now trigger the initial calculation |
| 63 | + store.dispatch('unreadCount/recalculateTotalUnreadCounters') |
| 64 | + |
| 65 | + if (appData.talkHash) { |
| 66 | + setTalkHash(appData.talkHash) |
| 67 | + } |
| 68 | + onTalkHashUpdate((hash: string) => { |
| 69 | + appData.setTalkHash(hash).persist() |
| 70 | + }) |
| 71 | + onTalkHashDirty(() => { |
| 72 | + appData.setTalkHashDirty(true).persist() |
| 73 | + }) |
| 74 | + |
| 75 | + emit('ready') |
| 76 | + } catch (error) { |
| 77 | + console.error('TalkWrapper: Error during initialization:', error) |
| 78 | + } |
| 79 | +}) |
24 | 80 |
|
25 |
| - // If there is a talkHash - set it initially |
26 |
| - if (appData.talkHash) { |
27 |
| - setTalkHash(appData.talkHash) |
| 81 | +// Cleanup event listeners |
| 82 | +onUnmounted(() => { |
| 83 | + if (unsubscribeUnreadCount) { |
| 84 | + unsubscribeUnreadCount() |
28 | 85 | }
|
29 |
| - // Handle Talk Hash updates |
30 |
| - onTalkHashUpdate((hash: string) => { |
31 |
| - appData.setTalkHash(hash).persist() |
32 |
| - }) |
33 |
| - onTalkHashDirty(() => { |
34 |
| - appData.setTalkHashDirty(true).persist() |
35 |
| - }) |
36 |
| - |
37 |
| - // Ready |
38 |
| - emit('ready') |
39 | 86 | })
|
40 | 87 | </script>
|
41 | 88 |
|
|
0 commit comments