| @@ -1,6 +1,6 @@ | ||
| 1 | -import { uploadLogo } from '@auto-launch/fetchers/get-logo'; | |
| 2 | -import { getThemeVariation } from '@auto-launch/fetchers/get-variation'; | |
| 1 | +import { isExternalLogo, uploadLogo } from '@auto-launch/fetchers/get-logo'; | |
| 2 | +import { getStyleDocument } from '@auto-launch/fetchers/get-style-document'; | |
| 3 | 3 | import { |
| 4 | 4 | getDesignBuildShape, |
| 5 | 5 | getLogoShape, |
| 6 | 6 | getPluginsShape, |
| @@ -5,8 +5,9 @@ | ||
| 5 | 5 | getLogoShape, |
| 6 | 6 | getPluginsShape, |
| 7 | 7 | getStyleShape, |
| 8 | 8 | } from '@auto-launch/fetchers/shape'; |
| 9 | +import { importBuiltPagesImages } from '@auto-launch/functions/get-imported-images'; | |
| 9 | 10 | import { |
| 10 | 11 | fetchWithTimeout, |
| 11 | 12 | retryTwice, |
| 12 | 13 | setStatus, |
| @@ -12,26 +13,49 @@ | ||
| 12 | 13 | setStatus, |
| 13 | 14 | } from '@auto-launch/functions/helpers'; |
| 14 | 15 | import { updateOption } from '@auto-launch/functions/wp'; |
| 15 | 16 | import { useLaunchDataStore } from '@auto-launch/state/launch-data'; |
| 17 | +import { launchStrings } from '@auto-launch/strings'; | |
| 16 | 18 | import { AI_HOST } from '@constants'; |
| 17 | -import { __ } from '@wordpress/i18n'; | |
| 19 | +import { digest } from '@shared/api/digest'; | |
| 18 | 20 | import { mutate } from 'swr'; |
| 19 | 21 | |
| 20 | 22 | const fallback = null; |
| 21 | 23 | const headers = { 'Content-Type': 'application/json' }; |
| 22 | 24 | |
| 25 | +// Left set, the launch screen waits forever on a design that never arrives. | |
| 26 | +const dropBuildId = () => | |
| 27 | + useLaunchDataStore.setState((s) => ({ | |
| 28 | + urlParams: { ...s.urlParams, 'build-id': '' }, | |
| 29 | + })); | |
| 30 | + | |
| 23 | 31 | export const handleDesignBuild = async ({ urlParams }) => { |
| 24 | 32 | const buildId = urlParams?.['build-id']; |
| 25 | 33 | if (!buildId) return fallback; |
| 26 | 34 | |
| 27 | 35 | // translators: this is for a action log UI. Keep it short |
| 28 | - setStatus(__('Loading your design', 'extendify-local')); | |
| 36 | + setStatus(launchStrings().statusDesign); | |
| 29 | 37 | |
| 30 | 38 | const url = `${AI_HOST}/api/design/${encodeURIComponent(buildId)}`; |
| 31 | - const response = await retryTwice(() => fetchWithTimeout(url, { headers })); | |
| 32 | - if (!response?.ok) return fallback; | |
| 39 | + const response = await retryTwice(() => | |
| 40 | + fetchWithTimeout(url, { headers }), | |
| 41 | + ).catch((error) => { | |
| 42 | + return { ok: false, statusText: error.message, status: 0 }; | |
| 43 | + }); | |
| 33 | 44 | |
| 45 | + if (!response?.ok) { | |
| 46 | + digest({ | |
| 47 | + error: { | |
| 48 | + message: response.statusText, | |
| 49 | + name: 'FetchError', | |
| 50 | + status: response.status, | |
| 51 | + }, | |
| 52 | + details: { source: 'auto-launch', caller: 'handleDesignBuild' }, | |
| 53 | + }); | |
| 54 | + dropBuildId(); | |
| 55 | + return fallback; | |
| 56 | + } | |
| 57 | + | |
| 34 | 58 | try { |
| 35 | 59 | const parsed = getDesignBuildShape.parse(await response.json()); |
| 36 | 60 | |
| 37 | 61 | // Stash the site profile |
| @@ -38,17 +62,16 @@ | ||
| 38 | 62 | const profile = parsed.siteProfile; |
| 39 | 63 | await updateOption('extendify_site_profile', JSON.stringify(profile)); |
| 40 | 64 | mutate('siteProfile', { siteProfile: profile }, false); |
| 41 | 65 | |
| 42 | - // Stash the site style and variation | |
| 43 | 66 | const style = parsed.siteStyle; |
| 44 | - const fonts = | |
| 45 | - style.fonts?.heading || style.fonts?.body ? style.fonts : null; | |
| 46 | - const variation = await getThemeVariation( | |
| 47 | - { slug: style.colorPalette, fonts }, | |
| 48 | - { fallback: true }, | |
| 49 | - ); | |
| 67 | + const variation = await getStyleDocument({ | |
| 68 | + colorPalette: style.colorPalette, | |
| 69 | + fonts: style.fonts, | |
| 70 | + }); | |
| 50 | 71 | const siteStyle = { ...style, variation }; |
| 72 | + // The Agent reads extendify_siteStyle; the legacy row keeps old readers. | |
| 73 | + await updateOption('extendify_siteStyle', siteStyle); | |
| 51 | 74 | await updateOption('extendify_site_style', siteStyle); |
| 52 | 75 | await updateOption('extendify_animation_settings', { |
| 53 | 76 | type: style.animation ?? 'fade', |
| 54 | 77 | speed: 'medium', |
| @@ -58,39 +81,69 @@ | ||
| 58 | 81 | // Stash the plugins |
| 59 | 82 | const sitePlugins = parsed.selectedPlugins; |
| 60 | 83 | mutate('sitePlugins', getPluginsShape.parse({ sitePlugins }), false); |
| 61 | 84 | |
| 62 | - // Stash the logo | |
| 63 | - await uploadLogo(parsed.logoUrl); | |
| 64 | - mutate('siteLogo', getLogoShape.parse({ logoUrl: parsed.logoUrl }), false); | |
| 85 | + // The logo upload swaps logoUrl for a Media Library URL without the | |
| 86 | + // logos-custom/ marker, so capture "is the brand's own logo" from the | |
| 87 | + // source URL now. | |
| 88 | + const hasExternalLogo = isExternalLogo(parsed.logoUrl); | |
| 65 | 89 | |
| 66 | - const designBuild = { buildId, ...parsed, siteProfile: profile, siteStyle }; | |
| 67 | - // Spreading it here sets it for other state values we override | |
| 68 | - return { designBuild, ...designBuild }; | |
| 90 | + // Sideload the built-page images, and upload the logo alongside when the | |
| 91 | + // build provided one. logoUrl is nullable; without it we skip the upload | |
| 92 | + // so the siteLogo step falls through to AI logo generation. | |
| 93 | + const uploads = [importBuiltPagesImages(parsed.builtPages)]; | |
| 94 | + if (parsed.logoUrl) { | |
| 95 | + mutate( | |
| 96 | + 'siteLogo', | |
| 97 | + getLogoShape.parse({ logoUrl: parsed.logoUrl }), | |
| 98 | + false, | |
| 99 | + ); | |
| 100 | + uploads.push(uploadLogo(parsed.logoUrl, { external: hasExternalLogo })); | |
| 101 | + } | |
| 102 | + const [builtPages] = await Promise.all(uploads); | |
| 103 | + | |
| 104 | + const designBuild = { | |
| 105 | + buildId, | |
| 106 | + ...parsed, | |
| 107 | + builtPages, | |
| 108 | + hasExternalLogo, | |
| 109 | + siteProfile: profile, | |
| 110 | + siteStyle, | |
| 111 | + }; | |
| 112 | + // Exclude `pages` from the spread: the pages step owns it (templates with | |
| 113 | + // patterns); designBuild.pages (slug/name only) would clobber it. | |
| 114 | + const { pages, ...topLevel } = designBuild; | |
| 115 | + return { designBuild, ...topLevel }; | |
| 69 | 116 | } catch (e) { |
| 117 | + digest({ | |
| 118 | + error: e, | |
| 119 | + details: { source: 'auto-launch', caller: 'handleDesignBuild::parsing' }, | |
| 120 | + }); | |
| 70 | 121 | console.error('handleDesignBuild:', e); |
| 71 | - // Drop the build-id so downstream checks (e.g. skipDescription) fallback | |
| 72 | - useLaunchDataStore.setState((s) => ({ | |
| 73 | - urlParams: { ...s.urlParams, 'build-id': '' }, | |
| 74 | - })); | |
| 122 | + dropBuildId(); | |
| 75 | 123 | return fallback; |
| 76 | 124 | } |
| 77 | 125 | }; |
| 78 | 126 | |
| 79 | -// Prepend the design build hero; flagged so it skips content regeneration. | |
| 127 | +// Prepend the design build's hero to the fetched home template. Full-page | |
| 128 | +// builds skip /api/home entirely and are assembled in handleHome. | |
| 80 | 129 | export const applyDesignBuildHero = (patterns, designBuild) => { |
| 81 | - if (!designBuild?.patternCode) return patterns; | |
| 82 | - return [ | |
| 83 | - { | |
| 84 | - name: designBuild.patternId, | |
| 85 | - code: designBuild.patternCode, | |
| 86 | - patternTypes: ['hero-header'], | |
| 87 | - contentGenerated: true, | |
| 88 | - }, | |
| 89 | - ...patterns, | |
| 90 | - ]; | |
| 130 | + const builtHome = designBuild?.builtPages?.find((p) => p.slug === 'home'); | |
| 131 | + if (!builtHome?.patterns?.length) return patterns; | |
| 132 | + return [...builtHome.patterns, ...patterns]; | |
| 91 | 133 | }; |
| 92 | 134 | |
| 135 | +// /api/home answers a locked nav with one section per entry, in order, and holds back | |
| 136 | +// the hero, so a count mismatch means that broke: leave the names to the pattern types. | |
| 137 | +export const applyDesignBuildNav = (patterns, designBuild, structure) => { | |
| 138 | + const pages = designBuild?.pages ?? []; | |
| 139 | + if (structure !== 'single-page' || patterns.length !== pages.length) { | |
| 140 | + return patterns; | |
| 141 | + } | |
| 142 | + | |
| 143 | + return patterns.map((pattern, i) => ({ ...pattern, navSlug: pages[i].slug })); | |
| 144 | +}; | |
| 145 | + | |
| 93 | 146 | // Reorder pages to match the design build's page order; extras fall to the end. |
| 94 | 147 | export const applyDesignBuildOrder = (pages, designBuild) => { |
| 95 | 148 | const order = designBuild?.pages?.map((p) => p.slug) ?? []; |
| 96 | 149 | if (!order.length) return pages; |
| @@ -97,19 +150,5 @@ | ||
| 97 | 150 | return [ |
| 98 | 151 | ...order.map((slug) => pages.find((t) => t.slug === slug)).filter(Boolean), |
| 99 | 152 | ...pages.filter((t) => !order.includes(t.slug)), |
| 100 | 153 | ]; |
| 101 | -}; | |
| 102 | - | |
| 103 | -// Tag non-hero patterns with their aligned design build page slug/name. | |
| 104 | -// For single-page sites mainly | |
| 105 | -export const applyDesignBuildNav = (patterns, designBuild) => { | |
| 106 | - const pages = designBuild?.pages ?? []; | |
| 107 | - if (!pages.length) return patterns; | |
| 108 | - let i = 0; | |
| 109 | - return patterns.map((pattern) => { | |
| 110 | - if (pattern.patternTypes?.includes('hero-header')) return pattern; | |
| 111 | - const page = pages[i++]; | |
| 112 | - if (!page) return pattern; | |
| 113 | - return { ...pattern, navSlug: page.slug, navLabel: page.name }; | |
| 114 | - }); | |
| 115 | 154 | }; |