From fd9dbedfdbed7d4402e452e81826d9af0db73b2d Mon Sep 17 00:00:00 2001 From: Ash Keel Date: Thu, 1 Dec 2022 16:40:04 +0100 Subject: [PATCH] fix: disallow creation of commands/timers already in use fixes #28 --- frontend/src/locale/en/translation.json | 6 ++++-- frontend/src/ui/pages/BotCommands.tsx | 13 ++++++++++++- frontend/src/ui/pages/BotTimers.tsx | 16 +++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/frontend/src/locale/en/translation.json b/frontend/src/locale/en/translation.json index 1a253ac..37abc2d 100644 --- a/frontend/src/locale/en/translation.json +++ b/frontend/src/locale/en/translation.json @@ -109,7 +109,8 @@ "streamer": "Streamer only" }, "remove-command-title": "Remove command {{name}}?", - "no-commands": "Chatbot has no commands configured" + "no-commands": "Chatbot has no commands configured", + "command-already-in-use": "Command name already in use" }, "bottimers": { "title": "Bot timers", @@ -126,7 +127,8 @@ "timer-activity": "Minimul chat activity (0 to disable)", "timer-activity-desc": "messages in the last 5 minutes", "timer-messages": "Messages", - "no-timers": "There are no timers configured" + "no-timers": "There are no timers configured", + "name-already-in-use": "Timer name already in use" }, "auth": { "title": "Authentication required", diff --git a/frontend/src/ui/pages/BotCommands.tsx b/frontend/src/ui/pages/BotCommands.tsx index 85db164..6687a4d 100644 --- a/frontend/src/ui/pages/BotCommands.tsx +++ b/frontend/src/ui/pages/BotCommands.tsx @@ -189,6 +189,7 @@ function CommandDialog({ item?: TwitchBotCustomCommand; onSubmit?: (name: string, item: TwitchBotCustomCommand) => void; }) { + const [commands] = useModule(modules.twitchBotCommands); const [commandName, setCommandName] = useState(name ?? ''); const [description, setDescription] = useState(item?.description ?? ''); const [response, setResponse] = useState(item?.response ?? ''); @@ -226,7 +227,17 @@ function CommandDialog({ id="command-name" value={commandName} required={true} - onChange={(e) => setCommandName(e.target.value)} + onChange={(e) => { + setCommandName(e.target.value); + // If command name is different but matches another defined command, set as invalid + if (e.target.value !== name && e.target.value in commands) { + (e.target as HTMLInputElement).setCustomValidity( + t('pages.botcommands.command-already-in-use'), + ); + } else { + (e.target as HTMLInputElement).setCustomValidity(''); + } + }} placeholder={t('pages.botcommands.command-name-placeholder')} /> diff --git a/frontend/src/ui/pages/BotTimers.tsx b/frontend/src/ui/pages/BotTimers.tsx index 4f9d192..f0ac70a 100644 --- a/frontend/src/ui/pages/BotTimers.tsx +++ b/frontend/src/ui/pages/BotTimers.tsx @@ -200,6 +200,7 @@ function TimerDialog({ item?: TwitchBotTimer; onSubmit?: (name: string, item: TwitchBotTimer) => void; }) { + const [timerConfig] = useModule(modules.twitchBotTimers); const [timerName, setName] = useState(name ?? ''); const [messages, setMessages] = useState(item?.messages ?? ['']); const [minDelay, setMinDelay] = useState(item?.minimum_delay ?? 300); @@ -234,7 +235,20 @@ function TimerDialog({ setName(e.target.value)} + onChange={(e) => { + setName(e.target.value); + // If timer name is different but matches another defined timer, set as invalid + if ( + e.target.value !== name && + e.target.value in timerConfig.timers + ) { + (e.target as HTMLInputElement).setCustomValidity( + t('pages.bottimers.name-already-in-use'), + ); + } else { + (e.target as HTMLInputElement).setCustomValidity(''); + } + }} placeholder={t('pages.bottimers.timer-name-placeholder')} required={true} />