PluginProbe
Extendify / trunk
Extendify vtrunk
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 / useThemeFontsVariations.js

useThemeFontsVariations.js in Extendify trunk, at src/Agent/hooks/useThemeFontsVariations.js

42 lines 1.2 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 export const useThemeFontsVariations = () => {
6 const { data, error, isLoading } = useSWRImmutable(
7 {
8 key: 'theme-fonts-variations',
9 themeSlug: window.extAgentData.context.themeSlug,
10 },
11 fetcher,
12 );
13 return { variations: data?.variations, error, isLoading };
14 };
15
16 const fetcher = async () => {
17 const path = window.location.pathname;
18 const s = new URLSearchParams(window.location.search);
19 const onEditor =
20 path.includes('/wp-admin/post.php') && s.get('action') === 'edit';
21
22 const variations = await apiFetch({
23 method: 'GET',
24 // On the frontend we need to include layout styles
25 path: `/extendify/v1/agent/theme-fonts-variations${onEditor ? '' : '?includeLayoutStyles'}`,
26 });
27 if (!variations || !Array.isArray(variations)) {
28 throw new Error(
29 __('Failed to fetch theme font variations.', 'extendify-local'),
30 );
31 }
32 return {
33 // remove duplicate variations by title
34 variations: variations.reduce((acc, variation) => {
35 if (!acc.some((v) => v.title === variation.title)) {
36 acc.push(variation);
37 }
38 return acc;
39 }, []),
40 };
41 };
42