Skip to content

Commit 4d8f613

Browse files
authored
Merge pull request #65 from hs-shell/fix/prevent-reload
fix: prevent duplicate view historys caused by page refresh
2 parents 1f44ee3 + 555e836 commit 4d8f613

File tree

1 file changed

+51
-32
lines changed

1 file changed

+51
-32
lines changed

src/player/components/PlayerIframe.tsx

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ interface PlayerIframeProps {
77
}
88

99
export default function PlayerIframe({ videoSrc, onNextVideo, isPlaying }: PlayerIframeProps) {
10-
const iframeRef = useRef<HTMLIFrameElement>(null);
10+
const iframeRef = useRef<HTMLIFrameElement | null>(null);
1111
const isPlayingRef = useRef(isPlaying);
1212
const onNextVideoRef = useRef(onNextVideo);
1313

1414
const [time, setTime] = useState({ current: 0, duration: 0 });
1515

16+
// iframe 랜더링 시 이벤트 등록
17+
const setIframeRef = (node: HTMLIFrameElement | null) => {
18+
iframeRef.current = node;
19+
if (node) {
20+
node.addEventListener('load', loadEventListener);
21+
}
22+
};
23+
1624
useEffect(() => {
1725
const interval = setInterval(() => {
1826
const video = getVideoElement();
@@ -54,7 +62,7 @@ export default function PlayerIframe({ videoSrc, onNextVideo, isPlaying }: Playe
5462
};
5563

5664
// load 이벤트 시 video listener 세팅
57-
const attachVideoListeners = () => {
65+
const loadEventListener = () => {
5866
const video = getVideoElement();
5967
if (!video) return;
6068

@@ -69,8 +77,8 @@ export default function PlayerIframe({ videoSrc, onNextVideo, isPlaying }: Playe
6977
useEffect(() => {
7078
const iframe = iframeRef.current;
7179
if (!iframe) return;
72-
iframe.addEventListener('load', attachVideoListeners);
73-
return () => iframe.removeEventListener('load', attachVideoListeners);
80+
iframe.addEventListener('load', loadEventListener);
81+
return () => iframe.removeEventListener('load', loadEventListener);
7482
}, [videoSrc]);
7583

7684
// isPlaying 변경 시 재생 상태 제어
@@ -89,38 +97,49 @@ export default function PlayerIframe({ videoSrc, onNextVideo, isPlaying }: Playe
8997
}, []);
9098

9199
return (
92-
<div className="relative w-full h-full items-center justify-center">
93-
<div className="absolute inset-0 flex items-center justify-center text-white text-6xl font-bold bg-transparent pointer-events-none">
94-
{time.duration > 0 ? (
95-
<div className="flex items-center justify-center p-8 bg-zinc-950 bg-opacity-80 rounded-xl">
96-
<span className="flex items-center justify-center text-white min-w-[4ch]">
97-
{Math.floor(time.current / 60)}:
98-
{Math.floor(time.current % 60)
99-
.toString()
100-
.padStart(2, '0')}{' '}
101-
</span>
102-
<span className="flex items-center justify-center text-zinc-300"> / </span>
103-
<span className="flex items-center justify-center text-zinc-300 min-w-[4ch]">
104-
{Math.floor(time.duration / 60)}:
105-
{Math.floor(time.duration % 60)
106-
.toString()
107-
.padStart(2, '0')}
108-
</span>
109-
</div>
100+
<div className={`relative w-full h-full items-center justify-center rounded-xl ${!isPlaying && 'bg-black'}`}>
101+
<div
102+
className={`absolute inset-0 flex items-center justify-center text-white
103+
text-6xl font-bold bg-transparent pointer-events-none`}
104+
>
105+
{isPlaying ? (
106+
time.duration > 0 ? (
107+
<div className="flex items-center justify-center p-8 bg-zinc-950 bg-opacity-80 rounded-xl">
108+
<span className="flex items-center justify-center text-white min-w-[4ch]">
109+
{Math.floor(time.current / 60)}:
110+
{Math.floor(time.current % 60)
111+
.toString()
112+
.padStart(2, '0')}{' '}
113+
</span>
114+
<span className="flex items-center justify-center text-zinc-300"> / </span>
115+
<span className="flex items-center justify-center text-zinc-300 min-w-[4ch]">
116+
{Math.floor(time.duration / 60)}:
117+
{Math.floor(time.duration % 60)
118+
.toString()
119+
.padStart(2, '0')}
120+
</span>
121+
</div>
122+
) : (
123+
<div>영상 정보가 없습니다</div>
124+
)
110125
) : (
111-
<div>영상 정보가 없습니다</div>
126+
<div className="flex justify-center items-center text-center text-3xl">
127+
자동수강을 시작하려면 수강시작 버튼을 눌러주세요
128+
</div>
112129
)}
113130
</div>
114131

115-
<iframe
116-
ref={iframeRef}
117-
src={videoSrc}
118-
sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-top-navigation-by-user-activation"
119-
allow="autoplay"
120-
width="100%"
121-
height="100%"
122-
className="rounded-lg pointer-events-none"
123-
/>
132+
{videoSrc && isPlaying && (
133+
<iframe
134+
ref={setIframeRef}
135+
src={videoSrc}
136+
sandbox="allow-scripts allow-same-origin allow-forms allow-popups allow-top-navigation-by-user-activation"
137+
allow="autoplay"
138+
width="100%"
139+
height="100%"
140+
className="rounded-lg pointer-events-none"
141+
/>
142+
)}
124143
</div>
125144
);
126145
}

0 commit comments

Comments
 (0)