import { useEffect, useState } from 'react'; import { Modal, SmallModal } from './Modal'; import { AccountList, ChatbotList, DeleteChatbot, NewChatbot, OpenFileDialog, UpdateChatbot, } from '../../wailsjs/go/main/App'; import { EventsOn } from '../../wailsjs/runtime/runtime'; import { ChevronLeft, ChevronRight, Gear, PlusCircle, Robot } from '../assets'; import './ChatBot.css'; function ChatBot(props) { const [chatbots, setChatbots] = useState([]); const [deleteChatbot, setDeleteChatbot] = useState(false); const [editChatbot, setEditChatbot] = useState(null); const [error, setError] = useState(''); const [openChatbot, setOpenChatbot] = useState(null); const [openNewChatbot, setOpenNewChatbot] = useState(false); const [openNewRule, setOpenNewRule] = useState(false); const [chatbotSettings, setChatbotSettings] = useState(true); useEffect(() => { EventsOn('ChatbotList', (event) => { setChatbots(event); if (openChatbot !== null) { for (const chatbot of event) { if (chatbot.id === openChatbot.id) { setOpenChatbot(chatbot); } } } }); }, []); useEffect(() => { ChatbotList() .then((response) => { setChatbots(response); }) .catch((error) => { setError(error); }); }, []); const open = (chatbot) => { setOpenChatbot(chatbot); }; const closeEdit = () => { setEditChatbot(null); }; const openEdit = (chatbot) => { setEditChatbot(chatbot); }; const openNew = () => { setOpenNewChatbot(true); }; const sortChatbots = () => { let sorted = [...chatbots].sort((a, b) => a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1 ); return sorted; }; const confirmDelete = () => { DeleteChatbot(openChatbot) .then(() => { setDeleteChatbot(false); setEditChatbot(null); setOpenChatbot(null); }) .catch((err) => { setDeleteChatbot(false); setError(err); }); }; return ( <> {error !== '' && ( setError('')} show={error !== ''} style={{ minWidth: '300px', maxWidth: '200px', maxHeight: '200px' }} title={'Error'} message={error} submitButton={'OK'} onSubmit={() => setError('')} /> )} {openNewChatbot && ( setOpenNewChatbot(false)} show={setOpenNewChatbot} submit={NewChatbot} submitButton={'Create'} submittingButton={'Creating...'} title={'New Chatbot'} /> )} {editChatbot !== null && ( setDeleteChatbot(true)} show={editChatbot !== null} submit={UpdateChatbot} submitButton={'Update'} submittingButton={'Updating...'} title={'Edit Chatbot'} /> )} {deleteChatbot && ( setDeleteChatbot(false)} onClose={() => setDeleteChatbot(false)} show={deleteChatbot} style={{ minWidth: '300px', maxWidth: '200px', maxHeight: '200px' }} title={'Delete Chatbot'} message={ 'Are you sure you want to delete the chatbot? All rules associated with this chatbot will be deleted as well.' } submitButton={'OK'} onSubmit={confirmDelete} /> )} {openNewRule && ( setOpenNewRule(false)} show={openNewRule} /> )}
{openChatbot === null ? ( <>
Bots
{sortChatbots().map((chatbot, index) => ( ))}
) : (
setOpenChatbot(null)} src={ChevronLeft} />
{openChatbot.name}
)}
); } export default ChatBot; function ChatbotListItem(props) { return (
); } function ModalChatbot(props) { const [error, setError] = useState(''); const [id, setId] = useState(props.chatbot === undefined ? null : props.chatbot.id); const [loading, setLoading] = useState(false); const [name, setName] = useState(props.chatbot === undefined ? '' : props.chatbot.name); const updateName = (event) => { if (loading) { return; } setName(event.target.value); }; const [nameValid, setNameValid] = useState(true); const [url, setUrl] = useState(props.chatbot === undefined ? '' : props.chatbot.url); const updateUrl = (event) => { if (loading) { return; } setUrl(event.target.value); }; useEffect(() => { if (loading) { props .submit({ id: id, name: name, url: url }) .then(() => { reset(); props.onClose(); }) .catch((err) => { setLoading(false); setError(err); }); } }, [loading]); const close = () => { if (loading) { return; } reset(); props.onClose(); }; const reset = () => { setLoading(false); setName(''); setNameValid(true); }; const submit = () => { if (name == '') { setNameValid(false); return; } setLoading(true); }; return ( <> {error !== '' && ( setError('')} show={error !== ''} style={{ minWidth: '300px', maxWidth: '200px', maxHeight: '200px' }} title={'Error'} message={error} submitButton={'OK'} onSubmit={() => setError('')} /> )}
{nameValid ? ( ) : ( )}
); } function ModalNewRule(props) { const [back, setBack] = useState([]); const [rule, setRule] = useState({}); const [stage, setStage] = useState('trigger'); const updateStage = (next, reverse) => { setBack([...back, { stage: stage, reverse: reverse }]); setStage(next); }; const goBack = () => { if (back.length === 0) { return; } const last = back.at(-1); setStage(last.stage); if (last.reverse !== undefined && last.reverse !== null) { setRule(last.reverse(rule)); } setBack(back.slice(0, back.length - 1)); }; const submit = () => {}; return ( <> {stage === 'trigger' && ( )} {stage === 'trigger-timer' && ( )} {stage === 'message' && ( )} {stage === 'sender' && ( )} {stage === 'review' && ( )} ); } function ModalNewRuleTrigger(props) { const next = (stage) => { const rule = props.rule; rule.trigger = {}; props.setRule(rule); props.setStage(stage, reverse); }; const reverse = (rule) => { rule.trigger = null; return rule; }; return (
Choose Rule Trigger
); } function ModalNewRuleTriggerTimer(props) { const [validTimer, setValidTimer] = useState(true); const [timer, setTimer] = useState( props.rule.trigger.on_timer !== undefined && props.rule.trigger.on_timer !== null ? props.rule.trigger.on_timer : '' ); const back = () => { const rule = props.rule; rule.trigger.on_timer = ''; props.setRule(rule); props.onBack(); }; const next = () => { if (timer === '') { setValidTimer(false); return; } const rule = props.rule; rule.trigger.on_timer = timer; props.setRule(rule); props.setStage('message', null); }; return (
Set Timer Chat rule will trigger at the set interval.
{validTimer ? ( Enter timer ) : ( Enter a valid timer interval. )}
); } function ModalNewRuleMessage(props) { const [error, setError] = useState(''); const [message, setMessage] = useState( props.rule.message !== undefined && props.rule.message !== null ? props.rule.message : {} ); const [refresh, setRefresh] = useState(false); const [validFile, setValidFile] = useState(true); const [validText, setValidText] = useState(true); const back = () => { const rule = props.rule; rule.message = null; props.setRule(rule); props.onBack(); }; const next = () => { if (fromFile()) { if (message.from_file.filepath === undefined || message.from_file.filepath === '') { setValidFile(false); return; } } else { if (message.from_text === undefined || message.from_text === '') { setValidText(false); return; } } const rule = props.rule; rule.message = message; props.setRule(rule); props.setStage('sender', null); }; const chooseFile = () => { OpenFileDialog() .then((filepath) => { if (filepath !== '') { message.from_file.filepath = filepath; setMessage(message); setRefresh(!refresh); } }) .catch((error) => setError(error)); }; const fromFile = () => { return message.from_file !== undefined && message.from_file !== null; }; const toggleFromFile = () => { if (fromFile()) { message.from_file = null; } else { message.from_file = {}; } setMessage(message); setRefresh(!refresh); }; const randomRead = () => { if (!fromFile()) { return false; } if (message.from_file.random_read === undefined || message.from_file.random_read === null) { return false; } return message.from_file.random_read; }; const toggleRandomRead = () => { if (!fromFile()) { return; } message.from_file.random_read = !randomRead(); setMessage(message); setRefresh(!refresh); }; const updateMessageText = (event) => { message.from_text = event.target.value; setMessage(message); }; const updateMessageFilepath = (filepath) => { if (!fromFile()) { message.from_file = {}; } message.from_file = filepath; setMessage(message); }; return ( <> {error !== '' && ( setError('')} show={error !== ''} style={{ minWidth: '300px', maxWidth: '200px', maxHeight: '200px' }} title={'Error'} message={error} submitButton={'OK'} onSubmit={() => setError('')} /> )}
Add Message
{fromFile() ? ( validFile ? ( ) : ( ) ) : validText ? ( ) : ( )} {fromFile() ? (
{message.from_file.filepath}
) : (