feat: tab broadcast

This commit is contained in:
Tristan Yang
2023-01-20 23:48:14 +08:00
parent 6620c6a3e4
commit 0891b77673
7 changed files with 140 additions and 11 deletions
+35
View File
@@ -0,0 +1,35 @@
import { FC, useEffect } from "react";
import useStreaming from "../hook/useStreaming";
// import clsx from "clsx";
import Button from "./styled/Button";
interface Props {
}
const InactiveScreen: FC<Props> = () => {
const { stopStreaming } = useStreaming();
// const [reloadVisible, setReloadVisible] = useState(false);
useEffect(() => {
stopStreaming();
const currTitle = document.title;
document.title = `[INACTIVE] ${currTitle}`;
return () => {
document.title = currTitle;
};
}, []);
const handleReload = () => {
location.reload();
};
return (
<div className="w-screen h-screen bg-orange-200/50 flex justify-center items-center text-4xl font-bold">
<div className="flex flex-col items-center gap-2">
<h1 className="text-4xl font-bold">Only one active tab is allowed for VoceChat</h1>
<p className="text-gray-400 text-base font-semibold" >Please reload this page to continue using this tab or close it</p>
<Button className="mt-4" onClick={handleReload}>RELOAD</Button>
</div>
</div>
);
};
export default InactiveScreen;
+2
View File
@@ -277,6 +277,8 @@ export default function useStreaming() {
}, [afterMid, usersVersion]);
const stopStreaming = () => {
// 先清掉定时器
window.clearTimeout(aliveInter);
if (SSE) {
SSE.close();
SSE = undefined;
+33
View File
@@ -0,0 +1,33 @@
import { useState, useEffect } from 'react';
import { BroadcastChannel } from 'broadcast-channel';
// import useStreaming from './useStreaming';
type ChannelData = {
type: "NEW_TAB",
message: string
}
const channel = new BroadcastChannel('VOCECHAT_CHANNEL');
const useTabBroadcast = () => {
// const { stopStreaming } = useStreaming();
const [tabActive, setTabActive] = useState(true);
useEffect(() => {
channel.postMessage({ type: "NEW_TAB", message: "new tab opened" } as ChannelData);
const handler = (data: ChannelData) => {
console.log("channel data", data);
if (data.type == "NEW_TAB") {
setTabActive(false);
}
};
channel.addEventListener('message', handler);
return () => {
channel.removeEventListener("message", handler);
channel.close();
};
}, []);
return {
tabActive
};
};
export default useTabBroadcast;