/** * External dependencies */ import { useAnalytics } from '@automattic/jetpack-shared-extension-utils'; import { Button, Icon } from '@wordpress/components'; import { useDispatch } from '@wordpress/data'; import { __, _x } from '@wordpress/i18n'; import debugFactory from 'debug'; /** * Internal dependencies */ import AiFeedbackThumbs from '../../components/ai-feedback/index.tsx'; import CheckIcon from '../assets/icons/check.tsx'; import LogoIcon from '../assets/icons/logo.tsx'; import MediaIcon from '../assets/icons/media.tsx'; import { EVENT_SAVE, EVENT_USE } from '../constants.ts'; import useLogoGenerator from '../hooks/use-logo-generator.ts'; import useRequestErrors from '../hooks/use-request-errors.ts'; import { updateLogo } from '../lib/logo-storage.ts'; import { STORE_NAME } from '../store/index.ts'; import { ImageLoader } from './image-loader.tsx'; import './logo-presenter.scss'; /** * Types */ import type { Logo } from '../store/types.ts'; import type { LogoPresenterProps } from '../types.ts'; import type { FC, ReactNode } from 'react'; const debug = debugFactory( 'jetpack-ai-calypso:logo-presenter' ); const SaveInLibraryButton: FC< { siteId: string } > = ( { siteId } ) => { const { tracks } = useAnalytics(); const { recordEvent: recordTracksEvent } = tracks; const { saveLogo, selectedLogo, isSavingLogoToLibrary: saving, logos, selectedLogoIndex, context, } = useLogoGenerator(); const saved = !! selectedLogo?.mediaId; const { loadLogoHistory } = useDispatch( STORE_NAME ); const handleClick = async () => { if ( ! saved && ! saving ) { recordTracksEvent( EVENT_SAVE, { context, logos_count: logos.length, selected_logo: selectedLogoIndex ? selectedLogoIndex + 1 : 0, } ); try { const savedLogo = await saveLogo( selectedLogo ); // Update localStorage updateLogo( { siteId, url: selectedLogo.url, newUrl: savedLogo.mediaURL, mediaId: savedLogo.mediaId, } ); // Update state loadLogoHistory( siteId ); } catch ( error ) { debug( 'Error saving logo', error ); } } }; const savingLabel = _x( 'Saving…', 'Logo save button', 'jetpack-ai-client' ); const savedLabel = __( 'Saved', 'jetpack-ai-client' ); return ! saving && ! saved ? ( ) : ( ); }; const UseOnSiteButton: FC< { onApplyLogo: ( mediaId: number ) => void } > = ( { onApplyLogo } ) => { const { tracks } = useAnalytics(); const { recordEvent: recordTracksEvent } = tracks; const { isSavingLogoToLibrary, selectedLogo, logos, selectedLogoIndex, context } = useLogoGenerator(); const handleClick = async () => { if ( ! isSavingLogoToLibrary ) { recordTracksEvent( EVENT_USE, { context, logos_count: logos.length, selected_logo: selectedLogoIndex != null ? selectedLogoIndex + 1 : 0, } ); onApplyLogo?.( selectedLogo?.mediaId ); } }; return ( ); }; const LogoLoading: FC = () => { return ( <> { __( 'Generating new logo…', 'jetpack-ai-client' ) } ); }; const LogoFetching: FC = () => { return ( <> { __( 'Fetching previous logos…', 'jetpack-ai-client' ) } ); }; const LogoEmpty: FC = () => { return ( <>
{ __( 'Once you generate a logo, it will show up here', 'jetpack-ai-client' ) } ); }; const RateLogo: FC< { disabled: boolean; ratedItem: string; onRate: ( rating: string ) => void; } > = ( { disabled, ratedItem, onRate } ) => { const { logos, selectedLogo } = useLogoGenerator(); const savedRatings = logos .filter( logo => logo.rating ) .reduce( ( acc, logo ) => { acc[ logo.url ] = logo.rating; return acc; }, {} ); return ( ); }; const LogoReady: FC< { siteId: string; logo: Logo; onApplyLogo: ( mediaId: number ) => void; } > = ( { siteId, logo, onApplyLogo } ) => { const handleRateLogo = ( rating: string ) => { // Update localStorage updateLogo( { siteId, url: logo.url, newUrl: logo.url, mediaId: logo.mediaId, rating, } ); }; return ( <> {
{ logo.description }
); }; const LogoUpdated: FC< { logo: Logo } > = ( { logo } ) => { return ( <> {
} /> { __( 'Your new logo was set to the block!', 'jetpack-ai-client' ) }
); }; export const LogoPresenter: FC< LogoPresenterProps > = ( { logo = null, loading = false, onApplyLogo, logoAccepted = false, siteId, } ) => { const { isRequestingImage } = useLogoGenerator(); const { saveToLibraryError, logoUpdateError } = useRequestErrors(); let logoContent: ReactNode; if ( ! logo && ! isRequestingImage ) { logoContent = ; } else if ( ! logo ) { debug( 'No logo provided, history still loading or logo being generated' ); logoContent = ; } else if ( loading || isRequestingImage ) { logoContent = ; } else if ( logoAccepted ) { logoContent = ; } else { logoContent = ( ); } return (
{ logoContent }
{ ! logoAccepted && (
) }
{ saveToLibraryError && (
{ __( 'Error saving the logo to your library. Please try again.', 'jetpack-ai-client' ) }
) } { logoUpdateError && (
{ __( 'Error applying the logo to your site. Please try again.', 'jetpack-ai-client' ) }
) }
); };