"use client";
import {
useAudioPlayer,
type AudioTrack,
AudioWaveAnimation,
} from "@/components/audio-player";
import {
rememberedQueue,
useTranscriptQueue,
yearQueue,
} from "@/components/audio-player/transcript-queue";
import { cn } from "@/lib/utils";
import { Play } from "lucide-react";
type PlayAudioButtonProps = {
audioUrl: string;
title: string;
subtitle?: string;
sourceUrl: string;
type?: string;
className?: string;
/** Compact icon-only variant for list items */
variant?: "default" | "compact";
/**
* On a lecture's own page: its year, queued as a fallback when the lecture
* wasn't opened from a list (search, a shared link…).
*/
fallbackYear?: number | null;
};
export function PlayAudioButton({
audioUrl,
title,
subtitle,
sourceUrl,
type,
className,
variant = "default",
fallbackYear,
}: PlayAudioButtonProps) {
const {
currentTrack,
isPlaying,
play,
playPlaylist,
attachQueue,
togglePlayPause,
} = useAudioPlayer();
// The list this button sits in (a transcription list row), if any.
const listQueue = useTranscriptQueue();
const isThisTrack = currentTrack?.url === audioUrl;
const isThisPlaying = isThisTrack && isPlaying;
function handleClick() {
if (isThisTrack) {
togglePlayPause();
} else {
const track: AudioTrack = {
url: audioUrl,
title,
subtitle,
sourceUrl,
type: type ?? "lecture",
};
// Queue the list this recording was found in, so Next / Previous
// follow it: the list the button sits in, or — on a lecture's own
// page — the list it was opened from.
const queue =
listQueue ??
rememberedQueue(sourceUrl.replace(/^\/transcriptions\//, ""));
const at = queue?.findIndex((q) => q.url === audioUrl) ?? -1;
if (queue && at >= 0 && queue.length > 1) {
playPlaylist(queue, at);
return;
}
// No list to follow: start this lecture now — audio must begin inside
// the tap, or iOS blocks it — then queue its year once that loads.
play(track);
if (fallbackYear) {
void yearQueue(fallbackYear).then((tracks) => {
if (tracks && tracks.length > 1) attachQueue(tracks);
});
}
}
}
if (variant === "compact") {
return (
);
}
return (
);
}