docs: open AI assistant based on search query (#13063)

* docs: open AI assistant based on search query

* use URLSearchParams

* fix vale error
This commit is contained in:
Shahed Nasser
2025-07-28 12:59:09 +03:00
committed by GitHub
parent ea9ed62ee0
commit 7a07319a69
5 changed files with 162 additions and 88 deletions
@@ -1,7 +1,7 @@
import React, { useEffect, useRef } from "react"
import React, { useEffect, useMemo, useRef } from "react"
import clsx from "clsx"
import { ArrowUpCircleSolid } from "@medusajs/icons"
import { useAiAssistant } from "../../../../providers"
import { useAiAssistant, useIsBrowser } from "../../../../providers"
import { useChat } from "@kapaai/react-sdk"
import { useAiAssistantChatNavigation } from "../../../../hooks"
@@ -12,8 +12,20 @@ type AiAssistantChatWindowInputProps = {
export const AiAssistantChatWindowInput = ({
chatWindowRef,
}: AiAssistantChatWindowInputProps) => {
const { chatOpened, inputRef, loading } = useAiAssistant()
const { chatOpened, inputRef, loading, setChatOpened } = useAiAssistant()
const { submitQuery, conversation } = useChat()
const { isBrowser } = useIsBrowser()
const { searchQuery, searchQueryType } = useMemo(() => {
if (!isBrowser) {
return {}
}
const searchParams = new URLSearchParams(location.search)
return {
searchQuery: searchParams.get("query"),
searchQueryType: searchParams.get("queryType"),
}
}, [isBrowser])
const [question, setQuestion] = React.useState("")
const formRef = useRef<HTMLFormElement | null>(null)
@@ -97,6 +109,21 @@ export const AiAssistantChatWindowInput = ({
question,
})
useEffect(() => {
if (searchQueryType === "submit") {
onSubmit()
}
}, [searchQueryType])
useEffect(() => {
if (!searchQuery) {
return
}
setQuestion(searchQuery)
setChatOpened(true)
}, [searchQuery])
return (
<div
className={clsx(