EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
AIRewordButton.jsx
Go to the documentation of this file.
1import React, { useState } from 'react';
2import { apiFetch } from '../lib/api';
3import './AIRewordButton.css';
4
5export default function AIRewordButton({ value, onReword, disabled, style, prompt = 'Rewrite this text to be clear and natural:' }) {
6 const [loading, setLoading] = useState(false);
7
8 const handleReword = async (e) => {
9 e.preventDefault();
10 if (!value || loading) return;
11 setLoading(true);
12 try {
13 const res = await apiFetch('/reword', {
14 method: 'POST',
15 headers: { 'Content-Type': 'application/json' },
16 body: JSON.stringify({ text: value, prompt })
17 });
18 const data = await res.json();
19 if (data.reworded) onReword(data.reworded);
20 } catch (err) {
21 alert('AI rewording failed.');
22 }
23 setLoading(false);
24 };
25
26 return (
27 <button
28 className="ai-reword-btn"
29 onClick={handleReword}
30 disabled={disabled || loading || !value}
31 type="button"
32 style={style}
33 title="Reword with AI"
34 >
35 {loading ? (
36 <span className="material-symbols-outlined spinning" style={{ fontSize: 18, margin: 0, padding: 0 }}>progress_activity</span>
37 ) : (
38 <span className="material-symbols-outlined" style={{ fontSize: 18, margin: 0, padding: 0 }}>auto_awesome</span>
39 )}
40 </button>
41 );
42}