Skip to content

Commit

Permalink
Add empty spectate view
Browse files Browse the repository at this point in the history
  • Loading branch information
magnetenstad committed Jan 29, 2024
1 parent 145eeec commit 946ff25
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const PasswordView = () => import('@/views/PasswordView.vue')
const StatisticsView = () => import('@/views/StatisticsView.vue')
const AdminView = () => import('@/views/AdminView.vue')
const SpectateLobbyView = () => import('@/views/SpectateLobbyView.vue')
const SpectateView = () => import('@/views/SpectateView.vue')

const routes: Readonly<RouteRecordRaw[]> = [
{ path: '/', name: 'root', redirect: { name: 'home' } },
Expand All @@ -19,6 +20,7 @@ const routes: Readonly<RouteRecordRaw[]> = [
{ path: '/password', name: 'password', component: PasswordView },
{ path: '/statistics', name: 'statistics', component: StatisticsView },
{ path: '/admin', name: 'admin', component: AdminView },
{ path: '/spectate', name: 'spectate', component: SpectateView },
{
path: '/spectate-lobby',
name: 'spectate-lobby',
Expand Down
2 changes: 1 addition & 1 deletion src/stores/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const initAuth = async (auth: AuthUser) => {

supabase.auth.onAuthStateChange(async (_, session) => {
const user = session?.user
if (user && useAuthStore().auth?.id != user?.id) {
if (user && useAuthStore().auth?.id != user.id) {
initAuth(user)
}
})
Expand Down
4 changes: 4 additions & 0 deletions src/stores/online.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const useOnlineStore = defineStore('online', {
return {
room,
usersOnline: new Set<string>(),
spectating: null as string | null,
}
},

Expand All @@ -34,6 +35,9 @@ export const useOnlineStore = defineStore('online', {
// @ts-ignore
const presence = p as OnlinePresence
this.usersOnline.delete(presence.userId)
if (presence.userId == this.spectating) {
this.spectating = null
}
})
})
.subscribe(async (status) => {
Expand Down
11 changes: 9 additions & 2 deletions src/views/SpectateLobbyView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

<h2>Users</h2>
<button
v-for="userId in useOnlineStore().usersOnline"
v-for="userId in onlineStore.usersOnline"
:disabled="userId == useAuthStore().auth?.id"
@click="router.push({ name: 'home' })"
@click="
() => {
onlineStore.spectating = userId
router.push({ name: 'spectate' })
}
"
>
{{ useUsersStore().getUser(userId)?.name }}
</button>
Expand All @@ -16,4 +21,6 @@ import { router } from '@/router'
import { useAuthStore } from '@/stores/auth'
import { useOnlineStore } from '@/stores/online'
import { useUsersStore } from '@/stores/users'
const onlineStore = useOnlineStore()
</script>
25 changes: 25 additions & 0 deletions src/views/SpectateView.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<button id="back" @click="router.push({ name: 'spectate-lobby' })">
Back
</button>

<h2>Spectating {{ onlineStore.spectating }}</h2>
</template>

<script lang="ts" setup>
import { router } from '@/router'
import { useOnlineStore } from '@/stores/online'
import { watch } from 'vue'
const onlineStore = useOnlineStore()
watch(
() => onlineStore.spectating,
(spectating) => {
if (!spectating) {
router.push({ name: 'spectate-lobby' })
}
},
{ immediate: true }
)
</script>

0 comments on commit 946ff25

Please sign in to comment.