Skip to content

Commit

Permalink
Improve log viewer search perf
Browse files Browse the repository at this point in the history
  • Loading branch information
Dartoxian committed Dec 5, 2023
1 parent 8991bdd commit 7e5ef75
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions enclave-manager/web/src/components/enclaves/logs/LogViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const LogViewer = ({
}
};

const handleSearchStateChange = (updater: ((prevState: SearchState) => SearchState) | SearchState) => {
const handleSearchStateChange = useCallback((updater: ((prevState: SearchState) => SearchState) | SearchState) => {
setSearchState((prevState) => {
const newState = typeof updater === "object" ? updater : updater(prevState);
if (
Expand All @@ -106,7 +106,7 @@ export const LogViewer = ({
}
return newState;
});
};
}, []);

const getLogsValue = () => {
return logLines
Expand Down Expand Up @@ -213,6 +213,8 @@ const SearchControls = ({ searchState, onChangeSearchState, logLines }: SearchCo
const searchRef: MutableRefObject<HTMLInputElement | null> = useRef(null);
const [showSearchForm, setShowSearchForm] = useState(false);

const maybeCurrentSearchIndex = searchState.type === "success" ? searchState.currentSearchIndex : null;

const updateMatches = useCallback(
(searchTerm: string) => {
if (isNotEmpty(searchTerm)) {
Expand Down Expand Up @@ -247,7 +249,7 @@ const SearchControls = ({ searchState, onChangeSearchState, logLines }: SearchCo
[logLines, onChangeSearchState],
);

const debouncedUpdateMatches = useMemo(() => debounce(updateMatches, 100), [updateMatches]);
const debouncedUpdateMatches = useMemo(() => debounce(updateMatches, 300), [updateMatches]);

const handleOnChange = (e: ChangeEvent<HTMLInputElement>) => {
onChangeSearchState((state) => ({ ...state, rawSearchTerm: e.target.value }));
Expand All @@ -256,35 +258,29 @@ const SearchControls = ({ searchState, onChangeSearchState, logLines }: SearchCo

const updateSearchIndexBounded = useCallback(
(newIndex: number) => {
if (searchState.type !== "success") {
return;
}
if (newIndex > searchState.searchMatchesIndices.length - 1) {
newIndex = 0;
}
if (newIndex < 0) {
newIndex = searchState.searchMatchesIndices.length - 1;
}
onChangeSearchState((state) => ({ ...state, currentSearchIndex: newIndex }));
onChangeSearchState((searchState) => {
if (searchState.type !== "success" || searchState.searchMatchesIndices.length === 0) {
return searchState;
}
if (newIndex > searchState.searchMatchesIndices.length - 1) {
newIndex = 0;
}
if (newIndex < 0) {
newIndex = searchState.searchMatchesIndices.length - 1;
}
return { ...searchState, currentSearchIndex: newIndex };
});
},
[onChangeSearchState, searchState],
[onChangeSearchState],
);

const handlePriorMatchClick = useCallback(() => {
updateSearchIndexBounded(
searchState.type === "success" && isDefined(searchState.currentSearchIndex)
? searchState.currentSearchIndex - 1
: 0,
);
}, [updateSearchIndexBounded, searchState]);
updateSearchIndexBounded(isDefined(maybeCurrentSearchIndex) ? maybeCurrentSearchIndex - 1 : 0);
}, [updateSearchIndexBounded, maybeCurrentSearchIndex]);

const handleNextMatchClick = useCallback(() => {
updateSearchIndexBounded(
searchState.type === "success" && isDefined(searchState.currentSearchIndex)
? searchState.currentSearchIndex + 1
: 0,
);
}, [updateSearchIndexBounded, searchState]);
updateSearchIndexBounded(isDefined(maybeCurrentSearchIndex) ? maybeCurrentSearchIndex + 1 : 0);
}, [updateSearchIndexBounded, maybeCurrentSearchIndex]);

const handleClearSearch = useCallback(() => {
onChangeSearchState({ type: "init", rawSearchTerm: "" });
Expand Down

0 comments on commit 7e5ef75

Please sign in to comment.