File size: 8,840 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
<script setup lang="ts">
import { useDisplay } from 'vuetify'
import { getAuth, signOut } from 'firebase/auth'
import {
  mdiPlus,
  mdiAccountCog,
  mdiLogout,
  mdiCellphoneKey,
  mdiDownload,
  mdiFinance,
  mdiBatteryChargingHigh,
  mdiPackageUp,
  mdiPackageDown,
  mdiDotsVertical,
  mdiMagnify,
  mdiCommentTextMultipleOutline,
  mdiAccountMultiple,
  mdiCircle,
} from '@mdi/js'
import type { EntitiesPhone } from '~~/shared/types/api'

const router = useRouter()
const route = useRoute()
const { mdAndDown, lgAndUp } = useDisplay()
const authStore = useAuthStore()
const phonesStore = usePhonesStore()
const threadsStore = useThreadsStore()
const appStore = useAppStore()
const notificationsStore = useNotificationsStore()
const redirectPreferenceStore = useRedirectPreferenceStore()
const { formatPhoneNumber, phoneCountry, humanizeTime } = useFilters()

const selectedMenuItem = ref(-1)

interface SelectItem {
  title: string
  value: string
}

const owners = computed<SelectItem[]>(() => {
  return phonesStore.phones.map((phone: EntitiesPhone) => ({
    title: formatPhoneNumber(phone.phone_number),
    value: phone.phone_number,
  }))
})

async function onOwnerChanged(owner: string) {
  await authStore.updateUser({ owner })
  if (route.name !== 'threads') {
    threadsStore.setThreadId(null)
    await router.push({ name: 'threads' })
    return
  }
  await threadsStore.loadThreads()
}

async function toggleArchive() {
  threadsStore.toggleArchive()
  setTimeout(() => {
    selectedMenuItem.value = -1
  }, 1000)
  if (route.name !== 'threads') {
    threadsStore.setThreadId(null)
    await router.push({ name: 'threads' })
    return
  }
  await threadsStore.loadThreads()
}

async function filterThreads(unreadOnly: boolean) {
  if (threadsStore.unreadOnly === unreadOnly) return

  const previous = threadsStore.unreadOnly
  threadsStore.unreadOnly = unreadOnly
  threadsStore.loadingThreads = true
  try {
    await threadsStore.loadThreads()
  } catch (error) {
    threadsStore.unreadOnly = previous
    notificationsStore.addNotification({
      type: 'error',
      message: 'The message thread filter could not be updated',
    })
    console.error(error)
  }
}

async function logout() {
  const auth = getAuth()
  await signOut(auth)
  authStore.resetState()
  phonesStore.resetState()
  threadsStore.resetState()
  redirectPreferenceStore.resetState()
  notificationsStore.addNotification({
    type: 'info',
    message: 'You have successfully logged out',
  })
  router.push({ name: 'index' })
}
</script>

