fix unnecessary rerenders
This commit is contained in:
1 parent
5b1fbf8817
commit
de01a430a3
3 files changed
+51
-23
No files matched your search
@@ -132,6 +132,7 @@ const Search = ({
|
|||||||
}
|
}
|
||||||
return filtered;
|
return filtered;
|
||||||
}}
|
}}
|
||||||
|
getOptionKey={(option) => option.id}
|
||||||
renderOption={(props, option) => {
|
renderOption={(props, option) => {
|
||||||
const { key, ...optionProps } = props;
|
const { key, ...optionProps } = props;
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -13,10 +13,47 @@ import ActionsMenu from "../actionsMenu";
|
|||||||
// Utils
|
// Utils
|
||||||
import { useTheme } from "@emotion/react";
|
import { useTheme } from "@emotion/react";
|
||||||
import useUtils from "../../utils";
|
import useUtils from "../../utils";
|
||||||
import { memo } from "react";
|
import { useState, memo, useCallback } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import "../index.css";
|
import "../index.css";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
|
import { useTraceUpdate } from "../../../../Hooks/useTraceUpdate";
|
||||||
|
|
||||||
|
const SearchComponent = memo(
|
||||||
|
({ monitors, debouncedSearch, onSearchChange, setIsSearching }) => {
|
||||||
|
const [localSearch, setLocalSearch] = useState(debouncedSearch);
|
||||||
|
const handleSearch = useCallback(
|
||||||
|
(value) => {
|
||||||
|
setIsSearching(true);
|
||||||
|
setLocalSearch(value);
|
||||||
|
onSearchChange(value);
|
||||||
|
},
|
||||||
|
[onSearchChange, setIsSearching]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box
|
||||||
|
width="25%"
|
||||||
|
minWidth={150}
|
||||||
|
ml="auto"
|
||||||
|
>
|
||||||
|
<Search
|
||||||
|
options={monitors}
|
||||||
|
filteredBy="name"
|
||||||
|
inputValue={localSearch}
|
||||||
|
handleInputChange={handleSearch}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
SearchComponent.displayName = "SearchComponent";
|
||||||
|
SearchComponent.propTypes = {
|
||||||
|
monitors: PropTypes.array,
|
||||||
|
debouncedSearch: PropTypes.string,
|
||||||
|
onSearchChange: PropTypes.func,
|
||||||
|
setIsSearching: PropTypes.func,
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UptimeDataTable displays a table of uptime monitors with sorting, searching, and action capabilities
|
* UptimeDataTable displays a table of uptime monitors with sorting, searching, and action capabilities
|
||||||
@@ -55,7 +92,7 @@ const UptimeDataTable = ({
|
|||||||
monitorCount,
|
monitorCount,
|
||||||
sort,
|
sort,
|
||||||
setSort,
|
setSort,
|
||||||
search,
|
debouncedSearch,
|
||||||
setSearch,
|
setSearch,
|
||||||
isSearching,
|
isSearching,
|
||||||
setIsSearching,
|
setIsSearching,
|
||||||
@@ -67,11 +104,6 @@ const UptimeDataTable = ({
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const handleSearch = (value) => {
|
|
||||||
setIsSearching(true);
|
|
||||||
setSearch(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSort = (field) => {
|
const handleSort = (field) => {
|
||||||
let order = "";
|
let order = "";
|
||||||
if (sort.field !== field) {
|
if (sort.field !== field) {
|
||||||
@@ -202,18 +234,13 @@ const UptimeDataTable = ({
|
|||||||
>
|
>
|
||||||
{monitorCount}
|
{monitorCount}
|
||||||
</Box>
|
</Box>
|
||||||
<Box
|
|
||||||
width="25%"
|
<SearchComponent
|
||||||
minWidth={150}
|
monitors={monitors}
|
||||||
ml="auto"
|
debouncedSearch={debouncedSearch}
|
||||||
>
|
onSearchChange={setSearch}
|
||||||
<Search
|
setIsSearching={setIsSearching}
|
||||||
options={monitors}
|
/>
|
||||||
filteredBy="name"
|
|
||||||
inputValue={search}
|
|
||||||
handleInputChange={handleSearch}
|
|
||||||
/>
|
|
||||||
</Box>
|
|
||||||
</Stack>
|
</Stack>
|
||||||
<Box position="relative">
|
<Box position="relative">
|
||||||
{(isSearching || isLoading) && (
|
{(isSearching || isLoading) && (
|
||||||
@@ -280,7 +307,7 @@ UptimeDataTable.propTypes = {
|
|||||||
order: PropTypes.oneOf(["asc", "desc"]),
|
order: PropTypes.oneOf(["asc", "desc"]),
|
||||||
}),
|
}),
|
||||||
setSort: PropTypes.func,
|
setSort: PropTypes.func,
|
||||||
search: PropTypes.string,
|
debouncedSearch: PropTypes.string,
|
||||||
setSearch: PropTypes.func,
|
setSearch: PropTypes.func,
|
||||||
isSearching: PropTypes.bool,
|
isSearching: PropTypes.bool,
|
||||||
setIsSearching: PropTypes.func,
|
setIsSearching: PropTypes.func,
|
||||||
|
|||||||
@@ -134,9 +134,9 @@ const UptimeMonitors = () => {
|
|||||||
setPage(0);
|
setPage(0);
|
||||||
};
|
};
|
||||||
|
|
||||||
const triggerUpdate = () => {
|
const triggerUpdate = useCallback(() => {
|
||||||
setMonitorUpdateTrigger((prev) => !prev);
|
setMonitorUpdateTrigger((prev) => !prev);
|
||||||
};
|
}, []);
|
||||||
const totalMonitors = monitorsSummary?.totalMonitors ?? 0;
|
const totalMonitors = monitorsSummary?.totalMonitors ?? 0;
|
||||||
const hasMonitors = monitorsSummary?.totalMonitors ?? 0;
|
const hasMonitors = monitorsSummary?.totalMonitors ?? 0;
|
||||||
const canAddMonitor = isAdmin && hasMonitors;
|
const canAddMonitor = isAdmin && hasMonitors;
|
||||||
@@ -203,7 +203,7 @@ const UptimeMonitors = () => {
|
|||||||
monitorCount={totalMonitors}
|
monitorCount={totalMonitors}
|
||||||
sort={sort}
|
sort={sort}
|
||||||
setSort={setSort}
|
setSort={setSort}
|
||||||
search={search}
|
debouncedSearch={debouncedFilter}
|
||||||
setSearch={setSearch}
|
setSearch={setSearch}
|
||||||
isSearching={isSearching}
|
isSearching={isSearching}
|
||||||
setIsSearching={setIsSearching}
|
setIsSearching={setIsSearching}
|
||||||
|
|||||||
Reference in new issue
Block a user