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 / workflows / theme / components / SelectThemeFontsVariation.jsx

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

132 lines 4.0 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, onLoad }) => {
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) return;
38 onLoad();
39 }, [isLoading, onLoad]);
40
41 useEffect(() => {
42 if (isLoading || !noVariations) return;
43 const timer = setTimeout(() => onCancel(), 100);
44 // translators: A chat message shown to the user
45 const content = __(
46 'We were unable to find any variations for your theme',
47 'extendify-local',
48 );
49 const last = messages.at(-1)?.details?.content;
50 if (content === last) return () => clearTimeout(timer);
51 addMessage('message', { role: 'assistant', content, error: true });
52 return () => clearTimeout(timer);
53 }, [addMessage, onCancel, noVariations, messages, isLoading]);
54
55 if (isLoading) {
56 return (
57 <div className="min-h-24 p-2 text-center text-sm">
58 {__('Loading font variations...', 'extendify-local')}
59 </div>
60 );
61 }
62
63 if (noVariations) return null;
64
65 return (
66 <div className="mb-4 ml-10 mr-2 flex flex-col rounded-lg border border-gray-300 bg-gray-50 rtl:ml-2 rtl:mr-10">
67 <div className="rounded-lg border-b border-gray-300 bg-white">
68 <div className="grid grid-cols-2 gap-2 p-3">
69 {shuffled?.slice(0, 10)?.map(({ title, css, styles }) => (
70 <button
71 key={title}
72 aria-label={title}
73 type="button"
74 style={{ fontFamily: getFont(styles)?.normal }}
75 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 ${
76 selected === title ? 'ring ring-design-main ring-wp' : ''
77 }`}
78 onClick={() => {
79 setSelected(title);
80 setCss(css);
81 }}
82 >
83 <div className="max-w-fit content-stretch items-center justify-center rounded-lg text-2xl rtl:space-x-reverse">
84 <span style={{ fontFamily: getFont(styles)?.heading }}>A</span>
85 <span style={{ fontFamily: getFont(styles)?.normal }}>a</span>
86 </div>
87 </button>
88 ))}
89 </div>
90 </div>
91 <div className="flex justify-start gap-2 p-3">
92 <button
93 type="button"
94 className="w-full rounded-sm border border-gray-500 bg-white p-2 text-sm text-gray-900"
95 onClick={onCancel}
96 >
97 {__('Cancel', 'extendify-local')}
98 </button>
99 <button
100 type="button"
101 className="w-full rounded-sm border border-design-main bg-design-main p-2 text-sm text-white"
102 disabled={!selected}
103 onClick={handleConfirm}
104 >
105 {__('Save', 'extendify-local')}
106 </button>
107 </div>
108 </div>
109 );
110 };
111
112 const getFont = (styles) => {
113 if (
114 styles?.typography?.fontFamily &&
115 styles?.elements?.heading?.typography?.fontFamily
116 ) {
117 return {
118 normal: styles.typography.fontFamily,
119 heading:
120 styles.elements.heading.typography.fontFamily ||
121 styles.typography.fontFamily,
122 };
123 }
124
125 if (styles?.typography?.fontFamily) {
126 return {
127 normal: styles.typography.fontFamily,
128 heading: styles.typography.fontFamily,
129 };
130 }
131 };
132