PluginProbe
Extendify / 3.1.5
Extendify v3.1.5
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 0.7.0 All 126 releases
extendify / src / Draft / Draft.jsx

Draft.jsx in Extendify 3.1.5, at src/Draft/Draft.jsx

162 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { Completion } from '@draft/components/Completion';
2 import { ConsentSidebar } from '@draft/components/ConsentSidebar';
3 import { DraftMenu } from '@draft/components/DraftMenu';
4 import { EditMenu } from '@draft/components/EditMenu';
5 import { Input } from '@draft/components/Input';
6 import { InsertMenu } from '@draft/components/InsertMenu';
7 import { SelectedText } from '@draft/components/SelectedText';
8 import { useCompletion } from '@draft/hooks/useCompletion';
9 import { useRouter } from '@draft/hooks/useRouter';
10 import { useSelectedText } from '@draft/hooks/useSelectedText';
11 import { useAIConsentStore } from '@shared/state/ai-consent';
12 import { store as blockEditorStore } from '@wordpress/block-editor';
13 import { BaseControl, Panel, PanelBody } from '@wordpress/components';
14 import { useSelect } from '@wordpress/data';
15 import { useEffect, useState } from '@wordpress/element';
16 import { __ } from '@wordpress/i18n';
17
18 export const Draft = () => {
19 const { selectedText } = useSelectedText();
20 const [inputText, setInputText] = useState('');
21 const [ready, setReady] = useState(false);
22 const [prompt, setPrompt] = useState({
23 text: '',
24 promptType: '',
25 systemMessageKey: '',
26 details: {},
27 });
28 const { completion, loading, error } = useCompletion(
29 prompt.text,
30 prompt.promptType,
31 prompt.systemMessageKey,
32 prompt.details,
33 );
34 const { selectedBlockClientIds, getBlock } = useSelect((select) => {
35 const blockEditor = select(blockEditorStore);
36 return {
37 selectedBlockClientIds: blockEditor.getSelectedBlockClientIds(),
38 getBlock: blockEditor.getBlock,
39 getBlocks: blockEditor.getBlocks,
40 };
41 }, []);
42
43 const { CurrentPage: PhotosSection } = useRouter();
44 // TODO: move to global state
45 const shouldShowAIConsent = useAIConsentStore((state) =>
46 state.shouldShowAIConsent('draft'),
47 );
48
49 // TODO: When doing a rewrite, make this global state
50 useEffect(() => {
51 // Allow for external updates
52 const handle = (event) => {
53 if (shouldShowAIConsent) return;
54 setPrompt(event.detail);
55 };
56 window.addEventListener('extendify-draft:set-prompt', handle);
57 return () =>
58 window.removeEventListener('extendify-draft:set-prompt', handle);
59 }, [shouldShowAIConsent]);
60
61 // Reset input text when an error occurs
62 useEffect(() => {
63 if (!error) return;
64 setInputText(prompt.text);
65 }, [error, prompt.text]);
66
67 const canEditContent = () => {
68 if (selectedBlockClientIds.length === 0) {
69 return false;
70 }
71 const targetBlock = getBlock(selectedBlockClientIds[0]);
72 if (!targetBlock) {
73 return false;
74 }
75 return (
76 typeof targetBlock?.attributes?.content !== 'undefined' &&
77 targetBlock?.attributes?.content !== ''
78 );
79 };
80
81 const isImageBlock = () => {
82 if (selectedBlockClientIds.length === 0) return false;
83
84 const supportedBlocks = [
85 'core/image',
86 'core/media-text',
87 'core/gallery',
88 'core/cover',
89 ];
90 const targetBlock = getBlock(selectedBlockClientIds[0]);
91 if (!targetBlock) return false;
92
93 return supportedBlocks.includes(targetBlock.name);
94 };
95
96 if (shouldShowAIConsent) {
97 return <ConsentSidebar />;
98 }
99
100 if (isImageBlock()) return <PhotosSection />;
101
102 return (
103 <Panel>
104 <PanelBody>
105 {selectedText && <SelectedText loading={loading} />}
106
107 <div className="mb-4 overflow-hidden rounded-xs border-none bg-gray-100">
108 {!completion && (
109 <Input
110 inputText={inputText}
111 setInputText={setInputText}
112 ready={ready}
113 setReady={setReady}
114 setPrompt={setPrompt}
115 loading={loading}
116 />
117 )}
118 {completion && <Completion completion={completion} />}
119 {error && (
120 <div className="mb-4 mt-2 px-4">
121 <p className="m-0 text-xs font-semibold text-red-500">
122 {error.message}
123 </p>
124 </div>
125 )}
126 </div>
127 {(completion || loading) && !error && (
128 <InsertMenu
129 prompt={prompt}
130 completion={completion}
131 setPrompt={setPrompt}
132 setInputText={setInputText}
133 loading={loading}
134 />
135 )}
136 {!loading && !completion && canEditContent() && (
137 <BaseControl>
138 <EditMenu
139 completion={completion}
140 disabled={loading}
141 setInputText={setInputText}
142 setPrompt={setPrompt}
143 />
144 </BaseControl>
145 )}
146 {!loading && !completion && !canEditContent() && (
147 <BaseControl
148 __nextHasNoMarginBottom
149 label={__('Suggested prompts', 'extendify-local')}
150 >
151 <DraftMenu
152 disabled={loading}
153 setInputText={setInputText}
154 setReady={setReady}
155 />
156 </BaseControl>
157 )}
158 </PanelBody>
159 </Panel>
160 );
161 };
162