Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

fix: style on thread page

Acho Arnold Ewin61b0463ccfcb
diff --git a/web/app/components/MessageThread.vue b/web/app/components/MessageThread.vue
index e14154e4..f9cc89db 100644
--- a/web/app/components/MessageThread.vue
+++ b/web/app/components/MessageThread.vue
@@ -1,7 +1,15 @@
<script setup lang="ts">
import { useDisplay } from 'vuetify'
-import { mdiPlus, mdiDownload, mdiCheckAll, mdiCheck, mdiAlert } from '@mdi/js'
+import {
+ mdiPlus,
+ mdiDownload,
+ mdiCheckAll,
+ mdiCheck,
+ mdiAlert,
+ mdiAccount,
+} from '@mdi/js'
import { formatPhoneNumber } from '~/utils/filters'
+import { isValidPhoneNumber } from 'libphonenumber-js'
const { mdAndDown } = useDisplay()
const threadsStore = useThreadsStore()
@@ -9,11 +17,6 @@ const phonesStore = usePhonesStore()
const appStore = useAppStore()
const notificationsStore = useNotificationsStore()
-function getInitials(contact: string): string {
- const formatted = formatPhoneNumber(contact)
- return formatted.substring(0, 2)
-}
-
function threadDate(date: string): string {
return new Date(date).toLocaleString(undefined, {
month: 'short',
@@ -96,6 +99,7 @@ function onInstallApp() {
</div>
<v-list
class="pt-0"
+ lines="two"
v-if="!threadsStore.loadingThreads && threadsStore.threads.length > 0"
>
<v-list-item
@@ -106,51 +110,59 @@ function onInstallApp() {
>
<template #prepend>
<v-avatar :color="thread.color" size="40">
- <span class="text-white text-body-medium">{{
- getInitials(thread.contact)
+ <v-icon v-if="isValidPhoneNumber(thread.contact)" color="white">{{
+ mdiAccount
+ }}</v-icon>
+ <span v-else class="text-white text-headline-small">{{
+ thread.contact.substring(0, 1)
}}</span>
</v-avatar>
</template>
<v-list-item-title>{{
formatPhoneNumber(thread.contact)
}}</v-list-item-title>
- <v-list-item-subtitle class="text-truncate" style="max-width: 250px">
+ <v-list-item-subtitle
+ class="text-truncate mt-1"
+ style="max-width: 250px"
+ >
{{ thread.last_message_content }}
</v-list-item-subtitle>
<template #append>
<div class="d-flex flex-column align-end">
- <span class="text-caption text-medium-emphasis">
+ <span class="text-body-small text-medium-emphasis">
{{ threadDate(thread.order_timestamp) }}
</span>
- <v-icon
- v-if="thread.status === 'expired'"
- color="warning"
- size="small"
- :icon="mdiAlert"
- />
- <v-icon
- v-else-if="thread.status === 'delivered'"
- color="primary"
- size="small"
- :icon="mdiCheckAll"
- />
- <v-icon
- v-else-if="thread.status === 'received'"
- color="success"
- size="small"
- :icon="mdiCheckAll"
- />
- <v-icon
- v-else-if="thread.status === 'sent'"
- size="small"
- :icon="mdiCheck"
- />
- <v-icon
- v-else-if="thread.status === 'failed'"
- color="error"
- size="small"
- :icon="mdiAlert"
- />
+ <div 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>
</div>
</template>
</v-list-item>
diff --git a/web/app/pages/heartbeats/[id].vue b/web/app/pages/heartbeats/[id].vue
index c3abbc6b..0d8d4d79 100644
--- a/web/app/pages/heartbeats/[id].vue
+++ b/web/app/pages/heartbeats/[id].vue
@@ -1,5 +1,11 @@
<script setup lang="ts">
-import { mdiArrowLeft, mdiHeartPulse } from '@mdi/js'
+import { mdiArrowLeft, mdiCircle } from '@mdi/js'
+import {
+ formatDuration as formatDurationFns,
+ intervalToDuration,
+} from 'date-fns'
+import { Bar } from 'vue-chartjs'
+import type { ChartData, ChartOptions } from 'chart.js'
import type { Heartbeat } from '~~/shared/types/heartbeat'
definePageMeta({
@@ -11,22 +17,127 @@ useHead({
})
const route = useRoute()
-const { mdAndDown, lgAndUp } = useDisplay()
+const { mdAndDown, mdAndUp, lgAndUp } = useDisplay()
const authStore = useAuthStore()
-const { useApi } = useApiComposable()
+const phonesStore = usePhonesStore()
+const { formatPhoneNumber, formatTimestamp } = useFilters()
const loading = ref(true)
const heartbeats = ref<Heartbeat[]>([])
const phoneId = computed(() => route.params.id as string)
+interface HeartbeatTableItem {
+ id: string
+ owner: string
+ timestamp: string
+ interval: number
+}
+
+const dataTableHeaders = [
+ { title: 'HEARTBEAT ID', key: 'id', sortable: false },
+ { title: 'PHONE NUMBER', key: 'owner', sortable: false },
+ { title: 'RECEIVED AT', key: 'timestamp' },
+ { title: 'TIME INTERVAL', key: 'interval' },
+] as const
+
+function getDiff(a: string, b: string): number {
+ return new Date(a).getTime() - new Date(b).getTime()
+}
+
+function formatInterval(duration: number): string {
+ if (duration === 0) {
+ return '-'
+ }
+ const start = new Date()
+ start.setMilliseconds(start.getMilliseconds() + duration)
+ return (
+ formatDurationFns(intervalToDuration({ start: new Date(), end: start })) ||
+ '0 seconds'
+ )
+}
+
+const dataTableItems = computed<HeartbeatTableItem[]>(() => {
+ return heartbeats.value.map((heartbeat, index) => {
+ let interval = 0
+ if (index < heartbeats.value.length - 1) {
+ interval = getDiff(
+ heartbeat.timestamp,
+ heartbeats.value[index + 1]!.timestamp,
+ )
+ }
+ return {
+ id: heartbeat.id,
+ timestamp: heartbeat.timestamp,
+ owner: heartbeat.owner,
+ interval,
+ }
+ })
+})
+
+const chartData = computed<ChartData<'bar'>>(() => {
+ const data = heartbeats.value.map((heartbeat) => ({
+ x: new Date(heartbeat.timestamp).toISOString(),
+ y: 1,
+ }))
+
+ if (!data.length) {
+ return {
+ datasets: [{ data: [], backgroundColor: '#2196f3' }],
+ } as unknown as ChartData<'bar'>
+ }
+
+ let prev = new Date(data[0]!.x)
+ const newData = [] as Array<{ x: string; y: number }>
+ for (let i = 1; i < data.length; i++) {
+ const current = new Date(data[i]!.x)
+ const diff = prev.getTime() - current.getTime()
+ if (diff > 600000) {
+ // 10 minutes
+ newData.push(data[i]!)
+ prev = current
+ }
+ }
+
+ return {
+ datasets: [{ data: newData, backgroundColor: '#2196f3' }],
+ } as unknown as ChartData<'bar'>
+})
+
+const chartOptions = computed<ChartOptions<'bar'>>(() => {
+ return {
+ responsive: true,
+ maintainAspectRatio: false,
+ plugins: {
+ legend: { display: false },
+ tooltip: {
+ callbacks: {
+ label(context) {
+ const dataset = context.dataset.data as unknown as Array<{
+ x: string
+ }>
+ if (context.dataIndex === dataset.length - 1) {
+ return '-'
+ }
+ const duration = intervalToDuration({
+ start: new Date(dataset[context.dataIndex + 1]!.x),
+ end: new Date(dataset[context.dataIndex]!.x),
+ })
+ return formatDurationFns(duration)
+ },
+ },
+ },
+ },
+ scales: {
+ x: { type: 'time' },
+ y: { display: false },
+ },
+ } as ChartOptions<'bar'>
+})
+
async function loadHeartbeats() {
loading.value = true
try {
- const api = useApi()
- const response = await api<{ data: Heartbeat[] }>(
- `/v1/heartbeats?owner=${phoneId.value}`,
- )
- heartbeats.value = response.data ?? []
+ heartbeats.value = await phonesStore.getHeartbeat(100)
} finally {
loading.value = false
}
@@ -34,6 +145,10 @@ async function loadHeartbeats() {
onMounted(async () => {
await authStore.loadUser()
+ await phonesStore.loadPhones()
+ if (!phonesStore.owner) {
+ phonesStore.setOwner(phoneId.value)
+ }
await loadHeartbeats()
})
</script>
@@ -46,17 +161,50 @@ onMounted(async () => {
<VIcon :icon="mdiArrowLeft" />
</VBtn>
<VToolbarTitle>
- <VIcon :icon="mdiHeartPulse" class="mr-2" />
Heartbeats
+ <VIcon
+ size="x-small"
+ class="mx-2"
+ color="primary"
+ :icon="mdiCircle"
+ />
+ <span v-if="phonesStore.owner">{{
+ formatPhoneNumber(phonesStore.owner)
+ }}</span>
</VToolbarTitle>
</VAppBar>
<VContainer class="mt-16">
<VRow>
- <VCol cols="12" md="8" offset-md="2">
- <h4 class="text-headline-large mb-3">Phone Heartbeats</h4>
- <p class="text-medium-emphasis">
- Monitor the connectivity status of your Android phones. Heartbeats
- are sent every 15 minutes when the phone is online.
+ <VCol cols="12">
+ <p>
+ Every 15 minutes, the httpSMS app on your Android phone sends a
+ heartbeat event to the httpsms API to show that it is alive. The
+ reason for this is because the Android operating system sometimes
+ kills an application to save battery
+ <a
+ href="https://dontkillmyapp.com"
+ class="text-decoration-none hover:text-decoration-underline"
+ target="_blank"
+ >https://dontkillmyapp.com</a
+ >.
+ </p>
+ <p>
+ If httpSMS doesn't get any heartbeat event in a 1-hour interval,
+ you will get an email notification about it so you can check if
+ there is an issue with your Android phone.
+ </p>
+ </VCol>
+ <VCol v-if="mdAndUp" cols="12" class="px-0">
+ <div class="heartbeat--chart">
+ <ClientOnly>
+ <Bar :data="chartData" :options="chartOptions" />
+ </ClientOnly>
+ </div>
+ </VCol>
+ <VCol cols="12">
+ <p>
+ The table below shows the last 100 heartbeat events received from
+ the httpSMS app on your Android phone.
</p>
<VProgressLinear
v-if="loading"
@@ -64,42 +212,44 @@ onMounted(async () => {
indeterminate
class="mb-4"
/>
- <VTable v-else-if="heartbeats.length">
- <thead>
- <tr class="text-uppercase text-title-medium">
- <th class="text-left">Phone</th>
- <th class="text-center">Status</th>
- <th class="text-center">Last Heartbeat</th>
- <th class="text-center">Charging</th>
- </tr>
- </thead>
- <tbody>
- <tr v-for="heartbeat in heartbeats" :key="heartbeat.id">
- <td>{{ useFilters().phoneNumber(heartbeat.owner) }}</td>
- <td class="text-center">
- <VChip
- :color="heartbeat.is_online ? 'success' : 'error'"
- size="small"
- >
- {{ heartbeat.is_online ? 'Online' : 'Offline' }}
- </VChip>
- </td>
- <td class="text-center">
- {{ useFilters().timestamp(heartbeat.timestamp) }}
- </td>
- <td class="text-center">
- {{ heartbeat.charging ? 'Yes' : 'No' }}
- </td>
- </tr>
- </tbody>
- </VTable>
- <p v-else class="text-medium-emphasis text-center mt-8">
- No heartbeats found. Make sure the httpSMS app is running on your
- phone.
- </p>
+ <VDataTable
+ v-else
+ :headers="dataTableHeaders"
+ :items="dataTableItems"
+ :items-per-page="100"
+ :sort-by="[{ key: 'timestamp', order: 'desc' }]"
+ hide-default-footer
+ class="heartbeat--table"
+ :row-props="
+ ({ item }) =>
+ item.interval > 3600000 ? { class: 'heartbeat--missed' } : {}
+ "
+ >
+ <template #[`item.interval`]="{ item }">
+ {{ formatInterval(item.interval) }}
+ </template>
+ <template #[`item.owner`]="{ item }">
+ {{ formatPhoneNumber(item.owner) }}
+ </template>
+ <template #[`item.timestamp`]="{ item }">
+ {{ formatTimestamp(item.timestamp) }}
+ </template>
+ </VDataTable>
</VCol>
</VRow>
</VContainer>
</div>
</VContainer>
</template>
+
+<style lang="scss">
+.heartbeat--chart {
+ height: 200px;
+}
+
+.v-application {
+ .heartbeat--table tbody tr.heartbeat--missed {
+ background: #b71c1c;
+ }
+}
+</style>
diff --git a/web/tsconfig.tsbuildinfo b/web/tsconfig.tsbuildinfo
index db97672f..f492acbc 100644
--- a/web/tsconfig.tsbuildinfo
+++ b/web/tsconfig.tsbuildinfo
@@ -1 +1 @@
-{"root":["./app/composables/useapi.ts","./app/composables/usefilters.ts","./app/middleware/auth.ts","./app/middleware/guest.ts","./app/plugins/chart.client.ts","./app/plugins/firebase.client.ts","./app/plugins/highlightjs.client.ts","./app/plugins/vphoneinput.client.ts","./app/plugins/vue-glow.client.ts","./app/stores/app.ts","./app/stores/auth.ts","./app/stores/billing.ts","./app/stores/messages.ts","./app/stores/notifications.ts","./app/stores/phones.ts","./app/stores/threads.ts","./app/types/nuxt-shims.d.ts","./app/utils/bag.ts","./app/utils/capitalize.ts","./app/utils/countries.ts","./app/utils/errors.ts","./app/utils/filters.ts","./shared/types/api.ts","./shared/types/billing.ts","./shared/types/heartbeat.ts","./shared/types/message-thread.ts","./shared/types/message.ts","./shared/types/user.ts"],"errors":true,"version":"6.0.3"}
\ No newline at end of file
+{"root":["./app/composables/useapi.ts","./app/composables/usefilters.ts","./app/middleware/auth.ts","./app/middleware/guest.ts","./app/plugins/chart.client.ts","./app/plugins/firebase.client.ts","./app/plugins/highlightjs.client.ts","./app/plugins/vphoneinput.client.ts","./app/plugins/vue-glow.client.ts","./app/stores/app.ts","./app/stores/auth.ts","./app/stores/billing.ts","./app/stores/messages.ts","./app/stores/notifications.ts","./app/stores/phones.ts","./app/stores/threads.ts","./app/types/nuxt-shims.d.ts","./app/utils/api-error.ts","./app/utils/bag.ts","./app/utils/capitalize.ts","./app/utils/countries.ts","./app/utils/errors.ts","./app/utils/filters.ts","./shared/types/api.ts","./shared/types/billing.ts","./shared/types/bulk-message.ts","./shared/types/heartbeat.ts","./shared/types/message-thread.ts","./shared/types/message.ts","./shared/types/user.ts"],"errors":true,"version":"6.0.3"}
\ No newline at end of file