"use client"; import { TagInput, type Tag as TagType } from "emblor"; import { useAtomValue, useSetAtom } from "jotai"; import { XIcon } from "lucide-react"; import { useCallback, useEffect, useRef, useState } from "react"; import { closeHitlEditPanelAtom, hitlEditPanelAtom, } from "@/atoms/chat/hitl-edit-panel.atom"; import type { ExtraField } from "@/atoms/chat/hitl-edit-panel.atom"; import { PlateEditor } from "@/components/editor/plate-editor"; import { Button } from "@/components/ui/button"; import { Drawer, DrawerContent, DrawerHandle, DrawerTitle } from "@/components/ui/drawer"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { useMediaQuery } from "@/hooks/use-media-query"; function parseEmailsToTags(value: string): TagType[] { if (!value.trim()) return []; return value .split(",") .map((s) => s.trim()) .filter(Boolean) .map((email, i) => ({ id: `${Date.now()}-${i}`, text: email })); } function tagsToEmailString(tags: TagType[]): string { return tags.map((t) => t.text).join(", "); } function EmailsTagField({ id, value, onChange, placeholder, }: { id: string; value: string; onChange: (value: string) => void; placeholder?: string; }) { const [tags, setTags] = useState(() => parseEmailsToTags(value)); const [activeTagIndex, setActiveTagIndex] = useState(null); const isInitialMount = useRef(true); useEffect(() => { if (isInitialMount.current) { isInitialMount.current = false; return; } onChange(tagsToEmailString(tags)); }, [tags, onChange]); const handleSetTags = useCallback( (newTags: TagType[] | ((prev: TagType[]) => TagType[])) => { setTags((prev) => typeof newTags === "function" ? newTags(prev) : newTags ); }, [] ); const handleAddTag = useCallback( (text: string) => { const trimmed = text.trim(); if (!trimmed) return; if (tags.some((tag) => tag.text === trimmed)) return; const newTag: TagType = { id: Date.now().toString(), text: trimmed }; setTags((prev) => [...prev, newTag]); }, [tags] ); return ( ); } export function HitlEditPanelContent({ title: initialTitle, content: initialContent, extraFields, onSave, onClose, showCloseButton = true, }: { title: string; content: string; toolName: string; extraFields?: ExtraField[]; onSave: (title: string, content: string, extraFieldValues?: Record) => void; onClose?: () => void; showCloseButton?: boolean; }) { const [editedTitle, setEditedTitle] = useState(initialTitle); const markdownRef = useRef(initialContent); const [isSaving, setIsSaving] = useState(false); const [extraFieldValues, setExtraFieldValues] = useState>(() => { if (!extraFields) return {}; const initial: Record = {}; for (const field of extraFields) { initial[field.key] = field.value; } return initial; }); const handleMarkdownChange = useCallback((md: string) => { markdownRef.current = md; }, []); const handleExtraFieldChange = useCallback((key: string, value: string) => { setExtraFieldValues((prev) => ({ ...prev, [key]: value })); }, []); const handleSave = useCallback(() => { if (!editedTitle.trim()) return; setIsSaving(true); const extras = extraFields && extraFields.length > 0 ? extraFieldValues : undefined; onSave(editedTitle, markdownRef.current, extras); onClose?.(); }, [editedTitle, onSave, onClose, extraFields, extraFieldValues]); return ( <>
setEditedTitle(e.target.value)} placeholder="Untitled" className="flex-1 min-w-0 bg-transparent text-sm font-semibold text-foreground outline-none placeholder:text-muted-foreground" aria-label="Page title" /> {onClose && showCloseButton && ( )}
{extraFields && extraFields.length > 0 && (
{extraFields.map((field) => (
{field.type === "emails" ? ( handleExtraFieldChange(field.key, v)} placeholder={`Add ${field.label.toLowerCase()}`} /> ) : field.type === "textarea" ? (