/* eslint-disable react/no-danger */ import type { Dispatch, SetStateAction } from "react"; import { useState } from "react"; import { classNames } from "@calcom/lib"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { markdownToSafeHTML } from "@calcom/lib/markdownToSafeHTML"; import { Icon, SkeletonContainer, SkeletonText } from "@calcom/ui"; import { getFormattedCitations, handleAiChat, optionallyAddBaseUrl } from "../mintlify-chat/util"; interface MintlifyChatProps { searchText: string; aiResponse: string; setAiResponse: Dispatch>; } export const MintlifyChat = ({ searchText, aiResponse, setAiResponse }: MintlifyChatProps) => { const { t } = useLocale(); const [topicId, setTopicId] = useState(""); const [baseUrl, setBaseUrl] = useState(process.env.NEXT_PUBLIC_DOCS_URL ?? ""); const [isGenerating, setIsGenerating] = useState(false); const [error, setError] = useState(""); const onChunkReceived = (chunk: string, baseUrl?: string, finalChunk?: boolean) => { setAiResponse((prev) => { return prev + chunk; }); if (baseUrl) { setBaseUrl(baseUrl); } if (finalChunk) { setIsGenerating(false); } }; const citations = getFormattedCitations(aiResponse.split("||")[1]) ?? []; const answer = aiResponse.split("||")[0] ?? ""; return ( <>
{ if (isGenerating) return; setIsGenerating(true); setAiResponse(""); setError(""); const { id, error } = await handleAiChat(onChunkReceived, searchText, topicId); if (id) { setTopicId(id); } else if (error) { setIsGenerating(false); setError(error); } }} className={classNames( "hover:bg-subtle flex items-center gap-3 px-4 py-2 transition", isGenerating ? "cursor-not-allowed" : "cursor-pointer" )}>
{t("can_you_tell_me_about")} {searchText}
{t("use_ai_to_answer_your_questions")}
{error &&

{error}

} {isGenerating && aiResponse === "" ? ( ) : ( <>
{baseUrl && citations.map((citation) => { if (citation.title) { const url = optionallyAddBaseUrl(baseUrl, citation.url); return (
{citation.title}
); } return null; })}
)}
); };