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