team-1/frontend/src/routes/admin/[id]/+page.svelte

96 lines
3 KiB
Svelte
Raw Normal View History

2025-08-02 00:52:13 +02:00
<script lang="ts">
import QueueSlider from "$lib/components/QueueSlider.svelte"
2025-08-02 03:54:26 +02:00
import { type Song } from "$lib/types"
import { onMount } from "svelte"
import type { FetchError } from "$lib/types"
2025-08-02 06:07:14 +02:00
import { getQueueSongs, getStreamingUrl, triggerPlayNext } from "$lib/utils.js"
2025-08-02 05:04:30 +02:00
import Error from "$lib/components/Error.svelte"
2025-08-02 00:52:13 +02:00
2025-08-02 03:54:26 +02:00
let { data } = $props()
let queueSongs = $state<Song[]>([])
2025-08-02 06:07:14 +02:00
let playingIndex = $state<number>(0)
2025-08-02 03:54:26 +02:00
let returnError = $state<FetchError | null>()
2025-08-02 06:07:14 +02:00
let currentPlaying = $derived<Song>(queueSongs[playingIndex])
let audioController = $state<HTMLAudioElement>()
2025-08-02 10:28:11 +02:00
let playerInfo = $state({
playing: false,
currentTime: 0,
duration: 0,
})
2025-08-02 06:07:14 +02:00
async function playOnEnd() {
let url = await getStreamingUrl(currentPlaying.uuid)
if (audioController) {
audioController.src = url
2025-08-02 09:19:39 +02:00
await audioController.play()
2025-08-02 06:07:14 +02:00
}
}
2025-08-02 10:28:11 +02:00
$effect(() => {
if (audioController) {
audioController.ontimeupdate = () => {
playerInfo.currentTime = audioController?.currentTime || 0
}
audioController.onloadedmetadata = () => {
playerInfo.duration = audioController?.duration || 0
}
audioController.onplay = () => {
playerInfo.playing = true
}
audioController.onpause = () => {
playerInfo.playing = false
}
}
})
2025-08-02 03:54:26 +02:00
onMount(async () => {
let songs, index
;[returnError, songs, index] = await getQueueSongs(data.roomId)
queueSongs = songs
playingIndex = index
2025-08-02 09:19:39 +02:00
})
2025-08-02 06:07:14 +02:00
2025-08-02 09:19:39 +02:00
$effect(() => {
playOnEnd()
2025-08-02 03:54:26 +02:00
})
2025-08-02 10:28:11 +02:00
const formatTime = (t: number) => {
const min = Math.floor(t / 60)
const sec = Math.floor(t % 60)
return `${min}:${sec.toString().padStart(2, "0")}`
}
2025-08-02 03:54:26 +02:00
async function playNext() {
let songs, index
;[returnError, songs, index] = await triggerPlayNext(data.roomId)
if (returnError) return
2025-08-02 05:04:30 +02:00
queueSongs = songs
2025-08-02 03:54:26 +02:00
playingIndex = index
}
2025-08-02 00:52:13 +02:00
</script>
2025-08-02 05:04:30 +02:00
{#if returnError}
<Error {returnError} />
{:else}
<div class="flex w-full flex-col items-center justify-center p-4 lg:p-10">
<QueueSlider {queueSongs} {playingIndex} />
2025-08-02 10:28:11 +02:00
<audio autoplay bind:this={audioController} hidden onended={playNext}></audio>
<div class="flex w-[30vw] flex-col items-start justify-start gap-4 p-2">
<p>{formatTime(playerInfo.currentTime)} - {formatTime(playerInfo.duration)}</p>
<input type="range" min="0" max={playerInfo.duration} disabled step="0.1" value={playerInfo.currentTime} class="w-full accent-blue-500" />
<div class="flex w-full flex-row items-center justify-center gap-6">
<button onclick={audioController.pause}>Pause</button>
<button onclick={playNext}>Next</button>
</div>
</div>
2025-08-02 05:04:30 +02:00
</div>
{/if}