mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-27 17:56:25 +02:00
Merge commit '31b0527e9a' into dev
This commit is contained in:
commit
9a29843866
3 changed files with 92 additions and 87 deletions
|
|
@ -30,6 +30,7 @@ function formatTime(seconds: number): string {
|
|||
|
||||
export function Audio({ id, src, title, durationMs, className }: AudioProps) {
|
||||
const audioRef = useRef<HTMLAudioElement>(null);
|
||||
const downloadControllerRef = useRef<AbortController | null>(null);
|
||||
const [isPlaying, setIsPlaying] = useState(false);
|
||||
const [currentTime, setCurrentTime] = useState(0);
|
||||
const [duration, setDuration] = useState(durationMs ? durationMs / 1000 : 0);
|
||||
|
|
@ -87,8 +88,12 @@ export function Audio({ id, src, title, durationMs, className }: AudioProps) {
|
|||
|
||||
// Handle download
|
||||
const handleDownload = useCallback(async () => {
|
||||
downloadControllerRef.current?.abort();
|
||||
const controller = new AbortController();
|
||||
downloadControllerRef.current = controller;
|
||||
|
||||
try {
|
||||
const response = await fetch(src);
|
||||
const response = await fetch(src, { signal: controller.signal });
|
||||
const blob = await response.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
|
|
@ -99,10 +104,16 @@ export function Audio({ id, src, title, durationMs, className }: AudioProps) {
|
|||
document.body.removeChild(a);
|
||||
window.URL.revokeObjectURL(url);
|
||||
} catch (err) {
|
||||
if (err instanceof DOMException && err.name === "AbortError") return;
|
||||
console.error("Error downloading audio:", err);
|
||||
}
|
||||
}, [src, title]);
|
||||
|
||||
// Abort in-flight download on unmount
|
||||
useEffect(() => {
|
||||
return () => downloadControllerRef.current?.abort();
|
||||
}, []);
|
||||
|
||||
// Set up audio event listeners
|
||||
useEffect(() => {
|
||||
const audio = audioRef.current;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue