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

View File

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