PluginProbe
Extendify / 3.2.1
Extendify v3.2.1
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
extendify / src / Agent / hooks / useSiteVibesVariations.js

useSiteVibesVariations.js in Extendify 3.2.1, at src/Agent/hooks/useSiteVibesVariations.js

87 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { isObject } from '@shared/lib/utils';
2 import {
3 buildVibeResetCss,
4 withAppliedVariation,
5 } from '@shared/lib/vibe-preview';
6 import { getVibes, vibeCssBySlug, vibesBySlug } from '@shared/lib/vibes';
7 import apiFetch from '@wordpress/api-fetch';
8 import useSWRImmutable from 'swr/immutable';
9
10 export const useSiteVibesVariations = () => {
11 const { data, error, isLoading } = useSWRImmutable(
12 { key: 'site-vibes-variations' },
13 fetcher,
14 );
15 return { data, error, isLoading };
16 };
17
18 const fetcher = async () => {
19 const optionsResponse = await apiFetch({
20 path: '/extendify/v1/launch/options?option=extendify_siteStyle',
21 });
22
23 const siteStyle = optionsResponse?.data;
24 const currentVibe = siteStyle?.vibe || 'natural-1';
25
26 // The site's current vibe is not necessarily offered on this surface.
27 const payloads = await getVibes(`agent,${currentVibe}`);
28 if (!payloads.length) return null;
29
30 const bySlug = vibesBySlug(payloads);
31 const [theme, variation] = await Promise.all([
32 getThemeGlobalStyles(),
33 getAppliedVariation(siteStyle),
34 ]);
35 const themeStyles = withAppliedVariation(theme?.styles, variation?.styles);
36 const themeSettings = withAppliedVariation(
37 theme?.settings,
38 variation?.settings,
39 );
40
41 return {
42 vibes: payloads.map(({ slug, title }) => ({ name: title || slug, slug })),
43 css: vibeCssBySlug(payloads),
44 payloads: bySlug,
45 resets: Object.fromEntries(
46 Object.keys(bySlug).map((slug) => [
47 slug,
48 buildVibeResetCss({
49 payloads: bySlug,
50 slug,
51 themeStyles,
52 themeSettings,
53 }),
54 ]),
55 ),
56 currentVibe,
57 };
58 };
59
60 const getThemeGlobalStyles = async () => {
61 const themeSlug = window.extAgentData?.context?.themeSlug;
62 if (!themeSlug) return null;
63
64 try {
65 return await apiFetch({
66 path: `/wp/v2/global-styles/themes/${themeSlug}?context=edit`,
67 });
68 } catch {
69 return null;
70 }
71 };
72
73 // Sites launched before extendify_siteStyle carried the variation hold it on
74 // the legacy AutoLaunch row only.
75 const getAppliedVariation = async (siteStyle) => {
76 if (isObject(siteStyle?.variation)) return siteStyle.variation;
77
78 try {
79 const { data } = await apiFetch({
80 path: '/extendify/v1/launch/options?option=extendify_site_style',
81 });
82 return isObject(data?.variation) ? data.variation : null;
83 } catch {
84 return null;
85 }
86 };
87