PluginProbe
Extendify / 3.0.5
Extendify v3.0.5
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 / useThemeVariations.js

useThemeVariations.js in Extendify 3.0.5, at src/Agent/hooks/useThemeVariations.js

40 lines 1.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import apiFetch from '@wordpress/api-fetch';
2 import { __ } from '@wordpress/i18n';
3 import useSWRImmutable from 'swr/immutable';
4
5 const fetcher = async () => {
6 const path = window.location.pathname;
7 const s = new URLSearchParams(window.location.search);
8 const onEditor =
9 path.includes('/wp-admin/post.php') && s.get('action') === 'edit';
10
11 const variations = await apiFetch({
12 method: 'GET',
13 // On the frontend we need to include layout styles
14 path: `/extendify/v1/agent/theme-variations${onEditor ? '' : '?includeLayoutStyles'}`,
15 });
16 if (!variations || !Array.isArray(variations)) {
17 throw new Error(__('Failed to fetch theme variations.', 'extendify-local'));
18 }
19 return {
20 // remove duplicate variations by title
21 variations: variations.reduce((acc, variation) => {
22 if (!acc.some((v) => v.title === variation.title)) {
23 acc.push(variation);
24 }
25 return acc;
26 }, []),
27 };
28 };
29
30 export const useThemeVariations = () => {
31 const { data, error, isLoading } = useSWRImmutable(
32 {
33 key: 'theme-variations',
34 themeSlug: window.extAgentData.context.themeSlug,
35 },
36 fetcher,
37 );
38 return { variations: data?.variations, error, isLoading };
39 };
40