Refactor renamed the component groups to something less obtuse:
Elements -> Groups -> Screens -> Views
This commit is contained in:
parent
ca6de91a96
commit
dd46acba87
2 changed files with 151 additions and 144 deletions
|
|
@ -0,0 +1,48 @@
|
|||
import type { Meta, StoryObj } from "@storybook/react-vite";
|
||||
import { MessageBubble } from "./MessageBubble";
|
||||
|
||||
const meta: Meta<typeof MessageBubble> = {
|
||||
title: "Elements/MessageBubble",
|
||||
component: MessageBubble,
|
||||
};
|
||||
|
||||
export default meta;
|
||||
type Story = StoryObj<typeof MessageBubble>;
|
||||
|
||||
export const UserText: Story = {
|
||||
args: {
|
||||
index: 0,
|
||||
role: "user",
|
||||
content: "Hello, my name is John and I like cats",
|
||||
},
|
||||
};
|
||||
|
||||
export const AssistantText: Story = {
|
||||
args: {
|
||||
index: 1,
|
||||
role: "assistant",
|
||||
content:
|
||||
"Nice to meet you, John! It's wonderful that you enjoy cats. They're such fascinating and independent creatures. Do you have any cats of your own?",
|
||||
},
|
||||
};
|
||||
|
||||
export const UserWithImage: Story = {
|
||||
args: {
|
||||
index: 0,
|
||||
role: "user",
|
||||
content: "Check this out",
|
||||
imageContent: {
|
||||
data: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",
|
||||
mimeType: "image/png",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const LongMessage: Story = {
|
||||
args: {
|
||||
index: 0,
|
||||
role: "user",
|
||||
content:
|
||||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.",
|
||||
},
|
||||
};
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
import { Group, Image, Paper, Stack, Text } from "@mantine/core";
|
||||
import { CopyButton } from "../CopyButton/CopyButton";
|
||||
|
||||
export interface MessageBubbleProps {
|
||||
index: number;
|
||||
role: "user" | "assistant";
|
||||
content: string;
|
||||
imageContent?: { data: string; mimeType: string };
|
||||
audioContent?: { data: string; mimeType: string };
|
||||
}
|
||||
|
||||
function buildDataUri(mimeType: string, data: string): string {
|
||||
return `data:${mimeType};base64,${data}`;
|
||||
}
|
||||
|
||||
function formatRoleLabel(index: number, role: string): string {
|
||||
return `[${index}] role: ${role}`;
|
||||
}
|
||||
|
||||
function formatQuotedContent(content: string): string {
|
||||
return `"${content}"`;
|
||||
}
|
||||
|
||||
const BubbleContainer = Paper.withProps({
|
||||
p: "md",
|
||||
radius: "md",
|
||||
withBorder: true,
|
||||
});
|
||||
|
||||
const RoleLabel = Text.withProps({
|
||||
size: "xs",
|
||||
c: "dimmed",
|
||||
});
|
||||
|
||||
const PreviewImage = Image.withProps({
|
||||
maw: 300,
|
||||
radius: "sm",
|
||||
mt: "xs",
|
||||
});
|
||||
|
||||
export function MessageBubble({
|
||||
index,
|
||||
role,
|
||||
content,
|
||||
imageContent,
|
||||
audioContent,
|
||||
}: MessageBubbleProps) {
|
||||
return (
|
||||
<BubbleContainer>
|
||||
<Stack gap="xs">
|
||||
<Group justify="space-between">
|
||||
<RoleLabel>{formatRoleLabel(index, role)}</RoleLabel>
|
||||
<CopyButton value={content} />
|
||||
</Group>
|
||||
<Text size="sm">{formatQuotedContent(content)}</Text>
|
||||
{imageContent && (
|
||||
<PreviewImage
|
||||
src={buildDataUri(imageContent.mimeType, imageContent.data)}
|
||||
/>
|
||||
)}
|
||||
{audioContent && (
|
||||
<audio controls>
|
||||
<source
|
||||
src={buildDataUri(audioContent.mimeType, audioContent.data)}
|
||||
type={audioContent.mimeType}
|
||||
/>
|
||||
</audio>
|
||||
)}
|
||||
</Stack>
|
||||
</BubbleContainer>
|
||||
);
|
||||
}
|
||||
Loading…
Reference in a new issue