import { PaperclipHorizontal } from "@phosphor-icons/react"; import { Tooltip } from "react-tooltip"; import { useTranslation } from "react-i18next"; import { useRef, useState, useEffect } from "react"; import { useParams } from "react-router-dom"; import Workspace from "@/models/workspace"; import { ATTACHMENTS_PROCESSED_EVENT, REMOVE_ATTACHMENT_EVENT, } from "../../DnDWrapper"; import { useTheme } from "@/hooks/useTheme"; import ParsedFilesMenu from "./ParsedFilesMenu"; /** * This is a simple proxy component that clicks on the DnD file uploader for the user. * @returns */ export default function AttachItem() { const { t } = useTranslation(); const { theme } = useTheme(); const { slug, threadSlug = null } = useParams(); const tooltipRef = useRef(null); const [isEmbedding, setIsEmbedding] = useState(false); const [files, setFiles] = useState([]); const [currentTokens, setCurrentTokens] = useState(0); const [contextWindow, setContextWindow] = useState(Infinity); const [showTooltip, setShowTooltip] = useState(false); const [isLoading, setIsLoading] = useState(true); const fetchFiles = () => { if (!slug) return; if (isEmbedding) return; setIsLoading(true); Workspace.getParsedFiles(slug, threadSlug) .then(({ files, contextWindow, currentContextTokenCount }) => { setFiles(files); setShowTooltip(files.length > 0); setContextWindow(contextWindow); setCurrentTokens(currentContextTokenCount); }) .finally(() => { setIsLoading(false); }); }; /** * Handles the removal of an attachment from the parsed files * and triggers a re-fetch of the parsed files. * This function handles when the user clicks the X on an Attachment via the AttachmentManager * so we need to sync the state in the ParsedFilesMenu picker here. */ async function handleRemoveAttachment(e) { const { document } = e.detail; await Workspace.deleteParsedFiles(slug, [document.id]); fetchFiles(); } /** * Handles the click event for the attach item button. * @param {MouseEvent} e - The click event. * @returns {void} */ function handleClick(e) { e?.target?.blur(); document?.getElementById("dnd-chat-file-uploader")?.click(); return; } useEffect(() => { fetchFiles(); window.addEventListener(ATTACHMENTS_PROCESSED_EVENT, fetchFiles); window.addEventListener(REMOVE_ATTACHMENT_EVENT, handleRemoveAttachment); return () => { window.removeEventListener(ATTACHMENTS_PROCESSED_EVENT, fetchFiles); window.removeEventListener( REMOVE_ATTACHMENT_EVENT, handleRemoveAttachment ); }; }, [slug, threadSlug]); return ( <> {showTooltip && ( )} ); }