react-teamspeak-remote-app-api
Version:
React hook/api for the TeamSpeak5/6 remote app feature
55 lines (54 loc) • 3.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useTSRemoteApp;
/* eslint-disable react-hooks/exhaustive-deps */
var connectionHandler_1 = require("../handlers/teamspeak/connectionHandler");
var react_1 = require("react");
var logger_1 = __importDefault(require("../utils/logger"));
function useTSRemoteApp(options) {
var _a = (0, react_1.useState)([]), clients = _a[0], setClients = _a[1];
var _b = (0, react_1.useState)([]), channels = _b[0], setChannels = _b[1];
var _c = (0, react_1.useState)([]), connections = _c[0], setConnections = _c[1];
var _d = (0, react_1.useState)(1), activeConnectionId = _d[0], setActiveConnectionId = _d[1];
var _e = (0, react_1.useState)(undefined), currentConnection = _e[0], setCurrentConnection = _e[1];
var _f = (0, react_1.useState)(undefined), currentChannel = _f[0], setCurrentChannel = _f[1];
var _g = (0, react_1.useState)(undefined), currentClient = _g[0], setCurrentClient = _g[1];
var _h = (0, react_1.useState)([]), clientsInChannel = _h[0], setClientsInChannel = _h[1];
(0, react_1.useEffect)(function () {
var _a, _b;
var tsConnection = new connectionHandler_1.TS5ConnectionHandler((_a = options.remoteAppPort) !== null && _a !== void 0 ? _a : 5899, options.auth, new logger_1.default((_b = options.logging) !== null && _b !== void 0 ? _b : false), setConnections, setChannels, setClients, setActiveConnectionId);
tsConnection.connect();
}, []);
(0, react_1.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,
};
}