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