Skip to content

Commit

Permalink
feat: add inline block document previews (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyphilemon authored Dec 19, 2024
1 parent b659dcc commit bb03c51
Show file tree
Hide file tree
Showing 24 changed files with 1,127 additions and 810 deletions.
20 changes: 12 additions & 8 deletions app/(chat)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { cookies } from 'next/headers';
import { Chat } from '@/components/chat';
import { DEFAULT_MODEL_NAME, models } from '@/lib/ai/models';
import { generateUUID } from '@/lib/utils';
import { DataStreamHandler } from '@/components/data-stream-handler';

export default async function Page() {
const id = generateUUID();
Expand All @@ -15,13 +16,16 @@ export default async function Page() {
DEFAULT_MODEL_NAME;

return (
<Chat
key={id}
id={id}
initialMessages={[]}
selectedModelId={selectedModelId}
selectedVisibilityType="private"
isReadonly={false}
/>
<>
<Chat
key={id}
id={id}
initialMessages={[]}
selectedModelId={selectedModelId}
selectedVisibilityType="private"
isReadonly={false}
/>
<DataStreamHandler id={id} />
</>
);
}
5 changes: 4 additions & 1 deletion biome.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
"noSvgWithoutTitle": "off", // We do not intend to adhere to this rule
"useMediaCaption": "off", // We would need a cultural change to turn this on
"noAutofocus": "off", // We're highly intentional about when we use autofocus
"noBlankTarget": "off" // Covered by Conformance
"noBlankTarget": "off", // Covered by Conformance
"useFocusableInteractive": "off", // Disable focusable interactive element requirement
"useAriaPropsForRole": "off", // Disable required ARIA attributes check
"useKeyWithClickEvents": "off" // Disable keyboard event requirement with click events
},
"complexity": {
"noUselessStringConcat": "warn", // Not in recommended ruleset, turning on manually
Expand Down
23 changes: 12 additions & 11 deletions components/block-close-button.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { memo, SetStateAction } from 'react';
import { memo } from 'react';
import { CrossIcon } from './icons';
import { Button } from './ui/button';
import { UIBlock } from './block';
import equal from 'fast-deep-equal';
import { initialBlockData, useBlock } from '@/hooks/use-block';

interface BlockCloseButtonProps {
setBlock: (value: SetStateAction<UIBlock>) => void;
}
function PureBlockCloseButton() {
const { setBlock } = useBlock();

function PureBlockCloseButton({ setBlock }: BlockCloseButtonProps) {
return (
<Button
variant="outline"
className="h-fit p-2 dark:hover:bg-zinc-700"
onClick={() => {
setBlock((currentBlock) => ({
...currentBlock,
isVisible: false,
}));
setBlock((currentBlock) =>
currentBlock.status === 'streaming'
? {
...currentBlock,
isVisible: false,
}
: { ...initialBlockData, status: 'idle' },
);
}}
>
<CrossIcon size={18} />
Expand Down
26 changes: 13 additions & 13 deletions components/block-messages.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Dispatch, memo, SetStateAction } from 'react';
import { UIBlock } from './block';
import { PreviewMessage } from './message';
import { useScrollToBottom } from './use-scroll-to-bottom';
import { Vote } from '@/lib/db/schema';
import { ChatRequestOptions, Message } from 'ai';
import { memo } from 'react';
import equal from 'fast-deep-equal';
import { UIBlock } from './block';

interface BlockMessagesProps {
chatId: string;
block: UIBlock;
setBlock: Dispatch<SetStateAction<UIBlock>>;
isLoading: boolean;
votes: Array<Vote> | undefined;
messages: Array<Message>;
Expand All @@ -19,12 +18,11 @@ interface BlockMessagesProps {
chatRequestOptions?: ChatRequestOptions,
) => Promise<string | null | undefined>;
isReadonly: boolean;
blockStatus: UIBlock['status'];
}

function PureBlockMessages({
chatId,
block,
setBlock,
isLoading,
votes,
messages,
Expand All @@ -45,8 +43,6 @@ function PureBlockMessages({
chatId={chatId}
key={message.id}
message={message}
block={block}
setBlock={setBlock}
isLoading={isLoading && index === messages.length - 1}
vote={
votes
Expand All @@ -72,13 +68,17 @@ function areEqual(
nextProps: BlockMessagesProps,
) {
if (
prevProps.block.status === 'streaming' &&
nextProps.block.status === 'streaming'
) {
prevProps.blockStatus === 'streaming' &&
nextProps.blockStatus === 'streaming'
)
return true;
}

return false;
if (prevProps.isLoading !== nextProps.isLoading) return false;
if (prevProps.isLoading && nextProps.isLoading) return false;
if (prevProps.messages.length !== nextProps.messages.length) return false;
if (!equal(prevProps.votes, nextProps.votes)) return false;

return true;
}

export const BlockMessages = memo(PureBlockMessages, areEqual);
43 changes: 0 additions & 43 deletions components/block-stream-handler.tsx

This file was deleted.

Loading

0 comments on commit bb03c51

Please sign in to comment.