mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: add useSubscribeBroadcastChannel
This commit is contained in:
parent
9cc2725674
commit
6e026830c8
24
widget/src/hooks/useSubscribeBroadcastChannel.ts
Normal file
24
widget/src/hooks/useSubscribeBroadcastChannel.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* Copyright © 2025 Hexastack. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||||
|
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||||
|
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { useEffect } from "react";
|
||||||
|
|
||||||
|
import {
|
||||||
|
IBroadcastChannelContext,
|
||||||
|
useBroadcastChannel,
|
||||||
|
} from "../providers/BroadcastChannelProvider";
|
||||||
|
|
||||||
|
export const useSubscribeBroadcastChannel: IBroadcastChannelContext["subscribe"] =
|
||||||
|
(...props) => {
|
||||||
|
const { subscribe } = useBroadcastChannel();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
subscribe(...props);
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [subscribe, ...props]);
|
||||||
|
};
|
||||||
@ -49,11 +49,11 @@ const getOrCreateTabId = () => {
|
|||||||
return storedTabId;
|
return storedTabId;
|
||||||
};
|
};
|
||||||
|
|
||||||
interface IBroadcastChannelContext {
|
export interface IBroadcastChannelContext {
|
||||||
subscribe: (
|
subscribe: (
|
||||||
event: `${EBCEvent}`,
|
event: `${EBCEvent}`,
|
||||||
callback: (message: BroadcastChannelData) => void,
|
callback: (message: BroadcastChannelData) => void,
|
||||||
) => () => void;
|
) => void;
|
||||||
postMessage: (payload: BroadcastChannelPayload) => void;
|
postMessage: (payload: BroadcastChannelPayload) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,6 +16,7 @@ import React, {
|
|||||||
useState,
|
useState,
|
||||||
} from "react";
|
} from "react";
|
||||||
|
|
||||||
|
import { useSubscribeBroadcastChannel } from "../hooks/useSubscribeBroadcastChannel";
|
||||||
import { StdEventType } from "../types/chat-io-messages.types";
|
import { StdEventType } from "../types/chat-io-messages.types";
|
||||||
import {
|
import {
|
||||||
Direction,
|
Direction,
|
||||||
@ -29,7 +30,6 @@ import {
|
|||||||
} from "../types/message.types";
|
} from "../types/message.types";
|
||||||
import { ConnectionState, OutgoingMessageState } from "../types/state.types";
|
import { ConnectionState, OutgoingMessageState } from "../types/state.types";
|
||||||
|
|
||||||
import { useBroadcastChannel } from "./BroadcastChannelProvider";
|
|
||||||
import { useConfig } from "./ConfigProvider";
|
import { useConfig } from "./ConfigProvider";
|
||||||
import { useSettings } from "./SettingsProvider";
|
import { useSettings } from "./SettingsProvider";
|
||||||
import { useSocket, useSubscribe } from "./SocketProvider";
|
import { useSocket, useSubscribe } from "./SocketProvider";
|
||||||
@ -385,7 +385,9 @@ const ChatProvider: React.FC<{
|
|||||||
}
|
}
|
||||||
}, [syncState, isOpen]);
|
}, [syncState, isOpen]);
|
||||||
|
|
||||||
const { subscribe } = useBroadcastChannel();
|
useSubscribeBroadcastChannel("logout", () => {
|
||||||
|
socketCtx.socket.disconnect();
|
||||||
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (screen === "chat" && connectionState === ConnectionState.connected) {
|
if (screen === "chat" && connectionState === ConnectionState.connected) {
|
||||||
@ -406,10 +408,6 @@ const ChatProvider: React.FC<{
|
|||||||
|
|
||||||
socketCtx.socket.io.on("reconnect", reSubscribe);
|
socketCtx.socket.io.on("reconnect", reSubscribe);
|
||||||
|
|
||||||
subscribe("logout", () => {
|
|
||||||
socketCtx.socket.disconnect();
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
socketCtx.socket.io.off("reconnect", reSubscribe);
|
socketCtx.socket.io.off("reconnect", reSubscribe);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user