1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/dock/dock-tab.tsx
Janne Savolainen f281df1693
Fix installation of helm charts (#5841)
* Relax validator for installing charts

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Tweak spacing between words in confirmation dialog

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Add mocks for monaco editor and virtualized auto sizer to allow components to be rendered in unit tests

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Improve typing for a function

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove usage of shared global state from a component

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Provide a way to unit test usages of storages

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Add way to get current value from select in behavioural unit tests

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Rework installation of helm charts to get rid of the majority of bugs

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Update snapshots

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove technical test for being covered in behaviours

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Split behaviour to smaller pieces

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Add tests accidentally removed back

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Mark functions causing side effects

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove behaviour covered by other behaviours

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Tweak naming

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Remove unused dependency

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2022-07-20 08:15:27 +03:00

133 lines
4.0 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./dock-tab.module.scss";
import React from "react";
import { observer } from "mobx-react";
import { autoBind, cssNames, prevDefault, isMiddleClick } from "../../utils";
import type { DockStore, DockTab as DockTabModel } from "./dock/store";
import type { TabProps } from "../tabs";
import { Tab } from "../tabs";
import { Icon } from "../icon";
import { Menu, MenuItem } from "../menu";
import { observable } from "mobx";
import { withInjectables } from "@ogre-tools/injectable-react";
import dockStoreInjectable from "./dock/store.injectable";
import { Tooltip, TooltipPosition } from "../tooltip";
import isMacInjectable from "../../../common/vars/is-mac.injectable";
export interface DockTabProps extends TabProps<DockTabModel> {
moreActions?: React.ReactNode;
}
interface Dependencies {
dockStore: DockStore;
isMac: boolean;
}
@observer
class NonInjectedDockTab extends React.Component<DockTabProps & Dependencies> {
private readonly menuVisible = observable.box(false);
constructor(props: DockTabProps & Dependencies) {
super(props);
autoBind(this);
}
close(id: string) {
this.props.dockStore.closeTab(id);
}
renderMenu(tabId: string) {
const { closeTab, closeAllTabs, closeOtherTabs, closeTabsToTheRight, tabs, getTabIndex } = this.props.dockStore;
const closeAllDisabled = tabs.length === 1;
const closeOtherDisabled = tabs.length === 1;
const closeRightDisabled = getTabIndex(tabId) === tabs.length - 1;
return (
<Menu
usePortal
htmlFor={`tab-${tabId}`}
className="DockTabMenu"
isOpen={this.menuVisible.get()}
open={() => this.menuVisible.set(true)}
close={() => this.menuVisible.set(false)}
toggleEvent="contextmenu"
>
<MenuItem onClick={() => closeTab(tabId)}>
Close
</MenuItem>
<MenuItem onClick={() => closeAllTabs()} disabled={closeAllDisabled}>
Close all tabs
</MenuItem>
<MenuItem onClick={() => closeOtherTabs(tabId)} disabled={closeOtherDisabled}>
Close other tabs
</MenuItem>
<MenuItem onClick={() => closeTabsToTheRight(tabId)} disabled={closeRightDisabled}>
Close tabs to the right
</MenuItem>
</Menu>
);
}
render() {
const { className, moreActions, dockStore, active, isMac, ...tabProps } = this.props;
if (!tabProps.value) {
return;
}
const { title, pinned, id } = tabProps.value;
const close = prevDefault(() => this.close(id));
return (
<>
<Tab
{...tabProps}
id={`tab-${id}`}
className={cssNames(styles.DockTab, className, {
[styles.pinned]: pinned,
})}
onContextMenu={() => this.menuVisible.set(true)}
label={(
<div className="flex align-center" onAuxClick={isMiddleClick(close)}>
<span className={styles.title}>{title}</span>
{moreActions}
{!pinned && (
<div className={styles.close}>
<Icon
small
material="close"
tooltip={`Close ${this.props.isMac ? "⌘+W" : "Ctrl+W"}`}
onClick={close}
/>
</div>
)}
<Tooltip
targetId={`tab-${id}`}
preferredPositions={[TooltipPosition.TOP, TooltipPosition.TOP_LEFT]}
style={{ transitionDelay: "700ms" }}
>
{title}
</Tooltip>
</div>
)}
data-testid={`dock-tab-for-${id}`}
/>
{this.renderMenu(id)}
</>
);
}
}
export const DockTab = withInjectables<Dependencies, DockTabProps>(NonInjectedDockTab, {
getProps: (di, props) => ({
dockStore: di.inject(dockStoreInjectable),
isMac: di.inject(isMacInjectable),
...props,
}),
});