File size: 10,695 Bytes
e69f256 | 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | "use client";
import { useState, useRef, useCallback, useEffect } from "react";
export default function SplitView({ paragraphs, classifications, enriched }) {
const leftRef = useRef(null);
const rightRef = useRef(null);
const [syncing, setSyncing] = useState(false);
const [hoveredIndex, setHoveredIndex] = useState(null);
// Build a lookup: paragraph index -> classification
const classMap = new Map();
if (classifications && classifications.length > 0) {
for (const c of classifications) {
classMap.set(c.index, c);
}
}
// Sync scroll between panels
const handleScroll = useCallback(
(source) => {
if (syncing) return;
setSyncing(true);
const sourceEl = source === "left" ? leftRef.current : rightRef.current;
const targetEl = source === "left" ? rightRef.current : leftRef.current;
if (sourceEl && targetEl) {
const ratio =
sourceEl.scrollTop /
(sourceEl.scrollHeight - sourceEl.clientHeight || 1);
targetEl.scrollTop =
ratio * (targetEl.scrollHeight - targetEl.clientHeight);
}
requestAnimationFrame(() => setSyncing(false));
},
[syncing]
);
// Scroll to a paragraph on click
const scrollToIndex = useCallback((index, panel) => {
const ref = panel === "left" ? leftRef : rightRef;
const el = ref.current?.querySelector(`[data-para-index="${index}"]`);
if (el) {
el.scrollIntoView({ behavior: "smooth", block: "center" });
}
}, []);
return (
<div className="flex-1 flex min-h-0">
{/* Left panel: Original document view */}
<div
className="flex-1 flex flex-col border-r"
style={{ borderColor: "var(--border-color)" }}
>
<div
className="px-4 py-2 text-xs font-medium border-b flex items-center gap-2"
style={{
background: "var(--bg-tertiary)",
borderColor: "var(--border-color)",
color: "var(--text-secondary)",
}}
>
<svg
className="w-3.5 h-3.5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"
/>
</svg>
Original Document
</div>
<div
ref={leftRef}
className="split-panel px-4 py-3"
onScroll={() => handleScroll("left")}
style={{ background: "var(--bg-secondary)" }}
>
{paragraphs.map((p) => (
<div
key={`orig-${p.index}`}
data-para-index={p.index}
className={`para-row px-3 py-2 rounded-md mb-1 transition-colors cursor-default ${
hoveredIndex === p.index ? "ring-1" : ""
}`}
style={{
...(hoveredIndex === p.index
? {
background: "var(--bg-hover)",
ringColor: "var(--accent)",
}
: {}),
}}
onMouseEnter={() => setHoveredIndex(p.index)}
onMouseLeave={() => setHoveredIndex(null)}
>
{p.text ? (
<p className="text-sm leading-relaxed" style={{ color: "var(--text-primary)" }}>
<span
className="text-xs mr-2 select-none"
style={{ color: "var(--text-muted)" }}
>
{p.index}
</span>
{p.text}
</p>
) : (
<p className="text-sm" style={{ color: "var(--text-muted)", opacity: 0.3 }}>
<span className="text-xs mr-2 select-none">{p.index}</span>
(empty)
</p>
)}
{p.fontSizePt && (
<span
className="text-xs ml-6"
style={{ color: "var(--text-muted)" }}
>
{p.fontSizePt}pt
{p.isBold ? " • bold" : ""}
</span>
)}
</div>
))}
</div>
</div>
{/* Right panel: Enriched view with toggle controls */}
<div className="flex-1 flex flex-col">
<div
className="px-4 py-2 text-xs font-medium border-b flex items-center gap-2"
style={{
background: "var(--bg-tertiary)",
borderColor: "var(--border-color)",
color: "var(--text-secondary)",
}}
>
<svg
className="w-3.5 h-3.5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"
/>
</svg>
Enriched Document
{enriched && (
<span className="ml-auto text-xs" style={{ color: "var(--text-muted)" }}>
Click checkboxes to toggle headings
</span>
)}
</div>
<div
ref={rightRef}
className="split-panel px-4 py-3"
onScroll={() => handleScroll("right")}
style={{ background: "var(--bg-secondary)" }}
>
{!enriched ? (
<div className="flex items-center justify-center h-full">
<div className="text-center">
<div
className="w-16 h-16 mx-auto mb-4 rounded-full flex items-center justify-center"
style={{ background: "var(--bg-tertiary)" }}
>
<svg
className="w-7 h-7"
style={{ color: "var(--text-muted)" }}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={1.5}
d="M13 10V3L4 14h7v7l9-11h-7z"
/>
</svg>
</div>
<p
className="text-sm font-medium mb-1"
style={{ color: "var(--text-secondary)" }}
>
Click "Enrich with AI" to start
</p>
<p className="text-xs" style={{ color: "var(--text-muted)" }}>
The AI will detect which paragraphs are headings
</p>
</div>
</div>
) : (
<EnrichedParagraphs
paragraphs={paragraphs}
classifications={classifications}
classMap={classMap}
hoveredIndex={hoveredIndex}
setHoveredIndex={setHoveredIndex}
/>
)}
</div>
</div>
</div>
);
}
function EnrichedParagraphs({
paragraphs,
classifications,
classMap,
hoveredIndex,
setHoveredIndex,
}) {
// We maintain local state for checkbox toggling
// This lets users toggle headings before clicking "Apply Changes"
const [localClassifications, setLocalClassifications] = useState(classifications);
// Sync when parent classifications change (after enrich or apply)
useEffect(() => {
setLocalClassifications(classifications);
}, [classifications]);
const localClassMap = new Map();
for (const c of localClassifications) {
localClassMap.set(c.index, c);
}
const toggleHeading = (index) => {
setLocalClassifications((prev) =>
prev.map((c) =>
c.index === index ? { ...c, isHeading: !c.isHeading } : c
)
);
// Also update the parent's classifications directly so "Apply Changes" picks them up
const existing = classMap.get(index);
if (existing) {
existing.isHeading = !existing.isHeading;
}
};
return (
<>
{paragraphs.map((p) => {
const cls = localClassMap.get(p.index);
const isHeading = cls?.isHeading || false;
return (
<div
key={`enr-${p.index}`}
data-para-index={p.index}
className={`para-row flex items-start gap-3 px-3 py-2 rounded-md mb-1 transition-colors ${
isHeading ? "is-heading" : ""
} ${hoveredIndex === p.index ? "ring-1" : ""}`}
style={{
...(hoveredIndex === p.index
? {
background: "var(--bg-hover)",
ringColor: "var(--accent)",
}
: {}),
}}
onMouseEnter={() => setHoveredIndex(p.index)}
onMouseLeave={() => setHoveredIndex(null)}
>
<input
type="checkbox"
className="heading-checkbox mt-1"
checked={isHeading}
onChange={() => toggleHeading(p.index)}
title={isHeading ? "Unmark as heading" : "Mark as heading"}
/>
<div className="flex-1 min-w-0">
{p.text ? (
<p
className={`leading-relaxed ${isHeading ? "font-bold" : ""}`}
style={{
color: isHeading
? "var(--accent)"
: "var(--text-primary)",
fontSize: isHeading ? "16px" : "14px",
}}
>
<span
className="text-xs mr-2 font-normal select-none"
style={{ color: "var(--text-muted)" }}
>
{p.index}
</span>
{p.text}
</p>
) : (
<p className="text-sm" style={{ color: "var(--text-muted)", opacity: 0.3 }}>
<span className="text-xs mr-2 select-none">{p.index}</span>
(empty)
</p>
)}
{isHeading && (
<span
className="text-xs ml-6 inline-block mt-0.5"
style={{ color: "var(--accent)" }}
>
✦ Heading — font size will be increased
</span>
)}
</div>
</div>
);
})}
</>
);
}
|