feat: tab broadcast
This commit is contained in:
@@ -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;
|
||||
@@ -277,6 +277,8 @@ export default function useStreaming() {
|
||||
}, [afterMid, usersVersion]);
|
||||
|
||||
const stopStreaming = () => {
|
||||
// 先清掉定时器
|
||||
window.clearTimeout(aliveInter);
|
||||
if (SSE) {
|
||||
SSE.close();
|
||||
SSE = undefined;
|
||||
|
||||
@@ -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;
|
||||
@@ -1,5 +1,7 @@
|
||||
import { memo } from "react";
|
||||
import { Outlet, NavLink, useLocation, useMatch } from "react-router-dom";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import StyledWrapper from "./styled";
|
||||
import User from "./User";
|
||||
// import Tools from "./Tools";
|
||||
@@ -15,7 +17,6 @@ import UserIcon from "../../assets/icons/user.svg";
|
||||
import FavIcon from "../../assets/icons/bookmark.svg";
|
||||
import FolderIcon from "../../assets/icons/folder.svg";
|
||||
import { useAppSelector } from "../../app/store";
|
||||
import { useTranslation } from "react-i18next";
|
||||
// const routes = ["/setting", "/setting/channel/:cid"];
|
||||
function HomePage() {
|
||||
const { t } = useTranslation();
|
||||
|
||||
+11
-2
@@ -3,6 +3,7 @@ import { Route, Routes, HashRouter } from "react-router-dom";
|
||||
import { Provider } from "react-redux";
|
||||
import toast from "react-hot-toast";
|
||||
import { isEqual } from "lodash";
|
||||
|
||||
import NotFoundPage from "./404";
|
||||
// import Welcome from './Welcome'
|
||||
// const HomePage = lazy(() => import("./home"));
|
||||
@@ -28,14 +29,15 @@ const HomePage = lazy(() => import("./home"));
|
||||
import RequireAuth from "../common/component/RequireAuth";
|
||||
import RequireNoAuth from "../common/component/RequireNoAuth";
|
||||
import Meta from "../common/component/Meta";
|
||||
// import HomePage from "./home";
|
||||
// import ChatPage from "./chat";
|
||||
import LazyIt from './lazy';
|
||||
import store, { useAppSelector } from "../app/store";
|
||||
import useDeviceToken from "../common/component/Notification/useDeviceToken";
|
||||
import { vapidKey } from "../app/config";
|
||||
import useTabBroadcast from "../common/hook/useTabBroadcast";
|
||||
import InactiveScreen from "../common/component/InactiveScreen";
|
||||
let toastId: string;
|
||||
const PageRoutes = () => {
|
||||
|
||||
const {
|
||||
ui: { online },
|
||||
fileMessages
|
||||
@@ -198,6 +200,13 @@ const PageRoutes = () => {
|
||||
};
|
||||
|
||||
export default function ReduxRoutes() {
|
||||
const { tabActive } = useTabBroadcast();
|
||||
if (!tabActive) return (
|
||||
<Provider store={store}>
|
||||
<Meta />
|
||||
<InactiveScreen />
|
||||
</Provider>
|
||||
);
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<Meta />
|
||||
|
||||
Reference in New Issue
Block a user