1import React, { useState } from 'react';
2
3const MAX_LIMIT = 280;
4
5export default function TextUtility() {
6 const [text, setText] = useState('');
7 const [copied, setCopied] = useState(false);
8
9 // Derived metrics (computed on the fly during render!)
10 const charCount = text.length;
11 const charsNoSpaces = text.replace(/\s+/g, '').length;
12 const wordCount = text.trim() === '' ? 0 : text.trim().split(/\s+/).length;
13 const sentenceCount = text.trim() === '' ? 0 : text.split(/[.!?]+/).filter(Boolean).length;
14 const readingTimeMinutes = Math.ceil(wordCount / 200); // Avg reading speed 200 wpm
15
16 const remainingChars = MAX_LIMIT - charCount;
17 const percentUsed = Math.min((charCount / MAX_LIMIT) * 100, 100);
18
19 // Transformations
20 const handleUppercase = () => setText(text.toUpperCase());
21 const handleLowercase = () => setText(text.toLowerCase());
22 const handleTrimSpaces = () => setText(text.replace(/\s+/g, ' ').trim());
23 const handleClear = () => setText('');
24
25 const handleCopy = async () => {
26 if (!text) return;
27 await navigator.clipboard.writeText(text);
28 setCopied(true);
29 setTimeout(() => setCopied(false), 2000);
30 };
31
32 return (
33 <div className="utility-card">
34 <h2>Text Analytics & Transformation Studio</h2>
35
36 {/* Controlled Textarea */}
37 <textarea
38 rows={5}
39 value={text}
40 onChange={(e) => setText(e.target.value)}
41 placeholder="Type or paste your text here..."
42 className={`text-input ${charCount > MAX_LIMIT ? 'input-over-limit' : ''}`}
43 />
44
45 {/* Progress Bar */}
46 <div className="progress-bar-container">
47 <div
48 className={`progress-fill ${percentUsed >= 100 ? 'bg-danger' : percentUsed >= 80 ? 'bg-warning' : 'bg-primary'}`}
49 style={{ width: `${percentUsed}%` }}
50 />
51 </div>
52
53 <div className="limit-info">
54 <span>{charCount} / {MAX_LIMIT} characters</span>
55 <span className={remainingChars < 0 ? 'text-danger font-bold' : ''}>
56 {remainingChars >= 0 ? `${remainingChars} remaining` : `${Math.abs(remainingChars)} characters over limit`}
57 </span>
58 </div>
59
60 {/* Metrics Row */}
61 <div className="metrics-grid">
62 <div className="metric-box">
63 <span className="metric-val">{wordCount}</span>
64 <span className="metric-lbl">Words</span>
65 </div>
66 <div className="metric-box">
67 <span className="metric-val">{charsNoSpaces}</span>
68 <span className="metric-lbl">No Spaces</span>
69 </div>
70 <div className="metric-box">
71 <span className="metric-val">{sentenceCount}</span>
72 <span className="metric-lbl">Sentences</span>
73 </div>
74 <div className="metric-box">
75 <span className="metric-val">~{readingTimeMinutes} min</span>
76 <span className="metric-lbl">Read Time</span>
77 </div>
78 </div>
79
80 {/* Actions Toolbar */}
81 <div className="toolbar">
82 <button onClick={handleUppercase} disabled={!text}>UPPERCASE</button>
83 <button onClick={handleLowercase} disabled={!text}>lowercase</button>
84 <button onClick={handleTrimSpaces} disabled={!text}>Trim Spaces</button>
85 <button onClick={handleClear} disabled={!text} className="btn-danger">Clear</button>
86 <button onClick={handleCopy} disabled={!text} className="btn-copy">
87 {copied ? '✓ Copied!' : 'Copy to Clipboard'}
88 </button>
89 </div>
90 </div>
91 );
92}