Skip to content

Commit

Permalink
store rating with other logo information
Browse files Browse the repository at this point in the history
  • Loading branch information
dhasilva committed Dec 20, 2024
1 parent e5dd2f3 commit 1950287
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getJetpackExtensionAvailability,
} from '@automattic/jetpack-shared-extension-utils';
import { Button, Tooltip } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { useEffect, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { thumbsUp, thumbsDown } from '@wordpress/icons';
import clsx from 'clsx';
Expand All @@ -24,6 +24,8 @@ type AiFeedbackThumbsProps = {
iconSize?: number;
ratedItem?: string;
feature?: string;
savedRatings?: Record< string, string >;
onRate?: ( rating: string ) => void;
};

/**
Expand All @@ -47,14 +49,20 @@ export default function AiFeedbackThumbs( {
iconSize = 24,
ratedItem = '',
feature = '',
savedRatings = {},
onRate,
}: AiFeedbackThumbsProps ): React.ReactElement {
if ( ! getFeatureAvailability( 'ai-response-feedback' ) ) {
return null;
}

const [ itemsRated, setItemsRated ] = useState( {} );
const [ itemsRated, setItemsRated ] = useState( savedRatings );
const { tracks } = useAnalytics();

useEffect( () => {
setItemsRated( savedRatings );
}, [ savedRatings ] );

const checkThumb = ( thumbValue: string ) => {
if ( ! itemsRated[ ratedItem ] ) {
return false;
Expand All @@ -72,6 +80,8 @@ export default function AiFeedbackThumbs( {
[ ratedItem ]: aiRating,
} );

onRate?.( aiRating );

tracks.recordEvent( 'jetpack_ai_feedback', {
type: feature,
rating: aiRating,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,25 @@ const LogoEmpty: React.FC = () => {
);
};

const RateLogo: React.FC< { disabled: boolean; ratedItem: string } > = ( {
disabled,
ratedItem,
} ) => {
const RateLogo: React.FC< {
disabled: boolean;
ratedItem: string;
onRate: ( rating: string ) => void;
} > = ( { disabled, ratedItem, onRate } ) => {
const { logos } = useLogoGenerator();
const savedRatings = logos.reduce( ( acc, logo ) => {
acc[ logo.url ] = logo.rating;
return acc;
}, {} );

return (
<>
<AiFeedbackThumbs disabled={ disabled } ratedItem={ ratedItem } feature="logo-generator" />
</>
<AiFeedbackThumbs
disabled={ disabled }
ratedItem={ ratedItem }
feature="logo-generator"
savedRatings={ savedRatings }
onRate={ onRate }
/>
);
};

Expand All @@ -169,6 +180,17 @@ const LogoReady: React.FC< {
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 (
<>
<img
Expand All @@ -183,7 +205,7 @@ const LogoReady: React.FC< {
<div className="jetpack-ai-logo-generator-modal-presenter__actions">
<SaveInLibraryButton siteId={ siteId } />
<UseOnSiteButton onApplyLogo={ onApplyLogo } />
<RateLogo ratedItem={ logo.url } disabled={ false } />
<RateLogo ratedItem={ logo.url } disabled={ false } onRate={ handleRateLogo } />
</div>
</div>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ export function stashLogo( { siteId, url, description, mediaId }: SaveToStorageP
* @param {UpdateInStorageProps.url} updateInStorageProps.url - The URL of the logo to update
* @param {UpdateInStorageProps.newUrl} updateInStorageProps.newUrl - The new URL of the logo
* @param {UpdateInStorageProps.mediaId} updateInStorageProps.mediaId - The new media ID of the logo
* @param {UpdateInStorageProps.rating} updateInStorageProps.rating - The new rating of the logo
* @return {Logo} The logo that was updated
*/
export function updateLogo( { siteId, url, newUrl, mediaId }: UpdateInStorageProps ) {
export function updateLogo( { siteId, url, newUrl, mediaId, rating }: UpdateInStorageProps ) {
const storedContent = getSiteLogoHistory( siteId );

const index = storedContent.findIndex( logo => logo.url === url );

if ( index > -1 ) {
storedContent[ index ].url = newUrl;
storedContent[ index ].mediaId = mediaId;
storedContent[ index ].rating = rating;
}

localStorage.setItem(
Expand Down Expand Up @@ -96,6 +98,7 @@ export function getSiteLogoHistory( siteId: string ) {
url: logo.url,
description: logo.description,
mediaId: logo.mediaId,
rating: logo.rating,
} ) );

return storedContent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export type Logo = {
url: string;
description: string;
mediaId?: number;
rating?: string;
};

export type RequestError = string | Error | null;
Expand Down
1 change: 1 addition & 0 deletions projects/js-packages/ai-client/src/logo-generator/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export type UpdateInStorageProps = {
url: Logo[ 'url' ];
newUrl: Logo[ 'url' ];
mediaId: Logo[ 'mediaId' ];
rating?: Logo[ 'rating' ];
};

export type RemoveFromStorageProps = {
Expand Down

0 comments on commit 1950287

Please sign in to comment.