2025-03-11 15:33:18 -07:00
|
|
|
import type { Attachment, Message } from 'ai';
|
2024-11-01 15:31:54 +05:30
|
|
|
import { formatDistance } from 'date-fns';
|
2024-10-30 16:01:24 +05:30
|
|
|
import { AnimatePresence, motion } from 'framer-motion';
|
|
|
|
|
import {
|
2024-11-15 12:18:17 -05:00
|
|
|
type Dispatch,
|
2024-12-03 17:49:38 +03:00
|
|
|
memo,
|
2024-11-15 12:18:17 -05:00
|
|
|
type SetStateAction,
|
2024-10-30 16:01:24 +05:30
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
|
|
|
|
useState,
|
|
|
|
|
} from 'react';
|
|
|
|
|
import useSWR, { useSWRConfig } from 'swr';
|
2024-12-03 17:49:38 +03:00
|
|
|
import { useDebounceCallback, useWindowSize } from 'usehooks-ts';
|
2025-01-27 14:19:47 +05:30
|
|
|
import type { Document, Vote } from '@/lib/db/schema';
|
2025-01-27 19:23:16 +05:30
|
|
|
import { fetcher } from '@/lib/utils';
|
2024-10-30 16:01:24 +05:30
|
|
|
import { MultimodalInput } from './multimodal-input';
|
|
|
|
|
import { Toolbar } from './toolbar';
|
2024-11-15 10:18:31 -05:00
|
|
|
import { VersionFooter } from './version-footer';
|
2025-02-13 08:25:57 -08:00
|
|
|
import { ArtifactActions } from './artifact-actions';
|
|
|
|
|
import { ArtifactCloseButton } from './artifact-close-button';
|
|
|
|
|
import { ArtifactMessages } from './artifact-messages';
|
2024-12-19 17:05:04 +05:30
|
|
|
import { useSidebar } from './ui/sidebar';
|
2025-02-13 08:25:57 -08:00
|
|
|
import { useArtifact } from '@/hooks/use-artifact';
|
|
|
|
|
import { imageArtifact } from '@/artifacts/image/client';
|
|
|
|
|
import { codeArtifact } from '@/artifacts/code/client';
|
|
|
|
|
import { sheetArtifact } from '@/artifacts/sheet/client';
|
|
|
|
|
import { textArtifact } from '@/artifacts/text/client';
|
2025-01-27 19:23:16 +05:30
|
|
|
import equal from 'fast-deep-equal';
|
2025-03-11 15:33:18 -07:00
|
|
|
import { UseChatHelpers } from '@ai-sdk/react';
|
2024-12-16 18:14:40 +05:30
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
export const artifactDefinitions = [
|
|
|
|
|
textArtifact,
|
|
|
|
|
codeArtifact,
|
|
|
|
|
imageArtifact,
|
|
|
|
|
sheetArtifact,
|
|
|
|
|
];
|
|
|
|
|
export type ArtifactKind = (typeof artifactDefinitions)[number]['kind'];
|
2024-12-03 17:49:38 +03:00
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
export interface UIArtifact {
|
2024-10-30 16:01:24 +05:30
|
|
|
title: string;
|
|
|
|
|
documentId: string;
|
2025-02-13 08:25:57 -08:00
|
|
|
kind: ArtifactKind;
|
2024-10-30 16:01:24 +05:30
|
|
|
content: string;
|
|
|
|
|
isVisible: boolean;
|
|
|
|
|
status: 'streaming' | 'idle';
|
|
|
|
|
boundingBox: {
|
|
|
|
|
top: number;
|
|
|
|
|
left: number;
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
function PureArtifact({
|
2024-11-05 17:15:51 +03:00
|
|
|
chatId,
|
2024-10-30 16:01:24 +05:30
|
|
|
input,
|
|
|
|
|
setInput,
|
|
|
|
|
handleSubmit,
|
2025-03-11 15:33:18 -07:00
|
|
|
status,
|
2024-10-30 16:01:24 +05:30
|
|
|
stop,
|
|
|
|
|
attachments,
|
|
|
|
|
setAttachments,
|
|
|
|
|
append,
|
|
|
|
|
messages,
|
|
|
|
|
setMessages,
|
2024-12-05 15:29:33 +03:00
|
|
|
reload,
|
2024-11-05 17:15:51 +03:00
|
|
|
votes,
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly,
|
2024-10-30 16:01:24 +05:30
|
|
|
}: {
|
2024-11-05 17:15:51 +03:00
|
|
|
chatId: string;
|
2024-10-30 16:01:24 +05:30
|
|
|
input: string;
|
2025-03-11 15:33:18 -07:00
|
|
|
setInput: UseChatHelpers['setInput'];
|
|
|
|
|
status: UseChatHelpers['status'];
|
|
|
|
|
stop: UseChatHelpers['stop'];
|
2024-10-30 16:01:24 +05:30
|
|
|
attachments: Array<Attachment>;
|
|
|
|
|
setAttachments: Dispatch<SetStateAction<Array<Attachment>>>;
|
|
|
|
|
messages: Array<Message>;
|
|
|
|
|
setMessages: Dispatch<SetStateAction<Array<Message>>>;
|
2024-11-05 17:15:51 +03:00
|
|
|
votes: Array<Vote> | undefined;
|
2025-03-11 15:33:18 -07:00
|
|
|
append: UseChatHelpers['append'];
|
|
|
|
|
handleSubmit: UseChatHelpers['handleSubmit'];
|
|
|
|
|
reload: UseChatHelpers['reload'];
|
2024-12-06 13:36:56 +03:00
|
|
|
isReadonly: boolean;
|
2024-10-30 16:01:24 +05:30
|
|
|
}) {
|
2025-02-13 08:25:57 -08:00
|
|
|
const { artifact, setArtifact, metadata, setMetadata } = useArtifact();
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
const {
|
|
|
|
|
data: documents,
|
|
|
|
|
isLoading: isDocumentsFetching,
|
|
|
|
|
mutate: mutateDocuments,
|
|
|
|
|
} = useSWR<Array<Document>>(
|
2025-02-13 08:25:57 -08:00
|
|
|
artifact.documentId !== 'init' && artifact.status !== 'streaming'
|
|
|
|
|
? `/api/document?id=${artifact.documentId}`
|
2024-10-30 16:01:24 +05:30
|
|
|
: null,
|
2024-11-15 13:00:15 -05:00
|
|
|
fetcher,
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const [mode, setMode] = useState<'edit' | 'diff'>('edit');
|
|
|
|
|
const [document, setDocument] = useState<Document | null>(null);
|
|
|
|
|
const [currentVersionIndex, setCurrentVersionIndex] = useState(-1);
|
|
|
|
|
|
2024-12-19 17:05:04 +05:30
|
|
|
const { open: isSidebarOpen } = useSidebar();
|
|
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
useEffect(() => {
|
|
|
|
|
if (documents && documents.length > 0) {
|
|
|
|
|
const mostRecentDocument = documents.at(-1);
|
|
|
|
|
|
|
|
|
|
if (mostRecentDocument) {
|
|
|
|
|
setDocument(mostRecentDocument);
|
|
|
|
|
setCurrentVersionIndex(documents.length - 1);
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact((currentArtifact) => ({
|
|
|
|
|
...currentArtifact,
|
2024-11-01 15:31:54 +05:30
|
|
|
content: mostRecentDocument.content ?? '',
|
|
|
|
|
}));
|
2024-10-30 16:01:24 +05:30
|
|
|
}
|
|
|
|
|
}
|
2025-02-13 08:25:57 -08:00
|
|
|
}, [documents, setArtifact]);
|
2024-10-30 16:01:24 +05:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
mutateDocuments();
|
2025-02-13 08:25:57 -08:00
|
|
|
}, [artifact.status, mutateDocuments]);
|
2024-10-30 16:01:24 +05:30
|
|
|
|
|
|
|
|
const { mutate } = useSWRConfig();
|
|
|
|
|
const [isContentDirty, setIsContentDirty] = useState(false);
|
|
|
|
|
|
|
|
|
|
const handleContentChange = useCallback(
|
|
|
|
|
(updatedContent: string) => {
|
2025-02-13 08:25:57 -08:00
|
|
|
if (!artifact) return;
|
2024-10-30 16:01:24 +05:30
|
|
|
|
|
|
|
|
mutate<Array<Document>>(
|
2025-02-13 08:25:57 -08:00
|
|
|
`/api/document?id=${artifact.documentId}`,
|
2024-10-30 16:01:24 +05:30
|
|
|
async (currentDocuments) => {
|
|
|
|
|
if (!currentDocuments) return undefined;
|
|
|
|
|
|
|
|
|
|
const currentDocument = currentDocuments.at(-1);
|
|
|
|
|
|
|
|
|
|
if (!currentDocument || !currentDocument.content) {
|
|
|
|
|
setIsContentDirty(false);
|
|
|
|
|
return currentDocuments;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentDocument.content !== updatedContent) {
|
2025-02-13 08:25:57 -08:00
|
|
|
await fetch(`/api/document?id=${artifact.documentId}`, {
|
2024-10-30 16:01:24 +05:30
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify({
|
2025-02-13 08:25:57 -08:00
|
|
|
title: artifact.title,
|
2024-10-30 16:01:24 +05:30
|
|
|
content: updatedContent,
|
2025-02-13 08:25:57 -08:00
|
|
|
kind: artifact.kind,
|
2024-10-30 16:01:24 +05:30
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setIsContentDirty(false);
|
|
|
|
|
|
|
|
|
|
const newDocument = {
|
|
|
|
|
...currentDocument,
|
|
|
|
|
content: updatedContent,
|
|
|
|
|
createdAt: new Date(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return [...currentDocuments, newDocument];
|
|
|
|
|
}
|
2024-11-15 12:18:17 -05:00
|
|
|
return currentDocuments;
|
2024-10-30 16:01:24 +05:30
|
|
|
},
|
2024-11-15 13:00:15 -05:00
|
|
|
{ revalidate: false },
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
},
|
2025-02-13 08:25:57 -08:00
|
|
|
[artifact, mutate],
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
|
2024-11-01 15:31:54 +05:30
|
|
|
const debouncedHandleContentChange = useDebounceCallback(
|
|
|
|
|
handleContentChange,
|
2024-11-15 13:00:15 -05:00
|
|
|
2000,
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
|
2024-11-01 15:31:54 +05:30
|
|
|
const saveContent = useCallback(
|
2024-10-30 20:36:45 +05:30
|
|
|
(updatedContent: string, debounce: boolean) => {
|
2024-10-30 16:01:24 +05:30
|
|
|
if (document && updatedContent !== document.content) {
|
2024-11-01 15:31:54 +05:30
|
|
|
setIsContentDirty(true);
|
|
|
|
|
|
2024-10-30 20:36:45 +05:30
|
|
|
if (debounce) {
|
2024-11-01 15:31:54 +05:30
|
|
|
debouncedHandleContentChange(updatedContent);
|
2024-10-30 20:36:45 +05:30
|
|
|
} else {
|
|
|
|
|
handleContentChange(updatedContent);
|
|
|
|
|
}
|
2024-10-30 16:01:24 +05:30
|
|
|
}
|
|
|
|
|
},
|
2024-11-15 13:00:15 -05:00
|
|
|
[document, debouncedHandleContentChange, handleContentChange],
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function getDocumentContentById(index: number) {
|
|
|
|
|
if (!documents) return '';
|
|
|
|
|
if (!documents[index]) return '';
|
|
|
|
|
return documents[index].content ?? '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleVersionChange = (type: 'next' | 'prev' | 'toggle' | 'latest') => {
|
|
|
|
|
if (!documents) return;
|
|
|
|
|
|
|
|
|
|
if (type === 'latest') {
|
|
|
|
|
setCurrentVersionIndex(documents.length - 1);
|
|
|
|
|
setMode('edit');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === 'toggle') {
|
|
|
|
|
setMode((mode) => (mode === 'edit' ? 'diff' : 'edit'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === 'prev') {
|
|
|
|
|
if (currentVersionIndex > 0) {
|
|
|
|
|
setCurrentVersionIndex((index) => index - 1);
|
|
|
|
|
}
|
|
|
|
|
} else if (type === 'next') {
|
|
|
|
|
if (currentVersionIndex < documents.length - 1) {
|
|
|
|
|
setCurrentVersionIndex((index) => index + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [isToolbarVisible, setIsToolbarVisible] = useState(false);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* NOTE: if there are no documents, or if
|
|
|
|
|
* the documents are being fetched, then
|
|
|
|
|
* we mark it as the current version.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const isCurrentVersion =
|
2024-11-01 15:31:54 +05:30
|
|
|
documents && documents.length > 0
|
|
|
|
|
? currentVersionIndex === documents.length - 1
|
|
|
|
|
: true;
|
2024-10-30 16:01:24 +05:30
|
|
|
|
|
|
|
|
const { width: windowWidth, height: windowHeight } = useWindowSize();
|
|
|
|
|
const isMobile = windowWidth ? windowWidth < 768 : false;
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
const artifactDefinition = artifactDefinitions.find(
|
|
|
|
|
(definition) => definition.kind === artifact.kind,
|
2025-01-27 14:19:47 +05:30
|
|
|
);
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
if (!artifactDefinition) {
|
|
|
|
|
throw new Error('Artifact definition not found!');
|
2025-01-27 14:19:47 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-02-13 08:25:57 -08:00
|
|
|
if (artifact.documentId !== 'init') {
|
|
|
|
|
if (artifactDefinition.initialize) {
|
|
|
|
|
artifactDefinition.initialize({
|
|
|
|
|
documentId: artifact.documentId,
|
2025-01-27 19:23:16 +05:30
|
|
|
setMetadata,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-01-27 14:19:47 +05:30
|
|
|
}
|
2025-02-13 08:25:57 -08:00
|
|
|
}, [artifact.documentId, artifactDefinition, setMetadata]);
|
2025-01-27 14:19:47 +05:30
|
|
|
|
2024-10-30 16:01:24 +05:30
|
|
|
return (
|
2024-12-19 17:05:04 +05:30
|
|
|
<AnimatePresence>
|
2025-02-13 08:25:57 -08:00
|
|
|
{artifact.isVisible && (
|
2024-10-30 16:01:24 +05:30
|
|
|
<motion.div
|
2025-03-11 14:39:36 -07:00
|
|
|
data-testid="artifact"
|
2024-12-19 17:05:04 +05:30
|
|
|
className="flex flex-row h-dvh w-dvw fixed top-0 left-0 z-50 bg-transparent"
|
|
|
|
|
initial={{ opacity: 1 }}
|
|
|
|
|
animate={{ opacity: 1 }}
|
|
|
|
|
exit={{ opacity: 0, transition: { delay: 0.4 } }}
|
2024-10-30 16:01:24 +05:30
|
|
|
>
|
2024-12-19 17:05:04 +05:30
|
|
|
{!isMobile && (
|
|
|
|
|
<motion.div
|
|
|
|
|
className="fixed bg-background h-dvh"
|
|
|
|
|
initial={{
|
|
|
|
|
width: isSidebarOpen ? windowWidth - 256 : windowWidth,
|
|
|
|
|
right: 0,
|
|
|
|
|
}}
|
|
|
|
|
animate={{ width: windowWidth, right: 0 }}
|
|
|
|
|
exit={{
|
|
|
|
|
width: isSidebarOpen ? windowWidth - 256 : windowWidth,
|
|
|
|
|
right: 0,
|
|
|
|
|
}}
|
2024-12-03 17:49:38 +03:00
|
|
|
/>
|
2024-12-19 17:05:04 +05:30
|
|
|
)}
|
2024-10-30 16:01:24 +05:30
|
|
|
|
2024-12-19 17:05:04 +05:30
|
|
|
{!isMobile && (
|
|
|
|
|
<motion.div
|
|
|
|
|
className="relative w-[400px] bg-muted dark:bg-background h-dvh shrink-0"
|
|
|
|
|
initial={{ opacity: 0, x: 10, scale: 1 }}
|
|
|
|
|
animate={{
|
2024-10-30 16:01:24 +05:30
|
|
|
opacity: 1,
|
|
|
|
|
x: 0,
|
2024-12-19 17:05:04 +05:30
|
|
|
scale: 1,
|
2024-10-30 16:01:24 +05:30
|
|
|
transition: {
|
2024-12-19 17:05:04 +05:30
|
|
|
delay: 0.2,
|
2024-10-30 16:01:24 +05:30
|
|
|
type: 'spring',
|
|
|
|
|
stiffness: 200,
|
|
|
|
|
damping: 30,
|
|
|
|
|
},
|
2024-12-19 17:05:04 +05:30
|
|
|
}}
|
|
|
|
|
exit={{
|
|
|
|
|
opacity: 0,
|
|
|
|
|
x: 0,
|
|
|
|
|
scale: 1,
|
|
|
|
|
transition: { duration: 0 },
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<AnimatePresence>
|
|
|
|
|
{!isCurrentVersion && (
|
|
|
|
|
<motion.div
|
|
|
|
|
className="left-0 absolute h-dvh w-[400px] top-0 bg-zinc-900/50 z-50"
|
|
|
|
|
initial={{ opacity: 0 }}
|
|
|
|
|
animate={{ opacity: 1 }}
|
|
|
|
|
exit={{ opacity: 0 }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
|
|
|
|
|
|
|
|
|
<div className="flex flex-col h-full justify-between items-center gap-4">
|
2025-02-13 08:25:57 -08:00
|
|
|
<ArtifactMessages
|
2024-12-19 17:05:04 +05:30
|
|
|
chatId={chatId}
|
2025-03-11 15:33:18 -07:00
|
|
|
status={status}
|
2024-12-19 17:05:04 +05:30
|
|
|
votes={votes}
|
|
|
|
|
messages={messages}
|
|
|
|
|
setMessages={setMessages}
|
|
|
|
|
reload={reload}
|
|
|
|
|
isReadonly={isReadonly}
|
2025-02-13 08:25:57 -08:00
|
|
|
artifactStatus={artifact.status}
|
2024-12-19 17:05:04 +05:30
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<form className="flex flex-row gap-2 relative items-end w-full px-4 pb-4">
|
|
|
|
|
<MultimodalInput
|
|
|
|
|
chatId={chatId}
|
|
|
|
|
input={input}
|
|
|
|
|
setInput={setInput}
|
|
|
|
|
handleSubmit={handleSubmit}
|
2025-03-11 15:33:18 -07:00
|
|
|
status={status}
|
2024-12-19 17:05:04 +05:30
|
|
|
stop={stop}
|
|
|
|
|
attachments={attachments}
|
|
|
|
|
setAttachments={setAttachments}
|
|
|
|
|
messages={messages}
|
|
|
|
|
append={append}
|
|
|
|
|
className="bg-background dark:bg-muted"
|
|
|
|
|
setMessages={setMessages}
|
|
|
|
|
/>
|
|
|
|
|
</form>
|
2024-10-30 16:01:24 +05:30
|
|
|
</div>
|
2024-12-19 17:05:04 +05:30
|
|
|
</motion.div>
|
|
|
|
|
)}
|
2024-10-30 16:01:24 +05:30
|
|
|
|
2024-12-19 17:05:04 +05:30
|
|
|
<motion.div
|
2025-01-15 20:12:14 +05:30
|
|
|
className="fixed dark:bg-muted bg-background h-dvh flex flex-col overflow-y-scroll md:border-l dark:border-zinc-700 border-zinc-200"
|
2024-12-19 17:05:04 +05:30
|
|
|
initial={
|
|
|
|
|
isMobile
|
|
|
|
|
? {
|
|
|
|
|
opacity: 1,
|
2025-02-13 08:25:57 -08:00
|
|
|
x: artifact.boundingBox.left,
|
|
|
|
|
y: artifact.boundingBox.top,
|
|
|
|
|
height: artifact.boundingBox.height,
|
|
|
|
|
width: artifact.boundingBox.width,
|
2024-12-19 17:05:04 +05:30
|
|
|
borderRadius: 50,
|
|
|
|
|
}
|
|
|
|
|
: {
|
|
|
|
|
opacity: 1,
|
2025-02-13 08:25:57 -08:00
|
|
|
x: artifact.boundingBox.left,
|
|
|
|
|
y: artifact.boundingBox.top,
|
|
|
|
|
height: artifact.boundingBox.height,
|
|
|
|
|
width: artifact.boundingBox.width,
|
2024-12-19 17:05:04 +05:30
|
|
|
borderRadius: 50,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
animate={
|
|
|
|
|
isMobile
|
|
|
|
|
? {
|
|
|
|
|
opacity: 1,
|
|
|
|
|
x: 0,
|
|
|
|
|
y: 0,
|
|
|
|
|
height: windowHeight,
|
|
|
|
|
width: windowWidth ? windowWidth : 'calc(100dvw)',
|
|
|
|
|
borderRadius: 0,
|
|
|
|
|
transition: {
|
|
|
|
|
delay: 0,
|
|
|
|
|
type: 'spring',
|
|
|
|
|
stiffness: 200,
|
|
|
|
|
damping: 30,
|
|
|
|
|
duration: 5000,
|
2024-11-15 13:00:15 -05:00
|
|
|
},
|
2024-12-19 17:05:04 +05:30
|
|
|
}
|
|
|
|
|
: {
|
|
|
|
|
opacity: 1,
|
|
|
|
|
x: 400,
|
|
|
|
|
y: 0,
|
|
|
|
|
height: windowHeight,
|
|
|
|
|
width: windowWidth
|
|
|
|
|
? windowWidth - 400
|
|
|
|
|
: 'calc(100dvw-400px)',
|
|
|
|
|
borderRadius: 0,
|
|
|
|
|
transition: {
|
|
|
|
|
delay: 0,
|
|
|
|
|
type: 'spring',
|
|
|
|
|
stiffness: 200,
|
|
|
|
|
damping: 30,
|
|
|
|
|
duration: 5000,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
exit={{
|
|
|
|
|
opacity: 0,
|
|
|
|
|
scale: 0.5,
|
|
|
|
|
transition: {
|
|
|
|
|
delay: 0.1,
|
|
|
|
|
type: 'spring',
|
|
|
|
|
stiffness: 600,
|
|
|
|
|
damping: 30,
|
|
|
|
|
},
|
|
|
|
|
}}
|
2024-12-16 18:14:40 +05:30
|
|
|
>
|
2024-12-19 17:05:04 +05:30
|
|
|
<div className="p-2 flex flex-row justify-between items-start">
|
|
|
|
|
<div className="flex flex-row gap-4 items-start">
|
2025-02-13 08:25:57 -08:00
|
|
|
<ArtifactCloseButton />
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
<div className="flex flex-col">
|
2025-02-13 08:25:57 -08:00
|
|
|
<div className="font-medium">{artifact.title}</div>
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
{isContentDirty ? (
|
|
|
|
|
<div className="text-sm text-muted-foreground">
|
|
|
|
|
Saving changes...
|
|
|
|
|
</div>
|
|
|
|
|
) : document ? (
|
|
|
|
|
<div className="text-sm text-muted-foreground">
|
|
|
|
|
{`Updated ${formatDistance(
|
|
|
|
|
new Date(document.createdAt),
|
|
|
|
|
new Date(),
|
|
|
|
|
{
|
|
|
|
|
addSuffix: true,
|
|
|
|
|
},
|
|
|
|
|
)}`}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="w-32 h-3 mt-2 bg-muted-foreground/20 rounded-md animate-pulse" />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
<ArtifactActions
|
|
|
|
|
artifact={artifact}
|
2024-10-30 16:01:24 +05:30
|
|
|
currentVersionIndex={currentVersionIndex}
|
2024-12-19 17:05:04 +05:30
|
|
|
handleVersionChange={handleVersionChange}
|
|
|
|
|
isCurrentVersion={isCurrentVersion}
|
|
|
|
|
mode={mode}
|
2025-01-27 14:19:47 +05:30
|
|
|
metadata={metadata}
|
|
|
|
|
setMetadata={setMetadata}
|
2024-10-30 16:01:24 +05:30
|
|
|
/>
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
2024-10-30 16:01:24 +05:30
|
|
|
|
2025-01-27 19:23:16 +05:30
|
|
|
<div className="dark:bg-muted bg-background h-full overflow-y-scroll !max-w-full items-center">
|
2025-02-13 08:25:57 -08:00
|
|
|
<artifactDefinition.content
|
|
|
|
|
title={artifact.title}
|
2025-01-27 19:23:16 +05:30
|
|
|
content={
|
|
|
|
|
isCurrentVersion
|
2025-02-13 08:25:57 -08:00
|
|
|
? artifact.content
|
2025-01-27 19:23:16 +05:30
|
|
|
: getDocumentContentById(currentVersionIndex)
|
|
|
|
|
}
|
|
|
|
|
mode={mode}
|
2025-02-13 08:25:57 -08:00
|
|
|
status={artifact.status}
|
2025-01-27 19:23:16 +05:30
|
|
|
currentVersionIndex={currentVersionIndex}
|
|
|
|
|
suggestions={[]}
|
|
|
|
|
onSaveContent={saveContent}
|
|
|
|
|
isInline={false}
|
|
|
|
|
isCurrentVersion={isCurrentVersion}
|
|
|
|
|
getDocumentContentById={getDocumentContentById}
|
2025-02-13 08:25:57 -08:00
|
|
|
isLoading={isDocumentsFetching && !artifact.content}
|
2025-01-27 19:23:16 +05:30
|
|
|
metadata={metadata}
|
|
|
|
|
setMetadata={setMetadata}
|
|
|
|
|
/>
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-01-27 19:23:16 +05:30
|
|
|
<AnimatePresence>
|
|
|
|
|
{isCurrentVersion && (
|
|
|
|
|
<Toolbar
|
|
|
|
|
isToolbarVisible={isToolbarVisible}
|
|
|
|
|
setIsToolbarVisible={setIsToolbarVisible}
|
|
|
|
|
append={append}
|
2025-03-11 15:33:18 -07:00
|
|
|
status={status}
|
2025-01-27 19:23:16 +05:30
|
|
|
stop={stop}
|
|
|
|
|
setMessages={setMessages}
|
2025-02-13 08:25:57 -08:00
|
|
|
artifactKind={artifact.kind}
|
2025-01-27 19:23:16 +05:30
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
2024-11-04 20:26:38 +03:00
|
|
|
|
|
|
|
|
<AnimatePresence>
|
2024-12-19 17:05:04 +05:30
|
|
|
{!isCurrentVersion && (
|
|
|
|
|
<VersionFooter
|
|
|
|
|
currentVersionIndex={currentVersionIndex}
|
|
|
|
|
documents={documents}
|
|
|
|
|
handleVersionChange={handleVersionChange}
|
2024-11-04 20:26:38 +03:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
2024-12-19 17:05:04 +05:30
|
|
|
</motion.div>
|
|
|
|
|
</motion.div>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
2024-10-30 16:01:24 +05:30
|
|
|
);
|
|
|
|
|
}
|
2024-12-03 17:49:38 +03:00
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
export const Artifact = memo(PureArtifact, (prevProps, nextProps) => {
|
2025-03-11 15:33:18 -07:00
|
|
|
if (prevProps.status !== nextProps.status) return false;
|
2024-12-19 17:05:04 +05:30
|
|
|
if (!equal(prevProps.votes, nextProps.votes)) return false;
|
|
|
|
|
if (prevProps.input !== nextProps.input) return false;
|
|
|
|
|
if (!equal(prevProps.messages, nextProps.messages.length)) return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
2024-12-03 17:49:38 +03:00
|
|
|
});
|