diff --git a/plugins/techdocs/src/home/components/Tables/DocsTable.tsx b/plugins/techdocs/src/home/components/Tables/DocsTable.tsx index 850ce4250b..29dbb3fd4f 100644 --- a/plugins/techdocs/src/home/components/Tables/DocsTable.tsx +++ b/plugins/techdocs/src/home/components/Tables/DocsTable.tsx @@ -18,11 +18,7 @@ import React from 'react'; import useCopyToClipboard from 'react-use/esm/useCopyToClipboard'; import { configApiRef, useApi, useRouteRef } from '@backstage/core-plugin-api'; -import { Entity, RELATION_OWNED_BY } from '@backstage/catalog-model'; -import { - getEntityRelations, - humanizeEntityRef, -} from '@backstage/plugin-catalog-react'; +import { Entity } from '@backstage/catalog-model'; import { rootDocsRouteRef } from '../../../routes'; import { EmptyState, @@ -34,8 +30,8 @@ import { } from '@backstage/core-components'; import { actionFactories } from './actions'; import { columnFactories, defaultColumns } from './columns'; -import { toLowerMaybe } from '../../../helpers'; import { DocsTableRow } from './types'; +import { entitiesToDocsMapper } from './helpers'; /** * Props for {@link DocsTable}. @@ -63,26 +59,11 @@ export const DocsTable = (props: DocsTableProps) => { const config = useApi(configApiRef); if (!entities) return null; - const documents = entities.map(entity => { - const ownedByRelations = getEntityRelations(entity, RELATION_OWNED_BY); - return { - entity, - resolved: { - docsUrl: getRouteToReaderPageFor({ - namespace: toLowerMaybe( - entity.metadata.namespace ?? 'default', - config, - ), - kind: toLowerMaybe(entity.kind, config), - name: toLowerMaybe(entity.metadata.name, config), - }), - ownedByRelations, - ownedByRelationsTitle: ownedByRelations - .map(r => humanizeEntityRef(r, { defaultKind: 'group' })) - .join(', '), - }, - }; - }); + const documents = entitiesToDocsMapper( + entities, + getRouteToReaderPageFor, + config, + ); const defaultActions: TableProps['actions'] = [ actionFactories.createCopyDocsUrlAction(copyToClipboard), diff --git a/plugins/techdocs/src/home/components/Tables/EntityListDocsTable.tsx b/plugins/techdocs/src/home/components/Tables/EntityListDocsTable.tsx index 959644ca19..08702d2fde 100644 --- a/plugins/techdocs/src/home/components/Tables/EntityListDocsTable.tsx +++ b/plugins/techdocs/src/home/components/Tables/EntityListDocsTable.tsx @@ -25,10 +25,7 @@ import { WarningPanel, } from '@backstage/core-components'; import { configApiRef, useApi, useRouteRef } from '@backstage/core-plugin-api'; -import { RELATION_OWNED_BY } from '@backstage/catalog-model'; import { - getEntityRelations, - humanizeEntityRef, useEntityList, useStarredEntities, } from '@backstage/plugin-catalog-react'; @@ -38,8 +35,8 @@ import { CursorPaginatedDocsTable } from './CursorPaginatedDocsTable'; import { actionFactories } from './actions'; import { columnFactories, defaultColumns } from './columns'; import { DocsTableRow } from './types'; -import { toLowerMaybe } from '../../../helpers'; import { rootDocsRouteRef } from '../../../routes'; +import { entitiesToDocsMapper } from './helpers'; /** * Props for {@link EntityListDocsTable}. @@ -76,26 +73,11 @@ export const EntityListDocsTable = (props: EntityListDocsTableProps) => { ), ]; - const documents = entities.map(entity => { - const ownedByRelations = getEntityRelations(entity, RELATION_OWNED_BY); - return { - entity, - resolved: { - docsUrl: getRouteToReaderPageFor({ - namespace: toLowerMaybe( - entity.metadata.namespace ?? 'default', - config, - ), - kind: toLowerMaybe(entity.kind, config), - name: toLowerMaybe(entity.metadata.name, config), - }), - ownedByRelations, - ownedByRelationsTitle: ownedByRelations - .map(r => humanizeEntityRef(r, { defaultKind: 'group' })) - .join(', '), - }, - }; - }); + const documents = entitiesToDocsMapper( + entities, + getRouteToReaderPageFor, + config, + ); if (paginationMode === 'cursor') { return ( diff --git a/plugins/techdocs/src/home/components/Tables/helpers.ts b/plugins/techdocs/src/home/components/Tables/helpers.ts new file mode 100644 index 0000000000..4c6383c35e --- /dev/null +++ b/plugins/techdocs/src/home/components/Tables/helpers.ts @@ -0,0 +1,56 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { RELATION_OWNED_BY, Entity } from '@backstage/catalog-model'; +import { + getEntityRelations, + humanizeEntityRef, +} from '@backstage/plugin-catalog-react'; +import { toLowerMaybe } from '../../../helpers'; +import { ConfigApi, RouteFunc } from '@backstage/core-plugin-api'; + +type getRouteFunc = RouteFunc<{ + namespace: string; + kind: string; + name: string; +}>; + +export function entitiesToDocsMapper( + entities: Entity[], + getRouteToReaderPageFor: getRouteFunc, + config: ConfigApi, +) { + return entities.map(entity => { + const ownedByRelations = getEntityRelations(entity, RELATION_OWNED_BY); + return { + entity, + resolved: { + docsUrl: getRouteToReaderPageFor({ + namespace: toLowerMaybe( + entity.metadata.namespace ?? 'default', + config, + ), + kind: toLowerMaybe(entity.kind, config), + name: toLowerMaybe(entity.metadata.name, config), + }), + ownedByRelations, + ownedByRelationsTitle: ownedByRelations + .map(r => humanizeEntityRef(r, { defaultKind: 'group' })) + .join(', '), + }, + }; + }); +}