diff --git a/client/src/pages/History.tsx b/client/src/pages/History.tsx index b2ea5c307..3bb2cc5c7 100644 --- a/client/src/pages/History.tsx +++ b/client/src/pages/History.tsx @@ -27,31 +27,31 @@ import { advancedFilter } from "@/utils/search_filters"; import { Sheet, SheetContent, SheetHeader, SheetTitle } from "@/components/ui/sheet"; import RiskTrendChart from "@/components/RiskTrendChart"; -function HighlightText({ text, search }: { text: string; search: string }) { - if (!search.trim()) return <>{text}; +function HighlightText({ text, searchRegex }: { text: string; searchRegex: RegExp | null }) { + if (!searchRegex) return <>{text}; - const regex = new RegExp( - `(${search.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")})`, - "gi" - ); - const parts = text.split(regex); + try { + const parts = text.split(searchRegex); - return ( - <> - {parts.map((part, i) => - regex.test(part) ? ( - - {part} - - ) : ( - part - ) - )} - - ); + return ( + <> + {parts.map((part, i) => + searchRegex.test(part) ? ( + + {part} + + ) : ( + part + ) + )} + + ); + } catch { + return <>{text}; + } } export default function History() { @@ -63,6 +63,19 @@ export default function History() { const [searchTerm, setSearchTerm] = useState(""); const [sortBy, setSortBy] = useState("date-desc"); + // Memoize the search regex so it's compiled once per search term change + const searchRegex = useMemo(() => { + if (!searchTerm.trim()) return null; + try { + return new RegExp( + `(${searchTerm.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")})`, + "gi" + ); + } catch { + return null; + } + }, [searchTerm]); + // Date filter state const [startDate, setStartDate] = useState(""); const [endDate, setEndDate] = useState(""); @@ -103,13 +116,13 @@ export default function History() { const getRiskBadge = (category: string) => { const key = (category || "").toUpperCase(); - const highlight = ; + const highlight = ; if (key === "LOW") return ( } + highlightedLabel={} /> ); if (key === "MODERATE") @@ -118,7 +131,7 @@ export default function History() { variant="moderate" label="MODERATE" highlightedLabel={ - + } /> ); @@ -127,7 +140,7 @@ export default function History() { } + highlightedLabel={} /> ); return ( @@ -462,26 +475,26 @@ export default function History() { % @@ -493,7 +506,7 @@ export default function History() {