Skip to content

Commit

Permalink
update to use createSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
Xelus22 committed Nov 18, 2023
1 parent 5c578a5 commit d0b1efc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
19 changes: 6 additions & 13 deletions src/components/panes/configure-panes/keycode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from 'src/store/definitionsSlice';
import {getSelectedConnectedDevice,
getSelectedKeyboardAPI,
getMacroCount,
} from 'src/store/devicesSlice';
import {
getSelectedKey,
Expand Down Expand Up @@ -149,21 +150,13 @@ export const KeycodePane: FC = () => {
const disableFastRemap = useAppSelector(getDisableFastRemap);
const selectedKeyDefinitions = useAppSelector(getSelectedKeyDefinitions);
const {basicKeyToByte} = useAppSelector(getBasicKeyToByte);
const [numMacros, setNumMacros] = useState(16);

const api = useAppSelector(getSelectedKeyboardAPI);
if (!api) {
return null;
}
api.getMacroCount().then((nMacros) =>{
setNumMacros(nMacros);
console.log("numMacros: ", numMacros);
})
// let numMacros = async() => await new Promise(resolve => api.getMacroCount());
const macroCount = useAppSelector(getMacroCount);

if (!macroCount) return null;
console.log(macroCount);
const KeycodeCategories = useMemo(
() => generateKeycodeCategories(basicKeyToByte, numMacros),
[basicKeyToByte, numMacros],
() => generateKeycodeCategories(basicKeyToByte, macroCount),
[basicKeyToByte, macroCount],
);

// TODO: improve typing so we can get rid of this
Expand Down
8 changes: 8 additions & 0 deletions src/store/devicesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,11 @@ export const getSelectedKeyboardAPI = createSelector(
getSelectedDevicePath,
(path) => path && new KeyboardAPI(path),
);
export const getMacroCount = createSelector(
getSelectedKeyboardAPI,
async (api) => {
if (!api) return null;
const value:number = await api.getMacroCount();
return value;
},
);
1 change: 0 additions & 1 deletion src/utils/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ function buildLayerMenu(): IKeycodeMenu {
}

function generateMacros(numMacros: number = 16): IKeycode[] {
const macroTemplate: IKeycode = {name: 'M0', code: 'MACRO(0)', title: 'Macro 0'};
let res: IKeycode[] = [];
for (let idx = 0; idx < numMacros; idx++) {
const newName = `M${idx}`;
Expand Down

0 comments on commit d0b1efc

Please sign in to comment.