import { sparkle } from '@agent/icons'; import { useDomainActivities } from '@agent/state/domain-activities'; import { useSuggestionsStore } from '@agent/state/suggestions'; import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { chevronRight, drafts, help, pencil, published, siteLogo, styles, swatch, typography, video, } from '@wordpress/icons'; const icons = { styles, edit: pencil, help, video, sparkle, drafts, published, typography, pencil, siteLogo, swatch, }; export const ChatSuggestions = ({ suggestions }) => { const { markAsClicked, isAvailable, getSuggestions } = useSuggestionsStore(); const { setDomainActivity } = useDomainActivities(); const [allSuggestions, setAllSuggestions] = useState(suggestions); const handleSelect = (suggestion) => { markAsClicked(suggestion); if (suggestion.tracking) { setDomainActivity({ ...suggestion.tracking, action: 'clicked' }); } // External-link suggestions are plain links: the anchor opens the new tab. // They don't trigger a workflow or post a message in the chat. if (suggestion.type === 'external-link') return; window.dispatchEvent( new CustomEvent('extendify-agent:chat-submit', { detail: { message: suggestion.message }, }), ); }; const handleShowMore = () => { const next = getSuggestions({ slice: 6, exclude: allSuggestions }); setAllSuggestions((prev) => [...prev, ...next]); }; if (!suggestions) return null; return ( <> {allSuggestions.map((suggestion) => { // Hide them if it's not available to the user if (!isAvailable(suggestion)) return null; return ( ); })} ); }; const SuggestionButton = ({ suggestion, onSelect }) => { const icon = icons[suggestion?.icon] ?? icons.sparkle; const 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'; const content = ( <>
{icon} {suggestion.message}
{chevronRight} ); // External-link suggestions render as a real anchor that opens in a new tab. if (suggestion.type === 'external-link') { return ( onSelect(suggestion)} > {content} ); } return ( ); }; const ShowMoreButton = ({ handleShowMore }) => { const [hide, setHide] = useState(false); const handleClick = () => { handleShowMore(); setHide(true); }; if (hide) return null; return ( ); };