import CopyToClipboard from '@/components/copy-to-clipboard'; import HightLightMarkdown from '@/components/highlight-markdown'; import { SelectWithSearch } from '@/components/originui/select-with-search'; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form'; import { Label } from '@/components/ui/label'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { Switch } from '@/components/ui/switch'; import { SharedFrom } from '@/constants/chat'; import { LanguageAbbreviation, LanguageAbbreviationMap, } from '@/constants/common'; import { useTranslate } from '@/hooks/common-hooks'; import { IModalProps } from '@/interfaces/common'; import { Routes } from '@/routes'; import { zodResolver } from '@hookform/resolvers/zod'; import { memo, useCallback, useMemo } from 'react'; import { useForm, useWatch } from 'react-hook-form'; import { z } from 'zod'; const FormSchema = z.object({ visibleAvatar: z.boolean(), locale: z.string(), embedType: z.enum(['fullscreen', 'widget']), enableStreaming: z.boolean(), }); type IProps = IModalProps & { token: string; from: SharedFrom; beta: string; isAgent: boolean; }; function EmbedDialog({ hideModal, token = '', from, beta = '', isAgent, }: IProps) { const { t } = useTranslate('chat'); const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { visibleAvatar: false, locale: '', embedType: 'fullscreen' as const, enableStreaming: false, }, }); const values = useWatch({ control: form.control }); const languageOptions = useMemo(() => { return Object.values(LanguageAbbreviation).map((x) => ({ label: LanguageAbbreviationMap[x], value: x, })); }, []); const generateIframeSrc = useCallback(() => { const { visibleAvatar, locale, embedType, enableStreaming } = values; const baseRoute = embedType === 'widget' ? Routes.ChatWidget : from === SharedFrom.Agent ? Routes.AgentShare : Routes.ChatShare; let src = `${location.origin}${baseRoute}?shared_id=${token}&from=${from}&auth=${beta}`; if (visibleAvatar) { src += '&visible_avatar=1'; } if (locale) { src += `&locale=${locale}`; } if (enableStreaming) { src += '&streaming=true'; } return src; }, [beta, from, token, values]); const text = useMemo(() => { const iframeSrc = generateIframeSrc(); const { embedType } = values; if (embedType === 'widget') { const { enableStreaming } = values; const streamingParam = enableStreaming ? '&streaming=true' : '&streaming=false'; return ` ~~~ html ~~~ `; } else { return ` ~~~ html ~~~ `; } }, [generateIframeSrc, values]); return ( {t('embedIntoSite', { keyPrefix: 'common' })}
( Embed Type
)} /> ( {t('avatarHidden')} )} /> {values.embedType === 'widget' && ( ( Enable Streaming Responses )} /> )} ( {t('locale')} )} />
{t('embedCode', { keyPrefix: 'search' })}
{text}
{t(isAgent ? 'flow' : 'chat', { keyPrefix: 'header' })} ID
{token}
{t('howUseId', { keyPrefix: isAgent ? 'flow' : 'chat' })}
); } export default memo(EmbedDialog);