fix: is in iframe check

This commit is contained in:
Tristan Yang
2023-06-02 21:24:26 +08:00
parent b969261cbd
commit 2bc8062a6f
4 changed files with 17 additions and 10 deletions
+6 -7
View File
@@ -16,13 +16,14 @@ import {
updateVoicingNetworkQuality
} from "../../app/slices/voice";
import { useAppSelector } from "../../app/store";
import { playAgoraVideo } from "../../utils";
import { isInIframe, playAgoraVideo } from "../../utils";
import DMCalling from "./DMCalling";
import useVoice from "./useVoice";
AgoraRTC.setLogLevel(process.env.NODE_ENV === "development" ? 0 : 4);
window.VOICE_TRACK_MAP = window.VOICE_TRACK_MAP ?? {};
window.VIDEO_TRACK_MAP = window.VIDEO_TRACK_MAP ?? {};
const inIframe = isInIframe();
// let tmpUids: number[] = [];
const Voice = () => {
const { from, to, voiceList, loginUid, voicingInfo } = useAppSelector((store) => {
@@ -174,9 +175,9 @@ const Voice = () => {
return (evt.returnValue = "");
}
};
window.addEventListener("beforeunload", handlePageUnload, { capture: true });
if (!window.VOICE_CLIENT) {
if (!window.VOICE_CLIENT && !inIframe) {
initializeAgoraClient();
window.addEventListener("beforeunload", handlePageUnload, { capture: true });
}
return () => {
@@ -185,6 +186,7 @@ const Voice = () => {
}, [voicingInfo]);
useEffect(() => {
if (inIframe) return;
// 有人呼叫我
const callMeList = voiceList.filter((item) => item.context == "dm" && item.id == loginUid);
if (callMeList.length) {
@@ -200,10 +202,7 @@ const Voice = () => {
});
}
}
// else {
// dispatch(updateCallInfo({ from: 0, to: 0, calling: false }));
// }
}, [voiceList, loginUid]);
}, [voiceList, loginUid, inIframe]);
// return <DMCalling uid={1} sendByMe={calling !== loginUid} />;
if (from !== 0) return <DMCalling from={from} to={to} />;
return null;
+6
View File
@@ -1,6 +1,7 @@
// import React from 'react';
// import Tippy from '@tippyjs/react';
// import { useState } from 'react';
import { toast } from "react-hot-toast";
import { useTranslation } from "react-i18next";
import { useDispatch } from "react-redux";
@@ -11,6 +12,7 @@ import { useAppSelector } from "@/app/store";
import { ChatContext } from "@/types/common";
import Tooltip from "@/components/Tooltip";
import { useVoice } from "@/components/Voice";
import { isInIframe } from "@/utils";
import IconHeadphone from "@/assets/icons/headphone.svg";
type Props = {
@@ -43,6 +45,10 @@ const VoiceChat = ({ id, context = "channel" }: Props) => {
alert("You have joined another channel, please leave first!");
return;
}
if (isInIframe()) {
toast.error("Voice is not supported in iframe");
return;
}
joinVoice();
const data = {
id,
+2
View File
@@ -417,3 +417,5 @@ export const transformInviteLink = (link: string) => {
return tmpLink;
};
export const isInIframe = () => window.location !== window.parent.location;
+3 -3
View File
@@ -2,18 +2,18 @@ import { createContext, ReactNode, useContext } from "react";
import { useGetLoginConfigQuery, useGetServerQuery } from "../app/services/server";
import { useAppSelector } from "../app/store";
import { getContrastColor } from "../utils";
import { getContrastColor, isInIframe } from "../utils";
const query = new URLSearchParams(location.search);
const welcome = decodeURIComponent(query.get("welcome") || "");
const color = decodeURIComponent(query.get("themeColor") || "#1fe1f9");
const from = decodeURIComponent(query.get("from") || "widget.link");
const fgColor = getContrastColor(color);
// 判断是否是iframe上下文
const embed = window.location !== window.parent.location;
const embed = isInIframe();
const WidgetContext = createContext({
color,
fgColor,
// 判断是否是iframe上下文
embed,
from,
loading: true,