2026-08-07 10:44:03 +07:00
|
|
|
"use client";
|
|
|
|
|
|
2026-08-14 10:49:44 +07:00
|
|
|
import {
|
2026-08-15 17:53:48 +07:00
|
|
|
ListMusic,
|
|
|
|
|
Play,
|
2026-08-15 20:03:55 +07:00
|
|
|
Radio,
|
2026-08-15 17:53:48 +07:00
|
|
|
Repeat,
|
|
|
|
|
SkipForward,
|
|
|
|
|
Square,
|
2026-08-18 09:05:12 +07:00
|
|
|
Volume2,
|
2026-08-15 17:53:48 +07:00
|
|
|
} from "lucide-react";
|
2026-08-15 20:03:55 +07:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
import { useAmbient } from "@/components/ambient/ambient-context";
|
|
|
|
|
import { Button, GlassPanel, Input, toast } from "@/components/primitives";
|
|
|
|
|
import { ErrorState, LoadingState, SectionHeader } from "@/components/shared";
|
2026-08-15 17:53:48 +07:00
|
|
|
import {
|
2026-08-15 20:03:55 +07:00
|
|
|
useMediaLoop,
|
2026-08-14 11:28:08 +07:00
|
|
|
useMediaQueue,
|
2026-08-14 10:49:44 +07:00
|
|
|
useMediaSkip,
|
2026-08-15 20:03:55 +07:00
|
|
|
useMediaState,
|
2026-08-14 10:49:44 +07:00
|
|
|
useMediaStop,
|
|
|
|
|
useMediaWsSync,
|
|
|
|
|
} from "@/hooks";
|
2026-08-18 09:05:12 +07:00
|
|
|
import { formatDuration } from "@/lib/format";
|
2026-08-07 10:44:03 +07:00
|
|
|
import type { MediaState } from "@/lib/types";
|
2026-08-15 20:03:55 +07:00
|
|
|
import { useWebSocket } from "@/lib/ws/context";
|
2026-08-07 10:44:03 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
export function MediaView({ initialStatus }: { initialStatus?: MediaState }) {
|
2026-08-07 10:44:03 +07:00
|
|
|
const ws = useWebSocket();
|
2026-08-15 17:53:48 +07:00
|
|
|
const { data: media, isLoading, error } = useMediaState(initialStatus);
|
|
|
|
|
const queue = useMediaQueue();
|
2026-08-14 10:49:44 +07:00
|
|
|
const skip = useMediaSkip();
|
|
|
|
|
const stop = useMediaStop();
|
2026-08-15 17:53:48 +07:00
|
|
|
const loop = useMediaLoop();
|
2026-08-14 10:49:44 +07:00
|
|
|
useMediaWsSync(ws);
|
2026-08-15 17:53:48 +07:00
|
|
|
const ambient = useAmbient();
|
2026-08-14 10:49:44 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
const [url, setUrl] = useState("");
|
2026-08-14 10:49:44 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
const playing = media?.playing ?? false;
|
|
|
|
|
const current = media?.current ?? null;
|
|
|
|
|
const queueList = media?.queue ?? [];
|
2026-08-18 09:05:12 +07:00
|
|
|
const queueTotal = queueList.reduce(
|
|
|
|
|
(acc, it) => acc + (it.durationMs ?? 0),
|
|
|
|
|
0,
|
|
|
|
|
);
|
2026-08-14 11:28:08 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
const tone = playing ? "signal" : queueList.length ? "amber" : "signal";
|
|
|
|
|
useEffect(() => {
|
2026-08-15 20:03:55 +07:00
|
|
|
ambient.set(
|
|
|
|
|
tone,
|
|
|
|
|
playing ? 0.5 : 0.25,
|
|
|
|
|
playing ? "now playing" : "media idle",
|
|
|
|
|
);
|
2026-08-15 17:53:48 +07:00
|
|
|
}, [tone, playing, ambient]);
|
|
|
|
|
|
|
|
|
|
const onPlay = async () => {
|
|
|
|
|
const u = url.trim();
|
|
|
|
|
if (!u) {
|
|
|
|
|
toast({ title: "Enter a media URL", tone: "vermilion" });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await queue.mutateAsync({ url: u, mode: "music" });
|
|
|
|
|
setUrl("");
|
|
|
|
|
toast({ title: "Queued", tone: "signal" });
|
|
|
|
|
} catch (e) {
|
2026-08-15 20:03:55 +07:00
|
|
|
toast({
|
|
|
|
|
title: "Queue failed",
|
|
|
|
|
description: String(e),
|
|
|
|
|
tone: "vermilion",
|
|
|
|
|
});
|
2026-08-15 17:53:48 +07:00
|
|
|
}
|
2026-08-14 11:28:08 +07:00
|
|
|
};
|
2026-08-07 10:44:03 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
if (error && !media) return <ErrorState error={error} />;
|
|
|
|
|
if (!media && isLoading) return <LoadingState label="Reading deck" />;
|
2026-08-14 11:28:08 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
return (
|
|
|
|
|
<div className="space-y-5">
|
|
|
|
|
<GlassPanel glow className="relative overflow-hidden">
|
|
|
|
|
<div className="scan-line absolute inset-x-0 top-0" />
|
|
|
|
|
<div className="flex flex-col gap-5 sm:flex-row sm:items-center">
|
|
|
|
|
<div
|
|
|
|
|
className={`flex size-32 shrink-0 items-center justify-center rounded-full border border-hairline bg-gradient-to-br from-white/10 to-white/[0.02] ${playing ? "animate-spin-disc" : "animate-spin-disc paused"}`}
|
2026-08-14 10:49:44 +07:00
|
|
|
>
|
2026-08-15 17:53:48 +07:00
|
|
|
<div className="flex size-28 items-center justify-center rounded-full bg-canvas/60">
|
|
|
|
|
<ListMusic className="size-10 text-signal" />
|
|
|
|
|
</div>
|
2026-08-14 10:49:44 +07:00
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
|
|
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
|
<div className="eyebrow mb-1">Now playing</div>
|
2026-08-17 21:29:37 +07:00
|
|
|
<h2 className="display text-balance text-2xl text-ink">
|
2026-08-15 17:53:48 +07:00
|
|
|
{current?.title ?? "Nothing queued"}
|
|
|
|
|
</h2>
|
2026-08-18 09:05:12 +07:00
|
|
|
<div className="mt-1 flex flex-wrap items-center gap-x-2 gap-y-1 text-xs text-ink-faint">
|
|
|
|
|
{current?.source && (
|
|
|
|
|
<span className="mono truncate">{current.source}</span>
|
|
|
|
|
)}
|
|
|
|
|
{current?.mode && (
|
|
|
|
|
<span className="pill capitalize">{current.mode}</span>
|
|
|
|
|
)}
|
|
|
|
|
{formatDuration(current?.durationMs) && (
|
|
|
|
|
<span className="mono">
|
|
|
|
|
{formatDuration(current?.durationMs)}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
<div className="mt-4 flex flex-wrap items-center gap-2">
|
2026-08-15 20:03:55 +07:00
|
|
|
<Button
|
|
|
|
|
variant="primary"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={onPlay}
|
|
|
|
|
disabled={queue.isPending}
|
|
|
|
|
>
|
2026-08-15 17:53:48 +07:00
|
|
|
<Play className="size-4" /> Queue & play
|
|
|
|
|
</Button>
|
2026-08-15 20:03:55 +07:00
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => skip.mutate()}
|
|
|
|
|
disabled={skip.isPending}
|
|
|
|
|
>
|
2026-08-15 17:53:48 +07:00
|
|
|
<SkipForward className="size-4" /> Skip
|
|
|
|
|
</Button>
|
2026-08-15 20:03:55 +07:00
|
|
|
<Button
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => stop.mutate()}
|
|
|
|
|
disabled={stop.isPending}
|
|
|
|
|
>
|
2026-08-15 17:53:48 +07:00
|
|
|
<Square className="size-4" /> Stop
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant={media?.loop ? "primary" : "outline"}
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={() => loop.mutate(!media?.loop)}
|
|
|
|
|
aria-pressed={!!media?.loop}
|
|
|
|
|
>
|
|
|
|
|
<Repeat className="size-4" /> Loop
|
|
|
|
|
</Button>
|
2026-08-14 10:49:44 +07:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-08-18 09:05:12 +07:00
|
|
|
<div className="mt-5 flex flex-wrap items-center gap-3">
|
|
|
|
|
<div className="relative min-w-0 flex-1">
|
|
|
|
|
<Input
|
|
|
|
|
placeholder="Paste a YouTube / music URL…"
|
|
|
|
|
value={url}
|
|
|
|
|
onChange={(e) => setUrl(e.target.value)}
|
|
|
|
|
onKeyDown={(e) => e.key === "Enter" && onPlay()}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex shrink-0 items-center gap-2 rounded-[10px] border border-hairline bg-white/5 px-3 py-2.5">
|
|
|
|
|
<Volume2 className="size-4 text-ink-faint" />
|
|
|
|
|
<div className="h-1.5 w-24 overflow-hidden rounded-full bg-white/10">
|
|
|
|
|
<div
|
|
|
|
|
className="h-full rounded-full bg-signal/70"
|
|
|
|
|
style={{
|
|
|
|
|
width: `${Math.round((media?.musicVolume ?? 0) * 100)}%`,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<span className="mono w-9 text-right text-[0.65rem] text-ink-faint">
|
|
|
|
|
{Math.round((media?.musicVolume ?? 0) * 100)}%
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
|
|
|
|
</GlassPanel>
|
2026-08-14 10:49:44 +07:00
|
|
|
|
2026-08-15 17:53:48 +07:00
|
|
|
<GlassPanel>
|
|
|
|
|
<SectionHeader
|
|
|
|
|
eyebrow="up next"
|
|
|
|
|
title="Queue"
|
2026-08-15 20:03:55 +07:00
|
|
|
action={
|
|
|
|
|
<span className="mono text-xs text-ink-faint">
|
2026-08-18 09:05:12 +07:00
|
|
|
{queueList.length} track{queueList.length === 1 ? "" : "s"}
|
|
|
|
|
{queueTotal && ` · ${formatDuration(queueTotal)}`}
|
2026-08-15 20:03:55 +07:00
|
|
|
</span>
|
|
|
|
|
}
|
2026-08-15 17:53:48 +07:00
|
|
|
/>
|
|
|
|
|
{queueList.length === 0 ? (
|
|
|
|
|
<div className="flex flex-col items-center gap-2 py-10 text-center">
|
|
|
|
|
<Radio className="size-6 text-ink-faint" />
|
|
|
|
|
<div className="text-sm text-ink-soft">Queue is empty</div>
|
2026-08-15 20:03:55 +07:00
|
|
|
<div className="text-xs text-ink-faint">
|
|
|
|
|
Paste a URL above to start playback.
|
|
|
|
|
</div>
|
2026-08-14 10:49:44 +07:00
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
) : (
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
{queueList.map((item, i) => (
|
2026-08-15 20:03:55 +07:00
|
|
|
<div
|
|
|
|
|
key={`${item.source}-${i}`}
|
|
|
|
|
className="flex items-center gap-3 rounded-[10px] border border-hairline bg-white/5 px-3 py-2.5"
|
|
|
|
|
>
|
2026-08-15 17:53:48 +07:00
|
|
|
<span className="mono w-5 text-ink-faint">{i + 1}</span>
|
|
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
|
<div className="truncate text-sm text-ink">{item.title}</div>
|
2026-08-15 20:03:55 +07:00
|
|
|
<div className="mono truncate text-[0.65rem] text-ink-faint">
|
|
|
|
|
{item.source}
|
|
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
2026-08-18 09:05:12 +07:00
|
|
|
<span className="pill capitalize">{item.mode ?? "music"}</span>
|
|
|
|
|
{formatDuration(item.durationMs) && (
|
|
|
|
|
<span className="mono w-10 text-right text-[0.65rem] text-ink-faint">
|
|
|
|
|
{formatDuration(item.durationMs)}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
2026-08-15 17:53:48 +07:00
|
|
|
</div>
|
2026-08-14 10:49:44 +07:00
|
|
|
))}
|
|
|
|
|
</div>
|
2026-08-15 17:53:48 +07:00
|
|
|
)}
|
|
|
|
|
</GlassPanel>
|
2026-08-07 10:44:03 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|