Skip to content

Commit 72ef65c

Browse files
committed
Better debounce mecanism
1 parent 053d380 commit 72ef65c

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"next": "12.3.1",
9999
"oppa": "^0.4.0",
100100
"parse-numeric-range": "^1.3.0",
101-
"powerhooks": "^0.21.0",
101+
"powerhooks": "^0.22.0",
102102
"prettier": "^2.3.0",
103103
"react": "18.2.0",
104104
"react-dom": "18.2.0",

stories/ColorHelper/ColorHelper.tsx

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from "react";
1+
import React, { useState, useTransition } from "react";
22
import { CallOut } from "../../dist/CallOut";
33
import { colorDecisionAndCorrespondingOption } from "../../dist/fr/generatedFromCss/colorDecisionAndCorrespondingOptions";
44
import type { ColorDecisionAndCorrespondingOption } from "../../src/scripts/build/cssToTs/colorDecisionAndCorrespondingOptions";
@@ -12,6 +12,7 @@ import { Evt } from "evt";
1212
import { useStyles } from "./makeStyles";
1313
import { ColorDecisionCard } from "./ColorDecisionCard";
1414
import type { Props as SearchProps } from "./Search";
15+
import { useEffectOnValueChange } from "powerhooks/useEffectOnValueChange";
1516

1617
const colors = Array.from(
1718
new Set(
@@ -69,27 +70,33 @@ export function ColorHelper() {
6970
const [color, setColor] = useState<SearchProps["color"]>(undefined);
7071
const [usage, setUsage] = useState<SearchProps["usage"]>(undefined);
7172

72-
useDebounce({
73-
"query": search + (context ?? "") + (color ?? "") + (usage ?? ""),
74-
"onDebounced": () =>
75-
setFilteredColorDecisionAndCorrespondingOption(
76-
fzf
77-
.find(search)
78-
.map(
79-
({ item: colorDecisionAndCorrespondingOption }) =>
80-
colorDecisionAndCorrespondingOption
81-
)
82-
.filter(({ parsedColorDecisionName }) =>
83-
context === undefined ? true : parsedColorDecisionName.context === context
84-
)
85-
.filter(({ parsedColorDecisionName }) =>
86-
color === undefined ? true : parsedColorDecisionName.colorName === color
87-
)
88-
.filter(({ parsedColorDecisionName }) =>
89-
usage === undefined ? true : parsedColorDecisionName.usage === usage
90-
)
91-
)
92-
});
73+
const [, startTransition] = useTransition();
74+
75+
const updateSearch = () => {
76+
setFilteredColorDecisionAndCorrespondingOption(
77+
fzf
78+
.find(search)
79+
.map(
80+
({ item: colorDecisionAndCorrespondingOption }) =>
81+
colorDecisionAndCorrespondingOption
82+
)
83+
.filter(({ parsedColorDecisionName }) =>
84+
context === undefined ? true : parsedColorDecisionName.context === context
85+
)
86+
.filter(({ parsedColorDecisionName }) =>
87+
color === undefined ? true : parsedColorDecisionName.colorName === color
88+
)
89+
.filter(({ parsedColorDecisionName }) =>
90+
usage === undefined ? true : parsedColorDecisionName.usage === usage
91+
)
92+
);
93+
};
94+
95+
useEffectOnValueChange(() => {
96+
startTransition(() => updateSearch());
97+
}, [context, color, usage]);
98+
99+
useDebounce(updateSearch, [search]);
93100

94101
const { css } = useStyles();
95102

stories/ColorHelper/Search.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ export function Search(props: Props) {
102102
className={classes.filtersWrapper}
103103
>
104104
<Select
105-
label="Filter by context"
105+
label={`Filter by context (${contextes.length})`}
106+
disabled={contextes.length === 0}
106107
nativeSelectProps={{
107108
"onChange": event =>
108109
onContextChange(event.target.value || (undefined as any)),
@@ -116,7 +117,8 @@ export function Search(props: Props) {
116117
))}
117118
</Select>
118119
<Select
119-
label="Filter by color name"
120+
label={`Filter by color name (${colors.length})`}
121+
disabled={colors.length === 0}
120122
nativeSelectProps={{
121123
"onChange": event =>
122124
onColorChange(event.target.value || (undefined as any)),
@@ -130,7 +132,8 @@ export function Search(props: Props) {
130132
))}
131133
</Select>
132134
<Select
133-
label="Filter by usage"
135+
label={`Filter by usage (${usages.length})`}
136+
disabled={usages.length === 0}
134137
nativeSelectProps={{
135138
"onChange": event =>
136139
onUsageChange(event.target.value || (undefined as any)),

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9191,10 +9191,10 @@ postcss@^8.2.15, postcss@^8.4.18:
91919191
picocolors "^1.0.0"
91929192
source-map-js "^1.0.2"
91939193

9194-
powerhooks@^0.21.0:
9195-
version "0.21.0"
9196-
resolved "https://registry.yarnpkg.com/powerhooks/-/powerhooks-0.21.0.tgz#bafe67c2ce8c4c38cd26dec08de27fdf0d81da70"
9197-
integrity sha512-O9JG79YKv3yJ6Tkcx+eNWUC/rUKPCFUBkDIDbnoKM29P0+8VAx9pjp/C9eE2qnKNXr5azgnGtEoyLzGB/UKorQ==
9194+
powerhooks@^0.22.0:
9195+
version "0.22.0"
9196+
resolved "https://registry.yarnpkg.com/powerhooks/-/powerhooks-0.22.0.tgz#501cbdd28fd5427dd54e1796cd1089a02a101543"
9197+
integrity sha512-3HTG7MA9i8rE9Pk25TTO9bXAY12WWi+bypO5ZWGUN03MlqM1gWQDtxrTPJjx2zR3e6DbO+0jaunTdHv22fYNbg==
91989198
dependencies:
91999199
evt "^2.4.13"
92009200
memoizee "^0.4.15"

0 commit comments

Comments
 (0)