Skip to content

Commit 8ae85d2

Browse files
refactor, simpler, cleanup
1 parent 61ffe27 commit 8ae85d2

File tree

5 files changed

+44
-51
lines changed

5 files changed

+44
-51
lines changed

src/components/VideoModal.css

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
margin-bottom: 16px;
1414
}
1515
.close-btn {
16-
margin-top: 16px;
1716
min-width: 100px;
1817
}
1918
.yt-controls-hint {
@@ -30,13 +29,22 @@
3029
}
3130
.yt-btn {
3231
--background: #ff0000;
32+
--background-hover: #e60000;
33+
--background-activated: #cc0000;
3334
--color: #fff;
3435
font-weight: 500;
3536
display: flex;
3637
align-items: center;
3738
font-size: 1rem;
3839
padding: 0 18px;
3940
min-width: 120px;
41+
border-radius: 4px;
42+
box-shadow: 0 1px 2px rgba(0,0,0,0.08);
43+
transition: background 0.2s;
44+
}
45+
.yt-btn:focus,
46+
.yt-btn:hover {
47+
--background: #e60000;
4048
}
4149
.yt-btn .yt-icon {
4250
width: 1.2em;

src/components/VideoModal.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ const VideoModal: React.FC<VideoModalProps> = ({ isOpen, onDidDismiss, selectedV
3737
<IonButton
3838
onClick={() => selectedVideo && openInYouTube(selectedVideo.videoId)}
3939
className="yt-btn"
40-
color="medium"
4140
>
42-
<IonIcon icon={logoYoutube} className="yt-icon" slot="start" />
41+
<IonIcon icon={logoYoutube} className="yt-icon" slot="start" />
4342
Play in YouTube
4443
</IonButton>
4544
<IonButton onClick={onDidDismiss} className="close-btn">Close</IonButton>

src/pages/Videos.tsx

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState, useRef } from "react";
1+
import { useEffect, useState } from "react";
22
import {
33
IonContent,
44
IonHeader,
@@ -12,7 +12,6 @@ import VideoModal from "../components/VideoModal";
1212
import "../components/VideoThumbnailCard.css";
1313
import "../components/VideoModal.css";
1414
import { REMOTE_BASE_URL } from "../constants";
15-
import { createYouTubePlayer, destroyYouTubePlayer } from "../utils/ytPlayer";
1615

1716
const Videos: React.FC = () => {
1817
const [videos, setVideos] = useState<VideoEntry[]>([]);
@@ -21,8 +20,6 @@ const Videos: React.FC = () => {
2120
videoId: string;
2221
title: string;
2322
} | null>(null);
24-
const playerRef = useRef<any>(null);
25-
const playerContainerRef = useRef<HTMLDivElement | null>(null);
2623

2724
useEffect(() => {
2825
fetch(`${REMOTE_BASE_URL}youtube-data.json`)
@@ -41,19 +38,6 @@ const Videos: React.FC = () => {
4138
}, new Map<string, VideoEntry[]>())
4239
);
4340

44-
// Handle modal open/close and YouTube Player API (same as News)
45-
useEffect(() => {
46-
if (!modalOpen || !selectedVideo) return;
47-
const container = playerContainerRef.current;
48-
playerRef.current = container && selectedVideo.videoId
49-
? createYouTubePlayer(container, selectedVideo.videoId)
50-
: null;
51-
return () => {
52-
destroyYouTubePlayer(playerRef.current, container!);
53-
playerRef.current = null;
54-
};
55-
}, [modalOpen, selectedVideo]);
56-
5741
// Keyboard navigation: Enter/OK to open, Escape to close
5842
const handleCardKeyDown = (
5943
e: React.KeyboardEvent,

src/utils/youtube.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* Loads the YouTube IFrame API script and calls the callback when ready.
55
* Ensures the script is loaded only once.
66
*/
7-
87
declare global {
98
interface Window {
109
onYouTubeIframeAPIReady?: () => void;
@@ -49,3 +48,36 @@ export function openInYouTube(videoId: string) {
4948
window.open(`https://www.youtube.com/watch?v=${videoId}`, "_blank");
5049
}
5150
}
51+
52+
/**
53+
* (Legacy) Create and destroy YouTube IFrame Player in a container. Not used with iframe modal, but kept for reference.
54+
*/
55+
export function createYouTubePlayer(
56+
container: HTMLElement,
57+
videoId: string,
58+
onReady?: () => void
59+
) {
60+
if (!container || !videoId) return null;
61+
container.innerHTML = "";
62+
function tryCreate() {
63+
// @ts-ignore
64+
if (window.YT && window.YT.Player) {
65+
// @ts-ignore
66+
const player = new window.YT.Player(container, {
67+
height: "360",
68+
width: "640",
69+
videoId,
70+
playerVars: { autoplay: 1 },
71+
events: onReady ? { onReady } : undefined,
72+
});
73+
return player;
74+
} else {
75+
setTimeout(tryCreate, 100);
76+
}
77+
}
78+
return tryCreate();
79+
}
80+
export function destroyYouTubePlayer(player: any, container?: HTMLElement) {
81+
if (player && typeof player.destroy === "function") player.destroy();
82+
if (container) container.innerHTML = "";
83+
}

src/utils/ytPlayer.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)