Skip to content

Commit

Permalink
Adjusted the method filterRows
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico4899 committed Dec 17, 2023
1 parent d601027 commit b51ffc2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
51 changes: 36 additions & 15 deletions src/components/TraceGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { SignalFrame } from "../types/SignalFrame";
import SignalFrameDetail from "./SignalFrameDetail";
import { UndefinedFrame } from "../types/UndefinedFrame";
import { ErrorFrame } from "../types/ErrorFrame";

import { levenshteinDistance } from "../utils/levenshteinDistance";
import TraceSearchBar from "./TraceSearchBar";

// Main Paper component with gradient background
Expand Down Expand Up @@ -267,6 +267,13 @@ function TraceGrid() {
}
}

function highlightText(text: string, query: string) {
// Simplified example. You might need a more complex logic
// to highlight based on the Levenshtein distance
const regex = new RegExp(`(${query})`, 'gi');
return text.replace(regex, '<b>$1</b>');
}

useEffect(() => {
invoke<TraceObjectEvent[]>("listen_to_trace").then((traceObjectEvents) => {
for (let traceObjectEvent of traceObjectEvents) {
Expand All @@ -293,20 +300,29 @@ function TraceGrid() {

const filterRows = (searchText: string) => {
searchStringRef.current = searchText;
if (searchText === "") {
setFilteredRows(rowsRef.current.slice());
} else {
const filtered = rowsRef.current.filter((o) => {
const frameName =
(o.frame.TypeFrame?.name ||
o.frame.SignalFrame?.name ||
o.frame.ErrorFrame?.name ||
"") as string; // Handle all possible frame types and cast to string
return frameName.includes(searchText);
});
setFilteredRows(filtered);
}

const searchThreshold = 3; // Set a suitable threshold for Levenshtein distance

const filtered = rowsRef.current.filter(o => {
const frameName = o.frame.TypeFrame?.name || o.frame.SignalFrame?.name || o.frame.ErrorFrame?.name || "";
const frameId = o.frame.TypeFrame?.id || o.frame.SignalFrame?.id || "";

const distanceName = levenshteinDistance(searchText, frameName);
const distanceId = levenshteinDistance(searchText, frameId.toString());

return distanceName <= searchThreshold || distanceId <= searchThreshold;
}).map(o => {
// Assuming highlightText function returns the string with highlighted portions
return {
...o,
highlightedName: highlightText(o.frame.TypeFrame?.name || o.frame.SignalFrame?.name || o.frame.ErrorFrame?.name || "", searchText),
highlightedId: highlightText((o.frame.TypeFrame?.id || o.frame.SignalFrame?.id || "").toString(), searchText)
};
});

setFilteredRows(filtered);
};


// maxHeight : 800 sucks asss
return (
Expand Down Expand Up @@ -354,7 +370,12 @@ function TraceGrid() {
</TableHead>
<TableBody>
{filteredRows.map((evt, index) => {
return <Row evt={evt} key={index} timeAbsolute={timeAbsolute} />
return (
<Row
evt={evt}
key={index}
timeAbsolute={timeAbsolute} />
);
})}
</TableBody>
</Table>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/levenshteinDistance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function levenshteinDistance(a: String, b: String) {
export function levenshteinDistance(a: string, b: string) {
const matrix = [];

for (let i = 0; i <= b.length; i++) {
Expand Down

0 comments on commit b51ffc2

Please sign in to comment.