1 import axios from 'axios';
2 import React from 'react';
3 import { Alert, Button, Col, Form, Row } from 'react-bootstrap';
4 import { useTranslation } from 'react-i18next';
5 import toastr from 'toastr';
7 import ChatSettingsForm from './ChatSettingsForm';
8 import CommandDialog from './CommandDialog';
9 import Commands from './Commands';
10 import GuessingSettingsForm from './GuessingSettingsForm';
11 import ChannelSelect from '../common/ChannelSelect';
12 import Icon from '../common/Icon';
13 import ToggleSwitch from '../common/ToggleSwitch';
15 const Controls = () => {
16 const [channel, setChannel] = React.useState(null);
17 const [chatText, setChatText] = React.useState('');
18 const [editCommand, setEditCommand] = React.useState('');
19 const [editCommandSettings, setEditCommandSettings] = React.useState({});
20 const [showCommandDialog, setShowCommandDialog] = React.useState(false);
22 const { t } = useTranslation();
24 const chat = React.useCallback(async (text, bot_nick) => {
26 await axios.post(`/api/channels/${channel.id}/chat`, {
30 toastr.success(t('twitchBot.chatSuccess'));
32 toastr.error(t('twitchBot.chatError'));
34 }, [channel, chatText, t]);
36 const join = React.useCallback(async (bot_nick) => {
38 const rsp = await axios.post(`/api/channels/${channel.id}/join`, { bot_nick });
40 toastr.success(t('twitchBot.joinSuccess'));
42 toastr.error(t('twitchBot.joinError'));
46 const part = React.useCallback(async (bot_nick) => {
48 const rsp = await axios.post(`/api/channels/${channel.id}/part`, { bot_nick });
50 toastr.success(t('twitchBot.partSuccess'));
52 toastr.error(t('twitchBot.partError'));
56 const saveChatSettings = React.useCallback(async (values) => {
58 const rsp = await axios.post(`/api/channels/${channel.id}/chat-settings`, values);
60 toastr.success(t('twitchBot.saveSuccess'));
62 toastr.error(t('twitchBot.saveError'));
66 const onAddCommand = React.useCallback(() => {
68 setEditCommandSettings({});
69 setShowCommandDialog(true);
72 const onEditCommand = React.useCallback((name, settings) => {
74 setEditCommandSettings(settings);
75 setShowCommandDialog(true);
78 const onRemoveCommand = React.useCallback(async (name) => {
80 const rsp = await axios.delete(`/api/channels/${channel.id}/commands/${name}`);
82 toastr.success(t('twitchBot.saveSuccess'));
84 toastr.error(t('twitchBot.saveError'));
88 const saveCommand = React.useCallback(async (values) => {
90 const rsp = await axios.put(
91 `/api/channels/${channel.id}/commands/${values.name}`,
95 setShowCommandDialog(false);
97 setEditCommandSettings({});
98 toastr.success(t('twitchBot.saveSuccess'));
100 toastr.error(t('twitchBot.saveError'));
105 const saveGuessingGame = React.useCallback(async (values) => {
107 const rsp = await axios.put(
108 `/api/channels/${channel.id}/guessing-game/${values.name}`,
111 setChannel(rsp.data);
112 toastr.success(t('twitchBot.saveSuccess'));
114 toastr.error(t('twitchBot.saveError'));
120 <Row className="mb-4">
121 <Form.Group as={Col} md={6}>
122 <Form.Label>{t('twitchBot.channel')}</Form.Label>
128 onChange={({ channel }) => { setChannel(channel); }}
129 value={channel ? channel.id : ''}
133 <Form.Group as={Col} md={3}>
134 <Form.Label>{t('twitchBot.joinApp')}</Form.Label>
138 onChange={({ target: { value } }) => {
140 join('localhorsttv');
142 part('localhorsttv');
149 <Form.Group as={Col} md={3}>
150 <Form.Label>{t('twitchBot.joinChat')}</Form.Label>
154 onChange={({ target: { value } }) => {
169 <Col className="mt-5" md={6}>
170 <h3>{t('twitchBot.chat')}</h3>
172 <Form.Label>{t('twitchBot.chat')}</Form.Label>
175 onChange={({ target: { value } }) => {
180 <div className="button-bar">
183 disabled={!chatText || !channel.join}
185 if (chatText) chat(chatText, 'localhorsttv');
189 {t('twitchBot.sendApp')}
193 disabled={!chatText || !channel.chat}
195 if (chatText) chat(chatText, 'horstiebot');
199 {t('twitchBot.sendChat')}
204 <Col className="mt-5" md={6}>
205 <h3>{t('twitchBot.chatSettings')}</h3>
206 <ChatSettingsForm channel={channel} onSubmit={saveChatSettings} />
208 <Col className="mt-5" md={12}>
209 <h3>{t('twitchBot.commands')}</h3>
212 onEditCommand={onEditCommand}
213 onRemoveCommand={onRemoveCommand}
218 setShowCommandDialog(false);
220 setEditCommandSettings({});
222 onSubmit={saveCommand}
223 settings={editCommandSettings}
224 show={showCommandDialog}
227 <Button onClick={onAddCommand} variant="primary">
228 {t('twitchBot.addCommand')}
232 <Col className="mt-5" md={12}>
233 <div className="d-flex align-items-end justify-content-between">
234 <h3>{t('twitchBot.guessingGame.settings')}</h3>
235 <div className="button-bar">
236 {channel.access_key ?
238 href={`/guessing-game/monitor/${channel.access_key}`}
240 title={t('button.browserSource')}
241 variant="outline-secondary"
243 <Icon.BROWSER_SOURCE title="" />
249 `/guessing-game/controls/${channel.id}`,
251 'width=640,height=800,titlebar=0,menubar=0,toolbar=0',
254 title={t('twitchBot.guessingGame.popoutControls')}
255 variant="outline-secondary"
257 <Icon.OPEN title="" />
261 <GuessingSettingsForm
263 onSubmit={saveGuessingGame}
264 settings={channel.guessing_settings?.gtbk || {}}
269 <Alert variant="info">
270 {t('twitchBot.selectChannel')}
276 export default Controls;