| @@ -1,5 +1,5 @@ | ||
| 1 | -import { getThemeVariation } from '@auto-launch/fetchers/get-variation'; | |
| 1 | +import { getStyleDocument } from '@auto-launch/fetchers/get-style-document'; | |
| 2 | 2 | import { getStyleShape, styleShape } from '@auto-launch/fetchers/shape'; |
| 3 | 3 | import { |
| 4 | 4 | failWithFallback, |
| 5 | 5 | fetchWithTimeout, |
| @@ -6,49 +6,65 @@ | ||
| 6 | 6 | retryTwice, |
| 7 | 7 | setStatus, |
| 8 | 8 | } from '@auto-launch/functions/helpers'; |
| 9 | 9 | import { updateOption } from '@auto-launch/functions/wp'; |
| 10 | +import { launchStrings } from '@auto-launch/strings'; | |
| 10 | 11 | import { AI_HOST } from '@constants'; |
| 12 | +import { digest } from '@shared/api/digest'; | |
| 11 | 13 | import { reqDataBasics } from '@shared/lib/data'; |
| 12 | -import { __ } from '@wordpress/i18n'; | |
| 13 | 14 | import { z } from 'zod'; |
| 14 | 15 | |
| 15 | 16 | const fallback = { siteStyle: {} }; |
| 16 | -const url = `${AI_HOST}/api/styles`; | |
| 17 | +const url = `${AI_HOST}/api/style-recipes`; | |
| 17 | 18 | const method = 'POST'; |
| 18 | 19 | const headers = { 'Content-Type': 'application/json' }; |
| 19 | 20 | |
| 20 | -// variation gets merged in after fetch | |
| 21 | +// The styles route serves no global-styles document. | |
| 21 | 22 | const shapeLocal = z.array(styleShape.omit({ variation: true })); |
| 22 | 23 | |
| 23 | 24 | export const handleSiteStyle = async ({ siteProfile }) => { |
| 24 | 25 | // translators: this is for a action log UI. Keep it short |
| 25 | - setStatus(__('Picking the perfect design', 'extendify-local')); | |
| 26 | + setStatus(launchStrings().statusStyle); | |
| 26 | 27 | |
| 27 | 28 | const body = JSON.stringify({ ...reqDataBasics, siteProfile, count: 1 }); |
| 28 | 29 | |
| 29 | 30 | const response = await retryTwice(() => |
| 30 | 31 | fetchWithTimeout(url, { method, headers, body }, 20_000), |
| 31 | - ); | |
| 32 | + ).catch((error) => { | |
| 33 | + return { ok: false, statusText: error.message, status: 0 }; | |
| 34 | + }); | |
| 32 | 35 | |
| 33 | - if (!response?.ok) return fallback; | |
| 36 | + if (!response?.ok) { | |
| 37 | + digest({ | |
| 38 | + error: { | |
| 39 | + message: response.statusText, | |
| 40 | + name: 'FetchError', | |
| 41 | + status: response.status, | |
| 42 | + }, | |
| 43 | + details: { source: 'auto-launch', caller: 'handleSiteStyle' }, | |
| 44 | + }); | |
| 45 | + return fallback; | |
| 46 | + } | |
| 34 | 47 | |
| 35 | - return failWithFallback(async () => { | |
| 36 | - const data = await response.json(); | |
| 37 | - const style = shapeLocal.parse(data)[0]; | |
| 38 | - const variation = await getThemeVariation( | |
| 39 | - { | |
| 40 | - slug: style.colorPalette, | |
| 48 | + return failWithFallback( | |
| 49 | + async () => { | |
| 50 | + const data = await response.json(); | |
| 51 | + const style = shapeLocal.parse(data)[0]; | |
| 52 | + const variation = await getStyleDocument({ | |
| 53 | + colorPalette: style.colorPalette, | |
| 41 | 54 | fonts: style.fonts, |
| 42 | - }, | |
| 43 | - { fallback: true }, | |
| 44 | - ); | |
| 45 | - const siteStyle = { ...style, variation }; | |
| 46 | - await updateOption('extendify_site_style', siteStyle); | |
| 47 | - // Set animation default | |
| 48 | - await updateOption('extendify_animation_settings', { | |
| 49 | - type: style.animation ?? 'fade', | |
| 50 | - speed: 'medium', | |
| 51 | - }); | |
| 52 | - return getStyleShape.parse({ siteStyle }); | |
| 53 | - }, fallback); | |
| 55 | + }); | |
| 56 | + const siteStyle = { ...style, variation }; | |
| 57 | + // The Agent reads extendify_siteStyle; the legacy row keeps old readers. | |
| 58 | + await updateOption('extendify_siteStyle', siteStyle); | |
| 59 | + await updateOption('extendify_site_style', siteStyle); | |
| 60 | + // Set animation default | |
| 61 | + await updateOption('extendify_animation_settings', { | |
| 62 | + type: style.animation ?? 'fade', | |
| 63 | + speed: 'medium', | |
| 64 | + }); | |
| 65 | + return getStyleShape.parse({ siteStyle }); | |
| 66 | + }, | |
| 67 | + fallback, | |
| 68 | + { caller: 'handleSiteStyle' }, | |
| 69 | + ); | |
| 54 | 70 | }; |