Skip to content

Commit d50fa6e

Browse files
committed
Provide messages on assignment selection as prop
1 parent c17ea90 commit d50fa6e

File tree

4 files changed

+28
-33
lines changed

4 files changed

+28
-33
lines changed

src/components/AiChat/AiChat.stories.tsx

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const JsonResponses: Story = {
110110
},
111111
}
112112

113-
export const Temp: Story = {
113+
export const AssignmentSelection: Story = {
114114
args: {
115115
requestOpts: {
116116
apiUrl: TEST_API_STREAMING,
@@ -126,30 +126,15 @@ export const Temp: Story = {
126126
role: "assistant",
127127
},
128128
],
129-
conversationStarters: STARTERS,
129+
conversationStarters: [],
130130
entryScreenEnabled: false,
131-
},
132-
}
133-
134-
export const AssignmentSelection: Story = {
135-
args: {
136-
requestOpts: {
137-
apiUrl: TEST_API_STREAMING,
138-
transformBody: (messages, body) => ({
139-
message: messages[messages.length - 1].content,
140-
problem_set_title: body?.problem_set_title,
141-
}),
142-
},
143-
initialMessages: [
131+
problemSetListUrl: TEST_API_PROBLEM_SET_LIST,
132+
problemSetInitialMessages: [
144133
{
145-
content:
146-
"Hi! Please select an assignment from the dropdown menu to begin.",
147134
role: "assistant",
135+
content: "Which question are you working on?",
148136
},
149137
],
150-
conversationStarters: [],
151-
entryScreenEnabled: false,
152-
problemSetListUrl: TEST_API_PROBLEM_SET_LIST,
153138
},
154139
}
155140

src/components/AiChat/AiChat.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ const AssignmentSelect = styled(SimpleSelectField)({
7777
"> div": {
7878
width: "inherit",
7979
},
80+
label: {
81+
display: "none",
82+
},
8083
})
8184

8285
const MessagesContainer = styled(ScrollSnap)<{ externalScroll: boolean }>(
@@ -235,6 +238,7 @@ const AiChatDisplay: FC<AiChatDisplayProps> = ({
235238
useMathJax = false,
236239
onSubmit,
237240
problemSetListUrl,
241+
problemSetInitialMessages,
238242
...others // Could contain data attributes
239243
}) => {
240244
const containerRef = useRef<HTMLDivElement>(null)
@@ -257,6 +261,7 @@ const AiChatDisplay: FC<AiChatDisplayProps> = ({
257261
initialMessages,
258262
status,
259263
additionalBody,
264+
setMessages,
260265
setAdditionalBody,
261266
} = useAiChat()
262267

@@ -298,6 +303,14 @@ const AiChatDisplay: FC<AiChatDisplayProps> = ({
298303
}
299304

300305
const onProblemSetChange = (event: SelectChangeEvent<string | string[]>) => {
306+
if (problemSetInitialMessages) {
307+
setMessages(
308+
problemSetInitialMessages.map((message, i) => ({
309+
...message,
310+
id: `initial-${i}`,
311+
})),
312+
)
313+
}
301314
setAdditionalBody?.({ problem_set_title: event.target.value as string })
302315
}
303316

src/components/AiChat/AiChatContext.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,6 @@ const AiChatProvider: React.FC<AiChatContextProps> = ({
112112
})
113113
}, [parseContent, unparsed, initialMessages])
114114

115-
const _setAdditionalBody = (body: Record<string, string>) => {
116-
setMessages([
117-
{
118-
role: "assistant",
119-
content: "Which question are you working on?",
120-
id: "initial-0",
121-
},
122-
])
123-
setAdditionalBody(body)
124-
}
125-
126115
return (
127116
<AiChatContext.Provider
128117
/**
@@ -132,9 +121,9 @@ const AiChatProvider: React.FC<AiChatContextProps> = ({
132121
value={{
133122
initialMessages,
134123
messages,
135-
additionalBody,
136-
setAdditionalBody: _setAdditionalBody,
137124
setMessages,
125+
additionalBody,
126+
setAdditionalBody,
138127
...others,
139128
}}
140129
>

src/components/AiChat/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,17 @@ type AiChatDisplayProps = {
124124

125125
/**
126126
* URL to fetch problem set list for dropdown.
127+
*
128+
* The problem set selection is passed as the second argument to the `transformBody` function
129+
* provided as `{ problem_set_title: string }`.
127130
*/
128131
problemSetListUrl?: string
129132

133+
/**
134+
* Initial messages to display on problem set selection.
135+
*/
136+
problemSetInitialMessages?: Omit<AiChatMessage, "id">[]
137+
130138
onSubmit?: (
131139
messageText: string,
132140
meta: {

0 commit comments

Comments
 (0)