Skip to content

Commit de2f08e

Browse files
committed
fix search input dropping typed characters when its own debounced write comes back
1 parent 16fc618 commit de2f08e

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

‎src/renderer/pages/common/SearchControl.tsx‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,28 @@ import { injectIntl } from "renderer/hocs/injectIntl";
1313
class SearchControl extends React.PureComponent<Props, State> {
1414
override state: State = { value: this.props.search ?? "" };
1515

16+
// values we wrote whose location update hasn't come back yet, oldest first
17+
inFlight: string[] = [];
18+
1619
override componentDidUpdate(prevProps: Props) {
20+
const search = this.props.search ?? "";
21+
if ((prevProps.search ?? "") === search) {
22+
return;
23+
}
24+
// our own write coming back: the input may already be ahead of it, so
25+
// it must not be reset to this older value
26+
const echoed = this.inFlight.indexOf(search);
27+
if (echoed >= 0) {
28+
this.inFlight.splice(0, echoed + 1);
29+
return;
30+
}
1731
// the page can change the search from outside (clicking a channel on
1832
// the builds page does this), cancel any pending debounced write so it
1933
// doesn't overwrite the new value
20-
if (
21-
prevProps.search !== this.props.search &&
22-
this.props.search !== this.state.value
23-
) {
34+
this.inFlight = [];
35+
if (search !== this.state.value) {
2436
this.setSearch.cancel();
25-
this.setState({ value: this.props.search ?? "" });
37+
this.setState({ value: search });
2638
}
2739
}
2840

@@ -53,6 +65,9 @@ class SearchControl extends React.PureComponent<Props, State> {
5365
// tab hasn't derived a location yet, nothing to evolve
5466
return;
5567
}
68+
if (search !== (this.props.search ?? "")) {
69+
this.inFlight.push(search);
70+
}
5671
dispatchTabEvolve(this.props, {
5772
replace: true,
5873
url: urlWithParams(url, { search }),

0 commit comments

Comments
 (0)