Skip to content

Commit bd29ae4

Browse files
Hari KiranHari Kiran
authored andcommitted
Fix #13 find keyword in dataset button in dataset details page
1 parent e357d5c commit bd29ae4

File tree

1 file changed

+23
-29
lines changed

1 file changed

+23
-29
lines changed

src/pages/DatasetDetailPage.tsx

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -354,65 +354,59 @@ const DatasetDetailPage: React.FC = () => {
354354
};
355355

356356
const highlightMatches = (keyword: string) => {
357-
// ✅ Step 1: Clean up all existing highlights
358-
document.querySelectorAll(".highlighted").forEach((el) => {
357+
const spans = document.querySelectorAll(
358+
".react-json-view span.string-value, .react-json-view span.object-key"
359+
);
360+
361+
// Clean up all existing highlights
362+
spans.forEach((el) => {
359363
const element = el as HTMLElement;
360-
const text = element.textContent || "";
361-
element.innerHTML = text; // ❗ This clears out <mark> completely
362-
element.classList.remove("highlighted");
364+
if (originalTextMap.has(element)) {
365+
element.innerHTML = originalTextMap.get(element)!; // Restore original HTML
366+
element.classList.remove("highlighted");
367+
}
363368
});
364369

365-
setOriginalTextMap(new Map()); // ✅ Also clear stored originals
370+
// Clear old state
371+
setMatches([]);
372+
setHighlightedIndex(-1);
373+
setExpandedPaths([]);
374+
setOriginalTextMap(new Map());
366375

367-
// ✅ Step 2: Early exit if search is too short
368-
if (!keyword.trim() || keyword.length < 3) {
369-
setMatches([]);
370-
setHighlightedIndex(-1);
371-
setExpandedPaths([]);
372-
return;
373-
}
376+
if (!keyword.trim() || keyword.length < 3) return;
374377

375-
// ✅ Step 3: Highlight matching keys/values
378+
const regex = new RegExp(`(${keyword})`, "gi");
376379
const matchedElements: HTMLElement[] = [];
377380
const matchedPaths: Set<string> = new Set();
378381
const newOriginalMap = new Map<HTMLElement, string>();
379382

380-
const spans = document.querySelectorAll(
381-
".react-json-view span.string-value, .react-json-view span.object-key"
382-
);
383-
384-
const regex = new RegExp(`(${keyword})`, "gi");
385-
386383
spans.forEach((el) => {
387384
const element = el as HTMLElement;
385+
const original = element.innerHTML;
388386
const text = element.textContent || "";
389387

390388
if (text.toLowerCase().includes(keyword.toLowerCase())) {
391-
newOriginalMap.set(element, text);
392-
393-
// Highlight match
389+
newOriginalMap.set(element, original); // Store original HTML
394390
const highlighted = text.replace(
395391
regex,
396392
`<mark class="highlighted" style="background-color: yellow; color: black;">$1</mark>`
397393
);
398394
element.innerHTML = highlighted;
399395
matchedElements.push(element);
400396

401-
// ✅ Collect path for expansion
402397
const parent = element.closest(".variable-row");
403398
const path = parent?.getAttribute("data-path");
404399
if (path) matchedPaths.add(path);
405400
}
406401
});
407402

408-
// ✅ Step 4: Update React state
403+
// Update state
409404
setOriginalTextMap(newOriginalMap);
410405
setMatches(matchedElements);
411-
setHighlightedIndex(-1);
412406
setExpandedPaths(Array.from(matchedPaths));
413-
};
414-
415-
const findNext = () => {
407+
};
408+
409+
const findNext = () => {
416410
if (matches.length === 0) return;
417411

418412
setHighlightedIndex((prevIndex) => {

0 commit comments

Comments
 (0)