1
0
Fork 0
polaris-task-force/src/components/frontend/story/BeatVoiceover.tsx
Z8MB1E dffcd0013c chore(chronicle): rename Story Beats to Chronicle Entries
The Chronicle collections were auto-labeled from their slugs ("Story
Beats"). Give them explicit Chronicle labels and adopt the word
Chronicle in the surrounding titles, descriptions, and event text.

- StoryBeats: labels Chronicle Entry/Entries, admin group Story ->
  Chronicle, descriptions updated
- Voiceovers / StoryBeatStates: Chronicle labels, group, descriptions
  (including the "beat" field label)
- GameEventLogs: event-log target labels -> Chronicle Entries / States
- lib/story: event message text -> "chronicle entry" (internal
  event-type strings left intact so existing logs stay valid)
- regenerate payload-types.ts

Slugs are unchanged, so no migration is required.
2026-10-09 13:48:14 -04:00

115 lines
3.9 KiB
TypeScript

"use client";
import { useEffect, useRef, useState, type ChangeEvent } from "react";
import { Pause, Play } from "lucide-react";
import { Button } from "@/components/ui/button";
function formatTime(seconds: number): string {
if (!Number.isFinite(seconds) || seconds <= 0) return "0:00";
const total = Math.floor(seconds);
const minutes = Math.floor(total / 60);
const secs = total % 60;
return `${minutes}:${secs.toString().padStart(2, "0")}`;
}
/**
* Minimal audiobook-style player for a chronicle entry narration. Play/pause plus
* a seek bar and time readout; ends back at zero (per-chapter playback, no
* auto-advance to other chapters). AI-generated narrations get a small note.
*/
export function BeatVoiceover({
src,
label,
aiGenerated = false,
}: {
readonly src: string;
readonly label: string;
readonly aiGenerated?: boolean;
}) {
const audioRef = useRef<HTMLAudioElement>(null);
const [playing, setPlaying] = useState(false);
const [currentTime, setCurrentTime] = useState(0);
const [duration, setDuration] = useState(0);
useEffect(() => {
const audio = audioRef.current;
if (!audio) return;
const onPlay = () => setPlaying(true);
const onPause = () => setPlaying(false);
const onTimeUpdate = () => setCurrentTime(audio.currentTime);
const onLoadedMetadata = () => setDuration(audio.duration);
const onEnded = () => {
setPlaying(false);
setCurrentTime(0);
};
audio.addEventListener("play", onPlay);
audio.addEventListener("pause", onPause);
audio.addEventListener("timeupdate", onTimeUpdate);
audio.addEventListener("loadedmetadata", onLoadedMetadata);
audio.addEventListener("ended", onEnded);
return () => {
audio.removeEventListener("play", onPlay);
audio.removeEventListener("pause", onPause);
audio.removeEventListener("timeupdate", onTimeUpdate);
audio.removeEventListener("loadedmetadata", onLoadedMetadata);
audio.removeEventListener("ended", onEnded);
};
}, []);
function togglePlay() {
const audio = audioRef.current;
if (!audio) return;
if (playing) {
audio.pause();
} else {
void audio.play().catch(() => setPlaying(false));
}
}
function handleSeek(event: ChangeEvent<HTMLInputElement>) {
const audio = audioRef.current;
if (!audio) return;
const next = Number(event.target.value);
if (!Number.isFinite(next)) return;
audio.currentTime = next;
setCurrentTime(next);
}
const seekMax = duration > 0 ? duration : 0;
return (
<div className="flex flex-col gap-1 items-center">
<div className="flex items-center gap-2.5" aria-label={`Audio narration: ${label}`}>
<audio ref={audioRef} src={src} preload="metadata" />
<Button
type="button"
variant="ghost"
size="icon"
className="size-7 text-muted-foreground hover:text-foreground"
aria-label={playing ? `Pause narration: ${label}` : `Play narration: ${label}`}
onClick={togglePlay}
>
{playing ? <Pause className="size-4" /> : <Play className="size-4" />}
</Button>
<input
type="range"
min={0}
max={seekMax}
step={0.1}
value={Math.min(currentTime, seekMax || 0)}
onChange={handleSeek}
disabled={duration <= 0}
aria-label={`Seek narration: ${label}`}
className="h-1 w-32 cursor-pointer accent-primary disabled:cursor-default disabled:opacity-40 sm:w-44"
/>
<span className="font-mono text-xs tabular-nums text-muted-foreground">
{formatTime(currentTime)} / {formatTime(duration)}
</span>
</div>
{aiGenerated && <p className="text-xs text-muted-foreground opacity-50 text-center">This audio narration was generated using AI. As a result, the quality of the narration may be inconsistent.<br/><br/>Want to narrate for the Chronicle? Reach out to Jason to sign up!</p>}
</div>
);
}