safetwitch/src/components/StreamPreview.vue

86 lines
2.2 KiB
Vue
Raw Normal View History

2023-03-31 07:50:46 -04:00
<script lang="ts">
2023-05-12 10:21:31 -04:00
import { inject } from 'vue'
2023-03-31 07:50:46 -04:00
export interface Stream {
tags: string[]
title: string
topic: string
startedAt: number
viewers: number
preview: string
streamer: {
pfp: string
name: string
}
2023-03-31 07:50:46 -04:00
}
export default {
props: {
stream: {},
name: {
type: String
}
},
async setup(props) {
2023-05-12 10:21:31 -04:00
const protocol = inject('protocol')
let streamData: Stream | null = null
2023-03-31 07:50:46 -04:00
if (!props.stream && props.name) {
const streamDataFetch = await fetch(
2023-06-13 12:08:43 -04:00
`${protocol}${import.meta.env.SAFETWITCH_BACKEND_DOMAIN}/api/users/${props.name}`
2023-03-31 07:50:46 -04:00
)
2023-04-11 20:43:44 -04:00
const data = (await streamDataFetch.json()).data
2023-03-31 07:50:46 -04:00
2023-04-10 00:19:39 -04:00
if (data.stream) {
data.stream.streamer = { name: props.name, pfp: data.pfp }
streamData = data.stream
}
2023-03-31 07:50:46 -04:00
} else {
streamData = props.stream as Stream
}
2023-06-13 12:08:43 -04:00
const frontend_url = protocol + import.meta.env.SAFETWITCH_INSTANCE_DOMAIN
2023-03-31 07:50:46 -04:00
return {
frontend_url,
2023-04-08 20:04:28 -04:00
streamData,
protocol
2023-03-31 07:50:46 -04:00
}
},
methods: {
abbreviate(text: number) {
return Intl.NumberFormat('en-US', {
//@ts-ignore
notation: 'compact',
maximumFractionDigits: 1
}).format(text)
}
}
}
</script>
<template>
<div v-if="streamData">
2023-04-10 21:49:02 -04:00
<div class="bg-ctp-crust rounded-lg w-[23rem] md:w-[27rem]">
2023-04-10 00:19:39 -04:00
<RouterLink :to="'/' + streamData.streamer.name">
<img :src="streamData.preview" class="rounded-lg rounded-b-none" />
</RouterLink>
<div class="text-white p-2 inline-flex space-x-2 w-full h-16">
2023-03-31 07:50:46 -04:00
<div class="inline-flex">
2023-04-10 00:19:39 -04:00
<div class="inline-flex">
<img :src="streamData.streamer.pfp" class="rounded-full mr-2" />
<div class="w-full">
2023-04-10 21:49:02 -04:00
<p class="font-bold w-[18rem] md:w-[22.9rem] truncate">{{ streamData.title }}</p>
2023-04-10 00:19:39 -04:00
<div class="inline-flex w-full justify-between">
<p class="text-gray-300">{{ streamData.streamer.name }}</p>
<p class="self-end float-right">
<v-icon name="io-person"></v-icon> {{ abbreviate(streamData.viewers) }}
</p>
</div>
2023-03-31 07:50:46 -04:00
</div>
</div>
</div>
</div>
</div>
</div>
</template>