| 1 |
import { sparkle } from '@agent/icons'; |
| 2 |
import { useSuggestionsStore } from '@agent/state/suggestions'; |
| 3 |
import { useState } from '@wordpress/element'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
import { |
| 6 |
chevronRight, |
| 7 |
drafts, |
| 8 |
help, |
| 9 |
pencil, |
| 10 |
published, |
| 11 |
siteLogo, |
| 12 |
styles, |
| 13 |
swatch, |
| 14 |
typography, |
| 15 |
video, |
| 16 |
} from '@wordpress/icons'; |
| 17 |
|
| 18 |
const icons = { |
| 19 |
styles, |
| 20 |
edit: pencil, |
| 21 |
help, |
| 22 |
video, |
| 23 |
sparkle, |
| 24 |
drafts, |
| 25 |
published, |
| 26 |
typography, |
| 27 |
pencil, |
| 28 |
siteLogo, |
| 29 |
swatch, |
| 30 |
}; |
| 31 |
|
| 32 |
export const ChatSuggestions = ({ suggestions }) => { |
| 33 |
const { markAsClicked, isAvailable, getSuggestions } = useSuggestionsStore(); |
| 34 |
const [allSuggestions, setAllSuggestions] = useState(suggestions); |
| 35 |
|
| 36 |
const handleSubmit = (suggestion) => { |
| 37 |
window.dispatchEvent( |
| 38 |
new CustomEvent('extendify-agent:chat-submit', { |
| 39 |
detail: { message: suggestion.message }, |
| 40 |
}), |
| 41 |
); |
| 42 |
markAsClicked(suggestion); |
| 43 |
}; |
| 44 |
|
| 45 |
const handleShowMore = () => { |
| 46 |
const next = getSuggestions({ slice: 6, exclude: allSuggestions }); |
| 47 |
setAllSuggestions((prev) => [...prev, ...next]); |
| 48 |
}; |
| 49 |
|
| 50 |
if (!suggestions) return null; |
| 51 |
|
| 52 |
return ( |
| 53 |
<> |
| 54 |
{allSuggestions.map((suggestion) => { |
| 55 |
// Hide them if it's not available to the user |
| 56 |
if (!isAvailable(suggestion)) return null; |
| 57 |
return ( |
| 58 |
<SuggestionButton |
| 59 |
key={suggestion.message} |
| 60 |
suggestion={suggestion} |
| 61 |
handleSubmit={handleSubmit} |
| 62 |
/> |
| 63 |
); |
| 64 |
})} |
| 65 |
<ShowMoreButton handleShowMore={handleShowMore} /> |
| 66 |
</> |
| 67 |
); |
| 68 |
}; |
| 69 |
|
| 70 |
const SuggestionButton = ({ suggestion, handleSubmit }) => { |
| 71 |
const icon = icons[suggestion?.icon] ?? icons.sparkle; |
| 72 |
return ( |
| 73 |
<button |
| 74 |
type="button" |
| 75 |
className="group flex items-center justify-between rounded-sm bg-transparent px-1 py-1 text-left text-sm not-italic text-gray-900 transition-colors duration-100 hover:bg-gray-100 focus:outline-hidden focus:ring-2 focus:ring-design-main" |
| 76 |
onClick={() => handleSubmit(suggestion)} |
| 77 |
> |
| 78 |
<div className="flex items-center gap-1.5 leading-none"> |
| 79 |
<span className="h-5 w-5 shrink-0 self-start fill-gray-700"> |
| 80 |
{icon} |
| 81 |
</span> |
| 82 |
<span className="leading-5">{suggestion.message}</span> |
| 83 |
</div> |
| 84 |
<span className="inline-block h-5 w-5 fill-gray-700 leading-none opacity-0 transition-opacity duration-100 group-hover:opacity-100 rtl:scale-x-[-1]"> |
| 85 |
{chevronRight} |
| 86 |
</span> |
| 87 |
</button> |
| 88 |
); |
| 89 |
}; |
| 90 |
|
| 91 |
const ShowMoreButton = ({ handleShowMore }) => { |
| 92 |
const [hide, setHide] = useState(false); |
| 93 |
const handleClick = () => { |
| 94 |
handleShowMore(); |
| 95 |
setHide(true); |
| 96 |
}; |
| 97 |
|
| 98 |
if (hide) return null; |
| 99 |
return ( |
| 100 |
<button |
| 101 |
type="button" |
| 102 |
className="group flex w-full items-center gap-3 rounded-sm bg-transparent px-1 py-1 text-xs not-italic text-gray-800 transition-colors duration-100 hover:text-gray-900 focus:outline-hidden group" |
| 103 |
onClick={handleClick} |
| 104 |
> |
| 105 |
<span className="h-px flex-1 bg-gray-200" /> |
| 106 |
<span className="group-hover:underline group-focus:ring-2 group-focus:ring-design-main"> |
| 107 |
{__('Show more', 'extendify-local')} |
| 108 |
</span> |
| 109 |
<span className="h-px flex-1 bg-gray-200" /> |
| 110 |
</button> |
| 111 |
); |
| 112 |
}; |
| 113 |
|