Spaces:
Running
Running
File size: 4,555 Bytes
803d869 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import React, { useState } from "react";
import api from "../../api";
import { diffWords } from "diff";
import "../../assets/css/style.css";
import VoiceInput from "./VoiceInput";
import ErrorPopup from "./ErrorPopup";
const ParagraphChecker = () => {
const [inputText, setInputText] = useState("");
const [correctedText, setCorrectedText] = useState("");
const [loading, setLoading] = useState(false);
const [error, setError] = useState("");
const [highlightedOriginal, setHighlightedOriginal] = useState("");
const [highlightedCorrected, setHighlightedCorrected] = useState("");
const token = localStorage.getItem("jwtToken");
const handleGrammarCheck = async () => {
if (!inputText.trim()) {
setError("Please enter text to check.");
return;
}
setError("");
setLoading(true);
setCorrectedText("");
setHighlightedOriginal("");
setHighlightedCorrected("");
try {
const response = await api.grammarCheck(
{ paragraph: inputText },
{ headers: { Authorization: `Bearer ${token}` } },
);
if (response && response.grammar_corrected) {
setCorrectedText(response.grammar_corrected);
highlightDifferences(inputText, response.grammar_corrected);
} else {
setError("No corrected text found.");
}
} catch (err) {
setError(err.message || "Something went wrong.");
} finally {
setLoading(false);
}
};
const highlightDifferences = (original, corrected) => {
if (!original || !corrected) return;
const differences = diffWords(original, corrected);
const originalHighlighted = differences.map((part, index) => {
if (part.removed) {
return (
<span
key={index}
style={{ color: "red", textDecoration: "line-through" }}
>
{part.value}
</span>
);
}
return part.added ? null : (
<span key={index} style={{ color: "black" }}>
{part.value}
</span>
);
});
const correctedHighlighted = differences.map((part, index) => {
if (part.added) {
return (
<span key={index} style={{ color: "green" }}>
{part.value}
</span>
);
}
return part.removed ? null : (
<span key={index} style={{ color: "black" }}>
{part.value}
</span>
);
});
setHighlightedOriginal(originalHighlighted);
setHighlightedCorrected(correctedHighlighted);
};
const handleClearText = () => {
setInputText("");
setCorrectedText("");
setHighlightedOriginal("");
setHighlightedCorrected("");
setError("");
};
const handleVoiceResult = (transcript) => {
setInputText(transcript);
};
const handleClosePopup = () => {
setError("");
};
return (
<div className="grammar-checker-container">
<h2>Paragraph Grammar Checker</h2>
<div className="text-area-row">
<div className="input-section-3">
<h3 className="box-title1">Input Text</h3>
<textarea
className="input-textarea-3"
placeholder="Type your paragraph here..."
value={inputText}
onChange={(e) => setInputText(e.target.value)}
/>
</div>
<div className="output-section-3">
<h3 className="box-title2">Corrected Text</h3>
<div className="output-display-3">
{highlightedCorrected.length > 0
? highlightedCorrected
: correctedText || "Corrected text will appear here..."}
</div>
</div>
</div>
<div className="controls">
{error && <ErrorPopup message={error} onClose={handleClosePopup} />}
{/* 🎯 Buttons all in one row */}
<div className="checker-button-row">
<VoiceInput
onResult={handleVoiceResult}
language="en-US"
buttonStyle={{}}
/>
<button
onClick={handleGrammarCheck}
disabled={loading}
className="checker-correct-button"
>
{loading ? "Checking..." : "Correct"}
</button>
<button onClick={handleClearText} className="checker-clear-button">
Clear
</button>
</div>
</div>
</div>
);
};
export default ParagraphChecker;
|