Raw
1 import React, { useState } from "react"
2 import clsx from "clsx"
3 import { Button, type ButtonProps } from "@/components/Button"
4 import {
5 ThumbDown,
6 ThumbUp,
7 Link as LinkIcon,
8 CheckCircle,
9 SquareTwoStack,
10 } from "@medusajs/icons"
11 import { useSiteConfig } from "../../../../providers/SiteConfig"
12 import { AiAssistantThreadItem as AiAssistantThreadItemType } from "../../../../providers/AiAssistant"
13 import { Reaction, useChat } from "@kapaai/react-sdk"
14 import { useCopy } from "../../../../hooks/use-copy"
15 import Link from "next/link"
16
17 export type AiAssistantThreadItemActionsProps = {
18 item: AiAssistantThreadItemType
19 }
20
21 export const AiAssistantThreadItemActions = ({
22 item,
23 }: AiAssistantThreadItemActionsProps) => {
24 const [feedback, setFeedback] = useState<Reaction | null>(null)
25 const { addFeedback } = useChat()
26 const {
27 config: { baseUrl },
28 } = useSiteConfig()
29 const { handleCopy: handleLinkCopy, isCopied: isLinkCopied } = useCopy(
30 `${baseUrl}?query=${encodeURI(item.content)}`
31 )
32 const { handleCopy: handleAnswerCopy, isCopied: isAnswerCopied } = useCopy(
33 item.content
34 )
35
36 const handleFeedback = async (
37 reaction: Reaction,
38 question_id?: string | null
39 ) => {
40 try {
41 if (!question_id || feedback) {
42 return
43 }
44 setFeedback(reaction)
45 addFeedback(question_id, reaction)
46 } catch (error) {
47 console.error("Error sending feedback:", error)
48 }
49 }
50
51 return (
52 <div
53 className={clsx(
54 "flex gap-docs_0.75 items-center",
55 item.type === "question" && "justify-end",
56 item.type === "answer" && "justify-between"
57 )}
58 >
59 {item.type === "question" && (
60 <div className="flex gap-docs_0.25 items-center text-medusa-fg-muted">
61 <ActionButton onClick={handleLinkCopy} data-testid="link-copy-button">
62 {isLinkCopied ? <CheckCircle /> : <LinkIcon />}
63 </ActionButton>
64 </div>
65 )}
66 {item.type === "answer" && (
67 <>
68 {item.sources !== undefined && item.sources.length > 0 && (
69 <div className="flex gap-[6px] items-center flex-wrap">
70 {item.sources.map((source, index) => (
71 <Link
72 key={index}
73 href={source.source_url}
74 className={clsx(
75 "flex items-center justify-center px-[6px] py-px",
76 "rounded-docs_xs bg-medusa-tag-neutral-bg font-monospace",
77 "text-medusa-fg-subtle hover:text-medusa-fg-base",
78 "text-code-paragraph-2xsmall"
79 )}
80 >
81 {source.title}
82 </Link>
83 ))}
84 </div>
85 )}
86 <div className="flex gap-docs_0.25 items-center text-medusa-fg-muted">
87 <ActionButton
88 onClick={handleAnswerCopy}
89 data-testid="answer-copy-button"
90 >
91 {isAnswerCopied ? <CheckCircle /> : <SquareTwoStack />}
92 </ActionButton>
93 {(feedback === null || feedback === "upvote") && (
94 <ActionButton
95 onClick={async () => handleFeedback("upvote", item.question_id)}
96 data-testid="upvote-button"
97 className={clsx(
98 feedback === "upvote" && "!text-medusa-fg-muted"
99 )}
100 >
101 <ThumbUp />
102 </ActionButton>
103 )}
104 {(feedback === null || feedback === "downvote") && (
105 <ActionButton
106 onClick={async () =>
107 handleFeedback("downvote", item.question_id)
108 }
109 data-testid="downvote-button"
110 className={clsx(
111 feedback === "downvote" && "!text-medusa-fg-muted"
112 )}
113 >
114 <ThumbDown />
115 </ActionButton>
116 )}
117 </div>
118 </>
119 )}
120 </div>
121 )
122 }
123
124 const ActionButton = ({ children, className, ...props }: ButtonProps) => {
125 return (
126 <Button
127 variant="transparent"
128 className={clsx(
129 "text-medusa-fg-muted hover:text-medusa-fg-muted",
130 "hover:bg-medusa-bg-subtle-hover",
131 "!p-[4.5px] rounded-docs_sm",
132 className
133 )}
134 {...props}
135 >
136 {children}
137 </Button>
138 )
139 }