import React, { lazy, Suspense, useCallback, useEffect, useMemo, useState } from "react"; import type { EmojiClickData } from "emoji-picker-react"; import { FaPaperPlane, FaSmile } from "react-icons/fa"; import styles from "./styles/chatbot.module.css"; import { ChatbotClientError, getChatbotUsage, sendChatbotMessage, type ChatMessage, type ChatbotClientOptions, type ChatbotUsage, } from "./client.js"; import { chatbotTranslationKeys, translateChatbotText, type ChatbotTranslate, } from "./i18n.js"; const EmojiPicker = lazy(() => import("emoji-picker-react/dist/emoji-picker-react.esm.js").then((module) => ({ default: module.EmojiPicker, })) ); type ChatbotState = "loading" | "ready" | "signed_out" | "limit_reached" | "error"; export interface ChatBotProps extends ChatbotClientOptions { initialMessages?: ChatMessage[]; systemPrompt?: string; placeholder?: string; title?: string; translate?: ChatbotTranslate; onUsageChange?: (usage: ChatbotUsage) => void; onAuthRequired?: () => void; } const DEFAULT_SYSTEM_PROMPT = "You are the Plasius assistant. Keep responses concise, practical, and factual."; function statusMessage( state: ChatbotState, usage: ChatbotUsage | null, translate?: ChatbotTranslate ): string { if (state === "loading") { return translateChatbotText(chatbotTranslationKeys.checkingAccess, undefined, translate); } if (state === "signed_out") { return translateChatbotText(chatbotTranslationKeys.signInToUse, undefined, translate); } if (state === "limit_reached") { if (usage) { return translateChatbotText( chatbotTranslationKeys.demoLimitReachedWithUsage, { used: usage.used, limit: usage.limit }, translate ); } return translateChatbotText(chatbotTranslationKeys.demoLimitReached, undefined, translate); } if (state === "error") { return translateChatbotText(chatbotTranslationKeys.unavailable, undefined, translate); } return ""; } export default function ChatBot( props: React.PropsWithChildren ): React.ReactElement { const [messages, setMessages] = useState(props.initialMessages ?? []); const [input, setInput] = useState(""); const [showEmojiPicker, setShowEmojiPicker] = useState(false); const [isSending, setIsSending] = useState(false); const [state, setState] = useState("loading"); const [usage, setUsage] = useState(null); const [errorMessage, setErrorMessage] = useState(null); const clientOptions = useMemo( () => ({ endpoint: props.endpoint, credentials: props.credentials, headers: props.headers, fetchFn: props.fetchFn, csrfCookieName: props.csrfCookieName, csrfHeaderName: props.csrfHeaderName, bootstrapCsrf: props.bootstrapCsrf, translate: props.translate, }), [ props.endpoint, props.credentials, props.headers, props.fetchFn, props.csrfCookieName, props.csrfHeaderName, props.bootstrapCsrf, props.translate, ] ); const applyUsage = useCallback( (nextUsage: ChatbotUsage) => { setUsage(nextUsage); props.onUsageChange?.(nextUsage); setState(nextUsage.exhausted ? "limit_reached" : "ready"); }, [props.onUsageChange] ); useEffect(() => { let active = true; const loadUsage = async () => { setState("loading"); setErrorMessage(null); try { const result = await getChatbotUsage(clientOptions); if (!active) return; applyUsage(result.usage); } catch (error) { if (!active) return; if (error instanceof ChatbotClientError && error.status === 401) { setState("signed_out"); setErrorMessage( translateChatbotText(chatbotTranslationKeys.signInStart, undefined, props.translate) ); props.onAuthRequired?.(); return; } setState("error"); setErrorMessage( error instanceof Error ? error.message : translateChatbotText(chatbotTranslationKeys.failedToLoad, undefined, props.translate) ); } }; void loadUsage(); return () => { active = false; }; }, [applyUsage, clientOptions, props.onAuthRequired, props.translate]); const sendDisabled = isSending || state === "loading" || state === "signed_out" || state === "limit_reached" || !input.trim(); const handleEmojiClick = (emojiData: EmojiClickData): void => { setInput((prev) => prev + (emojiData.emoji ?? "")); }; const handleSend = useCallback(async (): Promise => { const message = input.trim(); if (!message || sendDisabled) return; const userMessage: ChatMessage = { role: "user", content: message }; const nextHistory = [...messages, userMessage].slice(-20); setMessages((prev) => [...prev, userMessage]); setInput(""); setShowEmojiPicker(false); setIsSending(true); setErrorMessage(null); try { const response = await sendChatbotMessage( { message, history: nextHistory, systemPrompt: props.systemPrompt ?? DEFAULT_SYSTEM_PROMPT, }, clientOptions ); setMessages((prev) => [ ...prev, { role: "assistant", content: response.reply }, ]); applyUsage(response.usage); } catch (error) { if (error instanceof ChatbotClientError) { if (error.status === 401) { setState("signed_out"); setErrorMessage( translateChatbotText( chatbotTranslationKeys.signedInRequired, undefined, props.translate ) ); props.onAuthRequired?.(); return; } if (error.status === 429) { if (error.usage) { applyUsage(error.usage); } else { setState("limit_reached"); } setErrorMessage( translateChatbotText( chatbotTranslationKeys.userDemoLimitReached, undefined, props.translate ) ); return; } setState("error"); setErrorMessage(error.message); return; } setState("error"); setErrorMessage( error instanceof Error ? error.message : translateChatbotText(chatbotTranslationKeys.messageFailed, undefined, props.translate) ); } finally { setIsSending(false); } }, [ applyUsage, clientOptions, input, messages, props.onAuthRequired, props.systemPrompt, props.translate, sendDisabled, ]); return (
{props.title ?? translateChatbotText(chatbotTranslationKeys.defaultTitle, undefined, props.translate)}
{usage ? translateChatbotText( chatbotTranslationKeys.usageSummary, { used: usage.used, limit: usage.limit }, props.translate ) : translateChatbotText(chatbotTranslationKeys.noUsageData, undefined, props.translate)}
{(state !== "ready" || errorMessage) && (
{errorMessage ?? statusMessage(state, usage, props.translate)}
)}
{messages.map((msg, index) => (
{msg.content}
))}
setInput(event.target.value)} onKeyUp={async (event) => { if (event.key === "Enter" && !event.shiftKey) { await handleSend(); event.stopPropagation(); } }} placeholder={ props.placeholder ?? translateChatbotText( chatbotTranslationKeys.defaultPlaceholder, undefined, props.translate ) } /> {showEmojiPicker && (
{translateChatbotText( chatbotTranslationKeys.loadingEmojiPicker, undefined, props.translate )}
} >
)}
); }