<template>
  <v-sheet
    class="pa-4 flex-shrink-0"
    :elevation="lgAndUp ? 0 : 2"
    color="black"
  >
    <div class="d-flex">
      <div :class="{ 'px-2': mdAndDown }">
        <v-toolbar-title>
          <div class="d-flex pt-2" style="width: 260px">
            <v-select
              variant="outlined"
              density="compact"
              color="primary"
              :disabled="owners.length === 0"
              placeholder="Phone Numbers"
              :class="{ 'mb-n5': !phonesStore.owner }"
              :items="owners"
              :model-value="phonesStore.owner"
              @update:model-value="onOwnerChanged"
            />
            <div style="width: 50px">
              <v-progress-circular
                v-if="appStore.polling"
                indeterminate
                :size="20"
                :width="1"
                class="mt-3 ml-2"
                color="success"
              />
            </div>
          </div>
        </v-toolbar-title>
        <div v-if="phonesStore.owner" class="d-flex mt-n2">
          <p class="text-medium-emphasis mb-n1 mt-0">
            {{ phoneCountry(phonesStore.owner) }}
          </p>
          <v-tooltip v-if="phonesStore.heartbeat" location="end">
            <template #activator="{ props: tooltipProps }">
              <v-btn
                v-bind="tooltipProps"
                size="small"
                density="compact"
                :to="{
                  name: 'heartbeats-id',
                  params: { id: phonesStore.owner },
                }"
                color="success"
                class="ml-2 mb-0 mt-1"
                icon
                variant="text"
              >
                <v-icon
                  v-if="phonesStore.heartbeat.charging"
                  size="small"
                  class="mt-n1"
                  :icon="mdiBatteryChargingHigh"
                />
                <v-icon
                  v-else
                  size="x-small"
                  color="success"
                  :icon="mdiCircle"
                />
              </v-btn>
            </template>
            <h4 class="font-weight-bold mt-0 mb-0">Last Heartbeat</h4>
            {{ humanizeTime(phonesStore.heartbeat.timestamp) }} ago
          </v-tooltip>
        </div>
      </div>
      <v-spacer />
      <v-menu>
        <template #activator="{ props: menuProps }">
          <v-btn v-bind="menuProps" icon variant="text" class="mt-2 mr-n3">
            <v-icon :icon="mdiDotsVertical" />
          </v-btn>
        </template>
        <v-list
          class="pa-0"
          :density="mdAndDown ? 'compact' : 'default'"
          prepend-gap="20"
        >
          <v-list-item @click.prevent="toggleArchive">
            <template #prepend>
              <v-icon
                v-if="!threadsStore.archivedThreads"
                :icon="mdiPackageDown"
              />
              <v-icon v-else :icon="mdiPackageUp" />
            </template>
            <v-list-item-title>
              {{ threadsStore.archivedThreads ? 'Unarchived' : 'Archived' }}
            </v-list-item-title>
          </v-list-item>
          <v-list-item v-if="phonesStore.owner" :to="{ name: 'messages' }">
            <template #prepend><v-icon :icon="mdiPlus" /></template>
            <v-list-item-title>New Message</v-list-item-title>
          </v-list-item>
          <v-list-item v-if="phonesStore.owner" :to="{ name: 'bulk-messages' }">
            <template #prepend
              ><v-icon :icon="mdiCommentTextMultipleOutline"
            /></template>
            <v-list-item-title>Bulk Messages</v-list-item-title>
          </v-list-item>
          <v-list-item
            v-if="phonesStore.owner"
            :to="{ name: 'search-messages' }"
          >
            <template #prepend><v-icon :icon="mdiMagnify" /></template>
            <v-list-item-title>Search Messages</v-list-item-title>
          </v-list-item>
          <v-list-item :to="{ name: 'contacts' }">
            <template #prepend><v-icon :icon="mdiAccountMultiple" /></template>
            <v-list-item-title>Contacts</v-list-item-title>
          </v-list-item>
          <v-list-item :to="{ name: 'settings' }">
            <template #prepend><v-icon :icon="mdiAccountCog" /></template>
            <v-list-item-title>Settings</v-list-item-title>
          </v-list-item>
          <v-list-item :to="{ name: 'phone-api-keys' }">
            <template #prepend><v-icon :icon="mdiCellphoneKey" /></template>
            <v-list-item-title :class="{ 'pr-16': lgAndUp }"
              >Phone API Keys</v-list-item-title
            >
          </v-list-item>
          <v-list-item
            v-if="phonesStore.owner"
            :href="appStore.appData.appDownloadUrl"
          >
            <template #prepend><v-icon :icon="mdiDownload" /></template>
            <v-list-item-title>Download App</v-list-item-title>
          </v-list-item>
          <v-list-item :to="{ name: 'billing' }">
            <template #prepend><v-icon :icon="mdiFinance" /></template>
            <v-list-item-title>Usage & Billing</v-list-item-title>
          </v-list-item>
          <v-list-item @click.prevent="logout">
            <template #prepend><v-icon :icon="mdiLogout" /></template>
            <v-list-item-title>Logout</v-list-item-title>
          </v-list-item>
        </v-list>
      </v-menu>
    </div>
    <div
      role="group"
      aria-label="Message thread filters"
      class="d-flex ga-2 pt-3"
    >
      <v-btn
        class="text-medium-emphasis"
        density="comfortable"
        :color="!threadsStore.unreadOnly ? 'primary' : undefined"
        variant="tonal"
        :aria-pressed="!threadsStore.unreadOnly"
        :disabled="!phonesStore.owner || threadsStore.loadingThreads"
        @click="filterThreads(false)"
      >
        All
      </v-btn>
      <v-btn
        class="text-medium-emphasis"
        density="comfortable"
        :color="threadsStore.unreadOnly ? 'primary' : undefined"
        variant="tonal"
        :aria-pressed="threadsStore.unreadOnly"
        :disabled="!phonesStore.owner || threadsStore.loadingThreads"
        @click="filterThreads(true)"
      >
        Unread
      </v-btn>
    </div>
  </v-sheet>
</template>