Skip to content

Commit 055c4fc

Browse files
Adam Erbsvc-squareup-copybara
authored andcommitted
Simplify command panel filtering on misk web-actions
GitOrigin-RevId: 789d81e64da07e1712c975df88d5ba20b0a63490
1 parent 3eada58 commit 055c4fc

File tree

3 files changed

+23
-48
lines changed

3 files changed

+23
-48
lines changed

misk-admin/web-actions/package-lock.json

Lines changed: 0 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

misk-admin/web-actions/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"@chakra-ui/icons": "^2.1.1",
4343
"@chakra-ui/react": "^2.8.2",
4444
"ace-builds": "^1.36.2",
45-
"fuse.js": "^7.0.0",
4645
"react-select": "^5.8.2"
4746
}
4847
}

misk-admin/web-actions/src/web-actions/ui/EndpointSelection.tsx

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22
import Select, { OnChangeValue, StylesConfig } from 'react-select';
3-
import Fuse from 'fuse.js';
43
import { ActionGroup, MiskActions } from '@web-actions/api/responseTypes';
54
import RealMetadataClient from '@web-actions/api/RealMetadataClient';
65

76
export interface EndpointOption {
87
value: ActionGroup;
98
label: string;
9+
lowerCaseLabel: string;
1010
}
1111

1212
export type EndpointSelectionCallbacks = ((value: ActionGroup) => void)[];
@@ -17,69 +17,60 @@ interface Props {
1717
}
1818

1919
interface State {
20-
endpointOptions: EndpointOption[];
21-
filterOptions: EndpointOption[];
20+
filteredOptions: EndpointOption[];
2221
inputValue: string;
2322
menuIsOpen: boolean;
2423
}
2524

2625
export default class EndpointSelection extends React.Component<Props, State> {
2726
private selectRef = React.createRef<any>();
28-
private fuse: Fuse<EndpointOption> | null = null;
2927
private metadataClient = new RealMetadataClient();
28+
private options: EndpointOption[] = [];
3029

3130
constructor(props: Props) {
3231
super(props);
3332

3433
this.state = {
35-
endpointOptions: [],
36-
filterOptions: [],
34+
filteredOptions: [],
3735
inputValue: '',
3836
menuIsOpen: false,
3937
};
4038
}
4139

4240
componentDidMount() {
4341
this.metadataClient.fetchMetadata().then((actions: MiskActions) => {
44-
const options = Object.entries(actions)
45-
.map(([key, value]) => ({
46-
value: value,
47-
label: key,
48-
}))
49-
.sort((a, b) => a.value.name.localeCompare(b.value.name));
50-
this.fuse = new Fuse(options, {
51-
keys: ['label'],
52-
useExtendedSearch: true,
53-
});
54-
this.setState({
55-
endpointOptions: options,
56-
filterOptions: options,
57-
});
42+
this.options = Object.values(actions)
43+
.sort((a, b) => a.name.localeCompare(b.name))
44+
.map((it) => ({
45+
label: it.name,
46+
lowerCaseLabel: it.name.toLowerCase(),
47+
value: it,
48+
}));
49+
this.setState({ filteredOptions: this.options });
5850
this.focusSelect();
5951
});
6052
}
6153

6254
componentDidUpdate(_: any, prevState: State) {
63-
if (
64-
prevState.inputValue !== this.state.inputValue ||
65-
prevState.endpointOptions !== this.state.endpointOptions
66-
) {
55+
if (prevState.inputValue !== this.state.inputValue) {
6756
this.updateFilterOptions();
6857
}
6958
}
7059

7160
updateFilterOptions() {
72-
const { inputValue, endpointOptions } = this.state;
73-
if (!inputValue.trim() || !this.fuse) {
74-
this.setState({ filterOptions: endpointOptions });
61+
const terms = this.state.inputValue
62+
.split(/\s+/)
63+
.filter((it) => it.length > 0)
64+
.map((it) => it.toLowerCase());
65+
if (terms.length === 0) {
66+
this.setState({ filteredOptions: this.options });
7567
return;
7668
}
7769

78-
const results = this.fuse
79-
.search(inputValue)
80-
.sort((a, b) => b.score! - a.score!);
8170
this.setState({
82-
filterOptions: results.map((result) => result.item),
71+
filteredOptions: this.options.filter((option) =>
72+
terms.every((term) => option.lowerCaseLabel.includes(term)),
73+
),
8374
});
8475
}
8576

@@ -115,7 +106,6 @@ export default class EndpointSelection extends React.Component<Props, State> {
115106
};
116107

117108
render() {
118-
const { filterOptions } = this.state;
119109
return (
120110
<Select<EndpointOption, false>
121111
ref={this.selectRef}
@@ -128,7 +118,7 @@ export default class EndpointSelection extends React.Component<Props, State> {
128118
onMenuClose={() => this.setMenuOpen(false)}
129119
onInputChange={this.handleInputChange}
130120
onChange={this.handleChange}
131-
options={filterOptions}
121+
options={this.state.filteredOptions}
132122
styles={
133123
{
134124
container: (base) => ({ ...base, width: '100%' }),

0 commit comments

Comments
 (0)