From 7a07319a69180a198fa14a05336236218ab10ec2 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Mon, 28 Jul 2025 12:59:09 +0300 Subject: [PATCH] docs: open AI assistant based on search query (#13063) * docs: open AI assistant based on search query * use URLSearchParams * fix vale error --- .../learn/introduction/from-v1-to-v2/page.mdx | 2 +- .../AiAssistant/ChatWindow/Input/index.tsx | 33 ++++- .../AiAssistant/ThreadItem/Actions/index.tsx | 98 ++++++++++----- .../AiAssistant/ThreadItem/index.tsx | 115 ++++++++++-------- .../src/components/InlineCode/index.tsx | 2 +- 5 files changed, 162 insertions(+), 88 deletions(-) diff --git a/www/apps/book/app/learn/introduction/from-v1-to-v2/page.mdx b/www/apps/book/app/learn/introduction/from-v1-to-v2/page.mdx index ea3e1d8677..6c6e6de368 100644 --- a/www/apps/book/app/learn/introduction/from-v1-to-v2/page.mdx +++ b/www/apps/book/app/learn/introduction/from-v1-to-v2/page.mdx @@ -1897,7 +1897,7 @@ If your use case is complex and these rules are not enough, you can create a new Medusa v1 has gift card features out-of-the-box. -In Medusa v2, gift card features are now only available to [Medusa Cloud](https://medusajs.com/cloud/) users. +In Medusa v2, gift card features are now only available to [Cloud](https://medusajs.com/cloud/) users. --- diff --git a/www/packages/docs-ui/src/components/AiAssistant/ChatWindow/Input/index.tsx b/www/packages/docs-ui/src/components/AiAssistant/ChatWindow/Input/index.tsx index 43601efd03..02300ef9ed 100644 --- a/www/packages/docs-ui/src/components/AiAssistant/ChatWindow/Input/index.tsx +++ b/www/packages/docs-ui/src/components/AiAssistant/ChatWindow/Input/index.tsx @@ -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(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 (
{ const [feedback, setFeedback] = useState(null) const { addFeedback } = useChat() + const { + config: { baseUrl }, + } = useSiteConfig() + const { handleCopy, isCopied } = useCopy( + `${baseUrl}?query=${encodeURI(item.content)}` + ) const handleFeedback = async ( reaction: Reaction, @@ -31,36 +46,59 @@ export const AiAssistantThreadItemActions = ({ } return ( -
- {item.sources !== undefined && item.sources.length > 0 && ( -
- {item.sources.map((source) => ( - - - {source.title} - - - ))} +
+ {item.type === "question" && ( +
+ + {isCopied ? : } +
)} -
- {(feedback === null || feedback === "upvote") && ( - handleFeedback("upvote", item.question_id)} - className={clsx(feedback === "upvote" && "!text-medusa-fg-muted")} - > - - - )} - {(feedback === null || feedback === "downvote") && ( - handleFeedback("downvote", item.question_id)} - className={clsx(feedback === "downvote" && "!text-medusa-fg-muted")} - > - - - )} -
+ {item.type === "answer" && ( + <> + {item.sources !== undefined && item.sources.length > 0 && ( +
+ {item.sources.map((source) => ( + + + {source.title} + + + ))} +
+ )} +
+ {(feedback === null || feedback === "upvote") && ( + handleFeedback("upvote", item.question_id)} + className={clsx( + feedback === "upvote" && "!text-medusa-fg-muted" + )} + > + + + )} + {(feedback === null || feedback === "downvote") && ( + + handleFeedback("downvote", item.question_id) + } + className={clsx( + feedback === "downvote" && "!text-medusa-fg-muted" + )} + > + + + )} +
+ + )}
) } diff --git a/www/packages/docs-ui/src/components/AiAssistant/ThreadItem/index.tsx b/www/packages/docs-ui/src/components/AiAssistant/ThreadItem/index.tsx index 40680fcac8..510689d5e5 100644 --- a/www/packages/docs-ui/src/components/AiAssistant/ThreadItem/index.tsx +++ b/www/packages/docs-ui/src/components/AiAssistant/ThreadItem/index.tsx @@ -41,80 +41,89 @@ export const AiAssistantThreadItem = ({ item }: AiAssistantThreadItemProps) => {
- {item.type === "question" && ( - { - return ( - - ) - }, - }} - > - {item.content} - - )} - {item.type === "answer" && ( -
- {showLoading && } +
+ {item.type === "question" && ( { return ( ) }, }} - disallowedElements={["h1", "h2", "h3", "h4", "h5", "h6"]} > {item.content} - {item.question_id && } -
+ )} + {item.type === "answer" && ( + <> + {showLoading && } + { + return ( + + ) + }, + }} + disallowedElements={["h1", "h2", "h3", "h4", "h5", "h6"]} + > + {item.content} + + + )} +
+ {(item.question_id || item.type === "question") && ( + )} {item.type === "error" && ( {item.content} diff --git a/www/packages/docs-ui/src/components/InlineCode/index.tsx b/www/packages/docs-ui/src/components/InlineCode/index.tsx index cd84b9e593..b710cbc2c3 100644 --- a/www/packages/docs-ui/src/components/InlineCode/index.tsx +++ b/www/packages/docs-ui/src/components/InlineCode/index.tsx @@ -23,7 +23,7 @@ export const InlineCode = ({