diff --git a/client/src/components/Artifacts/Artifacts.tsx b/client/src/components/Artifacts/Artifacts.tsx index ac01d6bf42..80fbd440de 100644 --- a/client/src/components/Artifacts/Artifacts.tsx +++ b/client/src/components/Artifacts/Artifacts.tsx @@ -61,7 +61,7 @@ export function ArtifactPreview({ } export default function Artifacts() { - const { isSubmitting } = useChatContext(); + const { isSubmitting, latestMessage } = useChatContext(); const [isVisible, setIsVisible] = useState(false); const [activeTab, setActiveTab] = useState('code'); @@ -70,11 +70,34 @@ export default function Artifacts() { const [currentArtifactId, setCurrentArtifactId] = useState(null); const [orderedArtifactIds, setOrderedArtifactIds] = useState([]); const prevArtifactsRef = useRef({}); + const lastRunMessageIdRef = useRef(null); + const lastContentRef = useRef(null); useEffect(() => { setIsVisible(true); }, []); + useEffect(() => { + if (latestMessage?.messageId !== lastRunMessageIdRef.current) { + const currentArtifact = currentArtifactId != null ? artifacts[currentArtifactId] : null; + const currentContent = currentArtifact?.content ?? null; + + if ( + isSubmitting && + currentArtifactId != null && + activeTab !== 'code' && + currentContent !== lastContentRef.current + ) { + setActiveTab('code'); + lastContentRef.current = currentContent; + } else if (!isSubmitting && currentArtifactId == null && orderedArtifactIds.length > 0) { + setCurrentArtifactId(orderedArtifactIds[0]); + } + + lastRunMessageIdRef.current = latestMessage?.messageId ?? null; + } + }, [activeTab, currentArtifactId, isSubmitting, latestMessage, orderedArtifactIds, artifacts]); + useEffect(() => { const artifactIds = Object.keys(artifacts); const newArtifacts = artifactIds.filter((id) => !orderedArtifactIds.includes(id));