| 1 |
import { ErrorMessage } from '@help-center/components/ai-chat/Error'; |
| 2 |
import { robot, send } from '@help-center/components/ai-chat/icons'; |
| 3 |
import { Rating } from '@help-center/components/ai-chat/Rating'; |
| 4 |
import { useAIChatStore } from '@help-center/state/ai-chat'; |
| 5 |
import { pasteHandler, serialize } from '@wordpress/blocks'; |
| 6 |
import { useEffect, useRef } from '@wordpress/element'; |
| 7 |
import { __ } from '@wordpress/i18n'; |
| 8 |
import { Icon } from '@wordpress/icons'; |
| 9 |
import classNames from 'classnames'; |
| 10 |
|
| 11 |
export const Answer = ({ question, answer, reset, error, answerId }) => { |
| 12 |
const scrollRef = useRef(null); |
| 13 |
const { addHistory, setCurrentQuestion } = useAIChatStore(); |
| 14 |
|
| 15 |
// check https://github.com/extendify/extendify-sdk/issues/1560 |
| 16 |
const parsedAnswer = pasteHandler({ |
| 17 |
plainText: answer?.replace(/[\r\n]+/g, '<br />') ?? '', |
| 18 |
}); |
| 19 |
const htmlAnswer = Array.isArray(parsedAnswer) |
| 20 |
? serialize(parsedAnswer) |
| 21 |
: parsedAnswer; |
| 22 |
|
| 23 |
useEffect(() => { |
| 24 |
if (!answerId) return; |
| 25 |
const newQuestion = { answerId, htmlAnswer, question, time: Date.now() }; |
| 26 |
addHistory(newQuestion); |
| 27 |
setCurrentQuestion(newQuestion); |
| 28 |
}, [answerId, htmlAnswer, addHistory, question, setCurrentQuestion]); |
| 29 |
|
| 30 |
if (error) { |
| 31 |
return ( |
| 32 |
<div className="overflow-y-auto p-6 pb-10" ref={scrollRef}> |
| 33 |
<div className="relative mb-8 ml-4 flex justify-end"> |
| 34 |
<ErrorMessage |
| 35 |
text={__( |
| 36 |
'Oops! We were unable to send your question.', |
| 37 |
'extendify-local', |
| 38 |
)} |
| 39 |
reset={reset} |
| 40 |
/> |
| 41 |
</div> |
| 42 |
</div> |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
return ( |
| 47 |
<div className="flex h-full flex-col"> |
| 48 |
<div className="grow overflow-y-auto p-6 pb-10" ref={scrollRef}> |
| 49 |
<div className="relative mb-8 ml-4 flex justify-end"> |
| 50 |
<div className="m-0 rounded-lg bg-gray-800 p-5 text-sm text-design-text"> |
| 51 |
{question} |
| 52 |
</div> |
| 53 |
</div> |
| 54 |
<div className="relative"> |
| 55 |
<div className="absolute z-10 -ml-2 -mt-4 flex items-center rounded-full bg-design-main p-2"> |
| 56 |
<Icon |
| 57 |
icon={robot} |
| 58 |
className="h-4 w-4 fill-current text-design-text" |
| 59 |
/> |
| 60 |
</div> |
| 61 |
<div |
| 62 |
className={classNames( |
| 63 |
'm-0 inline-block rounded-lg bg-gray-100 p-5 text-sm text-gray-800', |
| 64 |
{ |
| 65 |
'animate-pulse bg-gray-300': answer === '...', |
| 66 |
'bg-gray-100': answer !== '...', |
| 67 |
}, |
| 68 |
)} |
| 69 |
dangerouslySetInnerHTML={{ |
| 70 |
__html: htmlAnswer, |
| 71 |
}} |
| 72 |
/> |
| 73 |
{answerId && <Rating answerId={answerId} />} |
| 74 |
</div> |
| 75 |
</div> |
| 76 |
<div className="ask-another-question relative flex justify-center p-4"> |
| 77 |
<button |
| 78 |
type="button" |
| 79 |
onClick={reset} |
| 80 |
className="flex items-center gap-2 rounded-xs border-none bg-design-main px-4 py-2 text-sm text-design-text rtl:flex-row-reverse" |
| 81 |
> |
| 82 |
{__('Ask Another Question', 'extendify-local')} |
| 83 |
<Icon |
| 84 |
icon={send} |
| 85 |
className="h-6 fill-current text-design-text rtl:rotate-180" |
| 86 |
/> |
| 87 |
</button> |
| 88 |
</div> |
| 89 |
</div> |
| 90 |
); |
| 91 |
}; |
| 92 |
|