| 1 |
import { replaceVibeBlocks } from '@shared/lib/vibe-blocks'; |
| 2 |
import { |
| 3 |
applyVibeGlobals, |
| 4 |
applyVibeStyles, |
| 5 |
vibeGlobalsEntry, |
| 6 |
} from '@shared/lib/vibe-globals'; |
| 7 |
import { getVibes, vibesBySlug } from '@shared/lib/vibes'; |
| 8 |
import apiFetch from '@wordpress/api-fetch'; |
| 9 |
|
| 10 |
const globalStylesPostID = window.extSharedData?.globalStylesPostID; |
| 11 |
|
| 12 |
export default async ({ selectedVibe }) => { |
| 13 |
if ( |
| 14 |
!selectedVibe || |
| 15 |
typeof selectedVibe !== 'string' || |
| 16 |
selectedVibe.trim() === '' |
| 17 |
) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
try { |
| 22 |
const [currentGlobalStyles, payloads] = await Promise.all([ |
| 23 |
apiFetch({ |
| 24 |
path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, |
| 25 |
}), |
| 26 |
// Same query the picker asked for, so this reads the cached response. |
| 27 |
getVibes(`agent,${selectedVibe}`), |
| 28 |
]); |
| 29 |
|
| 30 |
const vibes = vibesBySlug(payloads); |
| 31 |
const entry = vibeGlobalsEntry(vibes, selectedVibe); |
| 32 |
const currentStyles = currentGlobalStyles.styles; |
| 33 |
|
| 34 |
const styles = applyVibeStyles({ |
| 35 |
currentStyles, |
| 36 |
vibeStyles: entry?.styles, |
| 37 |
vibes, |
| 38 |
}); |
| 39 |
|
| 40 |
// One post holds all three, so a second POST would drop the first. |
| 41 |
await Promise.all([ |
| 42 |
apiFetch({ |
| 43 |
method: 'POST', |
| 44 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 45 |
data: { |
| 46 |
id: globalStylesPostID, |
| 47 |
settings: applyVibeGlobals({ |
| 48 |
currentSettings: currentGlobalStyles.settings, |
| 49 |
vibeSettings: entry?.settings, |
| 50 |
vibes, |
| 51 |
}), |
| 52 |
styles: { |
| 53 |
...styles, |
| 54 |
// Pre-apply blocks here would drop the css applyVibeStyles wrote. |
| 55 |
blocks: replaceVibeBlocks({ |
| 56 |
currentBlocks: styles?.blocks, |
| 57 |
vibeBlocks: vibes[selectedVibe]?.blocks, |
| 58 |
selectedVibe, |
| 59 |
}), |
| 60 |
}, |
| 61 |
}, |
| 62 |
}), |
| 63 |
updateSiteStyleOption(selectedVibe), |
| 64 |
]); |
| 65 |
} catch (error) { |
| 66 |
const errorMessage = |
| 67 |
error?.response?.data?.message || error?.message || 'Unknown error'; |
| 68 |
throw new Error(`Vibe update failed: ${errorMessage}`); |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
const updateSiteStyleOption = async (selectedVibe) => { |
| 73 |
const { data: currentSiteStyle } = await apiFetch({ |
| 74 |
path: '/extendify/v1/launch/options?option=extendify_siteStyle', |
| 75 |
}); |
| 76 |
|
| 77 |
const existingSiteStyle = |
| 78 |
currentSiteStyle && |
| 79 |
typeof currentSiteStyle === 'object' && |
| 80 |
!Array.isArray(currentSiteStyle) |
| 81 |
? currentSiteStyle |
| 82 |
: {}; |
| 83 |
|
| 84 |
const updatedSiteStyle = { ...existingSiteStyle, vibe: selectedVibe }; |
| 85 |
|
| 86 |
return await apiFetch({ |
| 87 |
path: '/extendify/v1/launch/options', |
| 88 |
method: 'POST', |
| 89 |
data: { option: 'extendify_siteStyle', value: updatedSiteStyle }, |
| 90 |
}); |
| 91 |
}; |
| 92 |
|