2025-09-21 12:03:29 +01:00
|
|
|
'use client';
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
import {
|
2025-09-20 12:47:10 -07:00
|
|
|
memo,
|
2025-09-21 12:03:29 +01:00
|
|
|
type MouseEvent,
|
2024-12-19 17:05:04 +05:30
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
|
|
|
|
useMemo,
|
|
|
|
|
useRef,
|
2025-09-21 12:03:29 +01:00
|
|
|
} from 'react';
|
|
|
|
|
import type { ArtifactKind, UIArtifact } from './artifact';
|
|
|
|
|
import { FileIcon, FullscreenIcon, ImageIcon, LoaderIcon } from './icons';
|
|
|
|
|
import { cn, fetcher } from '@/lib/utils';
|
|
|
|
|
import type { Document } from '@/lib/db/schema';
|
|
|
|
|
import { InlineDocumentSkeleton } from './document-skeleton';
|
|
|
|
|
import useSWR from 'swr';
|
|
|
|
|
import { Editor } from './text-editor';
|
|
|
|
|
import { DocumentToolCall, DocumentToolResult } from './document';
|
|
|
|
|
import { CodeEditor } from './code-editor';
|
|
|
|
|
import { useArtifact } from '@/hooks/use-artifact';
|
|
|
|
|
import equal from 'fast-deep-equal';
|
|
|
|
|
import { SpreadsheetEditor } from './sheet-editor';
|
|
|
|
|
import { ImageEditor } from './image-editor';
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
interface DocumentPreviewProps {
|
2024-12-19 17:05:04 +05:30
|
|
|
isReadonly: boolean;
|
|
|
|
|
result?: any;
|
|
|
|
|
args?: any;
|
2025-09-21 12:03:29 +01:00
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
export function DocumentPreview({
|
|
|
|
|
isReadonly,
|
|
|
|
|
result,
|
|
|
|
|
args,
|
|
|
|
|
}: DocumentPreviewProps) {
|
2025-02-13 08:25:57 -08:00
|
|
|
const { artifact, setArtifact } = useArtifact();
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
const { data: documents, isLoading: isDocumentsFetching } = useSWR<
|
2025-09-21 12:03:29 +01:00
|
|
|
Array<Document>
|
2024-12-19 17:05:04 +05:30
|
|
|
>(result ? `/api/document?id=${result.id}` : null, fetcher);
|
|
|
|
|
|
|
|
|
|
const previewDocument = useMemo(() => documents?.[0], [documents]);
|
|
|
|
|
const hitboxRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const boundingBox = hitboxRef.current?.getBoundingClientRect();
|
2025-02-03 13:33:48 +05:30
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
if (artifact.documentId && boundingBox) {
|
2025-09-21 12:03:29 +01:00
|
|
|
setArtifact((artifact) => ({
|
|
|
|
|
...artifact,
|
2024-12-19 17:05:04 +05:30
|
|
|
boundingBox: {
|
|
|
|
|
left: boundingBox.x,
|
|
|
|
|
top: boundingBox.y,
|
|
|
|
|
width: boundingBox.width,
|
|
|
|
|
height: boundingBox.height,
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
}
|
2025-02-13 08:25:57 -08:00
|
|
|
}, [artifact.documentId, setArtifact]);
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
if (artifact.isVisible) {
|
2024-12-19 17:05:04 +05:30
|
|
|
if (result) {
|
|
|
|
|
return (
|
|
|
|
|
<DocumentToolResult
|
2025-09-20 12:47:10 -07:00
|
|
|
type="create"
|
2025-09-21 12:03:29 +01:00
|
|
|
result={{ id: result.id, title: result.title, kind: result.kind }}
|
|
|
|
|
isReadonly={isReadonly}
|
2024-12-19 17:05:04 +05:30
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (args) {
|
|
|
|
|
return (
|
|
|
|
|
<DocumentToolCall
|
2025-09-21 12:03:29 +01:00
|
|
|
type="create"
|
2025-07-03 02:26:34 -07:00
|
|
|
args={{ title: args.title, kind: args.kind }}
|
2024-12-19 17:05:04 +05:30
|
|
|
isReadonly={isReadonly}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isDocumentsFetching) {
|
2025-02-13 08:25:57 -08:00
|
|
|
return <LoadingSkeleton artifactKind={result.kind ?? args.kind} />;
|
2024-12-19 17:05:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const document: Document | null = previewDocument
|
|
|
|
|
? previewDocument
|
2025-09-21 12:03:29 +01:00
|
|
|
: artifact.status === 'streaming'
|
2024-12-19 17:05:04 +05:30
|
|
|
? {
|
2025-02-13 08:25:57 -08:00
|
|
|
title: artifact.title,
|
|
|
|
|
kind: artifact.kind,
|
|
|
|
|
content: artifact.content,
|
|
|
|
|
id: artifact.documentId,
|
2024-12-19 17:05:04 +05:30
|
|
|
createdAt: new Date(),
|
2025-09-21 12:03:29 +01:00
|
|
|
userId: 'noop',
|
2024-12-19 17:05:04 +05:30
|
|
|
}
|
|
|
|
|
: null;
|
|
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
if (!document) return <LoadingSkeleton artifactKind={artifact.kind} />;
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="relative w-full cursor-pointer">
|
2025-02-13 08:25:57 -08:00
|
|
|
<HitboxLayer
|
|
|
|
|
hitboxRef={hitboxRef}
|
|
|
|
|
result={result}
|
|
|
|
|
setArtifact={setArtifact}
|
|
|
|
|
/>
|
2024-12-19 17:05:04 +05:30
|
|
|
<DocumentHeader
|
2025-09-20 12:47:10 -07:00
|
|
|
title={document.title}
|
2025-09-21 12:03:29 +01:00
|
|
|
kind={document.kind}
|
|
|
|
|
isStreaming={artifact.status === 'streaming'}
|
2024-12-19 17:05:04 +05:30
|
|
|
/>
|
|
|
|
|
<DocumentContent document={document} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
const LoadingSkeleton = ({ artifactKind }: { artifactKind: ArtifactKind }) => (
|
2024-12-19 17:05:04 +05:30
|
|
|
<div className="w-full">
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="flex h-[57px] flex-row items-center justify-between gap-2 rounded-t-2xl border border-b-0 p-4 dark:border-zinc-700 dark:bg-muted">
|
2024-12-19 17:05:04 +05:30
|
|
|
<div className="flex flex-row items-center gap-3">
|
|
|
|
|
<div className="text-muted-foreground">
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="size-4 animate-pulse rounded-md bg-muted-foreground/20" />
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="h-4 w-24 animate-pulse rounded-lg bg-muted-foreground/20" />
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
|
|
|
|
<div>
|
|
|
|
|
<FullscreenIcon />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-09-21 12:03:29 +01:00
|
|
|
{artifactKind === 'image' ? (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="overflow-y-scroll rounded-b-2xl border border-t-0 bg-muted dark:border-zinc-700">
|
|
|
|
|
<div className="h-[257px] w-full animate-pulse bg-muted-foreground/20" />
|
2025-01-15 19:59:29 +05:30
|
|
|
</div>
|
|
|
|
|
) : (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="overflow-y-scroll rounded-b-2xl border border-t-0 bg-muted p-8 pt-4 dark:border-zinc-700">
|
2025-01-15 19:59:29 +05:30
|
|
|
<InlineDocumentSkeleton />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const PureHitboxLayer = ({
|
|
|
|
|
hitboxRef,
|
|
|
|
|
result,
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact,
|
2024-12-19 17:05:04 +05:30
|
|
|
}: {
|
|
|
|
|
hitboxRef: React.RefObject<HTMLDivElement>;
|
|
|
|
|
result: any;
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact: (
|
2025-09-21 12:03:29 +01:00
|
|
|
updaterFn: UIArtifact | ((currentArtifact: UIArtifact) => UIArtifact),
|
2025-02-13 08:25:57 -08:00
|
|
|
) => void;
|
2024-12-19 17:05:04 +05:30
|
|
|
}) => {
|
|
|
|
|
const handleClick = useCallback(
|
|
|
|
|
(event: MouseEvent<HTMLElement>) => {
|
|
|
|
|
const boundingBox = event.currentTarget.getBoundingClientRect();
|
|
|
|
|
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact((artifact) =>
|
2025-09-21 12:03:29 +01:00
|
|
|
artifact.status === 'streaming'
|
2025-02-13 08:25:57 -08:00
|
|
|
? { ...artifact, isVisible: true }
|
2024-12-19 17:05:04 +05:30
|
|
|
: {
|
2025-02-13 08:25:57 -08:00
|
|
|
...artifact,
|
2025-01-08 19:31:40 +05:30
|
|
|
title: result.title,
|
2024-12-19 17:05:04 +05:30
|
|
|
documentId: result.id,
|
|
|
|
|
kind: result.kind,
|
|
|
|
|
isVisible: true,
|
|
|
|
|
boundingBox: {
|
|
|
|
|
left: boundingBox.x,
|
|
|
|
|
top: boundingBox.y,
|
|
|
|
|
width: boundingBox.width,
|
|
|
|
|
height: boundingBox.height,
|
|
|
|
|
},
|
2025-09-21 12:03:29 +01:00
|
|
|
},
|
2024-12-19 17:05:04 +05:30
|
|
|
);
|
|
|
|
|
},
|
2025-09-21 12:03:29 +01:00
|
|
|
[setArtifact, result],
|
2024-12-19 17:05:04 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2025-09-09 15:44:07 -04:00
|
|
|
className="absolute top-0 left-0 z-10 size-full rounded-xl"
|
2025-09-20 12:47:10 -07:00
|
|
|
ref={hitboxRef}
|
2025-09-21 12:03:29 +01:00
|
|
|
onClick={handleClick}
|
2024-12-19 17:05:04 +05:30
|
|
|
role="presentation"
|
2025-09-21 12:03:29 +01:00
|
|
|
aria-hidden="true"
|
2024-12-20 23:07:23 +05:30
|
|
|
>
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="flex w-full items-center justify-end p-4">
|
|
|
|
|
<div className="absolute top-[13px] right-[9px] rounded-md p-2 hover:bg-zinc-100 dark:hover:bg-zinc-700">
|
2024-12-20 23:07:23 +05:30
|
|
|
<FullscreenIcon />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-12-19 17:05:04 +05:30
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const HitboxLayer = memo(PureHitboxLayer, (prevProps, nextProps) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
if (!equal(prevProps.result, nextProps.result)) return false;
|
2024-12-19 17:05:04 +05:30
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const PureDocumentHeader = ({
|
|
|
|
|
title,
|
2025-01-15 19:59:29 +05:30
|
|
|
kind,
|
2024-12-19 17:05:04 +05:30
|
|
|
isStreaming,
|
|
|
|
|
}: {
|
|
|
|
|
title: string;
|
2025-02-13 08:25:57 -08:00
|
|
|
kind: ArtifactKind;
|
2024-12-19 17:05:04 +05:30
|
|
|
isStreaming: boolean;
|
|
|
|
|
}) => (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="flex flex-row items-start justify-between gap-2 rounded-t-2xl border border-b-0 p-4 sm:items-center dark:border-zinc-700 dark:bg-muted">
|
|
|
|
|
<div className="flex flex-row items-start gap-3 sm:items-center">
|
2024-12-19 17:05:04 +05:30
|
|
|
<div className="text-muted-foreground">
|
|
|
|
|
{isStreaming ? (
|
|
|
|
|
<div className="animate-spin">
|
|
|
|
|
<LoaderIcon />
|
|
|
|
|
</div>
|
2025-09-21 12:03:29 +01:00
|
|
|
) : kind === 'image' ? (
|
2025-01-15 19:59:29 +05:30
|
|
|
<ImageIcon />
|
2024-12-19 17:05:04 +05:30
|
|
|
) : (
|
|
|
|
|
<FileIcon />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="-translate-y-1 font-medium sm:translate-y-0">{title}</div>
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
2024-12-20 23:07:23 +05:30
|
|
|
<div className="w-8" />
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const DocumentHeader = memo(PureDocumentHeader, (prevProps, nextProps) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
if (prevProps.title !== nextProps.title) return false;
|
|
|
|
|
if (prevProps.isStreaming !== nextProps.isStreaming) return false;
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const DocumentContent = ({ document }: { document: Document }) => {
|
2025-02-13 08:25:57 -08:00
|
|
|
const { artifact } = useArtifact();
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
const containerClassName = cn(
|
2025-09-21 12:03:29 +01:00
|
|
|
'h-[257px] overflow-y-scroll border rounded-b-2xl dark:bg-muted border-t-0 dark:border-zinc-700',
|
2024-12-19 17:05:04 +05:30
|
|
|
{
|
2025-09-21 12:03:29 +01:00
|
|
|
'p-4 sm:px-14 sm:py-16': document.kind === 'text',
|
|
|
|
|
'p-0': document.kind === 'code',
|
|
|
|
|
},
|
2024-12-19 17:05:04 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const commonProps = {
|
2025-09-21 12:03:29 +01:00
|
|
|
content: document.content ?? '',
|
2024-12-19 17:05:04 +05:30
|
|
|
isCurrentVersion: true,
|
|
|
|
|
currentVersionIndex: 0,
|
2025-02-13 08:25:57 -08:00
|
|
|
status: artifact.status,
|
2025-09-21 12:03:29 +01:00
|
|
|
saveContent: () => {},
|
2024-12-19 17:05:04 +05:30
|
|
|
suggestions: [],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={containerClassName}>
|
2025-09-21 12:03:29 +01:00
|
|
|
{document.kind === 'text' ? (
|
|
|
|
|
<Editor {...commonProps} onSaveContent={() => {}} />
|
|
|
|
|
) : document.kind === 'code' ? (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="relative flex w-full flex-1">
|
2024-12-19 17:05:04 +05:30
|
|
|
<div className="absolute inset-0">
|
2025-09-21 12:03:29 +01:00
|
|
|
<CodeEditor {...commonProps} onSaveContent={() => {}} />
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-09-21 12:03:29 +01:00
|
|
|
) : document.kind === 'sheet' ? (
|
2025-09-09 15:44:07 -04:00
|
|
|
<div className="relative flex size-full flex-1 p-4">
|
2025-02-03 13:33:48 +05:30
|
|
|
<div className="absolute inset-0">
|
|
|
|
|
<SpreadsheetEditor {...commonProps} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-09-21 12:03:29 +01:00
|
|
|
) : document.kind === 'image' ? (
|
2025-01-15 19:59:29 +05:30
|
|
|
<ImageEditor
|
2025-09-21 12:03:29 +01:00
|
|
|
title={document.title}
|
|
|
|
|
content={document.content ?? ''}
|
2025-09-20 12:47:10 -07:00
|
|
|
isCurrentVersion={true}
|
2025-09-21 12:03:29 +01:00
|
|
|
currentVersionIndex={0}
|
2025-09-20 12:47:10 -07:00
|
|
|
status={artifact.status}
|
2025-09-21 12:03:29 +01:00
|
|
|
isInline={true}
|
2025-01-15 19:59:29 +05:30
|
|
|
/>
|
2024-12-19 17:05:04 +05:30
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|