Skip to content

Commit 76815f1

Browse files
Kent C. DoddsCompuIves
authored andcommitted
fix(genie): allow genie to sort wishes properly (codesandbox#1013)
* fix(genie): allow genie to sort wishes properly One of the cool things about genie is it's sorting algorithm takes into account the user's previous choices. By providing the input value when you make the wish, genie can keep track of what input values lead to which wishes and can optimistically suggest certain wishes as the user's typing. * keep track of inputValue * persist genie's enteredMagicWords to localstorage * Update index.js * Update index.js
1 parent bdc9ce6 commit 76815f1

File tree

1 file changed

+26
-1
lines changed
  • packages/app/src/app/pages/Sandbox/QuickActions

1 file changed

+26
-1
lines changed

packages/app/src/app/pages/Sandbox/QuickActions/index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import {
1616
} from './elements';
1717

1818
class QuickActions extends React.Component {
19+
// we'll just keep track of what the user changes the inputValue to be
20+
// so when the user makes a wish we can provide that info to genie
21+
inputValue = ''
1922
updateGenie = () => {
2023
const keybindings = this.props.store.preferences.keybindings;
2124
const signals = this.props.signals;
@@ -44,6 +47,7 @@ class QuickActions extends React.Component {
4447

4548
componentDidMount() {
4649
this.updateGenie();
50+
this.loadGenie();
4751
}
4852

4953
componentDidUpdate() {
@@ -63,10 +67,28 @@ class QuickActions extends React.Component {
6367
};
6468

6569
onChange = item => {
66-
genie.makeWish(item);
70+
genie.makeWish(item, this.inputValue);
71+
this.persistGenie();
6772
this.closeQuickActions();
6873
};
6974

75+
persistGenie() {
76+
const { enteredMagicWords } = genie.options();
77+
window.localStorage.setItem('genie', JSON.stringify({ enteredMagicWords }))
78+
}
79+
80+
loadGenie() {
81+
try {
82+
const { enteredMagicWords } = JSON.parse(window.localStorage.getItem('genie'))
83+
genie.options({ enteredMagicWords })
84+
} catch (error) {
85+
// it may not exist in localStorage yet, or the JSON was malformed somehow
86+
// so we'll persist it to update localStorage so it doesn't throw an error
87+
// next time the page is loaded.
88+
this.persistGenie()
89+
}
90+
}
91+
7092
itemToString = item => item && item.magicWords.join(', ');
7193

7294
render() {
@@ -92,6 +114,9 @@ class QuickActions extends React.Component {
92114
highlightedIndex,
93115
}) => {
94116
const inputProps = getInputProps({
117+
onChange: ev => {
118+
this.inputValue = ev.target.value
119+
},
95120
innerRef: el => el && el.focus(),
96121
onKeyUp: this.handleKeyUp,
97122
// Timeout so the fuzzy handler can still select the module

0 commit comments

Comments
 (0)