Skip to content

Commit

Permalink
add mediaLibraryId, prompt and revisedPrompt to event
Browse files Browse the repository at this point in the history
  • Loading branch information
dhasilva committed Dec 20, 2024
1 parent 691c09a commit 2180c1c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type AiFeedbackThumbsProps = {
ratedItem?: string;
feature?: string;
savedRatings?: Record< string, string >;
options?: {
mediaLibraryId?: number;
prompt?: string;
revisedPrompt?: string;
};
onRate?: ( rating: string ) => void;
};

Expand All @@ -50,6 +55,7 @@ export default function AiFeedbackThumbs( {
ratedItem = '',
feature = '',
savedRatings = {},
options = {},
onRate,
}: AiFeedbackThumbsProps ): React.ReactElement {
if ( ! getFeatureAvailability( 'ai-response-feedback' ) ) {
Expand All @@ -60,7 +66,11 @@ export default function AiFeedbackThumbs( {
const { tracks } = useAnalytics();

useEffect( () => {
setItemsRated( { ...savedRatings, ...itemsRated } );
const newItemsRated = { ...savedRatings, ...itemsRated };

if ( JSON.stringify( newItemsRated ) !== JSON.stringify( itemsRated ) ) {
setItemsRated( newItemsRated );
}
}, [ savedRatings ] );

const checkThumb = ( thumbValue: string ) => {
Expand All @@ -85,6 +95,9 @@ export default function AiFeedbackThumbs( {
tracks.recordEvent( 'jetpack_ai_feedback', {
type: feature,
rating: aiRating,
mediaLibraryId: options.mediaLibraryId || null,
prompt: options.prompt || null,
revisedPrompt: options.revisedPrompt || null,
} );
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const RateLogo: React.FC< {
ratedItem: string;
onRate: ( rating: string ) => void;
} > = ( { disabled, ratedItem, onRate } ) => {
const { logos } = useLogoGenerator();
const { logos, selectedLogo } = useLogoGenerator();
const savedRatings = logos
.filter( logo => logo.rating )
.reduce( ( acc, logo ) => {
Expand All @@ -172,6 +172,10 @@ const RateLogo: React.FC< {
ratedItem={ ratedItem }
feature="logo-generator"
savedRatings={ savedRatings }
options={ {
mediaLibraryId: selectedLogo.mediaId,
prompt: selectedLogo.description,
} }
onRate={ onRate }
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,13 @@ User request:${ prompt }`;
throw error;
}

const revisedPrompt = image.data[ 0 ].revised_prompt || null;

// response_format=url returns object with url, otherwise b64_json
const logo: Logo = {
url: 'data:image/png;base64,' + image.data[ 0 ].b64_json,
description: prompt,
revisedPrompt,
};

try {
Expand All @@ -424,6 +427,7 @@ User request:${ prompt }`;
url: savedLogo.mediaURL,
description: prompt,
mediaId: savedLogo.mediaId,
revisedPrompt,
} );
} catch ( error ) {
storeLogo( logo );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ const MAX_LOGOS = 10;
/**
* Add an entry to the site's logo history.
*
* @param {SaveToStorageProps} saveToStorageProps - The properties to save to storage
* @param {SaveToStorageProps.siteId} saveToStorageProps.siteId - The site ID
* @param {SaveToStorageProps.url} saveToStorageProps.url - The URL of the logo
* @param {SaveToStorageProps.description} saveToStorageProps.description - The description of the logo, based on the prompt used to generate it
* @param {SaveToStorageProps.mediaId} saveToStorageProps.mediaId - The media ID of the logo on the backend
*
* @param {SaveToStorageProps} saveToStorageProps - The properties to save to storage
* @param {SaveToStorageProps.siteId} saveToStorageProps.siteId - The site ID
* @param {SaveToStorageProps.url} saveToStorageProps.url - The URL of the logo
* @param {SaveToStorageProps.description} saveToStorageProps.description - The description of the logo, based on the prompt used to generate it
* @param {SaveToStorageProps.mediaId} saveToStorageProps.mediaId - The media ID of the logo on the backend
* @param {SaveToStorageProps.revisedPrompt} saveToStorageProps.revisedPrompt - The revised prompt of the logo
* @return {Logo} The logo that was saved
*/
export function stashLogo( { siteId, url, description, mediaId }: SaveToStorageProps ) {
export function stashLogo( {
siteId,
url,
description,
mediaId,
revisedPrompt,
}: SaveToStorageProps ) {
const storedContent = getSiteLogoHistory( siteId );

const logo: Logo = {
url,
description,
mediaId,
revisedPrompt,
};

storedContent.push( logo );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export type Logo = {
description: string;
mediaId?: number;
rating?: string;
revisedPrompt?: string;
};

export type RequestError = string | Error | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import './carrousel.scss';
export type CarrouselImageData = {
image?: string;
libraryId?: number | string;
prompt?: string;
revisedPrompt?: string;
libraryUrl?: string;
generating?: boolean;
error?: {
Expand Down Expand Up @@ -108,7 +110,7 @@ export default function Carrousel( {
<div className="ai-assistant-image__carrousel">
<div className="ai-assistant-image__carrousel-images">
{ images.length > 1 && prevButton }
{ images.map( ( { image, generating, error }, index ) => (
{ images.map( ( { image, generating, error, revisedPrompt }, index ) => (
<div
key={ `image:` + index }
className={ clsx( 'ai-assistant-image__carrousel-image-container', {
Expand Down Expand Up @@ -149,7 +151,11 @@ export default function Carrousel( {
<AiIcon />
</BlankImage>
) : (
<img className="ai-assistant-image__carrousel-image" src={ image } alt="" />
<img
className="ai-assistant-image__carrousel-image"
src={ image }
alt={ revisedPrompt }
/>
) }
</>
) }
Expand All @@ -171,6 +177,11 @@ export default function Carrousel( {
disabled={ aiFeedbackDisabled( images[ current ] ) }
ratedItem={ images[ current ].libraryUrl || '' }
iconSize={ 20 }
options={ {
mediaLibraryId: Number( images[ current ].libraryId ),
prompt: images[ current ].prompt,
revisedPrompt: images[ current ].revisedPrompt,
} }
feature="image-generator"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ export default function useAiImage( {
.then( result => {
if ( result.data.length > 0 ) {
const image = 'data:image/png;base64,' + result.data[ 0 ].b64_json;
updateImages( { image }, pointer.current );
const prompt = userPrompt || null;
const revisedPrompt = result.data[ 0 ].revised_prompt || null;
updateImages( { image, prompt, revisedPrompt }, pointer.current );
updateRequestsCount();
saveToMediaLibrary( image, name )
.then( savedImage => {
Expand All @@ -181,7 +183,7 @@ export default function useAiImage( {
image,
libraryId: savedImage?.id,
libraryUrl: savedImage?.url,
revisedPrompt: result.data[ 0 ].revised_prompt || '',
revisedPrompt,
} );
} )
.catch( () => {
Expand Down

0 comments on commit 2180c1c

Please sign in to comment.