Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: stories for supplementary chat messages #401

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/nextjs/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const decorators: Decorator[] = [
ClerkDecorator,
(Story) => (
<>
{/* TODO: Mock tRPC calls with MSW */}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are doing this now

<TRPCReactProvider>
<AnalyticsProvider>
<DialogProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Meta, StoryObj } from "@storybook/react";
import { ChatModerationDisplay } from "./ChatModerationDisplay";

const meta: Meta<typeof ChatModerationDisplay> = {
title: "Components/Chat/ChatModerationDisplay",
title: "Components/Dialogs/ChatModerationDisplay",
component: ChatModerationDisplay,
tags: ["autodocs"],
decorators: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Meta, StoryObj } from "@storybook/react";

import { ChatModerationProvider } from "@/components/ContextProviders/ChatModerationContext";

import { DemoLimitMessage } from "./demo-limit-message";

const meta: Meta<typeof DemoLimitMessage> = {
title: "Components/Chat/DemoLimitMessage",
component: DemoLimitMessage,
tags: ["autodocs"],
decorators: [
(Story) => (
<ChatModerationProvider chatId="test-chat-id">
<Story />
</ChatModerationProvider>
),
],
};

export default meta;
type Story = StoryObj<typeof DemoLimitMessage>;

export const Default: Story = {
args: {},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client";

import { ChatMessage } from "@/components/AppComponents/Chat/chat-message";

export function DemoLimitMessage({ id }: Readonly<{ id: string }>) {
return (
<div className="w-full flex-col gap-11">
<ChatMessage
chatId={id}
ailaStreamingStatus="Idle"
message={{
id: "demo-limit",
role: "assistant",
content:
'{"type": "error", "message": "**Your lesson is complete**\\nYou can no longer edit this lesson. [Create new lesson.](/aila)"}',
}}
persistedModerations={[]}
separator={<span className="my-10 flex" />}
/>
</div>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Meta, StoryObj } from "@storybook/react";

import { DemoContext } from "@/components/ContextProviders/Demo";

import { InChatDownloadButtons } from "./in-chat-download-buttons";

const DemoDecorator: Story["decorators"] = (Story, { parameters }) => (
<DemoContext.Provider
value={{
isDemoUser: false,
isSharingEnabled: true,
...parameters.demoContext,
}}
>
<Story />
</DemoContext.Provider>
);

const meta: Meta<typeof InChatDownloadButtons> = {
title: "Components/Chat/InChatDownloadButtons",
component: InChatDownloadButtons,
tags: ["autodocs"],
args: {
id: "test-chat-id",
},
decorators: [DemoDecorator],
};

export default meta;
type Story = StoryObj<typeof InChatDownloadButtons>;

export const Default: Story = {};

export const SharingDisabled: Story = {
parameters: {
demoContext: {
isSharingEnabled: false,
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { OakBox, OakFlex, OakIcon, OakSpan } from "@oaknational/oak-components";
import Link from "next/link";

import { useDemoUser } from "@/components/ContextProviders/Demo";

import { useDialog } from "../../DialogContext";

export const InChatDownloadButtons = ({ id }: { readonly id: string }) => {
const demo = useDemoUser();
const { setDialogWindow } = useDialog();

return (
<OakFlex $flexDirection="column" $gap="all-spacing-7" $mv="space-between-l">
{demo.isSharingEnabled && (
<Link
href={demo.isSharingEnabled ? `/aila/download/${id}` : "#"}
onClick={() => {
if (!demo.isSharingEnabled) {
setDialogWindow("demo-share-locked");
}
}}
>
<InnerInChatButton iconName="download">Download</InnerInChatButton>
</Link>
)}
<button
onClick={() => {
if (demo.isSharingEnabled) {
setDialogWindow("share-chat");
} else {
setDialogWindow("demo-share-locked");
}
}}
>
<InnerInChatButton iconName="share">Share</InnerInChatButton>
</button>
</OakFlex>
);
};

const InnerInChatButton = ({
iconName,
children,
}: {
readonly iconName: "download" | "share";
readonly children: string;
}) => {
return (
<OakFlex
$pa="inner-padding-m"
$gap="all-spacing-3"
$background="white"
$borderRadius="border-radius-m"
$alignItems="center"
$dropShadow="drop-shadow-standard"
>
<OakBox $transform="scale">
<OakIcon iconName={iconName} $width="all-spacing-7" />
</OakBox>
<OakSpan $font="body-2">{children}</OakSpan>
</OakFlex>
);
};
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
"use client";

import type { Dispatch, SetStateAction } from "react";
import { useCallback, useEffect, useRef, useState } from "react";

import type { PersistedModerationBase } from "@oakai/core/src/utils/ailaModeration/moderationSchema";
import { OakBox, OakFlex, OakIcon, OakSpan } from "@oaknational/oak-components";
import type { Message } from "ai";
import Link from "next/link";

import { ChatMessage } from "@/components/AppComponents/Chat/chat-message";
import { useLessonChat } from "@/components/ContextProviders/ChatProvider";
import type { DemoContextProps } from "@/components/ContextProviders/Demo";

import { useDialog } from "../DialogContext";
import type { AilaStreamingStatus } from "./Chat/hooks/useAilaStreamingStatus";
import { useProgressForDownloads } from "./Chat/hooks/useProgressForDownloads";
import type { DialogTypes } from "./Chat/types";
import type { AilaStreamingStatus } from "../Chat/hooks/useAilaStreamingStatus";
import { useProgressForDownloads } from "../Chat/hooks/useProgressForDownloads";
import { DemoLimitMessage } from "./demo-limit-message";
import { InChatDownloadButtons } from "./in-chat-download-buttons";

export interface ChatListProps {
isDemoLocked: boolean;
showLessonMobile: boolean;
demo: DemoContextProps;
}

function DemoLimitMessage({ id }: Readonly<{ id: string }>) {
return (
<div className="w-full flex-col gap-11">
<ChatMessage
chatId={id}
ailaStreamingStatus="Idle"
message={{
id: "demo-limit",
role: "assistant",
content:
'{"type": "error", "message": "**Your lesson is complete**\\nYou can no longer edit this lesson. [Create new lesson.](/aila)"}',
}}
persistedModerations={[]}
separator={<span className="my-10 flex" />}
/>
</div>
);
}
Comment on lines -26 to -43
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted to a new file and story


export function ChatList({
isDemoLocked,
showLessonMobile,
Expand Down Expand Up @@ -129,7 +106,6 @@ export const ChatMessagesDisplay = ({
demo,
}: ChatMessagesDisplayProps) => {
const { lessonPlan, isStreaming } = useLessonChat();
const { setDialogWindow } = useDialog();
const { totalSections, totalSectionsComplete } = useProgressForDownloads({
lessonPlan,
isStreaming,
Expand Down Expand Up @@ -228,71 +204,7 @@ export const ChatMessagesDisplay = ({
(message.role !== "user" &&
message.content.includes("download") &&
message.content.includes("share")),
) && <InChatDownloadButtons {...{ demo, id, setDialogWindow }} />}
) && <InChatDownloadButtons {...{ demo, id }} />}
</>
);
};

const InChatDownloadButtons = ({
demo,
id,
setDialogWindow,
}: {
readonly demo: DemoContextProps;
readonly id: string;
readonly setDialogWindow: Dispatch<SetStateAction<DialogTypes>>;
}) => {
return (
<OakFlex $flexDirection="column" $gap="all-spacing-7" $mv="space-between-l">
{demo.isSharingEnabled && (
<Link
href={demo.isSharingEnabled ? `/aila/download/${id}` : "#"}
onClick={() => {
if (!demo.isSharingEnabled) {
setDialogWindow("demo-share-locked");
}
}}
>
<InnerInChatButton iconName="download">Download</InnerInChatButton>
</Link>
)}
<button
onClick={() => {
if (demo.isSharingEnabled) {
setDialogWindow("share-chat");
} else {
setDialogWindow("demo-share-locked");
}
}}
>
<InnerInChatButton iconName="share">Share</InnerInChatButton>
</button>
</OakFlex>
);
};

const InnerInChatButton = ({
iconName,

children,
}: {
readonly iconName: "download" | "share";

readonly children: string;
}) => {
return (
<OakFlex
$pa="inner-padding-m"
$gap="all-spacing-3"
$background="white"
$borderRadius="border-radius-m"
$alignItems="center"
$dropShadow="drop-shadow-standard"
>
<OakBox $transform="scale">
<OakIcon iconName={iconName} $width="all-spacing-7" />
</OakBox>
<OakSpan $font="body-2">{children}</OakSpan>
</OakFlex>
);
};
Comment on lines -236 to -298
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted to a new file and story

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Meta, StoryObj } from "@storybook/react";
import { ChatStartForm } from "./chat-start-form";

const meta: Meta<typeof ChatStartForm> = {
title: "Components/Chat/ChatStartForm",
title: "Components/Chat Start/ChatStartForm",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that path meant to have a gap? Chat Start

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking. This is only used in the storybook UI so it does look right there

component: ChatStartForm,
tags: ["autodocs"],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ export const Default: Story = {
},
};

export const CustomClass: Story = {
args: {
moderation: mockModeration,
className: "custom-class",
},
};

export const Safe: Story = {
export const NoModeration: Story = {
args: {
moderation: {
id: "safe",
categories: [],
},
},
decorators: [
(Story) => (
<>
<Story />
<p>(Nothing should render here)</p>
</>
),
],
};
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading