Skip to content

Commit

Permalink
fix: phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
HardeepAsrani committed Jan 29, 2024
1 parent 967db6d commit 4d42335
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
18 changes: 12 additions & 6 deletions inc/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,15 @@ public function images( \WP_REST_Request $request ) {
/**
* Get JSON from response.
*
* @param array $data Response.
* @param array<object> $data Response.
*
* @return array|false
* @return array<object>|false
*/
private static function process_json_from_response( $data ) {
// Find the target item.
$target = current( $data );

if ( false === $target ) {
// Handle the case where the target is not found.
if ( false === $target || !isset($target->content) ) {
return false;
}

Expand All @@ -419,7 +418,12 @@ private static function process_json_from_response( $data ) {

try {
$json_object = json_decode( $json_string, true );
return $json_object;

if ( is_array( $json_object ) ) {
return $json_object;
}

return false;
} catch ( \Exception $e ) {
// If parsing failed, try to find a JSON array in the string.
preg_match( '/\[(.|\n)*\]/', $json_string, $matches );
Expand All @@ -428,7 +432,9 @@ private static function process_json_from_response( $data ) {
$json_array_string = $matches[0];
$json_object = json_decode( $json_array_string, true );

return $json_object;
if ( is_array( $json_object ) ) {
return $json_object;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/parts/ColorPalette.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const ColorPalette = () => {
<div className="flex items-center gap-4 mt-8">
{ palette.map( ( color ) => (
<ColorPicker
key={ color.slug }
value={ color.color }
onChange={ e => onChangeColor( e, color.slug ) }
/>
Expand Down
4 changes: 2 additions & 2 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const DEFAULT_STATE = {
activeImageKeyword: null,
selectedImages: [],
homepage: null,
siteTopic: null,
siteDescription: null,
siteTopic: '',
siteDescription: '',
isSavimg: false,
hasError: false
};
Expand Down
43 changes: 38 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const PageControlIcon = ({ isFilled = false }) => {
fill="none"
className="hover:fill-white"
stroke="white"
stroke-width="2"
strokeWidth="2"
/>
) }
</SVG>
Expand All @@ -53,8 +53,8 @@ export const Logo = () => {
fill="none"
xmlns="http://www.w3.org/2000/svg">
<Path
fill-rule="evenodd"
clip-rule="evenodd"
fillRule="evenodd"
clipRule="evenodd"
d="M57.5 93C77.6584 93 94 76.6584 94 56.5C94 36.3416 77.6584 20 57.5 20C37.3416 20 21 36.3416 21 56.5C21 76.6584 37.3416 93 57.5 93ZM57.5 73.9565C67.141 73.9565 74.9565 66.141 74.9565 56.5C74.9565 46.859 67.141 39.0435 57.5 39.0435C47.859 39.0435 40.0435 46.859 40.0435 56.5C40.0435 66.141 47.859 73.9565 57.5 73.9565Z"
fill="white"
/>
Expand All @@ -70,6 +70,39 @@ export const Logo = () => {
);
};

/**
* Sometimes OpenAI requests fail, so we try to redo them if that happens.
*/
const retryApiFetch = async( params = [], maxAttempts = 3, delay = 500 ) => {
const { setError } = dispatch( 'quickwp/data' );

let attempts = 0;

const makeRequest = async() => {
try {

const response = await apiFetch({
...params
});

return response;
} catch ( error ) {

attempts++;

if ( attempts < maxAttempts ) {
await new Promise( resolve => setTimeout( resolve, delay ) );
return makeRequest();
} else {
setError( true );
throw error;
}
}
};

return makeRequest();
};

const sendEvent = async( data ) => {
const {
setRunID,
Expand All @@ -91,7 +124,7 @@ const getEvent = async( type ) => {
const threadID = select( 'quickwp/data' ).getThreadID( type );
const route = 'homepage' !== type ? 'get' : 'templates';

const response = await apiFetch({
const response = await retryApiFetch({
path: addQueryArgs( `${ window.quickwp.api }/${ route }`, {
'thread_id': threadID
})
Expand Down Expand Up @@ -189,7 +222,7 @@ const awaitEvent = async( type, interval = 5000 ) => {
export const requestImages = async( query ) => {
const { setImages } = dispatch( 'quickwp/data' );

const response = await apiFetch({
const response = await retryApiFetch({
path: addQueryArgs( `${ window.quickwp.api }/images`, {
query
})
Expand Down

0 comments on commit 4d42335

Please sign in to comment.