safetwitch/src/views/HomepageView.vue

185 lines
5.5 KiB
Vue
Raw Normal View History

2023-03-19 19:51:55 -04:00
<script lang="ts">
2023-03-29 10:02:04 -04:00
import { ref, type Ref } from 'vue'
2023-03-31 07:50:46 -04:00
import StreamPreviewVue from '@/components/StreamPreview.vue'
import ErrorMessage from '@/components/ErrorMessage.vue'
import LoadingScreen from '@/components/LoadingScreen.vue'
2023-03-29 10:02:04 -04:00
2023-03-19 19:51:55 -04:00
export default {
async setup() {
2023-04-08 20:04:28 -04:00
const protocol = import.meta.env.VITE_HTTPS === 'true' ? 'https://' : 'http://'
2023-04-10 00:19:39 -04:00
let data: Ref<any | undefined> = ref()
2023-04-08 20:04:28 -04:00
let frontend_url = protocol + import.meta.env.VITE_INSTANCE_DOMAIN
2023-03-29 10:02:04 -04:00
2023-03-19 19:51:55 -04:00
return {
2023-03-29 10:02:04 -04:00
data,
2023-04-08 20:04:28 -04:00
protocol,
2023-03-25 12:53:10 -04:00
frontend_url,
2023-03-31 07:50:46 -04:00
filterTags: '',
following: ref([])
2023-03-19 19:51:55 -04:00
}
},
methods: {
abbreviate(text: number) {
return Intl.NumberFormat('en-US', {
//@ts-ignore
notation: 'compact',
maximumFractionDigits: 1
}).format(text)
2023-03-25 12:53:10 -04:00
},
filterSearches(toFilter: string) {
2023-04-10 00:19:39 -04:00
const categories = this.$refs.categoryItem
const wantedTags: string[] = toFilter.split(',').filter((v) => v.toLowerCase())
2023-03-25 12:53:10 -04:00
for (let category of categories as any) {
2023-03-29 10:26:55 -04:00
let tagElements = category.getElementsByTagName('span')
2023-03-25 12:53:10 -04:00
let tags = []
for (let tag of tagElements) {
tags.push(tag.innerText.toLowerCase())
}
// Create sets from the arrays
const [set1, set2] = [new Set(wantedTags), new Set(tags)]
2023-03-29 10:26:55 -04:00
const common = [...set1].filter((x) => set2.has(x))
if (common.length === wantedTags.length) {
category.style.display = ''
} else if (wantedTags[0] === '') {
category.style.display = ''
2023-03-25 12:53:10 -04:00
console.log('ok')
} else {
2023-03-29 10:26:55 -04:00
category.style.display = 'none'
2023-03-25 12:53:10 -04:00
}
}
2023-03-29 10:02:04 -04:00
},
getNextCategory() {
window.onscroll = async () => {
2023-03-29 10:26:55 -04:00
let bottomOfWindow =
document.documentElement.scrollTop + window.innerHeight ===
document.documentElement.offsetHeight
2023-03-29 10:02:04 -04:00
if (bottomOfWindow && this.data) {
const cursor = this.data[this.data.length - 1].cursor
2023-03-29 10:26:55 -04:00
if (!cursor) return
const res = await fetch(
2023-04-08 20:04:28 -04:00
`${this.protocol}${import.meta.env.VITE_BACKEND_DOMAIN}/api/discover/?cursor=${cursor}`
2023-03-29 10:26:55 -04:00
)
if (!res.ok) {
throw new Error('Failed to fetch data')
}
2023-03-29 10:02:04 -04:00
const data = await res.json()
for (let category of data) {
this.data.push(category)
}
2023-03-29 10:26:55 -04:00
}
2023-03-29 10:02:04 -04:00
}
2023-03-19 19:51:55 -04:00
}
2023-03-29 10:02:04 -04:00
},
async mounted() {
2023-03-29 10:02:04 -04:00
this.getNextCategory()
2023-03-31 07:50:46 -04:00
let following = localStorage.getItem('following')
if (following) {
this.following = JSON.parse(following)
} else {
this.following = []
}
try {
const res = await fetch(`${this.protocol}${import.meta.env.VITE_BACKEND_DOMAIN}/api/discover`)
const rawData = await res.json()
if (rawData.status === "ok") {
this.data = rawData.data
} else {
this.data = { status: 'error' }
}
} catch (error) {
console.error(error)
this.data = { status: 'error' }
}
2023-03-31 07:50:46 -04:00
},
components: {
StreamPreviewVue,
ErrorMessage,
LoadingScreen
2023-03-19 19:51:55 -04:00
}
}
</script>
<template>
<loading-screen v-if="!data"></loading-screen>
<error-message v-else-if="data.status === 'error'"></error-message>
<div v-else class="max-w-5xl mx-auto">
2023-03-31 07:50:46 -04:00
<div v-if="following.length > 0" class="p-2 text-white">
<h1 class="font-bold text-5xl">Following</h1>
<p class="text-xl">Streamers you follow</p>
<ul class="flex overflow-x-scroll flex-nowrap h-80 space-x-1">
2023-04-10 00:19:39 -04:00
<li
v-for="streamer in following"
:key="streamer"
class="inline-block hover:scale-105 transition-transform"
>
2023-03-31 07:50:46 -04:00
<stream-preview-vue :name="streamer"></stream-preview-vue>
</li>
</ul>
</div>
2023-03-19 19:51:55 -04:00
<div class="p-2">
<h1 class="font-bold text-5xl text-white">Discover</h1>
<p class="text-xl text-white">Sort through popular categories</p>
2023-03-19 19:51:55 -04:00
<div class="pt-5 inline-flex text-white">
<p class="mr-2 font-bold text-white">Filter by tag</p>
<form class="relative">
<label for="searchBar" class="hidden">Search</label>
<v-icon name="io-search-outline" class="absolute my-auto inset-y-0 left-2"></v-icon>
2023-03-19 19:51:55 -04:00
<input
type="text"
id="searchBar"
name="searchBar"
placeholder="Search"
2023-03-25 12:53:10 -04:00
v-model="filterTags"
@keyup="filterSearches(filterTags)"
2023-03-19 19:51:55 -04:00
class="rounded-md p-1 pl-8 text-black bg-neutral-500 placeholder:text-white"
/>
</form>
</div>
</div>
2023-03-25 12:53:10 -04:00
<ul ref="categoryList">
<li
v-for="category in data"
:key="category"
2023-03-25 12:53:10 -04:00
ref="categoryItem"
class="inline-flex m-2 hover:scale-105 transition-transform"
>
2023-04-10 21:49:02 -04:00
<div class="bg-ctp-crust w-40 lg:w-[13.5rem] md:w-[13.5rem] rounded-lg">
<router-link :to="`/game/${category.name}`">
<img :src="category.image" class="rounded-lg rounded-b-none" />
</router-link>
2023-03-19 19:51:55 -04:00
<div class="p-2">
<div>
2023-04-10 21:49:02 -04:00
<p class="font-bold text-white text-xl sm:text-base md:text-xl">{{ category.displayName }}</p>
<p class="text-sm text-white">{{ abbreviate(category.viewers) }} viewers</p>
2023-03-19 19:51:55 -04:00
</div>
<ul class="h-8 overflow-hidden">
<li v-for="tag in category.tags" :key="tag" class="inline-flex">
<span
class="p-2.5 py-1.5 bg-ctp-surface0 rounded-md m-0.5 text-xs font-bold text-white"
>{{ tag }}</span
>
2023-03-19 19:51:55 -04:00
</li>
</ul>
</div>
</div>
</li>
</ul>
</div>
</template>