import "./item-list-layout.scss"; import groupBy from "lodash/groupBy"; import React, { ReactNode } from "react"; import { computed } from "mobx"; import { disposeOnUnmount, observer } from "mobx-react"; import { ConfirmDialog, ConfirmDialogParams } from "../confirm-dialog"; import { Table, TableCell, TableCellProps, TableHead, TableProps, TableRow, TableRowProps, TableSortCallback } from "../table"; import { autobind, createStorage, cssNames, IClassName, isReactNode, noop, prevDefault, stopPropagation } from "../../utils"; import { AddRemoveButtons, AddRemoveButtonsProps } from "../add-remove-buttons"; import { NoItems } from "../no-items"; import { Spinner } from "../spinner"; import { ItemObject, ItemStore } from "../../item.store"; import { SearchInputUrl } from "../input"; import { Filter, FilterType, pageFilters } from "./page-filters.store"; import { PageFiltersList } from "./page-filters-list"; import { PageFiltersSelect } from "./page-filters-select"; import { NamespaceSelectFilter } from "../+namespaces/namespace-select-filter"; import { themeStore } from "../../theme.store"; import { MenuActions } from "../menu/menu-actions"; import { MenuItem } from "../menu"; import { Checkbox } from "../checkbox"; import { userStore } from "../../../common/user-store"; import { namespaceStore } from "../+namespaces/namespace.store"; // todo: refactor, split to small re-usable components export type SearchFilter = (item: T) => string | number | (string | number)[]; export type ItemsFilter = (items: T[]) => T[]; export interface IHeaderPlaceholders { title: ReactNode; search: ReactNode; filters: ReactNode; info: ReactNode; } export interface ItemListLayoutProps { tableId?: string; className: IClassName; items?: T[]; store: ItemStore; dependentStores?: ItemStore[]; preloadStores?: boolean; isClusterScoped?: boolean; hideFilters?: boolean; searchFilters?: SearchFilter[]; /** @deprecated */ filterItems?: ItemsFilter[]; // header (title, filtering, searching, etc.) showHeader?: boolean; headerClassName?: IClassName; renderHeaderTitle?: ReactNode | ((parent: ItemListLayout) => ReactNode); customizeHeader?: (placeholders: IHeaderPlaceholders, content: ReactNode) => Partial | ReactNode; // items list configuration isReady?: boolean; // show loading indicator while not ready isSelectable?: boolean; // show checkbox in rows for selecting items isSearchable?: boolean; // apply search-filter & add search-input isConfigurable?: boolean; copyClassNameFromHeadCells?: boolean; sortingCallbacks?: { [sortBy: string]: TableSortCallback }; tableProps?: Partial; // low-level table configuration renderTableHeader: TableCellProps[] | null; renderTableContents: (item: T) => (ReactNode | TableCellProps)[]; renderItemMenu?: (item: T, store: ItemStore) => ReactNode; customizeTableRowProps?: (item: T) => Partial; addRemoveButtons?: Partial; virtual?: boolean; // item details view hasDetailsView?: boolean; detailsItem?: T; onDetails?: (item: T) => void; // other customizeRemoveDialog?: (selectedItems: T[]) => Partial; renderFooter?: (parent: ItemListLayout) => React.ReactNode; } const defaultProps: Partial = { showHeader: true, isSearchable: true, isSelectable: true, isConfigurable: false, copyClassNameFromHeadCells: true, preloadStores: true, dependentStores: [], filterItems: [], hasDetailsView: true, onDetails: noop, virtual: true, customizeTableRowProps: () => ({} as TableRowProps), }; @observer export class ItemListLayout extends React.Component { static defaultProps = defaultProps as object; private storage = createStorage("item_list_layout", { showFilters: false, // setup defaults }); get showFilters(): boolean { return this.storage.get().showFilters; } set showFilters(showFilters: boolean) { this.storage.merge({ showFilters }); } async componentDidMount() { const { isClusterScoped, isConfigurable, tableId, preloadStores } = this.props; if (isConfigurable && !tableId) { throw new Error("[ItemListLayout]: configurable list require props.tableId to be specified"); } if (preloadStores) { this.loadStores(); if (!isClusterScoped) { disposeOnUnmount(this, [ namespaceStore.onContextChange(() => this.loadStores()) ]); } } } private loadStores() { const { store, dependentStores } = this.props; const stores = Array.from(new Set([store, ...dependentStores])); // load context namespaces by default (see also: ``) stores.forEach(store => store.loadAll(namespaceStore.contextNamespaces)); } private filterCallbacks: { [type: string]: ItemsFilter } = { [FilterType.SEARCH]: items => { const { searchFilters, isSearchable } = this.props; const search = pageFilters.getValues(FilterType.SEARCH)[0] || ""; if (search && isSearchable && searchFilters) { const normalizeText = (text: string) => String(text).toLowerCase(); const searchTexts = [search].map(normalizeText); return items.filter(item => { return searchFilters.some(getTexts => { const sourceTexts: string[] = [getTexts(item)].flat().map(normalizeText); return sourceTexts.some(source => searchTexts.some(search => source.includes(search))); }); }); } return items; }, [FilterType.NAMESPACE]: items => { const filterValues = pageFilters.getValues(FilterType.NAMESPACE); if (filterValues.length > 0) { return items.filter(item => filterValues.includes(item.getNs())); } return items; }, }; @computed get isReady() { return this.props.isReady ?? this.props.store.isLoaded; } @computed get failedToLoad() { return this.props.store.failedLoading; } @computed get filters() { let { activeFilters } = pageFilters; const { isSearchable, searchFilters } = this.props; if (!(isSearchable && searchFilters)) { activeFilters = activeFilters.filter(({ type }) => type !== FilterType.SEARCH); } return activeFilters; } applyFilters(filters: ItemsFilter[], items: T[]): T[] { if (!filters || !filters.length) return items; return filters.reduce((items, filter) => filter(items), items); } @computed get items() { const {filters, filterCallbacks } = this; const filterGroups = groupBy(filters, ({ type }) => type); const filterItems: ItemsFilter[] = []; Object.entries(filterGroups).forEach(([type, filtersGroup]) => { const filterCallback = filterCallbacks[type]; if (filterCallback && filtersGroup.length > 0) { filterItems.push(filterCallback); } }); const items = this.props.items ?? this.props.store.items; return this.applyFilters(filterItems.concat(this.props.filterItems), items); } @autobind() getRow(uid: string) { const { isSelectable, renderTableHeader, renderTableContents, renderItemMenu, store, hasDetailsView, onDetails, copyClassNameFromHeadCells, customizeTableRowProps, detailsItem, } = this.props; const { isSelected } = store; const item = this.items.find(item => item.getId() == uid); if (!item) return; const itemId = item.getId(); return ( onDetails(item)) : undefined} {...customizeTableRowProps(item)} > {isSelectable && ( store.toggleSelection(item))} /> )} { renderTableContents(item).map((content, index) => { const cellProps: TableCellProps = isReactNode(content) ? { children: content } : content; const headCell = renderTableHeader?.[index]; if (copyClassNameFromHeadCells && headCell) { cellProps.className = cssNames(cellProps.className, headCell.className); } if (!headCell || !this.isHiddenColumn(headCell)) { return ; } }) } {renderItemMenu && ( {renderItemMenu(item, store)} )} ); } @autobind() removeItemsDialog() { const { customizeRemoveDialog, store } = this.props; const { selectedItems, removeSelectedItems } = store; const visibleMaxNamesCount = 5; const selectedNames = selectedItems.map(ns => ns.getName()).slice(0, visibleMaxNamesCount).join(", "); const dialogCustomProps = customizeRemoveDialog ? customizeRemoveDialog(selectedItems) : {}; const selectedCount = selectedItems.length; const tailCount = selectedCount > visibleMaxNamesCount ? selectedCount - visibleMaxNamesCount : 0; const tail = tailCount > 0 ? <>, and {tailCount} more : null; const message = selectedCount <= 1 ?

