-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathApp.tsx
More file actions
235 lines (205 loc) · 8.44 KB
/
App.tsx
File metadata and controls
235 lines (205 loc) · 8.44 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import React, { useState, useEffect, useRef, useCallback } from 'react';
import { Header } from './components/Header';
import { EditorPanel } from './components/EditorPanel';
import { DiffPanel } from './components/DiffPanel';
import { SummaryPanel } from './components/SummaryPanel';
import { computeDiff, getDiffStats, generateHtmlDiff, DiffMode } from './services/diffService';
import { segmentDiffIntoSentences, generateRedlineSummary } from './services/aiService';
import { DiffPart, ScrollSource, Sentence, SummaryResult } from './types';
import { INITIAL_ORIGINAL, INITIAL_MODIFIED } from './constants';
export default function App() {
const [original, setOriginal] = useState(INITIAL_ORIGINAL);
const [modified, setModified] = useState(INITIAL_MODIFIED);
const [diffMode, setDiffMode] = useState<DiffMode>('char');
const [diffData, setDiffData] = useState<DiffPart[]>([]);
const [sentences, setSentences] = useState<Sentence[]>([]);
const [isSyncScrolling, setIsSyncScrolling] = useState(true);
// AI Summary State
const [summary, setSummary] = useState<SummaryResult | null>(null);
const [isSummaryOpen, setIsSummaryOpen] = useState(false);
const [isGeneratingSummary, setIsGeneratingSummary] = useState(false);
const [summaryError, setSummaryError] = useState<string | null>(null);
const [highlightedSentenceId, setHighlightedSentenceId] = useState<string | null>(null);
// Refs for scrolling synchronization
const originalRef = useRef<HTMLTextAreaElement>(null);
const modifiedRef = useRef<HTMLTextAreaElement>(null);
const diffRef = useRef<HTMLDivElement>(null);
// Ref to prevent circular scroll event loops
const isScrolling = useRef<ScrollSource>(ScrollSource.NONE);
// Compute diff whenever text changes
useEffect(() => {
// Simple debounce for very long text could be added here if needed,
// but React 18 auto-batching handles this reasonably well for typical legal docs.
const parts = computeDiff(original, modified, diffMode);
setDiffData(parts);
const sents = segmentDiffIntoSentences(parts);
setSentences(sents);
// Invalidate summary when text changes
if (summary) {
setSummary(null);
setHighlightedSentenceId(null);
}
}, [original, modified, diffMode]);
// Sync scroll logic
const handleScroll = useCallback((source: ScrollSource, el: HTMLElement) => {
// If sync is disabled or we are already handling a scroll event, ignore.
if (!isSyncScrolling || (isScrolling.current !== ScrollSource.NONE && isScrolling.current !== source)) return;
isScrolling.current = source;
// Calculate percentage
const percentage = el.scrollTop / (el.scrollHeight - el.offsetHeight);
const syncTo = (ref: React.RefObject<HTMLElement>) => {
if (ref.current && ref.current !== el) {
// Guard against division by zero or NaN if elements are hidden/empty
if (ref.current.scrollHeight > ref.current.offsetHeight) {
ref.current.scrollTop = percentage * (ref.current.scrollHeight - ref.current.offsetHeight);
} else {
ref.current.scrollTop = 0;
}
}
};
if (source === ScrollSource.ORIGINAL) {
syncTo(modifiedRef);
syncTo(diffRef);
} else if (source === ScrollSource.MODIFIED) {
syncTo(originalRef);
syncTo(diffRef);
} else if (source === ScrollSource.DIFF) {
syncTo(originalRef);
syncTo(modifiedRef);
}
// Debounce resetting the scroll lock
const timeout = setTimeout(() => {
isScrolling.current = ScrollSource.NONE;
}, 50);
return () => clearTimeout(timeout);
}, [isSyncScrolling]);
const handleGenerateSummary = async () => {
setIsSummaryOpen(true);
if (summary) return;
setIsGeneratingSummary(true);
setSummaryError(null);
try {
const result = await generateRedlineSummary(sentences);
setSummary(result);
} catch (err: any) {
setSummaryError(err.message || "Failed to generate summary");
} finally {
setIsGeneratingSummary(false);
}
};
const handleCopy = async () => {
try {
const htmlContent = generateHtmlDiff(diffData);
// For plain text, we just want the raw text content
const textContent = diffData.map(p => p.value).join('');
const blobHtml = new Blob([htmlContent], { type: 'text/html' });
const blobText = new Blob([textContent], { type: 'text/plain' });
// Use the Clipboard API to write both HTML and plain text
// This ensures formatting (colors, strikethrough) AND line breaks are preserved
const data = [new ClipboardItem({
'text/html': blobHtml,
'text/plain': blobText,
})];
await navigator.clipboard.write(data);
alert('Changes copied to clipboard!');
} catch (err) {
console.error('Failed to copy', err);
// Fallback for older browsers or if permission denied
try {
const diffElement = document.getElementById('diff-output-content');
if (diffElement) {
const range = document.createRange();
range.selectNode(diffElement);
const selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
selection.addRange(range);
document.execCommand('copy');
selection.removeAllRanges();
alert('Changes copied to clipboard (Fallback method)!');
}
}
} catch (fallbackErr) {
alert('Failed to copy changes.');
}
}
};
const handleExportPDF = () => {
window.print();
};
const handleResetAll = () => {
setOriginal('');
setModified('');
// Optionally focus the first input for convenience
originalRef.current?.focus();
};
const stats = getDiffStats(diffData);
return (
<div className="flex flex-col h-screen bg-slate-50">
<Header
onCopy={handleCopy}
onExportPdf={handleExportPDF}
onReset={handleResetAll}
onToggleSync={() => setIsSyncScrolling(!isSyncScrolling)}
onGenerateSummary={handleGenerateSummary}
diffMode={diffMode}
onDiffModeChange={setDiffMode}
syncEnabled={isSyncScrolling}
stats={stats}
/>
<main className="flex-1 overflow-hidden flex relative">
<div className="flex-1 p-4 overflow-hidden min-w-0">
<div className={`grid gap-4 h-full max-w-[1920px] mx-auto ${isSummaryOpen ? 'grid-cols-1' : 'grid-cols-1 md:grid-cols-3'}`}>
{/* Panel 1: Original */}
{!isSummaryOpen && (
<div className="h-full bg-white rounded-lg shadow-sm border border-slate-200 overflow-hidden">
<EditorPanel
title="Original Text"
value={original}
onChange={setOriginal}
onClear={() => setOriginal('')}
scrollRef={originalRef}
onScroll={(e) => handleScroll(ScrollSource.ORIGINAL, e.currentTarget)}
/>
</div>
)}
{/* Panel 2: Modified */}
{!isSummaryOpen && (
<div className="h-full bg-white rounded-lg shadow-sm border border-slate-200 overflow-hidden">
<EditorPanel
title="Modified Text"
value={modified}
onChange={setModified}
onClear={() => setModified('')}
scrollRef={modifiedRef}
onScroll={(e) => handleScroll(ScrollSource.MODIFIED, e.currentTarget)}
/>
</div>
)}
{/* Panel 3: Diff View */}
<div className="h-full bg-white rounded-lg shadow-lg border border-indigo-100 overflow-hidden relative ring-1 ring-indigo-500/10">
<DiffPanel
id="diff-output-content"
title="Redline Comparison"
diffParts={diffData}
sentences={sentences}
highlightedSentenceId={highlightedSentenceId}
scrollRef={diffRef}
onScroll={(e) => handleScroll(ScrollSource.DIFF, e.currentTarget)}
/>
</div>
</div>
</div>
{isSummaryOpen && (
<SummaryPanel
summary={summary}
isLoading={isGeneratingSummary}
error={summaryError}
onItemClick={setHighlightedSentenceId}
onClose={() => setIsSummaryOpen(false)}
/>
)}
</main>
</div>
);
}