mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-24 20:30:13 +01:00
refactor: optimize ordering of artifacts and add latest artifact activation in Artifacts component
This commit is contained in:
parent
e2e42db24a
commit
068ec2fceb
1 changed files with 24 additions and 1 deletions
|
|
@ -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<string | null>(null);
|
||||
const [orderedArtifactIds, setOrderedArtifactIds] = useState<string[]>([]);
|
||||
const prevArtifactsRef = useRef({});
|
||||
const lastRunMessageIdRef = useRef<string | null>(null);
|
||||
const lastContentRef = useRef<string | null>(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));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue