Spaces:
Paused
Paused
File size: 7,974 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 | <script setup lang="ts">
import {
mdiPlus,
mdiDownload,
mdiCheckAll,
mdiCheck,
mdiAlert,
mdiAccount,
} from '@mdi/js'
import type { EntitiesMessageThread } from '~~/shared/types/api'
import {
isUrlNotificationToken,
formatWhatsappText,
} from '~/utils/whatsapp-format'
const threadsStore = useThreadsStore()
const phonesStore = usePhonesStore()
const appStore = useAppStore()
const notificationsStore = useNotificationsStore()
const { formatPhoneNumber, startsWithLetter } = useFilters()
function threadDate(date: string): string {
return new Date(date).toLocaleString(undefined, {
month: 'short',
day: 'numeric',
})
}
function hasUnreadMessages(unreadCount: number): boolean {
return unreadCount > 0
}
function unreadBadge(
unreadCount: number,
): false | { color: string; content: string; dot: false } {
if (unreadCount === 0) return false
return {
color: 'primary',
content: unreadCount > 99 ? '99+' : String(unreadCount),
dot: false,
}
}
function onInstallApp() {
notificationsStore.addNotification({
type: 'info',
message: 'Downloading the httpSMS Android App',
})
}
async function loadMoreThreads() {
try {
await threadsStore.loadMoreThreads()
} catch (error) {
notificationsStore.addNotification({
type: 'error',
message: 'More conversations could not be loaded',
})
console.error(error)
}
}
function isWhatsappFormatted(thread: EntitiesMessageThread): boolean {
const phone = phonesStore.phones.find((x) => x.phone_number === thread.owner)
return isUrlNotificationToken(phone?.fcm_token)
}
function threadPreviewHtml(thread: EntitiesMessageThread): string {
// Links are unwrapped because the preview sits inside a router link.
return formatWhatsappText(thread.last_message_content ?? '').replace(
/<a [^>]*>(.*?)<\/a>/g,
'$1',
)
}
function threadContactName(thread: EntitiesMessageThread): string {
return thread.contact_details?.name?.trim() ?? ''
}
function threadContactTitle(thread: EntitiesMessageThread): string {
return threadContactName(thread) || formatPhoneNumber(thread.contact)
}
function threadAvatarInitial(thread: EntitiesMessageThread): string {
const contactName = threadContactName(thread)
if (contactName) return contactName.substring(0, 1)
return startsWithLetter(thread.contact) ? thread.contact.substring(0, 1) : ''
}
</script>
<template>
<div>
<v-progress-linear
v-if="threadsStore.loadingThreads"
color="primary"
indeterminate
/>
<div
v-if="!threadsStore.loadingThreads && threadsStore.archivedThreads"
class="bg-warning py-1 text-center text-uppercase text-title-medium"
>
Archived Messages
</div>
<div
v-if="
!threadsStore.loadingThreads &&
threadsStore.visibleThreads.length === 0 &&
!threadsStore.unreadOnly &&
!threadsStore.archivedThreads
"
class="text-center mt-6"
>
<p v-if="phonesStore.owner" class="text-medium-emphasis text-center">
Start sending messages
</p>
<v-btn
v-if="phonesStore.owner && phonesStore.phones.length !== 0"
color="primary"
:to="{ name: 'messages' }"
>
<v-icon :icon="mdiPlus" start />
New Message
</v-btn>
</div>
<div
v-if="phonesStore.phones.length === 0 && !threadsStore.loadingThreads"
class="px-4 text-center"
>
<p>
Install the mobile app on your Android phone to start sending messages.
You can also
<a
href="https://discord.gg/kGk8HVqeEZ"
target="_blank"
class="text-decoration-none hover:text-decoration-underline"
>message us on Discord</a
>
to help set things up.
</p>
<v-btn
color="primary"
:href="appStore.appData.appDownloadUrl"
@click="onInstallApp"
>
<v-icon :icon="mdiDownload" start />
Download App
</v-btn>
</div>
<p
v-if="
!threadsStore.loadingThreads &&
threadsStore.unreadOnly &&
threadsStore.visibleThreads.length === 0 &&
!threadsStore.hasMoreThreads &&
phonesStore.owner
"
class="text-medium-emphasis text-center mt-6"
>
No unread conversations
</p>
<v-list
v-if="
!threadsStore.loadingThreads && threadsStore.visibleThreads.length > 0
"
class="py-0"
lines="three"
>
<v-list-item
v-for="thread in threadsStore.visibleThreads"
:key="thread.id"
color="primary"
:to="{ name: 'threads-id', params: { id: thread.id } }"
:active="threadsStore.threadId === thread.id"
>
<template #prepend>
<v-avatar
:color="thread.color"
size="40"
:badge="unreadBadge(thread.unread_count)"
>
<v-icon v-if="!threadAvatarInitial(thread)" color="white">{{
mdiAccount
}}</v-icon>
<span v-else class="text-white text-headline-small">{{
threadAvatarInitial(thread)
}}</span>
</v-avatar>
</template>
<v-list-item-title
:class="{
'font-weight-bold': hasUnreadMessages(thread.unread_count),
}"
>
{{ threadContactTitle(thread) }}
</v-list-item-title>
<v-list-item-subtitle
class="text-truncate mt-1"
:class="{
'font-weight-bold opacity-100': hasUnreadMessages(
thread.unread_count,
),
}"
style="max-width: 250px"
>
<!-- eslint-disable vue/no-v-html -->
<span
v-if="isWhatsappFormatted(thread)"
v-html="threadPreviewHtml(thread)"
/>
<!-- eslint-enable vue/no-v-html -->
<template v-else>{{ thread.last_message_content }}</template>
</v-list-item-subtitle>
<template #append>
<div class="d-flex flex-column align-end">
<span class="text-body-small text-medium-emphasis">
{{ threadDate(thread.order_timestamp) }}
</span>
<VTooltip location="bottom">
<template #activator="{ props }">
<div v-bind="props" class="mt-1">
<v-icon
v-if="thread.status === 'expired'"
color="warning"
size="x-small"
:icon="mdiAlert"
/>
<v-icon
v-else-if="thread.status === 'delivered'"
color="primary"
size="x-small"
:icon="mdiCheckAll"
/>
<v-icon
v-else-if="thread.status === 'received'"
color="success"
size="x-small"
:icon="mdiCheckAll"
/>
<v-icon
v-else-if="thread.status === 'sent'"
size="x-small"
:icon="mdiCheck"
/>
<v-icon
v-else-if="thread.status === 'failed'"
color="error"
size="x-small"
:icon="mdiAlert"
/>
</div>
</template>
<span>{{ thread.status }}</span>
</VTooltip>
</div>
</template>
</v-list-item>
</v-list>
<div
v-if="!threadsStore.loadingThreads && threadsStore.hasMoreThreads"
class="d-flex justify-center pa-4"
>
<v-btn
color="primary"
variant="tonal"
:loading="threadsStore.loadingMoreThreads"
:disabled="threadsStore.loadingMoreThreads"
@click="loadMoreThreads"
>
Load More
</v-btn>
</div>
</div>
</template>
|