1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/cluster-settings/metrics-setting.tsx
Sebastian Malton 9589175604
Make EntitySettingRegistry fully injectable (#6604)
* Make EntitySettingRegistry fully injectable

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Add behavioural tests

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix lint

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Revert tsconfig change

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Fix type errors

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Update snapshot

Signed-off-by: Sebastian Malton <sebastian@malton.name>

* Improve naming

Signed-off-by: Sebastian Malton <sebastian@malton.name>

Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-11-28 12:13:15 -05:00

114 lines
3.1 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { disposeOnUnmount, observer } from "mobx-react";
import { onMultiSelectFor, Select } from "../select/select";
import { Icon } from "../icon/icon";
import { Button } from "../button/button";
import { SubTitle } from "../layout/sub-title";
import type { Cluster } from "../../../common/cluster/cluster";
import { observable, reaction, makeObservable } from "mobx";
import { ClusterMetricsResourceType } from "../../../common/cluster-types";
export interface ClusterMetricsSettingProps {
cluster: Cluster;
}
@observer
export class ClusterMetricsSetting extends React.Component<ClusterMetricsSettingProps> {
@observable hiddenMetrics = observable.set<string>();
constructor(props: ClusterMetricsSettingProps) {
super(props);
makeObservable(this);
}
componentDidMount() {
this.hiddenMetrics = observable.set<string>(this.props.cluster.preferences.hiddenMetrics ?? []);
disposeOnUnmount(this, [
reaction(() => this.props.cluster.preferences.hiddenMetrics, () => {
this.hiddenMetrics = observable.set<string>(this.props.cluster.preferences.hiddenMetrics ?? []);
}),
]);
}
save = () => {
this.props.cluster.preferences.hiddenMetrics = Array.from(this.hiddenMetrics);
};
onChangeButton = () => {
this.hiddenMetrics.replace(Object.keys(ClusterMetricsResourceType));
this.save();
};
reset = () => {
this.hiddenMetrics.clear();
this.save();
};
renderMetricsSelect() {
const metricResourceTypeOptions = Object.values(ClusterMetricsResourceType)
.map(type => ({
value: type,
label: type,
isSelected: this.hiddenMetrics.has(type),
}));
return (
<>
<Select
id="cluster-metric-resource-type-input"
className="box grow"
placeholder="Select metrics to hide..."
isMulti
isSearchable
onMenuClose={this.save}
closeMenuOnSelect={false}
controlShouldRenderValue={false}
options={metricResourceTypeOptions}
onChange={onMultiSelectFor(this.hiddenMetrics)}
formatOptionLabel={(option) => (
<div className="flex gaps align-center">
<span>{option.value}</span>
{option.isSelected && (
<Icon
smallest
material="check"
className="box right"
/>
)}
</div>
)}
themeName="lens"
/>
<Button
primary
label="Hide all metrics"
onClick={this.onChangeButton}
/>
<Button
primary
label="Reset"
onClick={this.reset}
/>
</>
);
}
render() {
return (
<div className="MetricsSelec0 mb-5">
<SubTitle title={"Hide metrics from the UI"}/>
<div className="flex gaps">
{this.renderMetricsSelect()}
</div>
</div>
);
}
}