-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.jsx
More file actions
143 lines (117 loc) · 4.46 KB
/
index.jsx
File metadata and controls
143 lines (117 loc) · 4.46 KB
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
import WordStatsCard from "./WordStatsCard";
import { useEffect, useRef, useState } from "react";
import WordTextArea from "./WordTextArea";
import WordCounterButtons from "./WordCounterButtons";
const WordCounter = () => {
const [drafts, setDrafts] = useState(() => {
const saved = JSON.parse(localStorage.getItem("wordDrafts")) || { 1: "" };
return saved;
});
const [activeDraftId, setActiveDraftId] = useState("1");
// Per-draft undo/redo history
const historyMapRef = useRef({
1: [""],
});
const indexMapRef = useRef({
1: 0,
});
useEffect(() => {
const savedDrafts = JSON.parse(localStorage.getItem("wordDrafts")) || { 1: "" };
setDrafts(savedDrafts);
// Initialize history for each draft
const historyInit = {};
const indexInit = {};
Object.entries(savedDrafts).forEach(([id, text]) => {
historyInit[id] = [text];
indexInit[id] = 0;
});
historyMapRef.current = historyInit;
indexMapRef.current = indexInit;
}, []);
const updateText = newText => {
const currentHistory = historyMapRef.current[activeDraftId] || [""];
const currentIndex = indexMapRef.current[activeDraftId] || 0;
const newHistory = currentHistory.slice(0, currentIndex + 1);
newHistory.push(newText);
historyMapRef.current[activeDraftId] = newHistory;
indexMapRef.current[activeDraftId] = newHistory.length - 1;
const updatedDrafts = { ...drafts, [activeDraftId]: newText };
setDrafts(updatedDrafts);
localStorage.setItem("wordDrafts", JSON.stringify(updatedDrafts));
};
const handleUndo = () => {
const index = indexMapRef.current[activeDraftId];
if (index > 0) {
indexMapRef.current[activeDraftId] = index - 1;
const prevText = historyMapRef.current[activeDraftId][index - 1];
const updatedDrafts = { ...drafts, [activeDraftId]: prevText };
setDrafts(updatedDrafts);
localStorage.setItem("wordDrafts", JSON.stringify(updatedDrafts));
}
};
const handleRedo = () => {
const index = indexMapRef.current[activeDraftId];
const history = historyMapRef.current[activeDraftId];
if (index < history.length - 1) {
indexMapRef.current[activeDraftId] = index + 1;
const nextText = history[index + 1];
const updatedDrafts = { ...drafts, [activeDraftId]: nextText };
setDrafts(updatedDrafts);
localStorage.setItem("wordDrafts", JSON.stringify(updatedDrafts));
}
};
const handleClear = () => {
historyMapRef.current[activeDraftId] = [""];
indexMapRef.current[activeDraftId] = 0;
const updatedDrafts = { ...drafts, [activeDraftId]: "" };
setDrafts(updatedDrafts);
localStorage.setItem("wordDrafts", JSON.stringify(updatedDrafts));
};
const handleAddDraft = () => {
const usedIds = Object.keys(drafts).map(Number);
const newId = String([1, 2, 3, 4, 5].find(id => !usedIds.includes(id)));
if (!newId) return; // All 5 slots used
const updatedDrafts = { ...drafts, [newId]: "" };
setDrafts(updatedDrafts);
setActiveDraftId(newId);
localStorage.setItem("wordDrafts", JSON.stringify(updatedDrafts));
historyMapRef.current[newId] = [""];
indexMapRef.current[newId] = 0;
};
const handleDeleteDraft = id => {
const updatedDrafts = { ...drafts };
delete updatedDrafts[id];
// Remove history and index
delete historyMapRef.current[id];
delete indexMapRef.current[id];
// If deleted active draft, switch to first available
const remainingIds = Object.keys(updatedDrafts);
const newActiveId = remainingIds[0] || "1";
setDrafts(updatedDrafts);
setActiveDraftId(newActiveId);
localStorage.setItem("wordDrafts", JSON.stringify(updatedDrafts));
};
return (
<div className="flex flex-col items-center justify-center">
<h1 className="text-4xl font-bold text-center">Word Counter</h1>
<WordStatsCard
text={drafts[activeDraftId] || ""}
activeDraftId={activeDraftId}
setActiveDraftId={setActiveDraftId}
drafts={drafts}
onAddDraft={handleAddDraft}
/>
<WordCounterButtons
text={drafts[activeDraftId] || ""}
setText={updateText}
onClear={handleClear}
onUndo={handleUndo}
onRedo={handleRedo}
onDeleteDraft={Object.keys(drafts).length > 1 ? handleDeleteDraft : null}
activeDraftId={activeDraftId}
/>
<WordTextArea text={drafts[activeDraftId] || ""} onChange={e => updateText(e.target.value)} />
</div>
);
};
export default WordCounter;