PluginProbe
Extendify / 3.2.0
Extendify v3.2.0
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 / workflows / theme / components / SelectThemeFontsVariation.jsx

SelectThemeFontsVariation.jsx in Extendify 3.2.0, at src/Agent/workflows/theme/components/SelectThemeFontsVariation.jsx

127 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useFontVariationOverride } from '@agent/hooks/useFontVariationOverride';
2 import { useThemeFontsVariations } from '@agent/hooks/useThemeFontsVariations';
3 import { useChatStore } from '@agent/state/chat';
4 import { useEffect, useMemo, useRef, useState } from '@wordpress/element';
5 import { __ } from '@wordpress/i18n';
6
7 export const SelectThemeFontsVariation = ({ onConfirm, onCancel }) => {
8 const [css, setCss] = useState('');
9 const [selected, setSelected] = useState(null);
10 const { undoChange } = useFontVariationOverride({ css });
11 const { variations, isLoading } = useThemeFontsVariations();
12
13 const confirmed = useRef(false);
14 useEffect(() => {
15 return () => {
16 if (!confirmed.current) undoChange();
17 };
18 }, []);
19
20 const noVariations = !variations || variations.length === 0;
21 const shuffled = useMemo(
22 () => (variations ? variations.sort(() => Math.random() - 0.5) : []),
23 [variations],
24 );
25 const { addMessage, messages } = useChatStore();
26
27 const handleConfirm = () => {
28 if (!selected) return;
29 confirmed.current = true;
30 onConfirm({
31 data: { variation: variations.find((v) => v.title === selected) },
32 shouldRefreshPage: true,
33 });
34 };
35
36 useEffect(() => {
37 if (isLoading || !noVariations) return;
38 const timer = setTimeout(() => onCancel(), 100);
39 // translators: A chat message shown to the user
40 const content = __(
41 'We were unable to find any variations for your theme',
42 'extendify-local',
43 );
44 const last = messages.at(-1)?.details?.content;
45 if (content === last) return () => clearTimeout(timer);
46 addMessage('message', { role: 'assistant', content, error: true });
47 return () => clearTimeout(timer);
48 }, [addMessage, onCancel, noVariations, messages, isLoading]);
49
50 if (isLoading) {
51 return (
52 <div className="min-h-24 p-2 text-center text-sm">
53 {__('Loading font variations...', 'extendify-local')}
54 </div>
55 );
56 }
57
58 if (noVariations) return null;
59
60 return (
61 <div className="mb-4 ms-12 me-2 flex flex-col rounded-lg border border-gray-300 bg-gray-50">
62 <div className="rounded-lg border-b border-gray-300 bg-white">
63 <div className="grid grid-cols-2 gap-2 p-3">
64 {shuffled?.slice(0, 10)?.map(({ title, css, styles }) => (
65 <button
66 key={title}
67 aria-label={title}
68 type="button"
69 style={{ fontFamily: getFont(styles)?.normal }}
70 className={`relative flex w-full items-center justify-center overflow-hidden rounded-lg border border-gray-300 bg-none p-2 text-center text-sm ${
71 selected === title ? 'ring ring-design-main ring-wp' : ''
72 }`}
73 onClick={() => {
74 setSelected(title);
75 setCss(css);
76 }}
77 >
78 <div className="max-w-fit content-stretch items-center justify-center rounded-lg text-2xl rtl:space-x-reverse">
79 <span style={{ fontFamily: getFont(styles)?.heading }}>A</span>
80 <span style={{ fontFamily: getFont(styles)?.normal }}>a</span>
81 </div>
82 </button>
83 ))}
84 </div>
85 </div>
86 <div className="flex justify-start gap-2 p-3">
87 <button
88 type="button"
89 className="w-full rounded-sm border border-gray-500 bg-white p-2 text-sm text-gray-900"
90 onClick={onCancel}
91 >
92 {__('Cancel', 'extendify-local')}
93 </button>
94 <button
95 type="button"
96 className="w-full rounded-sm border border-design-main bg-design-main p-2 text-sm text-white"
97 disabled={!selected}
98 onClick={handleConfirm}
99 >
100 {__('Save', 'extendify-local')}
101 </button>
102 </div>
103 </div>
104 );
105 };
106
107 const getFont = (styles) => {
108 if (
109 styles?.typography?.fontFamily &&
110 styles?.elements?.heading?.typography?.fontFamily
111 ) {
112 return {
113 normal: styles.typography.fontFamily,
114 heading:
115 styles.elements.heading.typography.fontFamily ||
116 styles.typography.fontFamily,
117 };
118 }
119
120 if (styles?.typography?.fontFamily) {
121 return {
122 normal: styles.typography.fontFamily,
123 heading: styles.typography.fontFamily,
124 };
125 }
126 };
127