import { useContext, useState } from "react"; import Input from "@moduletrace/twui/form/Input"; import Button from "@moduletrace/twui/layout/Button"; import Row from "@moduletrace/twui/layout/Row"; import Span from "@moduletrace/twui/layout/Span"; import Stack from "@moduletrace/twui/layout/Stack"; import { AppContext } from "@/src/pages/__root"; import type { SecretRequestView } from "@/src/types"; import decideSecretRequest from "../(functions)/decide-secret-request"; import sendBotMessage from "../(functions)/send-bot-message"; import secretRequestMessage from "../(utils)/secret-request-message"; import MessageDetailsHeading from "./message-details-heading"; type Props = { view: SecretRequestView; onDecided: (view: SecretRequestView) => void; onDone: () => void; }; /** * The card a user types a secret a bot asked for into. The value goes * from here to the app only: it's saved encrypted as their personal secret * for the bot, never put in a message, and the bot (and its model * provider) only learns its key. Saving or declining tells the bot in chat. */ export default function SecretRequestCard({ view, onDecided, onDone }: Props) { const { pageProps } = useContext(AppContext); const [value, setValue] = useState(""); const [busy, setBusy] = useState<"save" | "decline">(); const [error, setError] = useState(); const open = view.status == "pending"; const decide = async (decision: "save" | "decline") => { if (decision == "save" && !value) { setError(`Enter the value first.`); return; } const question = decision == "save" ? `Save ${view.key} for ${view.bot_name}? Its runs for you get it on their computers; it's never shown to the bot.` : `Decline? Nothing will be saved.`; if (!window.confirm(question)) return; setBusy(decision); setError(undefined); const res = await decideSecretRequest({ request_id: view.id, value: decision == "save" ? value : undefined, }).catch(() => null); if (!res?.success || !res.singleRes) { setBusy(undefined); setError(res?.msg || `Couldn't ${decision} it.`); return; } setValue(""); onDecided(res.singleRes); await sendBotMessage({ bot_id: Number(pageProps?.bot?.id) || 0, workspace_id: pageProps?.workspace?.id || null, text: secretRequestMessage({ view: res.singleRes }), media_ids: [], }).catch(() => null); setBusy(undefined); onDone(); }; return ( What it is {view.description} {`Saved as ${view.key}, for your runs of ${view.bot_name} only.`} Why (written by the bot) {view.reason} {open ? ( setValue(e.target.value)} /> ) : ( {view.status == "saved" ? `Saved. You can replace or remove it in Settings, under Secrets.` : `Declined. Nothing was saved.`} )} {error ? {error} : null} {open ? ( ) : null} ); }