1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

some more fixing type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-04-13 09:18:48 -04:00
parent bef48872fe
commit 56e5c601a8
2 changed files with 57 additions and 45 deletions

View File

@ -25,21 +25,35 @@ interface Dependencies {
commandOverlay: CommandOverlay; commandOverlay: CommandOverlay;
} }
function ignoreIf<T>(check: boolean, menuItems: T) { function ignoreIf<T>(check: boolean, menuItems: T[]): T[] {
return check ? [] : menuItems; return check ? [] : menuItems;
} }
function getHotbarSwitchOptions(hotbars: Hotbar[]): (Hotbar | typeof hotbarAddAction | typeof hotbarRemoveAction | typeof hotbarRenameAction)[] { interface HotbarSwitchActionOption {
action: typeof hotbarAddAction | typeof hotbarRemoveAction | typeof hotbarRenameAction;
}
interface SwitchToHotbarOption {
hotbar: Hotbar;
}
type HotbarSwitchOption = SwitchToHotbarOption | HotbarSwitchActionOption;
function getHotbarSwitchOptions(hotbars: Hotbar[]): HotbarSwitchOption[] {
return [ return [
...hotbars, ...hotbars.map(hotbar => ({ hotbar })),
hotbarAddAction, { action: hotbarAddAction },
...ignoreIf(hotbars.length > 1, [ ...ignoreIf(hotbars.length > 1, [
hotbarRemoveAction, { action: hotbarRemoveAction } as const,
] as const), ]),
hotbarRenameAction, { action: hotbarRenameAction },
]; ];
} }
function isActionOption(option: HotbarSwitchOption): option is HotbarSwitchActionOption {
return Boolean((option as HotbarSwitchActionOption).action);
}
const NonInjectedHotbarSwitchCommand = observer(({ const NonInjectedHotbarSwitchCommand = observer(({
hotbarStore, hotbarStore,
commandOverlay, commandOverlay,
@ -47,40 +61,45 @@ const NonInjectedHotbarSwitchCommand = observer(({
<Select <Select
id="switch-to-hotbar-input" id="switch-to-hotbar-input"
menuPortalTarget={null} menuPortalTarget={null}
onChange={(value) => { onChange={(option) => {
switch (value) { if (!option) {
case hotbarAddAction: return;
return commandOverlay.open(<HotbarAddCommand />); }
case hotbarRemoveAction:
return commandOverlay.open(<HotbarRemoveCommand />);
case hotbarRenameAction:
return commandOverlay.open(<HotbarRenameCommand />);
default: { if (isActionOption(option)) {
if (value) { switch (option.action) {
hotbarStore.setActiveHotbar(value); case hotbarAddAction:
commandOverlay.close(); return commandOverlay.open(<HotbarAddCommand />);
} case hotbarRemoveAction:
return commandOverlay.open(<HotbarRemoveCommand />);
case hotbarRenameAction:
return commandOverlay.open(<HotbarRenameCommand />);
} }
} }
} }
hotbarStore.setActiveHotbar(option.hotbar);
commandOverlay.close();
}}
components={{ DropdownIndicator: null, IndicatorSeparator: null }} components={{ DropdownIndicator: null, IndicatorSeparator: null }}
menuIsOpen={true} menuIsOpen={true}
options={getHotbarSwitchOptions(hotbarStore.hotbars)} options={getHotbarSwitchOptions(hotbarStore.hotbars)}
getOptionLabel={actionOrId => { getOptionLabel={option => {
switch (actionOrId) { if (isActionOption(option)) {
case hotbarAddAction: switch (option.action) {
return "Add hotbar ..."; case hotbarAddAction:
case hotbarRemoveAction: return "Add hotbar ...";
return "Remove hotbar ..."; case hotbarRemoveAction:
case hotbarRenameAction: return "Remove hotbar ...";
return "Rename hotbar ..."; case hotbarRenameAction:
default: return "Rename hotbar ...";
return hotbarStore.getDisplayLabel(actionOrId); }
} }
} }
return hotbarStore.getDisplayLabel(option.hotbar);
}}
autoFocus={true} autoFocus={true}
escapeClearsValue={false} escapeClearsValue={false}
isClearable={false}
placeholder="Switch to hotbar" placeholder="Switch to hotbar"
/> />
)); ));

View File

@ -5,7 +5,7 @@
import styles from "./react-table.module.scss"; import styles from "./react-table.module.scss";
import React, { useCallback, useMemo } from "react"; import React, { useCallback, useMemo } from "react";
import type { UseTableOptions } from "react-table"; import type { Row, UseTableOptions } from "react-table";
import { useFlexLayout, useSortBy, useTable } from "react-table"; import { useFlexLayout, useSortBy, useTable } from "react-table";
import { Icon } from "../icon"; import { Icon } from "../icon";
import { cssNames } from "../../utils"; import { cssNames } from "../../utils";
@ -39,19 +39,12 @@ export function ReactTable({ columns, data, headless }: ReactTableProps) {
useSortBy, useSortBy,
); );
const RenderRow = useCallback( const renderRow = useCallback(
({ index, style }) => { (row: Row<object>) => {
const row = rows[index];
prepareRow(row); prepareRow(row);
return ( return (
<div <div className={styles.tr}>
{...row.getRowProps({
style,
})}
className={styles.tr}
>
{row.cells.map((cell, index) => ( {row.cells.map((cell, index) => (
<div <div
{...cell.getCellProps()} {...cell.getCellProps()}
@ -64,7 +57,7 @@ export function ReactTable({ columns, data, headless }: ReactTableProps) {
</div> </div>
); );
}, },
[columns, prepareRow, rows], [columns, prepareRow],
); );
return ( return (
@ -106,7 +99,7 @@ export function ReactTable({ columns, data, headless }: ReactTableProps) {
)} )}
<div {...getTableBodyProps()}> <div {...getTableBodyProps()}>
{rows.map((row, index) => RenderRow({ index }))} {rows.map(renderRow)}
</div> </div>
</div> </div>
); );