Skip to content

Commit

Permalink
chore: use LessonPlanSectionWhileStreaming type
Browse files Browse the repository at this point in the history
  • Loading branch information
stefl committed Dec 9, 2024
1 parent af9645d commit e8ff60c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useRef, useState } from "react";

import { getLastAssistantMessage } from "@oakai/aila/src/helpers/chat/getLastAssistantMessage";
import type { LessonPlanSectionWhileStreaming } from "@oakai/aila/src/protocol/schema";
import { OakBox } from "@oaknational/oak-components";
import type { AilaUserModificationAction } from "@prisma/client";

Expand All @@ -18,7 +19,7 @@ import type { FeedbackOption } from "./drop-down-form-wrapper";
export type ActionButtonWrapperProps = Readonly<{
sectionTitle: string;
sectionPath: string;
sectionValue: Record<string, unknown> | string | Array<unknown>;
sectionValue: LessonPlanSectionWhileStreaming;
options: ModifyOptions | AdditionalMaterialOptions;
buttonText: string;
actionButtonLabel: string;
Expand Down Expand Up @@ -59,7 +60,7 @@ const ActionButtonWrapper = ({
chatId: id,
messageId: lastAssistantMessage.id,
sectionPath,
sectionValue,
sectionValue: String(sectionValue),
action: selectedRadio.enumValue,
actionOtherText: userFeedbackText || null,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LessonPlanSectionWhileStreaming } from "@oakai/aila/src/protocol/schema";
import type { AilaUserModificationAction } from "@prisma/client";

import ActionButtonWrapper from "./action-button-wrapper";
Expand All @@ -7,7 +8,7 @@ import type { FeedbackOption } from "./drop-down-form-wrapper";
export type AdditionalMaterialsProps = Readonly<{
sectionTitle: string;
sectionPath: string;
sectionValue: Record<string, unknown> | string | Array<unknown>;
sectionValue: LessonPlanSectionWhileStreaming;
}>;

const AddAdditionalMaterialsButton = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type {
LessonPlanKeys,
LessonPlanSectionWhileStreaming,
} from "@oakai/aila/src/protocol/schema";
import { sectionToMarkdown } from "@oakai/aila/src/protocol/sectionToMarkdown";
import { OakFlex } from "@oaknational/oak-components";
import { lessonSectionTitlesAndMiniDescriptions } from "data/lessonSectionTitlesAndMiniDescriptions";
Expand All @@ -9,9 +13,10 @@ import FlagButton from "./flag-button";
import ModifyButton from "./modify-button";

export type ChatSectionProps = Readonly<{
objectKey: string;
value: Record<string, unknown> | string | Array<unknown>;
objectKey: LessonPlanKeys;
value: LessonPlanSectionWhileStreaming;
}>;

const ChatSection = ({ objectKey, value }: ChatSectionProps) => {
return (
<OakFlex $flexDirection="column">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from "react";

import { getLastAssistantMessage } from "@oakai/aila/src/helpers/chat/getLastAssistantMessage";
import type { LessonPlanSectionWhileStreaming } from "@oakai/aila/src/protocol/schema";
import type { AilaUserFlagType } from "@oakai/db";
import { OakBox, OakP, OakRadioGroup } from "@oaknational/oak-components";
import styled from "styled-components";
Expand All @@ -26,7 +27,7 @@ type FlagButtonOptions = typeof flagOptions;
export type FlagButtonProps = Readonly<{
sectionTitle: string;
sectionPath: string;
sectionValue: Record<string, unknown> | string | Array<unknown>;
sectionValue: LessonPlanSectionWhileStreaming;
}>;

const FlagButton = ({
Expand All @@ -48,6 +49,25 @@ const FlagButton = ({

const { mutateAsync } = trpc.chat.chatFeedback.flagSection.useMutation();

const isPlainObject = (value: unknown): value is Record<string, unknown> => {
return typeof value === "object" && value !== null && !Array.isArray(value);
};

const prepareSectionValue = (
value: LessonPlanSectionWhileStreaming,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): string | any[] | Record<string, unknown> => {
if (
typeof value === "string" ||
Array.isArray(value) ||
isPlainObject(value)
) {
return value;
}
// For numbers or any other types, convert to string
return String(value);
};

const flagSectionContent = async () => {
if (selectedRadio && lastAssistantMessage) {
const payload = {
Expand All @@ -56,7 +76,7 @@ const FlagButton = ({
flagType: selectedRadio.enumValue,
userComment: userFeedbackText,
sectionPath,
sectionValue,
sectionValue: prepareSectionValue(sectionValue),
};
await mutateAsync(payload);
}
Expand Down Expand Up @@ -93,7 +113,7 @@ const FlagButton = ({
>
{flagOptions.map((option) => (
<FlagButtonFormItem
key={`flagbuttonformitem-${option.enumValue}`}
key={`flagButtonFormItem-${option.enumValue}`}
option={option}
setSelectedRadio={setSelectedRadio}
setDisplayTextBox={setDisplayTextBox}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { LessonPlanSectionWhileStreaming } from "@oakai/aila/src/protocol/schema";
import type { AilaUserModificationAction } from "@prisma/client";

import ActionButtonWrapper from "./action-button-wrapper";
Expand All @@ -7,7 +8,7 @@ import type { FeedbackOption } from "./drop-down-form-wrapper";
export type ModifyButtonProps = Readonly<{
sectionTitle: string;
sectionPath: string;
sectionValue: Record<string, unknown> | string | Array<unknown>;
sectionValue: LessonPlanSectionWhileStreaming;
}>;

const ModifyButton = ({
Expand Down

0 comments on commit e8ff60c

Please sign in to comment.