All files / stream StreamEventsPanel.vue

0% Statements 0/22
0% Branches 0/9
0% Functions 0/6
0% Lines 0/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                           
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { fetchLive } from './liveApi'
import { fmt, plural } from '@shared/naming'
import type { StreamEvent, StreamEvents } from './types'
 
const REFRESH_MS = 60000
const events = ref<StreamEvents | null>(null)
let timer: number | undefined
 
const load = async () => {
  try {
    events.value = (await fetchLive<StreamEvents>('stream-events', 'no-store', 'json')).data
  } catch {
    events.value = null
  }
}
 
const withAmount = (event: StreamEvent | null, unit: string) =>
  event === null ? 'โ€”' : `${event.name} ยท ${fmt(event.amount, 0)} ${plural(event.amount, unit)}`
 
const rows = computed(() => {
  const current = events.value
  if (current === null) return []
  return [
    { label: 'New follower', value: current.follower ?? 'โ€”' },
    { label: 'New subscriber', value: current.subscriber ?? 'โ€”' },
    { label: 'Last raid', value: withAmount(current.raid, 'viewer') },
    { label: 'Last cheer', value: withAmount(current.cheer, 'bit') },
    { label: 'Followers', value: fmt(current.followers, 0) },
    { label: 'Subscribers', value: fmt(current.subscribers, 0) },
  ]
})
 
onMounted(() => {
  load()
  timer = window.setInterval(load, REFRESH_MS)
})
onBeforeUnmount(() => window.clearInterval(timer))
</script>
 
<template>
  <section class="panel events-panel">
    <span class="panel-title">On the channel</span>
    <dl v-if="Irows.length" class="events-grid">
      <div v-for="row in rows" :key="row.label">
        <dt>{{ row.label }}</dt>
        <dd>{{ row.value }}</dd>
      </div>
    </dl>
    <span v-else class="panel-placeholder">Connecting to Twitch</span>
  </section>
</template>