mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-15 15:32:39 -05:00
Add useEffect clean up to YouTube embed
This commit is contained in:
parent
49b563e5ef
commit
4a021cc1ce
|
|
@ -64,19 +64,6 @@ export function YouTubeEmbed({
|
|||
useEffect(() => {
|
||||
if (!enableApi || !isApiReady || !containerRef.current) return;
|
||||
|
||||
if (playerRef.current) {
|
||||
try {
|
||||
playerRef.current.loadVideoById({ videoId: id, startSeconds: start });
|
||||
} catch {
|
||||
toastQueue.add({
|
||||
message: t("vods:errors.youtubePreviewFailed"),
|
||||
variant: "error",
|
||||
});
|
||||
setHasError(true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const player = new window.YT.Player(containerRef.current, {
|
||||
height: "281",
|
||||
|
|
@ -103,12 +90,18 @@ export function YouTubeEmbed({
|
|||
},
|
||||
},
|
||||
});
|
||||
|
||||
return () => {
|
||||
player.destroy();
|
||||
playerRef.current = null;
|
||||
};
|
||||
} catch {
|
||||
toastQueue.add({
|
||||
message: t("vods:errors.youtubePreviewFailed"),
|
||||
variant: "error",
|
||||
});
|
||||
setHasError(true);
|
||||
return;
|
||||
}
|
||||
}, [enableApi, isApiReady, id, start, autoplay, onPlayerReady, t]);
|
||||
|
||||
|
|
|
|||
1
types/youtube.d.ts
vendored
1
types/youtube.d.ts
vendored
|
|
@ -26,5 +26,6 @@ declare namespace YT {
|
|||
);
|
||||
loadVideoById(options: { videoId: string; startSeconds?: number }): void;
|
||||
getCurrentTime(): number;
|
||||
destroy(): void;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user