Skip to content

Commit

Permalink
test: extract and add stories for in chat download-buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
codeincontext committed Nov 26, 2024
1 parent 11f5c77 commit b37d0aa
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 94 deletions.
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 */}
<TRPCReactProvider>
<AnalyticsProvider>
<DialogProvider>
Expand Down
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,64 @@
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 }: { 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,
}: {
iconName: "download" | "share";

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>
);
}

export function ChatList({
isDemoLocked,
showLessonMobile,
Expand Down Expand Up @@ -127,7 +104,6 @@ export const ChatMessagesDisplay = ({
demo: DemoContextProps;
}) => {
const { lessonPlan, isStreaming } = useLessonChat();
const { setDialogWindow } = useDialog();
const { totalSections, totalSectionsComplete } = useProgressForDownloads({
lessonPlan,
isStreaming,
Expand Down Expand Up @@ -226,71 +202,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,
}: {
demo: DemoContextProps;
id: string;
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,
}: {
iconName: "download" | "share";

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>
);
};

0 comments on commit b37d0aa

Please sign in to comment.