feat(core): Update opengrammar/extension/src/rewrite/index.tsx (Commit #18)
Swadhin Biswas committed 7 months ago
e95f1f9
1 parent 5a765e2
commit e95f1f9
1 file changed
+23 -23
Showing 1 changed file+23-23
▼opengrammar/extension/src/rewrite/index.tsx
+23-23
| @@ -10,22 +10,16 @@ const RewritePopup = () => { | ||
10 | 10 | const [rewrittenText, setRewrittenText] = useState(''); |
11 | 11 | const [loading, setLoading] = useState(false); |
12 | 12 | const [error, setError] = useState(''); |
13 | + const [applySuccess, setApplySuccess] = useState(''); | |
13 | 14 | |
14 | 15 | useEffect(() => { |
15 | - // Get selected text from page | |
16 | - chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { | |
17 | - const tab = tabs[0]; | |
18 | - if (tab?.id) { | |
19 | - chrome.tabs.sendMessage(tab.id, { type: 'GET_SELECTION' }, (response) => { | |
20 | - if (response?.text) { | |
21 | - setSelectedText(response.text); | |
22 | - } else { | |
23 | - setError('No text selected. Please select text to rewrite.'); | |
24 | - } | |
25 | - }); | |
26 | - } else { | |
27 | - setError('No active tab found.'); | |
16 | + chrome.runtime.sendMessage({ type: 'GET_REWRITE_CONTEXT' }, (response) => { | |
17 | + if (response?.selectedText) { | |
18 | + setSelectedText(response.selectedText); | |
19 | + return; | |
28 | 20 | } |
21 | + | |
22 | + setError('No text selected. Select text first, then open Rewrite.'); | |
29 | 23 | }); |
30 | 24 | }, []); |
31 | 25 | |
| @@ -67,16 +61,21 @@ const RewritePopup = () => { | ||
67 | 61 | }; |
68 | 62 | |
69 | 63 | const handleApply = () => { |
70 | - chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { | |
71 | - const tab = tabs[0]; | |
72 | - if (tab?.id) { | |
73 | - chrome.tabs.sendMessage(tab.id, { | |
74 | - type: 'APPLY_REWRITE', | |
75 | - original: selectedText, | |
76 | - rewritten: rewrittenText, | |
77 | - }); | |
78 | - } | |
79 | - }); | |
64 | + setApplySuccess(''); | |
65 | + chrome.runtime.sendMessage( | |
66 | + { | |
67 | + type: 'APPLY_REWRITE_TO_SOURCE', | |
68 | + original: selectedText, | |
69 | + rewritten: rewrittenText, | |
70 | + }, | |
71 | + (response) => { | |
72 | + if (response?.success) { | |
73 | + setApplySuccess('Applied to the original editor.'); | |
74 | + } else { | |
75 | + setError(response?.error || 'Failed to apply rewrite.'); | |
76 | + } | |
77 | + }, | |
78 | + ); | |
80 | 79 | }; |
81 | 80 | |
82 | 81 | const handleCopy = () => { |
| @@ -105,6 +104,7 @@ const RewritePopup = () => { | ||
105 | 104 | <div className="error-message">{error}</div> |
106 | 105 | ) : ( |
107 | 106 | <> |
107 | + {applySuccess && <div className="success-message">{applySuccess}</div>} | |
108 | 108 | <div className="tone-selector"> |
109 | 109 | <label>Choose Tone:</label> |
110 | 110 | <div className="tone-grid"> |
111 | 111 | |