| @@ -5,29 +5,38 @@ | ||
| 5 | 5 | fetchWithTimeout, |
| 6 | 6 | retryTwice, |
| 7 | 7 | setStatus, |
| 8 | 8 | } from '@auto-launch/functions/helpers'; |
| 9 | +import { launchStrings } from '@auto-launch/strings'; | |
| 9 | 10 | import { IMAGES_HOST } from '@constants'; |
| 10 | 11 | import { reqDataBasics } from '@shared/lib/data'; |
| 11 | -import { __ } from '@wordpress/i18n'; | |
| 12 | 12 | |
| 13 | -const fallback = { siteImages: [] }; | |
| 14 | -const url = `${IMAGES_HOST}/api/search`; | |
| 13 | +const fallback = { siteImages: { hero: [], general: [] } }; | |
| 14 | +const url = `${IMAGES_HOST}/api/images`; | |
| 15 | +const { wpLanguage } = window.extSharedData; | |
| 15 | 16 | const method = 'POST'; |
| 16 | 17 | const headers = { 'Content-Type': 'application/json' }; |
| 18 | +// A shipped build can never change a count it sends; the service can. | |
| 19 | +const imageTypes = [{ type: 'hero' }, { type: 'general' }]; | |
| 17 | 20 | const MIN_POOL_SIZE = 10; |
| 18 | 21 | |
| 22 | +const asImages = (urls) => urls.map((link) => ({ url: link })); | |
| 23 | +// The build's reusable images exclude its hero, so they are section photos. | |
| 24 | +const asSections = (urls) => ({ hero: [], general: asImages(urls) }); | |
| 25 | + | |
| 19 | 26 | export const handleSiteImages = async ({ siteProfile, designBuild }) => { |
| 20 | 27 | // translators: this is for a action log UI. Keep it short |
| 21 | - setStatus(__('Finding the perfect images', 'extendify-local')); | |
| 28 | + setStatus(launchStrings().statusImages); | |
| 22 | 29 | |
| 23 | 30 | // Reuse the design preview's own images; only search for what is missing. |
| 24 | 31 | const seeded = collectBuiltPageImageUrls(designBuild?.builtPages); |
| 25 | - if (seeded.length >= MIN_POOL_SIZE) return { siteImages: seeded }; | |
| 32 | + if (seeded.length >= MIN_POOL_SIZE) return { siteImages: asSections(seeded) }; | |
| 26 | 33 | |
| 27 | 34 | const body = JSON.stringify({ |
| 28 | 35 | ...reqDataBasics, |
| 29 | 36 | siteProfile, |
| 37 | + lang: wpLanguage, | |
| 38 | + imageTypes, | |
| 30 | 39 | source: 'auto-launch', |
| 31 | 40 | }); |
| 32 | 41 | |
| 33 | 42 | const response = await retryTwice(() => |
| @@ -33,12 +42,15 @@ | ||
| 33 | 42 | const response = await retryTwice(() => |
| 34 | 43 | fetchWithTimeout(url, { method, headers, body }), |
| 35 | 44 | ); |
| 36 | 45 | |
| 37 | - if (!response?.ok) return { siteImages: seeded }; | |
| 46 | + if (!response?.ok) return { siteImages: asSections(seeded) }; | |
| 38 | 47 | |
| 39 | 48 | const { siteImages: found } = await failWithFallback( |
| 40 | - async () => getImagesShape.parse(await response.json()), | |
| 49 | + async () => { | |
| 50 | + const { images } = await response.json(); | |
| 51 | + return getImagesShape.parse({ siteImages: images }); | |
| 52 | + }, | |
| 41 | 53 | fallback, |
| 42 | 54 | { caller: 'handleSiteImages' }, |
| 43 | 55 | ); |
| 44 | 56 | |
| @@ -44,9 +56,11 @@ | ||
| 44 | 56 | |
| 45 | 57 | if (!seeded.length) return { siteImages: found }; |
| 46 | 58 | |
| 47 | 59 | const seededSet = new Set(seeded); |
| 48 | - const topup = found | |
| 49 | - .filter((u) => !seededSet.has(u)) | |
| 60 | + const topup = found.general | |
| 61 | + .filter((image) => !seededSet.has(image.url)) | |
| 50 | 62 | .slice(0, MIN_POOL_SIZE - seeded.length); |
| 51 | - return { siteImages: [...seeded, ...topup] }; | |
| 63 | + return { | |
| 64 | + siteImages: { hero: found.hero, general: [...asImages(seeded), ...topup] }, | |
| 65 | + }; | |
| 52 | 66 | }; |