Skip to content

Commit 6254cab

Browse files
authored
Merge pull request #9 from palico-ai/copilot-widget
FIX: Fixed ChatWidget AutoFocus
2 parents 7370af6 + ea942a2 commit 6254cab

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

src/components/chat/index.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import { PalicoContext } from '../../context'
55
import { ChatHistory } from './history'
66
import { ChatInput } from './input'
77

8+
export type GetSendMessageParamsFN = (userInput: string) => Promise<{
9+
message: string
10+
context: Record<string, unknown>
11+
}>
12+
813
export interface ChatUIProps {
914
headerTitle?: string
1015
inputPlaceholder?: string
1116
initialPlaceholderAgentMessage?: string
12-
getSendMessageParams?: (userInput: string) => Promise<{
13-
message: string
14-
params: Record<string, unknown>
15-
}>
17+
getSendMessageParams?: GetSendMessageParamsFN
1618
}
1719

1820
const DEFAULT_VALUES = {
@@ -27,16 +29,17 @@ const ChatUI: React.FC<ChatUIProps> = ({
2729
initialPlaceholderAgentMessage = DEFAULT_VALUES.initialPlaceholderAgentMessage,
2830
getSendMessageParams
2931
}) => {
30-
const { conversationHistory, sendMessage } = useContext(PalicoContext)
32+
const { conversationHistory, loading, sendMessage } = useContext(PalicoContext)
3133
const [errorMessage, setErrorMessage] = React.useState<string | null>(null)
3234

3335
const handleSend = async (message: string): Promise<void> => {
36+
if (!sendMessage) throw new Error('sendMessage is not defined')
3437
try {
3538
if (getSendMessageParams) {
36-
const { message: newMessage, params } = await getSendMessageParams(
39+
const { message: newMessage, context } = await getSendMessageParams(
3740
message
3841
)
39-
await sendMessage(newMessage, params)
42+
await sendMessage(newMessage, context)
4043
} else {
4144
await sendMessage(message, {})
4245
}
@@ -56,8 +59,10 @@ const ChatUI: React.FC<ChatUIProps> = ({
5659
alignItems="stretch"
5760
spacing={2}
5861
sx={{
59-
p: 2,
60-
height: '100%'
62+
width: '100%',
63+
height: '100%',
64+
padding: 2,
65+
boxSizing: 'border-box'
6166
}}
6267
>
6368
<Box>
@@ -83,7 +88,7 @@ const ChatUI: React.FC<ChatUIProps> = ({
8388
<Box>
8489
<ChatInput
8590
placeholder={inputPlaceholder}
86-
disabled={errorMessage !== null}
91+
disabled={errorMessage !== null || loading}
8792
onSend={handleSend}
8893
/>
8994
</Box>

src/components/chat/input.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { TextField } from '@mui/material'
2-
import React, { useMemo } from 'react'
1+
import React, { useEffect, useMemo } from 'react'
32

43
export interface ChatInputProps {
54
onSend: (message: string) => Promise<void>
@@ -12,11 +11,13 @@ export const ChatInput: React.FC<ChatInputProps> = ({
1211
disabled,
1312
placeholder = 'Type a message'
1413
}) => {
15-
const inputRef = React.useRef<HTMLInputElement>(null)
14+
const inputRef = React.useRef<HTMLInputElement | null>(null)
1615
const [message, setMessage] = React.useState('')
1716
const [loading, setLoading] = React.useState(false)
1817

19-
const handleFormSubmit = async (event: React.FormEvent<HTMLFormElement>): Promise<void> => {
18+
const handleFormSubmit = async (
19+
event: React.FormEvent<HTMLFormElement>
20+
): Promise<void> => {
2021
event.preventDefault()
2122
try {
2223
setLoading(true)
@@ -30,20 +31,30 @@ export const ChatInput: React.FC<ChatInputProps> = ({
3031
const handleChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
3132
setMessage(event.target.value)
3233
}
33-
3434
const enableInput = useMemo(() => !loading && !disabled, [loading, disabled])
3535

36+
useEffect(() => {
37+
if (inputRef.current) {
38+
inputRef.current?.focus()
39+
}
40+
}, [inputRef, enableInput])
41+
3642
return (
3743
<form onSubmit={handleFormSubmit}>
38-
<TextField
39-
autoComplete="off"
44+
<input
45+
style={{
46+
width: '100%',
47+
padding: '10px',
48+
fontSize: '16px',
49+
border: '1px solid #ccc',
50+
borderRadius: '4px',
51+
boxSizing: 'border-box'
52+
}}
53+
autoComplete='off'
54+
disabled={!enableInput}
4055
placeholder={placeholder}
4156
ref={inputRef}
42-
disabled={!enableInput}
43-
inputRef={inputRef}
44-
size="small"
45-
fullWidth
46-
variant="outlined"
57+
type="text"
4758
value={message}
4859
onChange={handleChange}
4960
/>

src/components/chat_widget/index.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import { Paper, Popover } from '@mui/material'
2+
import { Box, Popover } from '@mui/material'
33
import ChatUI, { type ChatUIProps } from '../chat'
44
import { type PropsOf } from '@emotion/react'
55

@@ -37,14 +37,9 @@ export const ChatWidget: React.FC<ChatWidgetProps> = ({
3737
anchorOrigin={anchorOrigin}
3838
transformOrigin={transformOrigin}
3939
>
40-
<Paper
41-
sx={{
42-
width,
43-
height
44-
}}
45-
>
40+
<Box sx={{ width, height }}>
4641
<ChatUI {...chatUIProps} />
47-
</Paper>
42+
</Box>
4843
</Popover>
4944
)
5045
}

0 commit comments

Comments
 (0)