Add useAllKinds & filterKinds() to both plugin-catalog and plugin-catalog-react

We really don't want to expose and export the helper functions, hence
some code duplication is needed (for now).

Signed-off-by: Gustaf Lundh <gustaf.lundh@axis.com>
This commit is contained in:
Gustaf Lundh
2022-12-01 14:15:17 +01:00
parent 8cd7acf865
commit d48165dde8
5 changed files with 85 additions and 7 deletions
@@ -20,7 +20,7 @@ import { Box } from '@material-ui/core';
import React, { useEffect, useMemo, useState } from 'react';
import { EntityKindFilter } from '../../filters';
import { useEntityList } from '../../hooks';
import { filterKinds, useAllKinds } from '../../utils/kindFilterUtils';
import { filterKinds, useAllKinds } from './kindFilterUtils';
function useEntityKindFilter(opts: { initialFilter: string }): {
loading: boolean;
@@ -16,7 +16,7 @@
import { useApi } from '@backstage/core-plugin-api';
import useAsync from 'react-use/lib/useAsync';
import { catalogApiRef } from '../api';
import { catalogApiRef } from '../../api';
/**
* Fetch and return all availible kinds.
-1
View File
@@ -18,4 +18,3 @@ export { getEntityRelations } from './getEntityRelations';
export { getEntitySourceLocation } from './getEntitySourceLocation';
export type { EntitySourceLocation } from './getEntitySourceLocation';
export { isOwnerOf } from './isOwnerOf';
export { useAllKinds, filterKinds } from './kindFilterUtils';
@@ -27,11 +27,8 @@ import {
EntityKindFilter,
useEntityList,
} from '@backstage/plugin-catalog-react';
import {
filterKinds,
useAllKinds,
} from '@backstage/plugin-catalog-react/src/utils';
import pluralize from 'pluralize';
import { filterKinds, useAllKinds } from './kindFilterUtils';
const useStyles = makeStyles((theme: Theme) =>
createStyles({
@@ -0,0 +1,82 @@
/*
* Copyright 2022 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 { useApi } from '@backstage/core-plugin-api';
import { catalogApiRef } from '@backstage/plugin-catalog-react';
import useAsync from 'react-use/lib/useAsync';
/**
* Fetch and return all availible kinds.
*/
export function useAllKinds(): {
loading: boolean;
error?: Error;
allKinds: string[];
} {
const catalogApi = useApi(catalogApiRef);
const {
error,
loading,
value: allKinds,
} = useAsync(async () => {
const items = await catalogApi
.getEntityFacets({ facets: ['kind'] })
.then(response => response.facets.kind?.map(f => f.value).sort() || []);
return items;
}, [catalogApi]);
return { loading, error, allKinds: allKinds ?? [] };
}
/**
* Filter and capitalize accessible kinds.
*/
export function filterKinds(
allKinds: string[],
allowedKinds?: string[],
forcedKinds?: string,
): Record<string, string> {
// Before allKinds is loaded, or when a kind is entered manually in the URL, selectedKind may not
// be present in allKinds. It should still be shown in the dropdown, but may not have the nice
// enforced casing from the catalog-backend. This makes a key/value record for the Select options,
// including selectedKind if it's unknown - but allows the selectedKind to get clobbered by the
// more proper catalog kind if it exists.
let availableKinds = allKinds;
if (allowedKinds) {
availableKinds = availableKinds.filter(k =>
allowedKinds.some(
a => a.toLocaleLowerCase('en-US') === k.toLocaleLowerCase('en-US'),
),
);
}
if (
forcedKinds &&
!allKinds.some(
a =>
a.toLocaleLowerCase('en-US') === forcedKinds.toLocaleLowerCase('en-US'),
)
) {
availableKinds = availableKinds.concat([forcedKinds]);
}
const kindsMap = availableKinds.sort().reduce((acc, kind) => {
acc[kind.toLocaleLowerCase('en-US')] = kind;
return acc;
}, {} as Record<string, string>);
return kindsMap;
}