-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathChatMessageInput.tsx
More file actions
203 lines (183 loc) · 6.18 KB
/
ChatMessageInput.tsx
File metadata and controls
203 lines (183 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import {
autoUpdate,
flip,
FloatingFocusManager,
FloatingList,
offset,
shift,
useDismiss,
useFloating,
useInteractions,
useListNavigation,
useRole,
} from '@floating-ui/react'
import cx from 'classnames'
import { FC, memo, ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { GoBook } from 'react-icons/go'
import { SiMagisk } from 'react-icons/si'
import { trackEvent } from '~app/plausible'
import { Prompt } from '~services/prompts'
import Button from '../Button'
import PromptCombobox, { ComboboxContext } from '../PromptCombobox'
import { PromptLibraryDialog, MagiskLibraryDialog} from '../PromptLibrary/Dialog'
import TextInput from './TextInput'
interface Props {
mode: 'full' | 'compact'
onSubmit: (value: string) => void
className?: string
disabled?: boolean
placeholder?: string
actionButton?: ReactNode | null
autoFocus?: boolean
}
const ChatMessageInput: FC<Props> = (props) => {
const { t } = useTranslation()
const { placeholder = t('Use / to select prompts, Shift+Enter to add new line') } = props
const [value, setValue] = useState('')
const formRef = useRef<HTMLFormElement>(null)
const inputRef = useRef<HTMLTextAreaElement>(null)
const [isPromptLibraryDialogOpen, setIsPromptLibraryDialogOpen] = useState(false)
const [isMagiskLibraryDialogOpen, setIsMagiskLibraryDialogOpen] = useState(false)
const [activeIndex, setActiveIndex] = useState<number | null>(null)
const [isComboboxOpen, setIsComboboxOpen] = useState(false)
const { refs, floatingStyles, context } = useFloating({
whileElementsMounted: autoUpdate,
middleware: [offset(15), flip(), shift()],
placement: 'top-start',
open: isComboboxOpen,
onOpenChange: setIsComboboxOpen,
})
const floatingListRef = useRef([])
const handleSelect = useCallback((p: Prompt) => {
if (p.id === 'PROMPT_LIBRARY') {
setIsPromptLibraryDialogOpen(true)
setIsComboboxOpen(false)
trackEvent('open_prompt_library', { source: 'combobox' })
} else {
setValue(p.prompt)
setIsComboboxOpen(false)
inputRef.current?.focus()
trackEvent('use_prompt', { source: 'combobox' })
}
}, [])
const listNavigation = useListNavigation(context, {
listRef: floatingListRef,
activeIndex,
onNavigate: setActiveIndex,
loop: true,
focusItemOnOpen: true,
openOnArrowKeyDown: false,
})
const dismiss = useDismiss(context)
const role = useRole(context, { role: 'listbox' })
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions([role, dismiss, listNavigation])
const comboboxContext = useMemo(
() => ({
activeIndex,
getItemProps,
handleSelect,
setIsComboboxOpen,
}),
[activeIndex, getItemProps, handleSelect],
)
useEffect(() => {
if (!props.disabled && props.autoFocus) {
inputRef.current?.focus()
}
}, [props.autoFocus, props.disabled])
useEffect(() => {
if (!props.disabled && !isComboboxOpen && props.mode === 'full') {
inputRef.current?.focus()
}
}, [setIsComboboxOpen, props.disabled, isComboboxOpen, props.mode])
const onFormSubmit = useCallback(
(e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
if (value.trim()) {
props.onSubmit(value)
}
setValue('')
},
[props, value],
)
const onValueChange = useCallback((v: string) => {
setValue(v)
setIsComboboxOpen(v === '/')
}, [])
const insertTextAtCursor = useCallback(
(text: string) => {
const cursorPosition = inputRef.current?.selectionStart || 0
const textBeforeCursor = value.slice(0, cursorPosition)
const textAfterCursor = value.slice(cursorPosition)
setValue(`${textBeforeCursor}${text}${textAfterCursor}`)
setIsPromptLibraryDialogOpen(false)
inputRef.current?.focus()
},
[value],
)
const openPromptLibrary = useCallback(() => {
setIsPromptLibraryDialogOpen(true)
trackEvent('open_prompt_library')
}, [])
const openMagiskLibrary = useCallback(() => {
setIsMagiskLibraryDialogOpen(true)
trackEvent('open_magisk_library')
}, [])
return (
<form className={cx('flex flex-row items-center gap-3', props.className)} onSubmit={onFormSubmit} ref={formRef}>
{props.mode === 'full' && (
<>
<GoBook size={22} color="#707070" className="cursor-pointer" onClick={openPromptLibrary} />
{isPromptLibraryDialogOpen && (
<PromptLibraryDialog
isOpen={true}
onClose={() => setIsPromptLibraryDialogOpen(false)}
insertPrompt={insertTextAtCursor}
/>
)}
<SiMagisk size={22} color="#707070" className="cursor-pointer" onClick={openMagiskLibrary} />
{isMagiskLibraryDialogOpen && (
<MagiskLibraryDialog
isOpen={true}
onClose={() => setIsMagiskLibraryDialogOpen(false)}
insertMagisk={insertTextAtCursor}
/>
)}
<ComboboxContext.Provider value={comboboxContext}>
{isComboboxOpen && (
<FloatingFocusManager context={context} modal={false} initialFocus={-1}>
<div
ref={refs.setFloating}
style={{
...floatingStyles,
}}
{...getFloatingProps()}
>
<FloatingList elementsRef={floatingListRef}>
<PromptCombobox />
</FloatingList>
</div>
</FloatingFocusManager>
)}
</ComboboxContext.Provider>
</>
)}
<div className="w-full flex flex-col justify-center" ref={refs.setReference} {...getReferenceProps()}>
<TextInput
ref={inputRef}
formref={formRef}
name="input"
disabled={props.disabled}
placeholder={placeholder as string}
value={value}
onValueChange={onValueChange}
/>
</div>
{props.actionButton || (
<Button text="-" className="invisible" size={props.mode === 'full' ? 'normal' : 'small'} />
)}
</form>
)
}
export default memo(ChatMessageInput)