Updated structure
This commit is contained in:
+27
-24
@@ -4,64 +4,67 @@ import MessageBox from "@/components/channel/MessageBox";
|
|||||||
import AppLayout from "@/components/app/AppLayout";
|
import AppLayout from "@/components/app/AppLayout";
|
||||||
import { useParams, useRouter } from "next/navigation";
|
import { useParams, useRouter } from "next/navigation";
|
||||||
import useApp from "@/hooks/use-app";
|
import useApp from "@/hooks/use-app";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect } from "react";
|
||||||
import { NodeClient } from "@/lib/node";
|
|
||||||
import useAsync from "@/hooks/use-async";
|
import useAsync from "@/hooks/use-async";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import useIndicators from "@/hooks/use-indicators";
|
import useIndicators from "@/hooks/use-indicators";
|
||||||
import { useEffectOnceWhenReady } from "@/hooks/use-once";
|
|
||||||
import { ClientMessage } from "@/types/protocol";
|
import { ClientMessage } from "@/types/protocol";
|
||||||
|
import { getNode, useNullNode } from "@/hooks/use-node";
|
||||||
|
import { useEffectOnceWhenReady } from "@/hooks/use-once";
|
||||||
|
|
||||||
export default function DMs() {
|
export default function DMs() {
|
||||||
const { id } = useParams<{ id: string }>();
|
const { id } = useParams<{ id: string }>();
|
||||||
const app = useApp();
|
const app = useApp();
|
||||||
const targetNode = useRef<WebSocket | null>(null);
|
|
||||||
const { value: target, loading } = useAsync(() => app.getUserById(id));
|
const { value: target, loading } = useAsync(() => app.getUserById(id));
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { indicators, addIndicator } = useIndicators();
|
const { indicators, addIndicator } = useIndicators();
|
||||||
|
|
||||||
const sendRequest = async (req: ClientMessage) => {
|
const sendRequest = async (req: ClientMessage) => {
|
||||||
console.log("Sending request:", req);
|
console.log("Sending request:", req);
|
||||||
await waitForOpen(targetNode.current);
|
|
||||||
await waitForOpen(app.node.current);
|
|
||||||
|
|
||||||
switch (req.type) {
|
switch (req.type) {
|
||||||
case "send_message":
|
case "send_message":
|
||||||
targetNode.current?.send(JSON.stringify(req));
|
targetNode.current?.socket?.send(JSON.stringify(req));
|
||||||
|
if (target?.node_address !== app.profile?.node_address)
|
||||||
|
app.node.current?.socket?.send(JSON.stringify(req));
|
||||||
break;
|
break;
|
||||||
case "load_chunk":
|
case "load_chunk":
|
||||||
targetNode.current?.send(JSON.stringify(req));
|
targetNode.current?.socket?.send(JSON.stringify(req));
|
||||||
app.node.current?.send(JSON.stringify(req));
|
if (target?.node_address !== app.profile?.node_address)
|
||||||
|
app.node.current?.socket?.send(JSON.stringify(req));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
app.node.current?.send(JSON.stringify(req));
|
app.node.current?.socket?.send(JSON.stringify(req));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const targetNode = useNullNode();
|
||||||
|
|
||||||
useEffectOnceWhenReady(
|
useEffectOnceWhenReady(
|
||||||
() => {
|
() => {
|
||||||
async function connectTargetNode() {
|
|
||||||
if (!target) return;
|
if (!target) return;
|
||||||
await auth({
|
|
||||||
id: target.node_address,
|
|
||||||
wsRef: targetNode,
|
|
||||||
messageStore: app.privateMessages,
|
|
||||||
onIndicator: addIndicator,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
connectTargetNode().then(() => {
|
return getNode(
|
||||||
|
target?.node_address,
|
||||||
|
app.privateMessages,
|
||||||
|
{
|
||||||
|
onIndicator: addIndicator,
|
||||||
|
},
|
||||||
|
targetNode
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[target],
|
||||||
|
[(v) => v]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
app.setCurrentChannel({
|
app.setCurrentChannel({
|
||||||
id: id,
|
id: id,
|
||||||
name: target?.display_name || id,
|
name: target?.display_name || id,
|
||||||
kind: "text",
|
kind: "text",
|
||||||
});
|
});
|
||||||
});
|
}, [targetNode]);
|
||||||
},
|
|
||||||
[target],
|
|
||||||
[(v) => v]
|
|
||||||
);
|
|
||||||
|
|
||||||
return loading || target ? (
|
return loading || target ? (
|
||||||
<AppLayout app={app}>
|
<AppLayout app={app}>
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect } from "react";
|
||||||
|
import { useParams, useSearchParams } from "next/navigation";
|
||||||
|
import AppLayout from "@/components/app/AppLayout";
|
||||||
|
import useMessages from "@/hooks/use-messages";
|
||||||
|
import useApp from "@/hooks/use-app";
|
||||||
|
import ChannelView from "@/components/channel/ChannelView";
|
||||||
|
import useIndicators from "@/hooks/use-indicators";
|
||||||
|
import useNode from "@/hooks/use-node";
|
||||||
|
|
||||||
|
export default function Server() {
|
||||||
|
const { id } = useParams<{ id: string }>();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const app = useApp();
|
||||||
|
const messageStore = useMessages();
|
||||||
|
const { indicators, addIndicator } = useIndicators();
|
||||||
|
|
||||||
|
const serverNode = useNode(id, messageStore, {
|
||||||
|
setServer: app.setServer,
|
||||||
|
onIndicator: addIndicator,
|
||||||
|
onVoiceJoin: (userId, channelId, voiceId) =>
|
||||||
|
app.setVoiceConns((prev) => ({
|
||||||
|
...prev,
|
||||||
|
[channelId]: { ...prev[channelId], [userId]: voiceId },
|
||||||
|
})),
|
||||||
|
onVoiceLeave: (userId, channelId) =>
|
||||||
|
app.setVoiceConns((prev) => {
|
||||||
|
const { [userId]: _, ...remainingUsers } = prev[channelId] || {};
|
||||||
|
return {
|
||||||
|
...prev,
|
||||||
|
[channelId]: remainingUsers,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
onSpeaking: (voiceId) =>
|
||||||
|
app.setSpeaking((speaking) => ({
|
||||||
|
...speaking,
|
||||||
|
[voiceId]: Date.now(),
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const channelId = searchParams.get("ch");
|
||||||
|
if (channelId) {
|
||||||
|
app.setCurrentChannel(
|
||||||
|
app.server?.channels.find((v) => v.id === channelId) ||
|
||||||
|
app.server?.channels[0] ||
|
||||||
|
null
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
app.setCurrentChannel(app.server?.channels[0] || null);
|
||||||
|
}
|
||||||
|
}, [app.server]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<AppLayout app={app}>
|
||||||
|
<ChannelView
|
||||||
|
app={app}
|
||||||
|
node={serverNode}
|
||||||
|
messages={messageStore.messages.filter(
|
||||||
|
(m) => m.channel_id === app.currentChannel?.id
|
||||||
|
)}
|
||||||
|
ip={id}
|
||||||
|
indicators={indicators}
|
||||||
|
/>
|
||||||
|
</AppLayout>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useEffect, useRef, useState } from "react";
|
|
||||||
import { useParams, useSearchParams } from "next/navigation";
|
|
||||||
import AppLayout from "@/components/app/AppLayout";
|
|
||||||
import auth from "@/lib/node";
|
|
||||||
import useMessages from "@/hooks/use-messages";
|
|
||||||
import useApp from "@/hooks/use-app";
|
|
||||||
import { useEffectOnceWhenReady } from "@/hooks/use-once";
|
|
||||||
import ChannelView from "@/components/channel/ChannelView";
|
|
||||||
import { IndicatorContext } from "@/types/protocol";
|
|
||||||
import useIndicators from "@/hooks/use-indicators";
|
|
||||||
|
|
||||||
export default function Server() {
|
|
||||||
const { ip } = useParams<{ ip: string }>();
|
|
||||||
const searchParams = useSearchParams();
|
|
||||||
const serverRef = useRef<WebSocket | null>(null);
|
|
||||||
const app = useApp();
|
|
||||||
const messageStore = useMessages();
|
|
||||||
const { indicators, addIndicator } = useIndicators();
|
|
||||||
|
|
||||||
useEffectOnceWhenReady(
|
|
||||||
() => {
|
|
||||||
if (!ip) return;
|
|
||||||
auth({
|
|
||||||
id: ip,
|
|
||||||
wsRef: serverRef,
|
|
||||||
setServer: app.setServer,
|
|
||||||
messageStore: messageStore,
|
|
||||||
onIndicator: addIndicator,
|
|
||||||
onVoiceJoin: (userId, channelId, voiceId) =>
|
|
||||||
app.setVoiceConns((prev) => ({
|
|
||||||
...prev,
|
|
||||||
[channelId]: { ...prev[channelId], [userId]: voiceId },
|
|
||||||
})),
|
|
||||||
onVoiceLeave: (userId, channelId) =>
|
|
||||||
app.setVoiceConns((prev) => {
|
|
||||||
const { [userId]: _, ...remainingUsers } = prev[channelId] || {};
|
|
||||||
return {
|
|
||||||
...prev,
|
|
||||||
[channelId]: remainingUsers,
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
onSpeaking: (voiceId) =>
|
|
||||||
app.setSpeaking((speaking) => ({
|
|
||||||
...speaking,
|
|
||||||
[voiceId]: Date.now(),
|
|
||||||
})),
|
|
||||||
});
|
|
||||||
},
|
|
||||||
[ip, messageStore.messages],
|
|
||||||
[undefined, (v) => v]
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const channelId = searchParams.get("ch");
|
|
||||||
if (channelId) {
|
|
||||||
app.setCurrentChannel(
|
|
||||||
app.server?.channels.find((v) => v.id === channelId) ||
|
|
||||||
app.server?.channels[0] ||
|
|
||||||
null
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
app.setCurrentChannel(app.server?.channels[0] || null);
|
|
||||||
}
|
|
||||||
}, [app.server]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<AppLayout app={app}>
|
|
||||||
<ChannelView
|
|
||||||
app={app}
|
|
||||||
messages={messageStore.messages.filter(
|
|
||||||
(m) => m.channel_id === app.currentChannel?.id
|
|
||||||
)}
|
|
||||||
ip={ip}
|
|
||||||
serverRef={serverRef}
|
|
||||||
indicators={indicators}
|
|
||||||
/>
|
|
||||||
</AppLayout>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -4,7 +4,6 @@ import React, { useEffect } from "react";
|
|||||||
import { cn } from "@/lib/utils";
|
import { cn } from "@/lib/utils";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import auth from "@/lib/node";
|
|
||||||
import { useIsMobile } from "@/hooks/is-mobile";
|
import { useIsMobile } from "@/hooks/is-mobile";
|
||||||
import App from "@/types/app";
|
import App from "@/types/app";
|
||||||
import { Message } from "@/types/types";
|
import { Message } from "@/types/types";
|
||||||
@@ -14,6 +13,7 @@ import SidebarDMs from "./SidebarDMs";
|
|||||||
import SidebarChannels from "./SidebarChannels";
|
import SidebarChannels from "./SidebarChannels";
|
||||||
import DMItem from "./DMItem";
|
import DMItem from "./DMItem";
|
||||||
import { useEffectOnceWhenReady } from "@/hooks/use-once";
|
import { useEffectOnceWhenReady } from "@/hooks/use-once";
|
||||||
|
import { getNode, useNullNode } from "@/hooks/use-node";
|
||||||
|
|
||||||
export default function AppLayout({
|
export default function AppLayout({
|
||||||
children,
|
children,
|
||||||
@@ -28,14 +28,16 @@ export default function AppLayout({
|
|||||||
}) {
|
}) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const isMobile = useIsMobile();
|
const isMobile = useIsMobile();
|
||||||
|
const node = useNullNode();
|
||||||
|
|
||||||
useEffectOnceWhenReady(
|
useEffectOnceWhenReady(
|
||||||
() => {
|
() => {
|
||||||
if (!app.profile?.node_address) return;
|
if (!app.profile?.node_address) return;
|
||||||
|
|
||||||
auth({
|
return getNode(
|
||||||
id: app.profile.node_address,
|
app.profile?.node_address,
|
||||||
wsRef: app.node,
|
app.privateMessages,
|
||||||
|
{
|
||||||
onNewMessage: (m) => {
|
onNewMessage: (m) => {
|
||||||
if (m.from !== chatWith && m.from !== app.profile?.id)
|
if (m.from !== chatWith && m.from !== app.profile?.id)
|
||||||
toast(
|
toast(
|
||||||
@@ -54,18 +56,18 @@ export default function AppLayout({
|
|||||||
|
|
||||||
if (onNewMessage) onNewMessage(m);
|
if (onNewMessage) onNewMessage(m);
|
||||||
},
|
},
|
||||||
messageStore: app.privateMessages,
|
},
|
||||||
});
|
node
|
||||||
|
);
|
||||||
app.setProfiles((prev) => {
|
|
||||||
if (app.profile) prev[app.profile.id] = app.profile;
|
|
||||||
return prev;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
[chatWith, app.profile],
|
[chatWith, app.profile],
|
||||||
[undefined, undefined]
|
[undefined, undefined]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
app.node.current = node.current;
|
||||||
|
}, [node]);
|
||||||
|
|
||||||
useEffect(() => app.setSidebarOpen(!isMobile), [isMobile]);
|
useEffect(() => app.setSidebarOpen(!isMobile), [isMobile]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,29 +1,36 @@
|
|||||||
import { Message } from "@/types/types";
|
import { Message } from "@/types/types";
|
||||||
import MessageBox from "./MessageBox";
|
import MessageBox from "./MessageBox";
|
||||||
import App from "@/types/app";
|
import App from "@/types/app";
|
||||||
import React from "react";
|
import React, { RefObject } from "react";
|
||||||
import PluginChannel from "./PluginChannel";
|
import PluginChannel from "./PluginChannel";
|
||||||
import { IndicatorContext } from "@/types/protocol";
|
import { ClientMessage, IndicatorContext } from "@/types/protocol";
|
||||||
import VoiceBox from "./voice/VoiceBox";
|
import VoiceBox from "./voice/VoiceBox";
|
||||||
import { sendVoice } from "@/lib/audio";
|
import { sendVoice } from "@/lib/audio";
|
||||||
|
import { NodeClient } from "@/lib/node";
|
||||||
|
|
||||||
export default function ChannelView({
|
export default function ChannelView({
|
||||||
app,
|
app,
|
||||||
messages,
|
messages,
|
||||||
ip,
|
ip,
|
||||||
serverRef,
|
node,
|
||||||
indicators,
|
indicators,
|
||||||
}: {
|
}: {
|
||||||
app: App;
|
app: App;
|
||||||
messages: Message[];
|
messages: Message[];
|
||||||
ip: string;
|
ip: string;
|
||||||
serverRef: React.RefObject<WebSocket | null>;
|
|
||||||
indicators: IndicatorContext[];
|
indicators: IndicatorContext[];
|
||||||
|
node: RefObject<NodeClient | null>;
|
||||||
}) {
|
}) {
|
||||||
if (!app.currentChannel)
|
if (!app.currentChannel)
|
||||||
return (
|
return (
|
||||||
<div className="h-svh w-full max-h-svh flex flex-col pb-5 pl-5 gap-5" />
|
<div className="h-svh w-full max-h-svh flex flex-col pb-5 pl-5 gap-5" />
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const sendRequest = (req: ClientMessage) => {
|
||||||
|
node.current?.socket?.send(JSON.stringify(req));
|
||||||
|
console.log(node.current?.socket);
|
||||||
|
};
|
||||||
|
|
||||||
switch (app.currentChannel.kind) {
|
switch (app.currentChannel.kind) {
|
||||||
case "text":
|
case "text":
|
||||||
return (
|
return (
|
||||||
@@ -31,10 +38,7 @@ export default function ChannelView({
|
|||||||
app={app}
|
app={app}
|
||||||
channelName={app.currentChannel.name}
|
channelName={app.currentChannel.name}
|
||||||
messages={messages}
|
messages={messages}
|
||||||
sendRequest={(req) => {
|
sendRequest={sendRequest}
|
||||||
while (serverRef.current?.CONNECTING) {}
|
|
||||||
serverRef.current?.send(JSON.stringify(req));
|
|
||||||
}}
|
|
||||||
indicators={indicators.filter(
|
indicators={indicators.filter(
|
||||||
(indicator) =>
|
(indicator) =>
|
||||||
indicator.indicator.params.user_id !== app.profile?.id
|
indicator.indicator.params.user_id !== app.profile?.id
|
||||||
@@ -49,13 +53,13 @@ export default function ChannelView({
|
|||||||
channelName={app.currentChannel.name}
|
channelName={app.currentChannel.name}
|
||||||
channelId={app.currentChannel.id}
|
channelId={app.currentChannel.id}
|
||||||
messages={messages}
|
messages={messages}
|
||||||
sendRequest={(req) => {
|
sendRequest={sendRequest}
|
||||||
while (serverRef.current?.CONNECTING) {}
|
|
||||||
serverRef.current?.send(JSON.stringify(req));
|
|
||||||
}}
|
|
||||||
sendVoice={(data) => {
|
sendVoice={(data) => {
|
||||||
while (!serverRef.current || serverRef.current?.CONNECTING) {}
|
if (!node.current?.socket) {
|
||||||
sendVoice(serverRef.current, data);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sendVoice(node.current?.socket, data);
|
||||||
}}
|
}}
|
||||||
indicators={indicators.filter(
|
indicators={indicators.filter(
|
||||||
(indicator) =>
|
(indicator) =>
|
||||||
@@ -66,12 +70,7 @@ export default function ChannelView({
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
return (
|
return (
|
||||||
<PluginChannel
|
<PluginChannel app={app} messages={messages} ip={ip} node={node} />
|
||||||
app={app}
|
|
||||||
messages={messages}
|
|
||||||
ip={ip}
|
|
||||||
serverRef={serverRef}
|
|
||||||
/>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,17 +2,18 @@ import { Message } from "@/types/types";
|
|||||||
import App from "@/types/app";
|
import App from "@/types/app";
|
||||||
import React, { useEffect, useRef } from "react";
|
import React, { useEffect, useRef } from "react";
|
||||||
import { useParentCommunication } from "react-iframes-bridge";
|
import { useParentCommunication } from "react-iframes-bridge";
|
||||||
|
import { NodeClient } from "@/lib/node";
|
||||||
|
|
||||||
export default function PluginChannel({
|
export default function PluginChannel({
|
||||||
app,
|
app,
|
||||||
messages,
|
messages,
|
||||||
ip,
|
ip,
|
||||||
serverRef,
|
node,
|
||||||
}: {
|
}: {
|
||||||
app: App;
|
app: App;
|
||||||
messages: Message[];
|
messages: Message[];
|
||||||
ip: string;
|
ip: string;
|
||||||
serverRef: React.RefObject<WebSocket | null>;
|
node: React.RefObject<NodeClient | null>;
|
||||||
}) {
|
}) {
|
||||||
if (!app.currentChannel)
|
if (!app.currentChannel)
|
||||||
return (
|
return (
|
||||||
@@ -32,7 +33,7 @@ export default function PluginChannel({
|
|||||||
});
|
});
|
||||||
|
|
||||||
parentComm.onMessage("SEND_MESSAGE", (msg: string) => {
|
parentComm.onMessage("SEND_MESSAGE", (msg: string) => {
|
||||||
serverRef.current?.send(
|
node.current?.socket?.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
type: "send_message",
|
type: "send_message",
|
||||||
params: {
|
params: {
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ import Cookies from "js-cookie";
|
|||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
import { getProfileById } from "@/actions/get-profile";
|
import { getProfileById } from "@/actions/get-profile";
|
||||||
import { getServerById } from "@/actions/get-server";
|
import { getServerById } from "@/actions/get-server";
|
||||||
|
import { NodeClient } from "@/lib/node";
|
||||||
|
|
||||||
export default function useApp(): App {
|
export default function useApp(): App {
|
||||||
const node = useRef<WebSocket | null>(null);
|
const node = useRef<NodeClient | null>(null);
|
||||||
const [profiles, setProfiles] = useState<StringMap<UserProfile | null>>({});
|
const [profiles, setProfiles] = useState<StringMap<UserProfile | null>>({});
|
||||||
const [sidebarOpen, setSidebarOpen] = useState(false);
|
const [sidebarOpen, setSidebarOpen] = useState(false);
|
||||||
const profile = useUser();
|
const profile = useUser();
|
||||||
|
|||||||
+32
-2
@@ -1,17 +1,47 @@
|
|||||||
import { useEffect } from "react";
|
import { RefObject, useEffect, useRef } from "react";
|
||||||
import { MessageStore } from "./use-messages";
|
import { MessageStore } from "./use-messages";
|
||||||
import { NodeCallbacks, NodeClient } from "@/lib/node";
|
import { NodeCallbacks, NodeClient } from "@/lib/node";
|
||||||
|
import { asyncWrapProviders } from "async_hooks";
|
||||||
|
|
||||||
export default function useNode(
|
export default function useNode(
|
||||||
nodeId: string,
|
nodeId: string,
|
||||||
messageStore: MessageStore,
|
messageStore: MessageStore,
|
||||||
callbacks: NodeCallbacks
|
callbacks: NodeCallbacks
|
||||||
) {
|
) {
|
||||||
|
const wsClientRef = useRef<NodeClient | null>(null);
|
||||||
|
useEffect(() => getNode(nodeId, messageStore, callbacks, wsClientRef), []);
|
||||||
|
return wsClientRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useDynNode(
|
||||||
|
nodeId: string | undefined | null,
|
||||||
|
messageStore: MessageStore,
|
||||||
|
callbacks: NodeCallbacks
|
||||||
|
) {
|
||||||
|
const wsClientRef = useRef<NodeClient | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!nodeId) return;
|
||||||
|
return getNode(nodeId, messageStore, callbacks, wsClientRef);
|
||||||
|
}, [nodeId]);
|
||||||
|
|
||||||
|
return wsClientRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNode(
|
||||||
|
nodeId: string,
|
||||||
|
messageStore: MessageStore,
|
||||||
|
callbacks: NodeCallbacks,
|
||||||
|
ref: RefObject<NodeClient | null>
|
||||||
|
) {
|
||||||
const client = new NodeClient(nodeId, messageStore, callbacks);
|
const client = new NodeClient(nodeId, messageStore, callbacks);
|
||||||
|
|
||||||
|
ref.current = client;
|
||||||
client.connect();
|
client.connect();
|
||||||
|
|
||||||
return () => client.disconnect();
|
return () => client.disconnect();
|
||||||
}, [nodeId]);
|
}
|
||||||
|
|
||||||
|
export function useNullNode() {
|
||||||
|
return useRef<NodeClient | null>(null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useRef } from "react";
|
import { useEffect, useRef } from "react";
|
||||||
|
|
||||||
export function useEffectOnceWhenReady(
|
export function useEffectOnceWhenReady(
|
||||||
effect: () => void,
|
effect: () => any,
|
||||||
deps: any[],
|
deps: any[],
|
||||||
testers: (undefined | ((m: any) => boolean))[]
|
testers: (undefined | ((m: any) => boolean))[]
|
||||||
) {
|
) {
|
||||||
@@ -13,6 +13,7 @@ export function useEffectOnceWhenReady(
|
|||||||
);
|
);
|
||||||
if (!allReady || hasRun.current) return;
|
if (!allReady || hasRun.current) return;
|
||||||
hasRun.current = true;
|
hasRun.current = true;
|
||||||
effect();
|
|
||||||
|
return effect();
|
||||||
}, deps);
|
}, deps);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -13,7 +13,7 @@ export interface NodeCallbacks {
|
|||||||
onVoiceJoin?: (userId: string, channelId: string, voiceId: number) => void;
|
onVoiceJoin?: (userId: string, channelId: string, voiceId: number) => void;
|
||||||
onVoiceLeave?: (userId: string, channelId: string, voiceId: number) => void;
|
onVoiceLeave?: (userId: string, channelId: string, voiceId: number) => void;
|
||||||
onSpeaking?: (voiceId: number) => void;
|
onSpeaking?: (voiceId: number) => void;
|
||||||
};
|
}
|
||||||
|
|
||||||
export class NodeClient {
|
export class NodeClient {
|
||||||
private ws: WebSocket | null = null;
|
private ws: WebSocket | null = null;
|
||||||
@@ -57,6 +57,7 @@ export class NodeClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
disconnect() {
|
disconnect() {
|
||||||
|
console.log("Disconnecting");
|
||||||
this.ws?.close();
|
this.ws?.close();
|
||||||
this.ws = null;
|
this.ws = null;
|
||||||
}
|
}
|
||||||
@@ -78,6 +79,8 @@ export class NodeClient {
|
|||||||
|
|
||||||
const data = JSON.parse(e.data);
|
const data = JSON.parse(e.data);
|
||||||
|
|
||||||
|
console.log("Message received:", data);
|
||||||
|
|
||||||
if (data.error) {
|
if (data.error) {
|
||||||
toast.error(`Error from ${this.name} (${data.error})`, {
|
toast.error(`Error from ${this.name} (${data.error})`, {
|
||||||
description: data.message,
|
description: data.message,
|
||||||
|
|||||||
+3
-2
@@ -1,14 +1,15 @@
|
|||||||
import { Dispatch, RefObject, SetStateAction } from "react";
|
import { Dispatch, RefObject, SetStateAction } from "react";
|
||||||
import { StringMap } from "./typeUtils";
|
import { StringMap } from "./typeUtils";
|
||||||
import { UserProfile } from "@/hooks/get-user";
|
import { UserProfile } from "@/hooks/get-user";
|
||||||
import { Channel, Message, Server } from "./types";
|
import { Channel, Server } from "./types";
|
||||||
import { ClientSettings } from "./settings";
|
import { ClientSettings } from "./settings";
|
||||||
import { MessageStore } from "@/hooks/use-messages";
|
import { MessageStore } from "@/hooks/use-messages";
|
||||||
|
import { NodeClient } from "@/lib/node";
|
||||||
|
|
||||||
export type SetState<T> = Dispatch<SetStateAction<T>>;
|
export type SetState<T> = Dispatch<SetStateAction<T>>;
|
||||||
|
|
||||||
export default interface App {
|
export default interface App {
|
||||||
node: RefObject<WebSocket | null>;
|
node: RefObject<NodeClient | null>;
|
||||||
profile: UserProfile | null;
|
profile: UserProfile | null;
|
||||||
|
|
||||||
server: Server | undefined;
|
server: Server | undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user