Skip to content

Commit 5d79dc8

Browse files
authored
docs: handle recaptcha loading before submitting AI assistant queries (medusajs#13065)
1 parent 7a07319 commit 5d79dc8

File tree

2 files changed

+34
-12
lines changed
  • www/packages/docs-ui/src

2 files changed

+34
-12
lines changed

www/packages/docs-ui/src/components/AiAssistant/ChatWindow/Input/index.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ type AiAssistantChatWindowInputProps = {
1212
export const AiAssistantChatWindowInput = ({
1313
chatWindowRef,
1414
}: AiAssistantChatWindowInputProps) => {
15-
const { chatOpened, inputRef, loading, setChatOpened } = useAiAssistant()
15+
const { chatOpened, inputRef, loading, setChatOpened, isCaptchaLoaded } =
16+
useAiAssistant()
1617
const { submitQuery, conversation } = useChat()
1718
const { isBrowser } = useIsBrowser()
1819
const { searchQuery, searchQueryType } = useMemo(() => {
@@ -29,9 +30,12 @@ export const AiAssistantChatWindowInput = ({
2930
const [question, setQuestion] = React.useState("")
3031
const formRef = useRef<HTMLFormElement | null>(null)
3132

32-
const onSubmit = (e?: React.FormEvent<HTMLFormElement>) => {
33+
const onSubmit = (
34+
e?: React.FormEvent<HTMLFormElement>,
35+
overrideQuestion?: string
36+
) => {
3337
e?.preventDefault()
34-
submitQuery(question)
38+
submitQuery(overrideQuestion || question)
3539
setQuestion("")
3640
}
3741

@@ -110,19 +114,17 @@ export const AiAssistantChatWindowInput = ({
110114
})
111115

112116
useEffect(() => {
113-
if (searchQueryType === "submit") {
114-
onSubmit()
115-
}
116-
}, [searchQueryType])
117-
118-
useEffect(() => {
119-
if (!searchQuery) {
117+
if (!searchQuery || !isCaptchaLoaded) {
120118
return
121119
}
122120

123121
setQuestion(searchQuery)
124122
setChatOpened(true)
125-
}, [searchQuery])
123+
if (searchQueryType !== "submit") {
124+
return
125+
}
126+
onSubmit(undefined, searchQuery)
127+
}, [searchQuery, searchQueryType, isCaptchaLoaded])
126128

127129
return (
128130
<div
@@ -158,7 +160,7 @@ export const AiAssistantChatWindowInput = ({
158160
"appearance-none p-0 text-medusa-fg-base disabled:text-medusa-fg-disabled",
159161
"transition-colors"
160162
)}
161-
disabled={!question || loading}
163+
disabled={!question || loading || !isCaptchaLoaded}
162164
>
163165
<ArrowUpCircleSolid />
164166
</button>

www/packages/docs-ui/src/providers/AiAssistant/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type AiAssistantContextType = {
2424
inputRef: React.RefObject<HTMLInputElement | HTMLTextAreaElement | null>
2525
contentRef: React.RefObject<HTMLDivElement | null>
2626
loading: boolean
27+
isCaptchaLoaded: boolean
2728
}
2829

2930
export type AiAssistantThreadItem = {
@@ -59,6 +60,7 @@ const AiAssistantInnerProvider = ({
5960
setOnCompleteAction,
6061
type,
6162
}: AiAssistantInnerProviderProps) => {
63+
const [isCaptchaLoaded, setIsCaptchaLoaded] = useState(false)
6264
const [chatOpened, setChatOpened] = useState(false)
6365
const inputRef = useRef<HTMLInputElement | HTMLTextAreaElement>(null)
6466
const contentRef = useRef<HTMLDivElement>(null)
@@ -146,6 +148,23 @@ const AiAssistantInnerProvider = ({
146148

147149
const recaptchaElm = document.querySelector(".grecaptcha-badge")
148150
recaptchaElm?.parentElement?.classList.add("absolute")
151+
const maxRetry = 10
152+
let retries = 0
153+
const interval = setInterval(() => {
154+
if (window.grecaptcha) {
155+
setIsCaptchaLoaded(true)
156+
clearInterval(interval)
157+
return
158+
}
159+
retries++
160+
if (retries > maxRetry) {
161+
clearInterval(interval)
162+
}
163+
}, 1000)
164+
165+
return () => {
166+
clearInterval(interval)
167+
}
149168
}, [isBrowser])
150169

151170
return (
@@ -157,6 +176,7 @@ const AiAssistantInnerProvider = ({
157176
inputRef,
158177
contentRef,
159178
loading,
179+
isCaptchaLoaded,
160180
}}
161181
>
162182
{children}

0 commit comments

Comments
 (0)