| 1 |
import { thumbDown, thumbUp } from '@help-center/components/ai-chat/icons'; |
| 2 |
import { rateAnswer } from '@help-center/lib/api'; |
| 3 |
import { useEffect, useState } from '@wordpress/element'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { Icon } from '@wordpress/icons'; |
| 6 |
import classnames from 'classnames'; |
| 7 |
|
| 8 |
export const Rating = ({ answerId }) => { |
| 9 |
const [rating, setRating] = useState(undefined); |
| 10 |
|
| 11 |
useEffect(() => { |
| 12 |
if (!answerId) return; |
| 13 |
if (rating === undefined) return; |
| 14 |
rateAnswer({ answerId, rating }); |
| 15 |
}, [rating, answerId]); |
| 16 |
|
| 17 |
return ( |
| 18 |
<div className="mt-1 flex items-center justify-end gap-0.5 text-right"> |
| 19 |
<button |
| 20 |
type="button" |
| 21 |
aria-pressed={rating === 1} |
| 22 |
aria-live="polite" |
| 23 |
onClick={() => setRating((current) => (current === 1 ? 0 : 1))} |
| 24 |
aria-label={ |
| 25 |
rating === 1 |
| 26 |
? __('Remove rating', 'extendify-local') |
| 27 |
: __('Rate that this answer was helpful', 'extendify-local') |
| 28 |
} |
| 29 |
className={classnames( |
| 30 |
'm-0 h-5 w-5 border-0 bg-transparent p-0 hover:text-design-main', |
| 31 |
{ |
| 32 |
'text-design-main': rating === 1, |
| 33 |
'text-gray-500': rating !== 1, |
| 34 |
}, |
| 35 |
)} |
| 36 |
> |
| 37 |
<Icon className="fill-current" icon={thumbUp} /> |
| 38 |
</button> |
| 39 |
|
| 40 |
<button |
| 41 |
type="button" |
| 42 |
aria-pressed={rating === -1} |
| 43 |
aria-live="polite" |
| 44 |
onClick={() => setRating((current) => (current === -1 ? 0 : -1))} |
| 45 |
aria-label={ |
| 46 |
rating === -1 |
| 47 |
? __('Remove rating', 'extendify-local') |
| 48 |
: __('Rate that this answer was not helpful', 'extendify-local') |
| 49 |
} |
| 50 |
className={classnames( |
| 51 |
'm-0 h-5 w-5 border-0 bg-transparent p-0 hover:text-design-main', |
| 52 |
{ |
| 53 |
'text-design-main': rating === -1, |
| 54 |
'text-gray-500': rating !== -1, |
| 55 |
}, |
| 56 |
)} |
| 57 |
> |
| 58 |
<Icon className="fill-current" icon={thumbDown} /> |
| 59 |
</button> |
| 60 |
</div> |
| 61 |
); |
| 62 |
}; |
| 63 |
|