| 1 |
import { apiFetchWithTimeout } from '@auto-launch/functions/helpers'; |
| 2 |
import { deepMerge } from '@shared/lib/utils'; |
| 3 |
|
| 4 |
// TODO: add zod types - this was copy/pasted from legacy launch |
| 5 |
export const getThemeVariation = async ({ slug, fonts }, opts) => { |
| 6 |
const { fallback = false } = opts || {}; |
| 7 |
const rawVariations = await apiFetchWithTimeout({ |
| 8 |
path: 'wp/v2/global-styles/themes/extendable/variations', |
| 9 |
}); |
| 10 |
|
| 11 |
const variations = rawVariations.filter( |
| 12 |
(v) => |
| 13 |
(v.settings?.color || v.styles?.color) && |
| 14 |
(v.settings?.typography || v.styles?.typography), |
| 15 |
); |
| 16 |
|
| 17 |
let variation = variations.find((v) => { |
| 18 |
const matchSlug = |
| 19 |
v.slug || v.title.toLowerCase().trim().replace(/\s+/g, '-'); |
| 20 |
return matchSlug === slug; |
| 21 |
}); |
| 22 |
|
| 23 |
// Fallback to random variation if slug doesn't match |
| 24 |
if (!variation && fallback) { |
| 25 |
variation = variations.sort(() => Math.random() - 0.5)[0]; |
| 26 |
} |
| 27 |
|
| 28 |
if (!fonts) return variation; |
| 29 |
|
| 30 |
return deepMerge(variation, { |
| 31 |
styles: { |
| 32 |
elements: { |
| 33 |
heading: { |
| 34 |
typography: { |
| 35 |
fontFamily: `var(--wp--preset--font-family--${fonts.heading.slug})`, |
| 36 |
}, |
| 37 |
}, |
| 38 |
}, |
| 39 |
typography: { |
| 40 |
fontFamily: `var(--wp--preset--font-family--${fonts.body.slug})`, |
| 41 |
}, |
| 42 |
}, |
| 43 |
settings: { |
| 44 |
typography: { |
| 45 |
fontFamilies: { |
| 46 |
custom: [fonts.heading, fonts.body].filter((font) => !!font.host), |
| 47 |
}, |
| 48 |
}, |
| 49 |
}, |
| 50 |
}); |
| 51 |
}; |
| 52 |
|