import React, { useEffect, useState } from 'react'; import { BookOpenIcon, CircleCheckIcon, LoaderCircleIcon } from 'lucide-react'; import { useNavigate } from 'react-router-dom'; import { useCopilot } from '../../../context/copilot.context'; import { getTextEntryContent, pushTextEntry, } from '../../../service/documentation/documentation.service'; import type { TextEntryContent } from '../../../types/documentation'; import { Button } from '../../ui/button'; /** Said when the draft itself could not be read. */ const LOAD_ERROR = "We couldn't load that document here. Open your Knowledge Base to review it there."; /** Said when publishing did not land. Never carries a server message. */ const PUBLISH_ERROR = "We couldn't publish that document. Please try again, or open your Knowledge Base."; /** * The knowledge-base draft the copilot just wrote, opened in the conversation * that asked for it. Nothing is live until the merchant says so: they read it * here and publish it with one button, and the draft stays saved either way, * so declining costs nothing. * * @param props - Component props. * @param props.fileName - The draft's stored file name. * @return The card. */ export function KbDocumentCard({ fileName, }: { fileName: string; }): React.ReactElement { const copilot = useCopilot(); const navigate = useNavigate(); const token: string = copilot.token; const clientId: string = copilot.clientId; const [draft, setDraft] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isPublishing, setIsPublishing] = useState(false); const [isPublished, setIsPublished] = useState(false); const [errorMessage, setErrorMessage] = useState(null); useEffect(() => { if (!token || !clientId) return undefined; let stale = false; setIsLoading(true); setErrorMessage(null); void (async () => { const response = await getTextEntryContent(fileName, clientId, token); if (stale) return; const loaded = response?.data ?? null; setDraft(loaded && typeof loaded.content === 'string' ? loaded : null); if (!loaded) setErrorMessage(LOAD_ERROR); setIsLoading(false); setIsPublished(Boolean(loaded?.pushed_to_rag)); })(); return () => { stale = true; }; }, [fileName, token, clientId]); const publish = async (): Promise => { setIsPublishing(true); setErrorMessage(null); const response = await pushTextEntry(fileName, clientId, token); setIsPublishing(false); if (response?.data?.pushed) { setIsPublished(true); return; } setErrorMessage(PUBLISH_ERROR); }; return (

{draft?.title || 'Knowledge base document'}

Read it, and add it when it is right. Your assistant answers shoppers from what is in here.

{isLoading ? ( Loading the document... ) : (

{draft?.content}

)}
{isPublished ? ( In your Knowledge Base ) : ( )} {errorMessage && ( {errorMessage} )}
); }