import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import conversationService from '../api/services/conversationService'; import Spinner from '../assets/spinner.svg'; import Dropdown from '../components/Dropdown'; import ToggleSwitch from '../components/ToggleSwitch'; import { Doc } from '../models/misc'; import { selectChunks, selectPrompt, selectSelectedDocs, selectSourceDocs, selectToken, } from '../preferences/preferenceSlice'; import WrapperModal from './WrapperModal'; const apiHost = import.meta.env.VITE_API_HOST || 'https://docsapi.arc53.com'; const embeddingsName = import.meta.env.VITE_EMBEDDINGS_NAME || 'huggingface_sentence-transformers/all-mpnet-base-v2'; type StatusType = 'loading' | 'idle' | 'fetched' | 'failed'; export const ShareConversationModal = ({ close, conversationId, }: { close: () => void; conversationId: string; }) => { const { t } = useTranslation(); const token = useSelector(selectToken); const domain = window.location.origin; const [identifier, setIdentifier] = useState(null); const [isCopied, setIsCopied] = useState(false); const [status, setStatus] = useState('idle'); const [allowPrompt, setAllowPrompt] = useState(false); const sourceDocs = useSelector(selectSourceDocs); const preSelectedDoc = useSelector(selectSelectedDocs); const selectedPrompt = useSelector(selectPrompt); const selectedChunk = useSelector(selectChunks); const extractDocPaths = (docs: Doc[]) => docs ? docs .filter((doc) => doc.model === embeddingsName) .map((doc: Doc) => { return { label: doc.name, value: doc.id ?? 'default', }; }) : []; const [sourcePath, setSourcePath] = useState<{ label: string; value: string; } | null>(preSelectedDoc ? extractDocPaths(preSelectedDoc)[0] : null); const handleCopyKey = (url: string) => { navigator.clipboard.writeText(url); setIsCopied(true); }; const togglePromptPermission = () => { setAllowPrompt(!allowPrompt); setStatus('idle'); setIdentifier(null); }; const shareCoversationPublicly: (isPromptable: boolean) => void = ( isPromptable = false, ) => { setStatus('loading'); const payload: { conversation_id: string; chunks?: string; prompt_id?: string; source?: string; } = { conversation_id: conversationId }; if (isPromptable) { payload.chunks = selectedChunk; payload.prompt_id = selectedPrompt.id; sourcePath && (payload.source = sourcePath.value); } conversationService .shareConversation(isPromptable, payload, token) .then((res) => { return res.json(); }) .then((data) => { if (data.success && data.identifier) { setIdentifier(data.identifier); setStatus('fetched'); } else setStatus('failed'); }) .catch((err) => setStatus('failed')); }; return (

{t('modals.shareConv.label')}

{t('modals.shareConv.note')}

{t('modals.shareConv.option')}
{allowPrompt && (
setSourcePath(selection) } options={extractDocPaths(sourceDocs ?? [])} size="w-full" rounded="xl" />
)}
{`${domain}/share/${identifier ?? '....'}`} {status === 'fetched' ? ( ) : ( )}
); };