From 63351cb855655f860e9e0bb052f2e1689bf1cb32 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Thu, 17 Feb 2022 12:01:35 +0100 Subject: [PATCH] wrap columnFactories in object Signed-off-by: Johan Haals --- plugins/catalog-react/api-report.md | 110 +++---- .../components/EntityTable/EntityTable.tsx | 8 +- .../src/components/EntityTable/columns.tsx | 270 +++++++++--------- .../src/components/EntityTable/index.ts | 1 + .../src/components/EntityTable/presets.tsx | 31 +- 5 files changed, 202 insertions(+), 218 deletions(-) diff --git a/plugins/catalog-react/api-report.md b/plugins/catalog-react/api-report.md index 4a1b9a9871..104472cbfe 100644 --- a/plugins/catalog-react/api-report.md +++ b/plugins/catalog-react/api-report.md @@ -102,42 +102,32 @@ export type CatalogReactUserListPickerClassKey = export const catalogRouteRef: RouteRef; // @public (undocumented) -function createDomainColumn(): TableColumn; - -// @public (undocumented) -function createEntityRefColumn(options: { - defaultKind?: string; -}): TableColumn; - -// @public (undocumented) -function createEntityRelationColumn({ - title, - relation, - defaultKind, - filter: entityFilter, -}: { - title: string; - relation: string; - defaultKind?: string; - filter?: { - kind: string; - }; -}): TableColumn; - -// @public (undocumented) -function createMetadataDescriptionColumn(): TableColumn; - -// @public (undocumented) -function createOwnerColumn(): TableColumn; - -// @public (undocumented) -function createSpecLifecycleColumn(): TableColumn; - -// @public (undocumented) -function createSpecTypeColumn(): TableColumn; - -// @public (undocumented) -function createSystemColumn(): TableColumn; +export const columnFactories: Readonly<{ + createEntityRefColumn(options: { + defaultKind?: string; + }): TableColumn; + createEntityRelationColumn({ + title, + relation, + defaultKind, + filter: entityFilter, + }: { + title: string; + relation: string; + defaultKind?: string | undefined; + filter?: + | { + kind: string; + } + | undefined; + }): TableColumn; + createOwnerColumn(): TableColumn; + createDomainColumn(): TableColumn; + createSystemColumn(): TableColumn; + createMetadataDescriptionColumn(): TableColumn; + createSpecLifecycleColumn(): TableColumn; + createSpecTypeColumn(): TableColumn; +}>; // @public (undocumented) export type DefaultEntityFilters = { @@ -329,24 +319,38 @@ export type EntitySourceLocation = { integrationType?: string; }; -// Warning: (ae-missing-release-tag) "EntityTable" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public -export function EntityTable( - props: EntityTableProps, -): JSX.Element; - -// @public (undocumented) -export namespace EntityTable { - var // Warning: (ae-forgotten-export) The symbol "columnFactories" needs to be exported by the entry point index.d.ts - // - // (undocumented) - columns: typeof columnFactories; - var // (undocumented) - systemEntityColumns: TableColumn[]; - var // (undocumented) - componentEntityColumns: TableColumn[]; -} +export const EntityTable: { + (props: EntityTableProps): JSX.Element; + columns: Readonly<{ + createEntityRefColumn(options: { + defaultKind?: string | undefined; + }): TableColumn; + createEntityRelationColumn({ + title, + relation, + defaultKind, + filter: entityFilter, + }: { + title: string; + relation: string; + defaultKind?: string | undefined; + filter?: + | { + kind: string; + } + | undefined; + }): TableColumn; + createOwnerColumn(): TableColumn; + createDomainColumn(): TableColumn; + createSystemColumn(): TableColumn; + createMetadataDescriptionColumn(): TableColumn; + createSpecLifecycleColumn(): TableColumn; + createSpecTypeColumn(): TableColumn; + }>; + systemEntityColumns: TableColumn[]; + componentEntityColumns: TableColumn[]; +}; // @public export interface EntityTableProps { diff --git a/plugins/catalog-react/src/components/EntityTable/EntityTable.tsx b/plugins/catalog-react/src/components/EntityTable/EntityTable.tsx index 1248721246..879a35ffd0 100644 --- a/plugins/catalog-react/src/components/EntityTable/EntityTable.tsx +++ b/plugins/catalog-react/src/components/EntityTable/EntityTable.tsx @@ -17,12 +17,12 @@ import { Entity } from '@backstage/catalog-model'; import { makeStyles } from '@material-ui/core'; import React, { ReactNode } from 'react'; -import * as columnFactories from './columns'; +import { columnFactories } from './columns'; import { componentEntityColumns, systemEntityColumns } from './presets'; import { Table, TableColumn } from '@backstage/core-components'; /** - * Props for {@link (EntityTable:function)}. + * Props for {@link EntityTable}. * * @public */ @@ -48,7 +48,7 @@ const useStyles = makeStyles(theme => ({ * * @public */ -export function EntityTable(props: EntityTableProps) { +export const EntityTable = (props: EntityTableProps) => { const { entities, title, @@ -85,7 +85,7 @@ export function EntityTable(props: EntityTableProps) { data={entities} /> ); -} +}; EntityTable.columns = columnFactories; diff --git a/plugins/catalog-react/src/components/EntityTable/columns.tsx b/plugins/catalog-react/src/components/EntityTable/columns.tsx index f6198fa1dc..d6c3582af6 100644 --- a/plugins/catalog-react/src/components/EntityTable/columns.tsx +++ b/plugins/catalog-react/src/components/EntityTable/columns.tsx @@ -30,150 +30,136 @@ import { } from '../EntityRefLink'; /** @public */ -export function createEntityRefColumn(options: { - defaultKind?: string; -}): TableColumn { - const { defaultKind } = options; - function formatContent(entity: T): string { - return ( - entity.metadata?.title || - formatEntityRefTitle(entity, { - defaultKind, - }) - ); - } - - return { - title: 'Name', - highlight: true, - customFilterAndSearch(filter, entity) { - // TODO: We could implement this more efficiently, like searching over - // each field that is displayed individually (kind, namespace, name). - // but that might confuse the user as it will behave different than a - // simple text search. - // Another alternative would be to cache the values. But writing them - // into the entity feels bad too. - return formatContent(entity).includes(filter); - }, - customSort(entity1, entity2) { - // TODO: We could implement this more efficiently by comparing field by field. - // This has similar issues as above. - return formatContent(entity1).localeCompare(formatContent(entity2)); - }, - render: entity => ( - - ), - }; -} - -/** @public */ -export function createEntityRelationColumn({ - title, - relation, - defaultKind, - filter: entityFilter, -}: { - title: string; - relation: string; - defaultKind?: string; - filter?: { kind: string }; -}): TableColumn { - function getRelations(entity: T): EntityName[] { - return getEntityRelations(entity, relation, entityFilter); - } - - function formatContent(entity: T): string { - return getRelations(entity) - .map(r => formatEntityRefTitle(r, { defaultKind })) - .join(', '); - } - - return { - title, - customFilterAndSearch(filter, entity) { - return formatContent(entity).includes(filter); - }, - customSort(entity1, entity2) { - return formatContent(entity1).localeCompare(formatContent(entity2)); - }, - render: entity => { +export const columnFactories = Object.freeze({ + createEntityRefColumn(options: { + defaultKind?: string; + }): TableColumn { + const { defaultKind } = options; + function formatContent(entity: T): string { return ( - + entity.metadata?.title || + formatEntityRefTitle(entity, { + defaultKind, + }) ); - }, - }; -} + } -/** @public */ -export function createOwnerColumn(): TableColumn { - return createEntityRelationColumn({ - title: 'Owner', - relation: RELATION_OWNED_BY, - defaultKind: 'group', - }); -} + return { + title: 'Name', + highlight: true, + customFilterAndSearch(filter, entity) { + // TODO: We could implement this more efficiently, like searching over + // each field that is displayed individually (kind, namespace, name). + // but that might confuse the user as it will behave different than a + // simple text search. + // Another alternative would be to cache the values. But writing them + // into the entity feels bad too. + return formatContent(entity).includes(filter); + }, + customSort(entity1, entity2) { + // TODO: We could implement this more efficiently by comparing field by field. + // This has similar issues as above. + return formatContent(entity1).localeCompare(formatContent(entity2)); + }, + render: entity => ( + + ), + }; + }, + createEntityRelationColumn({ + title, + relation, + defaultKind, + filter: entityFilter, + }: { + title: string; + relation: string; + defaultKind?: string; + filter?: { kind: string }; + }): TableColumn { + function getRelations(entity: T): EntityName[] { + return getEntityRelations(entity, relation, entityFilter); + } -/** @public */ -export function createDomainColumn(): TableColumn { - return createEntityRelationColumn({ - title: 'Domain', - relation: RELATION_PART_OF, - defaultKind: 'domain', - filter: { - kind: 'domain', - }, - }); -} + function formatContent(entity: T): string { + return getRelations(entity) + .map(r => formatEntityRefTitle(r, { defaultKind })) + .join(', '); + } -/** @public */ -export function createSystemColumn(): TableColumn { - return createEntityRelationColumn({ - title: 'System', - relation: RELATION_PART_OF, - defaultKind: 'system', - filter: { - kind: 'system', - }, - }); -} - -/** @public */ -export function createMetadataDescriptionColumn< - T extends Entity, ->(): TableColumn { - return { - title: 'Description', - field: 'metadata.description', - render: entity => ( - - ), - width: 'auto', - }; -} - -/** @public */ -export function createSpecLifecycleColumn(): TableColumn { - return { - title: 'Lifecycle', - field: 'spec.lifecycle', - }; -} - -/** @public */ -export function createSpecTypeColumn(): TableColumn { - return { - title: 'Type', - field: 'spec.type', - }; -} + return { + title, + customFilterAndSearch(filter, entity) { + return formatContent(entity).includes(filter); + }, + customSort(entity1, entity2) { + return formatContent(entity1).localeCompare(formatContent(entity2)); + }, + render: entity => { + return ( + + ); + }, + }; + }, + createOwnerColumn(): TableColumn { + return this.createEntityRelationColumn({ + title: 'Owner', + relation: RELATION_OWNED_BY, + defaultKind: 'group', + }); + }, + createDomainColumn(): TableColumn { + return this.createEntityRelationColumn({ + title: 'Domain', + relation: RELATION_PART_OF, + defaultKind: 'domain', + filter: { + kind: 'domain', + }, + }); + }, + createSystemColumn(): TableColumn { + return this.createEntityRelationColumn({ + title: 'System', + relation: RELATION_PART_OF, + defaultKind: 'system', + filter: { + kind: 'system', + }, + }); + }, + createMetadataDescriptionColumn(): TableColumn { + return { + title: 'Description', + field: 'metadata.description', + render: entity => ( + + ), + width: 'auto', + }; + }, + createSpecLifecycleColumn(): TableColumn { + return { + title: 'Lifecycle', + field: 'spec.lifecycle', + }; + }, + createSpecTypeColumn(): TableColumn { + return { + title: 'Type', + field: 'spec.type', + }; + }, +}); diff --git a/plugins/catalog-react/src/components/EntityTable/index.ts b/plugins/catalog-react/src/components/EntityTable/index.ts index 2ed07f9823..5fb651fe72 100644 --- a/plugins/catalog-react/src/components/EntityTable/index.ts +++ b/plugins/catalog-react/src/components/EntityTable/index.ts @@ -15,4 +15,5 @@ */ export { EntityTable } from './EntityTable'; +export { columnFactories } from './columns'; export type { EntityTableProps } from './EntityTable'; diff --git a/plugins/catalog-react/src/components/EntityTable/presets.tsx b/plugins/catalog-react/src/components/EntityTable/presets.tsx index 2ddffb0b7a..971ecfed20 100644 --- a/plugins/catalog-react/src/components/EntityTable/presets.tsx +++ b/plugins/catalog-react/src/components/EntityTable/presets.tsx @@ -15,29 +15,22 @@ */ import { ComponentEntity, SystemEntity } from '@backstage/catalog-model'; -import { - createDomainColumn, - createEntityRefColumn, - createMetadataDescriptionColumn, - createOwnerColumn, - createSpecLifecycleColumn, - createSpecTypeColumn, - createSystemColumn, -} from './columns'; +import { columnFactories } from './columns'; + import { TableColumn } from '@backstage/core-components'; export const systemEntityColumns: TableColumn[] = [ - createEntityRefColumn({ defaultKind: 'system' }), - createDomainColumn(), - createOwnerColumn(), - createMetadataDescriptionColumn(), + columnFactories.createEntityRefColumn({ defaultKind: 'system' }), + columnFactories.createDomainColumn(), + columnFactories.createOwnerColumn(), + columnFactories.createMetadataDescriptionColumn(), ]; export const componentEntityColumns: TableColumn[] = [ - createEntityRefColumn({ defaultKind: 'component' }), - createSystemColumn(), - createOwnerColumn(), - createSpecTypeColumn(), - createSpecLifecycleColumn(), - createMetadataDescriptionColumn(), + columnFactories.createEntityRefColumn({ defaultKind: 'component' }), + columnFactories.createSystemColumn(), + columnFactories.createOwnerColumn(), + columnFactories.createSpecTypeColumn(), + columnFactories.createSpecLifecycleColumn(), + columnFactories.createMetadataDescriptionColumn(), ];