Remove item {selectedNames}?

:

Remove {selectedCount} items {selectedNames}{tail}?

; ConfirmDialog.open({ ok: removeSelectedItems, labelOk: "Remove", message, ...dialogCustomProps, }); } @autobind() toggleFilters() { this.showFilters = !this.showFilters; } renderFilters() { const { hideFilters } = this.props; const { isReady, filters } = this; if (!isReady || !filters.length || hideFilters || !this.showFilters) { return; } return ; } renderNoItems() { if (this.failedToLoad) { return Failed to load items.; } if (!this.isReady) { return ; } if (this.filters.length > 0) { return ( No items found.

pageFilters.reset()} className="contrast"> Reset filters?

); } return ; } renderItems() { if (this.props.virtual) { return null; } return this.items.map(item => this.getRow(item.getId())); } renderHeaderContent(placeholders: IHeaderPlaceholders): ReactNode { const { isSearchable, searchFilters } = this.props; const { title, filters, search, info } = placeholders; return ( <> {title}
{info}
{filters} {isSearchable && searchFilters && search} ); } renderInfo() { const { items, filters } = this; const allItemsCount = this.props.store.getTotalCount(); const itemsCount = items.length; if (filters.length > 0) { return ( <>Filtered: {itemsCount} / {allItemsCount} ); } return allItemsCount === 1 ? `${allItemsCount} item` : `${allItemsCount} items`; } renderHeader() { const { showHeader, customizeHeader, renderHeaderTitle, headerClassName, isClusterScoped } = this.props; if (!showHeader) return; const title = typeof renderHeaderTitle === "function" ? renderHeaderTitle(this) : renderHeaderTitle; const placeholders: IHeaderPlaceholders = { title:
{title}
, info: this.renderInfo(), filters: <> {!isClusterScoped && } , search: , }; let header = this.renderHeaderContent(placeholders); if (customizeHeader) { const modifiedHeader = customizeHeader(placeholders, header) ?? {}; if (isReactNode(modifiedHeader)) { header = modifiedHeader; } else { header = this.renderHeaderContent({ ...placeholders, ...modifiedHeader as IHeaderPlaceholders, }); } } return (
{header}
); } renderTableHeader() { const { customizeTableRowProps, renderTableHeader, isSelectable, isConfigurable, store } = this.props; if (!renderTableHeader) { return; } const enabledItems = this.items.filter(item => !customizeTableRowProps(item).disabled); return ( {isSelectable && ( store.toggleSelectionAll(enabledItems))} /> )} {renderTableHeader.map((cellProps, index) => { if (!this.isHiddenColumn(cellProps)) { return ; } })} {isConfigurable && this.renderColumnVisibilityMenu()} ); } renderList() { const { store, hasDetailsView, addRemoveButtons = {}, virtual, sortingCallbacks, detailsItem, className, tableProps = {}, tableId, } = this.props; const { removeItemsDialog, items } = this; const { selectedItems } = store; const selectedItemId = detailsItem && detailsItem.getId(); const classNames = cssNames(className, "box", "grow", themeStore.activeTheme.type); return (
{this.renderTableHeader()} {this.renderItems()}
); } @computed get hiddenColumns() { return userStore.getHiddenTableColumns(this.props.tableId); } isHiddenColumn({ id: columnId, showWithColumn }: TableCellProps): boolean { if (!this.props.isConfigurable) { return false; } return this.hiddenColumns.has(columnId) || ( showWithColumn && this.hiddenColumns.has(showWithColumn) ); } updateColumnVisibility({ id: columnId }: TableCellProps, isVisible: boolean) { const hiddenColumns = new Set(this.hiddenColumns); if (!isVisible) { hiddenColumns.add(columnId); } else { hiddenColumns.delete(columnId); } userStore.setHiddenTableColumns(this.props.tableId, hiddenColumns); } renderColumnVisibilityMenu() { const { renderTableHeader } = this.props; return ( {renderTableHeader.map((cellProps, index) => ( !cellProps.showWithColumn && ( `} value={!this.isHiddenColumn(cellProps)} onChange={isVisible => this.updateColumnVisibility(cellProps, isVisible)} /> ) ))} ); } renderFooter() { if (this.props.renderFooter) { return this.props.renderFooter(this); } } render() { const { className } = this.props; return (
{this.renderHeader()} {this.renderFilters()} {this.renderList()} {this.renderFooter()}
); } }