import { t } from "../lib/console-text"; import { useEffect, useId, useRef, useState } from "react"; import { MessageSquareIcon, ThumbsDownIcon, ThumbsUpIcon } from "lucide-react"; import type { OpenGeniClient } from "@opengeni/sdk"; import type { CreateFeedbackRequest, FeedbackSentiment } from "@opengeni/sdk"; import { Button } from "../../ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../../ui/dialog"; import { Label } from "../../ui/label"; import { Textarea } from "../../ui/textarea"; import { userErrorText } from "../../ui/api-error"; import { feedbackDialogCopy, feedbackSentimentLabel, feedbackSubmission, } from "../../session-feedback-model"; type FeedbackClient = Pick; export function FeedbackDialog(props: { client: FeedbackClient; workspaceId: string; sessionId?: string | undefined; turnId?: string | undefined; sentiment?: FeedbackSentiment | undefined; open: boolean; onOpenChange: (open: boolean) => void; onSubmitted?: ((sentiment: FeedbackSentiment | undefined) => void) | undefined; }) { const [comment, setComment] = useState(""); const [busy, setBusy] = useState(false); const [submissionError, setError] = useState(null); const pending = useRef(null); const submitting = useRef(false); const fieldId = useId(); const copy = feedbackDialogCopy(t, props); async function submit() { if (submitting.current) return; pending.current = feedbackSubmission( { sessionId: props.sessionId, turnId: props.turnId, sentiment: props.sentiment, comment: comment || undefined, }, pending.current, () => crypto.randomUUID(), ); submitting.current = true; setBusy(true); setError(null); try { await props.client.createFeedback(props.workspaceId, pending.current!); pending.current = null; setComment(""); props.onSubmitted?.(props.sentiment); props.onOpenChange(false); } catch (error) { setError(`Couldn't send feedback. ${userErrorText(error, "Try again.")}`); } finally { submitting.current = false; setBusy(false); } } return ( { if (!busy) props.onOpenChange(open); }} > {copy.title} {copy.description} {props.sentiment ? (

{feedbackSentimentLabel(t, props.sentiment)}

) : null}