import "./namespace-select.scss" import React from "react"; import { computed } from "mobx"; import { observer } from "mobx-react"; import { t, Trans } from "@lingui/macro"; import { Select, SelectOption, SelectProps } from "../select"; import { cssNames, noop } from "../../utils"; import { Icon } from "../icon"; import { namespaceStore } from "./namespace.store"; import { _i18n } from "../../i18n"; import { FilterIcon } from "../item-object-list/filter-icon"; import { FilterType } from "../item-object-list/page-filters.store"; interface Props extends SelectProps { showIcons?: boolean; showClusterOption?: boolean; // show cluster option on the top (default: false) clusterOptionLabel?: React.ReactNode; // label for cluster option (default: "Cluster") customizeOptions?(nsOptions: SelectOption[]): SelectOption[]; } const defaultProps: Partial = { showIcons: true, showClusterOption: false, get clusterOptionLabel() { return _i18n._(t`Cluster`); }, }; @observer export class NamespaceSelect extends React.Component { static defaultProps = defaultProps as object; private unsubscribe = noop; async componentDidMount() { if (!namespaceStore.isLoaded) await namespaceStore.loadAll(); this.unsubscribe = namespaceStore.subscribe(); } componentWillUnmount() { this.unsubscribe(); } @computed get options(): SelectOption[] { const { customizeOptions, showClusterOption, clusterOptionLabel } = this.props; let options: SelectOption[] = namespaceStore.items.map(ns => ({ value: ns.getName() })); options = customizeOptions ? customizeOptions(options) : options; if (showClusterOption) { options.unshift({ value: null, label: clusterOptionLabel }); } return options; } formatOptionLabel = (option: SelectOption) => { const { showIcons } = this.props; const { value, label } = option; return label || ( <> {showIcons && } {value} ); } render() { const { className, showIcons, showClusterOption, clusterOptionLabel, customizeOptions, ...selectProps } = this.props; return (