react-teamspeak-remote-app-api
Version:
React hook/api for the TeamSpeak5/6 remote app feature
49 lines (48 loc) • 2.78 kB
JavaScript
/* eslint-disable react-hooks/exhaustive-deps */
import { TS5ConnectionHandler } from "../handlers/teamspeak/connectionHandler";
import { useEffect, useState } from "react";
import Logger from "../utils/logger";
export default function useTSRemoteApp(options) {
var _a = useState([]), clients = _a[0], setClients = _a[1];
var _b = useState([]), channels = _b[0], setChannels = _b[1];
var _c = useState([]), connections = _c[0], setConnections = _c[1];
var _d = useState(1), activeConnectionId = _d[0], setActiveConnectionId = _d[1];
var _e = useState(undefined), currentConnection = _e[0], setCurrentConnection = _e[1];
var _f = useState(undefined), currentChannel = _f[0], setCurrentChannel = _f[1];
var _g = useState(undefined), currentClient = _g[0], setCurrentClient = _g[1];
var _h = useState([]), clientsInChannel = _h[0], setClientsInChannel = _h[1];
useEffect(function () {
var _a, _b;
var tsConnection = new TS5ConnectionHandler((_a = options.remoteAppPort) !== null && _a !== void 0 ? _a : 5899, options.auth, new Logger((_b = options.logging) !== null && _b !== void 0 ? _b : false), setConnections, setChannels, setClients, setActiveConnectionId);
tsConnection.connect();
}, []);
useEffect(function () {
var currentConnection = connections.find(function (connection) { return connection.id === activeConnectionId; });
setCurrentConnection(currentConnection);
if (currentConnection) {
var currentClient_1 = clients.find(function (client) { return client.id === currentConnection.clientId; });
setCurrentClient(currentClient_1);
if (currentClient_1) {
var currentChannel_1 = channels.find(function (channel) { var _a; return channel.id === ((_a = currentClient_1.channel) === null || _a === void 0 ? void 0 : _a.id); });
setCurrentChannel(currentChannel_1);
}
}
if (currentChannel) {
var clientsInChannel_1 = clients.filter(function (client) {
var _a;
return ((_a = client.channel) === null || _a === void 0 ? void 0 : _a.id) === (currentChannel === null || currentChannel === void 0 ? void 0 : currentChannel.id) && client.channel.connection.id === activeConnectionId;
});
setClientsInChannel(clientsInChannel_1);
}
}, [clients, channels, connections, activeConnectionId]);
return {
clients: clients,
channels: channels,
connections: connections,
activeConnectionId: activeConnectionId,
currentConnection: currentConnection,
currentChannel: currentChannel,
currentClient: currentClient,
clientsInChannel: clientsInChannel,
};
}