2025-09-21 11:02:31 -07:00
|
|
|
"use client";
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
import equal from "fast-deep-equal";
|
2024-12-19 17:05:04 +05:30
|
|
|
import {
|
2025-09-21 12:03:29 +01:00
|
|
|
type MouseEvent,
|
2025-09-21 11:02:31 -07:00
|
|
|
memo,
|
2024-12-19 17:05:04 +05:30
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
|
|
|
|
useMemo,
|
|
|
|
|
useRef,
|
2025-09-21 11:02:31 -07:00
|
|
|
} from "react";
|
|
|
|
|
import useSWR from "swr";
|
|
|
|
|
import { useArtifact } from "@/hooks/use-artifact";
|
|
|
|
|
import type { Document } from "@/lib/db/schema";
|
|
|
|
|
import { cn, fetcher } from "@/lib/utils";
|
|
|
|
|
import type { ArtifactKind, UIArtifact } from "./artifact";
|
|
|
|
|
import { CodeEditor } from "./code-editor";
|
|
|
|
|
import { InlineDocumentSkeleton } from "./document-skeleton";
|
2026-03-20 09:37:02 +00:00
|
|
|
import {
|
|
|
|
|
CodeIcon,
|
|
|
|
|
FileIcon,
|
|
|
|
|
FullscreenIcon,
|
|
|
|
|
ImageIcon,
|
|
|
|
|
LoaderIcon,
|
|
|
|
|
} from "./icons";
|
2025-09-21 11:02:31 -07:00
|
|
|
import { ImageEditor } from "./image-editor";
|
|
|
|
|
import { SpreadsheetEditor } from "./sheet-editor";
|
|
|
|
|
import { Editor } from "./text-editor";
|
2024-12-19 17:05:04 +05:30
|
|
|
|
2026-03-20 09:37:02 +00:00
|
|
|
type DocumentToolOutput = {
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
|
|
|
|
kind: ArtifactKind;
|
|
|
|
|
content?: string;
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
type DocumentPreviewProps = {
|
2024-12-19 17:05:04 +05:30
|
|
|
isReadonly: boolean;
|
2026-03-20 09:37:02 +00:00
|
|
|
result?: Partial<DocumentToolOutput>;
|
|
|
|
|
args?: Partial<DocumentToolOutput> & { isUpdate?: boolean };
|
2025-09-21 11:02:31 -07:00
|
|
|
};
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
export function DocumentPreview({
|
2026-03-20 09:37:02 +00:00
|
|
|
isReadonly: _isReadonly,
|
2024-12-19 17:05:04 +05:30
|
|
|
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 11:02:31 -07:00
|
|
|
Document[]
|
2026-03-20 09:37:02 +00:00
|
|
|
>(
|
|
|
|
|
result
|
|
|
|
|
? `${process.env.NEXT_PUBLIC_BASE_PATH ?? ""}/api/document?id=${result.id}`
|
|
|
|
|
: null,
|
|
|
|
|
fetcher
|
|
|
|
|
);
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
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 11:02:31 -07:00
|
|
|
setArtifact((currentArtifact) => ({
|
|
|
|
|
...currentArtifact,
|
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
|
|
|
|
|
|
|
|
if (isDocumentsFetching) {
|
2026-03-20 09:37:02 +00:00
|
|
|
const kind = result?.kind ?? args?.kind ?? artifact.kind;
|
|
|
|
|
const title = result?.title ?? args?.title ?? artifact.title;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="w-full max-w-[450px]">
|
|
|
|
|
{title ? (
|
|
|
|
|
<DocumentHeader isStreaming={true} kind={kind} title={title} />
|
|
|
|
|
) : (
|
|
|
|
|
<div className="flex flex-row items-center justify-between gap-2 rounded-t-2xl border border-b-0 border-border/50 px-4 py-3 dark:bg-muted">
|
|
|
|
|
<div className="flex flex-row items-center gap-2.5">
|
|
|
|
|
<div className="size-3.5 animate-pulse rounded bg-muted-foreground/15" />
|
|
|
|
|
<div className="h-3.5 w-24 animate-pulse rounded bg-muted-foreground/15" />
|
|
|
|
|
</div>
|
|
|
|
|
<div className="w-8" />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<div className="h-[257px] overflow-hidden rounded-b-2xl border border-t-0 border-border/50 bg-muted p-6">
|
|
|
|
|
<InlineDocumentSkeleton />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-12-19 17:05:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const document: Document | null = previewDocument
|
|
|
|
|
? previewDocument
|
2025-09-21 11:02:31 -07: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 11:02:31 -07:00
|
|
|
userId: "noop",
|
2024-12-19 17:05:04 +05:30
|
|
|
}
|
|
|
|
|
: null;
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
if (!document) {
|
|
|
|
|
return <LoadingSkeleton artifactKind={artifact.kind} />;
|
|
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
|
|
|
|
|
return (
|
2025-12-19 23:24:24 +00:00
|
|
|
<div className="relative w-full max-w-[450px] 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-21 11:02:31 -07:00
|
|
|
isStreaming={artifact.status === "streaming"}
|
2025-09-21 12:03:29 +01:00
|
|
|
kind={document.kind}
|
2025-09-21 11:02:31 -07:00
|
|
|
title={document.title}
|
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 }) => (
|
2025-12-19 23:24:24 +00:00
|
|
|
<div className="w-full max-w-[450px]">
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="flex flex-row items-center justify-between gap-2 rounded-t-2xl border border-b-0 border-border/50 px-4 py-3 dark:bg-muted">
|
|
|
|
|
<div className="flex flex-row items-center gap-2.5">
|
|
|
|
|
<div className="size-3.5 animate-pulse rounded bg-muted-foreground/15" />
|
|
|
|
|
<div className="h-3.5 w-24 animate-pulse rounded bg-muted-foreground/15" />
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="w-8" />
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
2025-09-21 11:02:31 -07:00
|
|
|
{artifactKind === "image" ? (
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="overflow-hidden rounded-b-2xl border border-t-0 border-border/50 bg-muted">
|
|
|
|
|
<div className="h-[257px] w-full animate-pulse bg-muted-foreground/10" />
|
2025-01-15 19:59:29 +05:30
|
|
|
</div>
|
|
|
|
|
) : (
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="h-[257px] overflow-hidden rounded-b-2xl border border-t-0 border-border/50 bg-muted p-6">
|
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>;
|
2026-03-20 09:37:02 +00:00
|
|
|
result?: Partial<DocumentToolOutput>;
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact: (
|
2025-09-21 11:02:31 -07: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();
|
|
|
|
|
|
2026-03-20 09:37:02 +00:00
|
|
|
setArtifact((artifact) => ({
|
|
|
|
|
...artifact,
|
|
|
|
|
...(result?.id && { documentId: result.id }),
|
|
|
|
|
...(result?.title && { title: result.title }),
|
|
|
|
|
...(result?.kind && { kind: result.kind }),
|
|
|
|
|
isVisible: true,
|
|
|
|
|
boundingBox: {
|
|
|
|
|
left: boundingBox.x,
|
|
|
|
|
top: boundingBox.y,
|
|
|
|
|
width: boundingBox.width,
|
|
|
|
|
height: boundingBox.height,
|
|
|
|
|
},
|
|
|
|
|
}));
|
2024-12-19 17:05:04 +05:30
|
|
|
},
|
2025-09-21 11:02:31 -07:00
|
|
|
[setArtifact, result]
|
2024-12-19 17:05:04 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2025-09-21 11:02:31 -07:00
|
|
|
aria-hidden="true"
|
2025-09-09 15:44:07 -04:00
|
|
|
className="absolute top-0 left-0 z-10 size-full rounded-xl"
|
2025-09-21 12:03:29 +01:00
|
|
|
onClick={handleClick}
|
2025-09-21 11:02:31 -07:00
|
|
|
ref={hitboxRef}
|
2024-12-19 17:05:04 +05:30
|
|
|
role="presentation"
|
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">
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="absolute top-[13px] right-[9px] rounded-lg p-1.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground">
|
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 11:02:31 -07: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;
|
|
|
|
|
}) => (
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="flex flex-row items-center justify-between gap-2 rounded-t-2xl border border-b-0 border-border/50 px-4 py-3 dark:bg-muted">
|
|
|
|
|
<div className="flex flex-row items-center gap-2.5">
|
2024-12-19 17:05:04 +05:30
|
|
|
<div className="text-muted-foreground">
|
|
|
|
|
{isStreaming ? (
|
|
|
|
|
<div className="animate-spin">
|
2026-03-20 09:37:02 +00:00
|
|
|
<LoaderIcon size={14} />
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
2025-09-21 11:02:31 -07:00
|
|
|
) : kind === "image" ? (
|
2026-03-20 09:37:02 +00:00
|
|
|
<ImageIcon size={14} />
|
|
|
|
|
) : kind === "code" ? (
|
|
|
|
|
<CodeIcon size={14} />
|
2024-12-19 17:05:04 +05:30
|
|
|
) : (
|
2026-03-20 09:37:02 +00:00
|
|
|
<FileIcon size={14} />
|
2024-12-19 17:05:04 +05:30
|
|
|
)}
|
|
|
|
|
</div>
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="text-sm font-medium">{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 11:02:31 -07: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(
|
2026-03-20 09:37:02 +00:00
|
|
|
"h-[257px] overflow-hidden rounded-b-2xl border border-t-0 border-border/50 dark:bg-muted",
|
2024-12-19 17:05:04 +05:30
|
|
|
{
|
2026-03-20 09:37:02 +00:00
|
|
|
"p-4 sm:px-10 sm:py-10": document.kind === "text",
|
2025-09-21 11:02:31 -07:00
|
|
|
"p-0": document.kind === "code",
|
|
|
|
|
}
|
2024-12-19 17:05:04 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const commonProps = {
|
2025-09-21 11:02:31 -07: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 11:02:31 -07:00
|
|
|
saveContent: () => null,
|
2024-12-19 17:05:04 +05:30
|
|
|
suggestions: [],
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-21 11:02:31 -07:00
|
|
|
const handleSaveContent = () => null;
|
|
|
|
|
|
2024-12-19 17:05:04 +05:30
|
|
|
return (
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className={cn(containerClassName, "relative")}>
|
2025-09-21 11:02:31 -07:00
|
|
|
{document.kind === "text" ? (
|
|
|
|
|
<Editor {...commonProps} onSaveContent={handleSaveContent} />
|
|
|
|
|
) : 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 11:02:31 -07:00
|
|
|
<CodeEditor {...commonProps} onSaveContent={handleSaveContent} />
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-09-21 11:02:31 -07: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 11:02:31 -07:00
|
|
|
) : document.kind === "image" ? (
|
2025-01-15 19:59:29 +05:30
|
|
|
<ImageEditor
|
2025-09-21 11:02:31 -07:00
|
|
|
content={document.content ?? ""}
|
2025-09-21 12:03:29 +01:00
|
|
|
currentVersionIndex={0}
|
2025-09-21 11:02:31 -07:00
|
|
|
isCurrentVersion={true}
|
2025-09-21 12:03:29 +01:00
|
|
|
isInline={true}
|
2025-09-21 11:02:31 -07:00
|
|
|
status={artifact.status}
|
|
|
|
|
title={document.title}
|
2025-01-15 19:59:29 +05:30
|
|
|
/>
|
2024-12-19 17:05:04 +05:30
|
|
|
) : null}
|
2026-03-20 09:37:02 +00:00
|
|
|
<div className="pointer-events-none absolute inset-x-0 bottom-0 h-16 bg-gradient-to-t from-muted to-transparent dark:from-muted" />
|
|
|
|
|
{document.kind === "code" && (
|
|
|
|
|
<div className="pointer-events-none absolute inset-y-0 right-0 w-12 bg-gradient-to-l from-muted to-transparent dark:from-muted" />
|
|
|
|
|
)}
|
2024-12-19 17:05:04 +05:30
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|