481 lines
13 KiB
TypeScript
481 lines
13 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
import { safeCall, toast } from '../lib/app';
|
|
import type { RpcMethodName } from '../lib/rpc';
|
|
import { confirmDialog, promptDialog } from '../lib/dialog';
|
|
import { EmptyRow } from './TrackRow';
|
|
import { t, tf } from '../i18n';
|
|
import { teamSpeakChannelDisplayName } from '../lib/teamspeak';
|
|
|
|
interface AdminClient {
|
|
id: number;
|
|
databaseId: number | null;
|
|
uid: string | null;
|
|
nickname: string;
|
|
channelId: number;
|
|
talking: boolean;
|
|
muted: boolean;
|
|
deafened: boolean;
|
|
away: boolean;
|
|
groups: number[];
|
|
}
|
|
|
|
interface AdminChannel {
|
|
id: number;
|
|
name: string;
|
|
parentId: number | null;
|
|
default: boolean;
|
|
password: boolean;
|
|
maxClients: number;
|
|
currentClients: number;
|
|
clients: AdminClient[];
|
|
channels: AdminChannel[];
|
|
}
|
|
|
|
interface DirectoryResponse {
|
|
server: {
|
|
name: string;
|
|
connected: boolean;
|
|
clientCount: number;
|
|
maxClients: number;
|
|
};
|
|
channels: AdminChannel[];
|
|
}
|
|
|
|
interface Ban {
|
|
id: number;
|
|
uid: string | null;
|
|
nickname: string | null;
|
|
reason: string;
|
|
createdAt: number;
|
|
durationSeconds: number;
|
|
}
|
|
|
|
interface ServerGroup {
|
|
id: number;
|
|
name: string;
|
|
default: boolean;
|
|
}
|
|
|
|
export function TeamSpeakAdmin() {
|
|
const [directory, setDirectory] = useState<DirectoryResponse | null>(null);
|
|
const [bans, setBans] = useState<Ban[]>([]);
|
|
const [groups, setGroups] = useState<ServerGroup[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const refresh = useCallback(async () => {
|
|
setLoading(true);
|
|
const [nextDirectory, nextBans, nextGroups] = await Promise.all([
|
|
safeCall<DirectoryResponse>('GetTeamSpeakDirectory'),
|
|
safeCall<{ bans?: Ban[] }>('GetTeamSpeakBans'),
|
|
safeCall<{ groups?: ServerGroup[] }>('GetTeamSpeakServerGroups'),
|
|
]);
|
|
if (nextDirectory) setDirectory(nextDirectory);
|
|
if (nextBans) setBans(nextBans.bans || []);
|
|
if (nextGroups) setGroups(nextGroups.groups || []);
|
|
setLoading(false);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void refresh();
|
|
}, [refresh]);
|
|
|
|
const mutate = useCallback(
|
|
async (
|
|
method: RpcMethodName,
|
|
params: Record<string, unknown>,
|
|
message: string
|
|
) => {
|
|
const result = await safeCall(method, params);
|
|
if (!result) return false;
|
|
toast(message);
|
|
await refresh();
|
|
return true;
|
|
},
|
|
[refresh]
|
|
);
|
|
|
|
async function sendServerMessage() {
|
|
const message = await promptDialog({
|
|
title: t('Server message'),
|
|
message: t('Message sent to every connected TeamSpeak client:'),
|
|
confirmLabel: t('Send'),
|
|
});
|
|
if (message) {
|
|
await mutate(
|
|
'SendTeamSpeakServerMessage',
|
|
{ message },
|
|
t('Server message sent')
|
|
);
|
|
}
|
|
}
|
|
|
|
async function createChannel(parentId = 0) {
|
|
const name = await promptDialog({
|
|
title: t('Create channel'),
|
|
message: t('Channel name:'),
|
|
confirmLabel: t('Create'),
|
|
});
|
|
if (name) {
|
|
await mutate(
|
|
'CreateTeamSpeakChannel',
|
|
{ name, parentId, permanent: true },
|
|
t('Channel created')
|
|
);
|
|
}
|
|
}
|
|
|
|
function ClientRow({ client }: { client: AdminClient }) {
|
|
return (
|
|
<div className="ts-admin-client">
|
|
<div className="ts-admin-client-main">
|
|
<span
|
|
className={'ts-presence' + (client.talking ? ' talking' : '')}
|
|
/>
|
|
<strong>{client.nickname}</strong>
|
|
<span className="muted">#{client.id}</span>
|
|
{client.away && <span className="ts-tag">{t('away')}</span>}
|
|
{client.muted && <span className="ts-tag">{t('muted')}</span>}
|
|
{client.deafened && <span className="ts-tag">{t('deafened')}</span>}
|
|
</div>
|
|
<div className="ts-admin-private muted">
|
|
<span>UID: {client.uid || '—'}</span>
|
|
<span>DB: {client.databaseId || '—'}</span>
|
|
<span>{t('Groups')}: {client.groups.join(', ') || '—'}</span>
|
|
</div>
|
|
<div className="ts-admin-actions">
|
|
<button
|
|
className="btn btn-ghost"
|
|
onClick={async () => {
|
|
const message = await promptDialog({
|
|
title: tf('Message {0}', client.nickname),
|
|
message: t('Private message:'),
|
|
confirmLabel: t('Send'),
|
|
});
|
|
if (message)
|
|
await mutate(
|
|
'SendTeamSpeakPrivateMessage',
|
|
{ clientId: client.id, message },
|
|
t('Message sent')
|
|
);
|
|
}}
|
|
>
|
|
{t('Message')}
|
|
</button>
|
|
<button
|
|
className="btn btn-ghost"
|
|
onClick={async () => {
|
|
const channelId = await promptDialog({
|
|
title: tf('Move {0}', client.nickname),
|
|
message: t('Destination channel ID:'),
|
|
confirmLabel: t('Move'),
|
|
});
|
|
if (channelId && /^\d+$/.test(channelId))
|
|
await mutate(
|
|
'MoveTeamSpeakClient',
|
|
{ clientId: client.id, channelId: Number(channelId) },
|
|
t('Client moved')
|
|
);
|
|
}}
|
|
>
|
|
{t('Move')}
|
|
</button>
|
|
{client.databaseId && (
|
|
<button
|
|
className="btn btn-ghost"
|
|
onClick={async () => {
|
|
const groupId = await promptDialog({
|
|
title: t('Add server group'),
|
|
message: groups.length
|
|
? groups.map((g) => `${g.id}: ${g.name}`).join('\n')
|
|
: t('Server group ID:'),
|
|
confirmLabel: t('Add'),
|
|
});
|
|
if (groupId && /^\d+$/.test(groupId))
|
|
await mutate(
|
|
'AddTeamSpeakServerGroupMember',
|
|
{
|
|
groupId: Number(groupId),
|
|
clientDatabaseId: client.databaseId,
|
|
},
|
|
t('Group added')
|
|
);
|
|
}}
|
|
>
|
|
{t('Add group')}
|
|
</button>
|
|
)}
|
|
{client.databaseId && client.groups.length > 0 && (
|
|
<button
|
|
className="btn btn-ghost"
|
|
onClick={async () => {
|
|
const groupId = await promptDialog({
|
|
title: t('Remove server group'),
|
|
message: `${t('Current groups')}: ${client.groups.join(', ')}`,
|
|
confirmLabel: t('Remove'),
|
|
});
|
|
if (groupId && /^\d+$/.test(groupId))
|
|
await mutate(
|
|
'RemoveTeamSpeakServerGroupMember',
|
|
{
|
|
groupId: Number(groupId),
|
|
clientDatabaseId: client.databaseId,
|
|
},
|
|
t('Group removed')
|
|
);
|
|
}}
|
|
>
|
|
{t('Remove group')}
|
|
</button>
|
|
)}
|
|
<button
|
|
className="btn btn-danger-ghost"
|
|
onClick={async () => {
|
|
if (
|
|
await confirmDialog({
|
|
title: tf('Kick {0}', client.nickname),
|
|
message: t('Kick this client from the server?'),
|
|
confirmLabel: t('Kick'),
|
|
danger: true,
|
|
})
|
|
)
|
|
await mutate(
|
|
'KickTeamSpeakClient',
|
|
{ clientId: client.id, fromServer: true },
|
|
t('Client kicked')
|
|
);
|
|
}}
|
|
>
|
|
{t('Kick')}
|
|
</button>
|
|
<button
|
|
className="btn btn-danger-ghost"
|
|
onClick={async () => {
|
|
const duration = await promptDialog({
|
|
title: tf('Ban {0}', client.nickname),
|
|
message: t('Duration in seconds (0 = permanent):'),
|
|
confirmLabel: t('Continue'),
|
|
});
|
|
if (!duration || !/^\d+$/.test(duration)) return;
|
|
if (
|
|
await confirmDialog({
|
|
title: tf('Ban {0}', client.nickname),
|
|
message: t('Apply this ban now?'),
|
|
confirmLabel: t('Ban'),
|
|
danger: true,
|
|
})
|
|
)
|
|
await mutate(
|
|
'BanTeamSpeakClient',
|
|
{
|
|
clientId: client.id,
|
|
durationSeconds: Number(duration),
|
|
},
|
|
t('Client banned')
|
|
);
|
|
}}
|
|
>
|
|
{t('Ban')}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Channel({ channel, depth = 0 }: { channel: AdminChannel; depth?: number }) {
|
|
const displayName = teamSpeakChannelDisplayName(channel.name);
|
|
|
|
return (
|
|
<div className="ts-admin-channel" style={{ marginLeft: depth * 16 }}>
|
|
<div className="ts-admin-channel-head">
|
|
<strong
|
|
className={displayName.spacer ? 'ts-admin-spacer' : undefined}
|
|
title={channel.name}
|
|
>
|
|
{displayName.label}
|
|
</strong>
|
|
<span className="muted">#{channel.id}</span>
|
|
<span className="ts-capacity">
|
|
{channel.currentClients}/{channel.maxClients || '∞'}
|
|
</span>
|
|
<button className="btn btn-ghost" onClick={() => createChannel(channel.id)}>
|
|
{t('Add child')}
|
|
</button>
|
|
<button
|
|
className="btn btn-ghost"
|
|
onClick={async () => {
|
|
const name = await promptDialog({
|
|
title: t('Rename channel'),
|
|
message: channel.name,
|
|
confirmLabel: t('Save'),
|
|
});
|
|
if (name)
|
|
await mutate(
|
|
'EditTeamSpeakChannel',
|
|
{ channelId: channel.id, name },
|
|
t('Channel updated')
|
|
);
|
|
}}
|
|
>
|
|
{t('Edit')}
|
|
</button>
|
|
<button
|
|
className="btn btn-ghost"
|
|
onClick={async () => {
|
|
const message = await promptDialog({
|
|
title: tf('Message {0}', channel.name),
|
|
message: t('Channel message:'),
|
|
confirmLabel: t('Send'),
|
|
});
|
|
if (message)
|
|
await mutate(
|
|
'SendTeamSpeakChannelMessage',
|
|
{ channelId: channel.id, message },
|
|
t('Message sent')
|
|
);
|
|
}}
|
|
>
|
|
{t('Message')}
|
|
</button>
|
|
<button
|
|
className="btn btn-ghost"
|
|
onClick={async () => {
|
|
const parentId = await promptDialog({
|
|
title: t('Reorder channel'),
|
|
message: t('Parent channel ID (0 = root):'),
|
|
confirmLabel: t('Continue'),
|
|
});
|
|
if (parentId === null || !/^\d+$/.test(parentId)) return;
|
|
const orderAfterId = await promptDialog({
|
|
title: t('Reorder channel'),
|
|
message: t('Previous sibling channel ID (0 = first):'),
|
|
confirmLabel: t('Move'),
|
|
});
|
|
if (orderAfterId === null || !/^\d+$/.test(orderAfterId)) return;
|
|
await mutate(
|
|
'ReorderTeamSpeakChannel',
|
|
{
|
|
channelId: channel.id,
|
|
parentId: Number(parentId),
|
|
orderAfterId: Number(orderAfterId),
|
|
},
|
|
t('Channel reordered')
|
|
);
|
|
}}
|
|
>
|
|
{t('Reorder')}
|
|
</button>
|
|
{!channel.default && (
|
|
<button
|
|
className="btn btn-danger-ghost"
|
|
onClick={async () => {
|
|
if (
|
|
await confirmDialog({
|
|
title: t('Delete channel'),
|
|
message: tf('Delete {0} and its contents?', channel.name),
|
|
confirmLabel: t('Delete'),
|
|
danger: true,
|
|
})
|
|
)
|
|
await mutate(
|
|
'DeleteTeamSpeakChannel',
|
|
{ channelId: channel.id, force: true },
|
|
t('Channel deleted')
|
|
);
|
|
}}
|
|
>
|
|
{t('Delete')}
|
|
</button>
|
|
)}
|
|
</div>
|
|
{channel.clients.map((client) => (
|
|
<ClientRow key={client.id} client={client} />
|
|
))}
|
|
{channel.channels.map((child) => (
|
|
<Channel key={child.id} channel={child} depth={depth + 1} />
|
|
))}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="admin-stack">
|
|
<div className="card">
|
|
<div className="card-head">
|
|
<div>
|
|
<h2>{directory?.server.name || t('TeamSpeak')}</h2>
|
|
<div className="muted">
|
|
{directory?.server.connected ? t('Connected') : t('Disconnected')}
|
|
{' · '}
|
|
{directory?.server.clientCount || 0}/{directory?.server.maxClients || '∞'}
|
|
</div>
|
|
</div>
|
|
<div className="row-gap">
|
|
<button className="btn btn-ghost" onClick={sendServerMessage}>
|
|
{t('Server message')}
|
|
</button>
|
|
<button className="btn btn-ghost" onClick={() => createChannel()}>
|
|
{t('Create channel')}
|
|
</button>
|
|
<button className="btn btn-ghost" disabled={loading} onClick={refresh}>
|
|
{loading ? t('Loading…') : t('Refresh')}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<div className="ts-admin-tree">
|
|
{directory?.channels.length ? (
|
|
directory.channels.map((channel) => (
|
|
<Channel key={channel.id} channel={channel} />
|
|
))
|
|
) : (
|
|
<EmptyRow text={t('No TeamSpeak topology available')} />
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="card">
|
|
<div className="card-head">
|
|
<h2>{t('Bans')}</h2>
|
|
<span className="muted">{bans.length}</span>
|
|
</div>
|
|
<div className="tracklist">
|
|
{bans.length ? (
|
|
bans.map((ban) => (
|
|
<div className="ts-ban-row" key={ban.id}>
|
|
<div>
|
|
<strong>{ban.nickname || ban.uid || `#${ban.id}`}</strong>
|
|
<div className="muted">{ban.reason || t('No reason')}</div>
|
|
</div>
|
|
<span className="muted">
|
|
{ban.durationSeconds ? `${ban.durationSeconds}s` : t('Permanent')}
|
|
</span>
|
|
<button
|
|
className="btn btn-danger-ghost"
|
|
onClick={async () => {
|
|
if (
|
|
await confirmDialog({
|
|
title: t('Remove ban'),
|
|
message: t('Remove this TeamSpeak ban?'),
|
|
confirmLabel: t('Unban'),
|
|
danger: true,
|
|
})
|
|
)
|
|
await mutate(
|
|
'UnbanTeamSpeakClient',
|
|
{ banId: ban.id },
|
|
t('Ban removed')
|
|
);
|
|
}}
|
|
>
|
|
{t('Unban')}
|
|
</button>
|
|
</div>
|
|
))
|
|
) : (
|
|
<EmptyRow text={t('No bans')} />
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|