import { useEffect, useRef } from 'react'; import debounce from 'lodash/debounce'; import { useLocation } from 'react-router-dom'; import { useRecoilState, useSetRecoilState, useResetRecoilState } from 'recoil'; import type { Artifact } from '~/common'; import FilePreview from '~/components/Chat/Input/Files/FilePreview'; import { cn, getFileType, logger } from '~/utils'; import { useLocalize } from '~/hooks'; import store from '~/store'; const ArtifactButton = ({ artifact }: { artifact: Artifact | null }) => { const localize = useLocalize(); const location = useLocation(); const setVisible = useSetRecoilState(store.artifactsVisibility); const [artifacts, setArtifacts] = useRecoilState(store.artifactsState); const [currentArtifactId, setCurrentArtifactId] = useRecoilState(store.currentArtifactId); const resetCurrentArtifactId = useResetRecoilState(store.currentArtifactId); const isSelected = artifact?.id === currentArtifactId; const [visibleArtifacts, setVisibleArtifacts] = useRecoilState(store.visibleArtifacts); const debouncedSetVisibleRef = useRef( debounce((artifactToSet: Artifact) => { logger.log( 'artifacts_visibility', 'Setting artifact to visible state from Artifact button', artifactToSet, ); setVisibleArtifacts((prev) => ({ ...prev, [artifactToSet.id]: artifactToSet, })); }, 750), ); useEffect(() => { if (artifact == null || artifact?.id == null || artifact.id === '') { return; } if (!location.pathname.includes('/c/')) { return; } const debouncedSetVisible = debouncedSetVisibleRef.current; debouncedSetVisible(artifact); return () => { debouncedSetVisible.cancel(); }; }, [artifact, location.pathname]); if (artifact === null || artifact === undefined) { return null; } const fileType = getFileType('artifact'); return (