Restore functionality to OwnedEntityPicker

Signed-off-by: Devon Paluso <devon.paluso@gmail.com>
This commit is contained in:
Devon Paluso
2023-09-09 12:38:57 -04:00
parent 7e8f10e199
commit 6e69c11a35
3 changed files with 56 additions and 22 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder': patch
---
Restored functionality to OwnedEntityPicker by converting deprecated ui:options input to catalogFilter
@@ -22,6 +22,7 @@ import useAsync from 'react-use/lib/useAsync';
import { EntityPicker } from '../EntityPicker/EntityPicker';
import { OwnedEntityPickerProps } from './schema';
import { EntityPickerProps } from '../EntityPicker/schema';
export { OwnedEntityPickerSchema } from './schema';
@@ -44,7 +45,6 @@ export const OwnedEntityPicker = (props: OwnedEntityPickerProps) => {
return identity.ownershipEntityRefs;
});
const allowedKinds = uiSchema['ui:options']?.allowedKinds;
if (loading)
return (
<Autocomplete
@@ -65,25 +65,46 @@ export const OwnedEntityPicker = (props: OwnedEntityPickerProps) => {
/>
);
return (
<EntityPicker
{...props}
schema={{ title, description }}
allowedKinds={allowedKinds}
catalogFilter={
allowedKinds
? {
filter: {
kind: allowedKinds,
[`relations.${RELATION_OWNED_BY}`]: identityRefs || [],
},
}
: {
filter: {
[`relations.${RELATION_OWNED_BY}`]: identityRefs || [],
},
}
}
/>
const entityPickerUISchema = buildEntityPickerUISchema(
uiSchema,
identityRefs,
);
return <EntityPicker {...props} uiSchema={entityPickerUISchema} />;
};
/**
* Builds a `uiSchema` for an `EntityPicker` from a parent `OwnedEntityPicker`.
* Migrates deprecated parameters such as `allowedKinds` to `catalogFilter` structure.
*
* @param uiSchema The `uiSchema` of an `OwnedEntityPicker` component.
* @param identityRefs The user and group entities that the user claims ownership through.
* @returns The `uiSchema` for an `EntityPicker` component.
*/
function buildEntityPickerUISchema(
uiSchema: OwnedEntityPickerProps['uiSchema'],
identityRefs: string[] | undefined,
): EntityPickerProps['uiSchema'] {
// Note: This is typed to avoid es-lint rule TS2698
const uiOptions: EntityPickerProps['uiSchema']['ui:options'] =
uiSchema?.['ui:options'] || {};
const allowedKinds = uiOptions.allowedKinds;
const catalogFilter = {
...uiOptions.catalogFilter,
...(allowedKinds
? {
kind: allowedKinds,
[`relations.${RELATION_OWNED_BY}`]: identityRefs || [],
}
: {
[`relations.${RELATION_OWNED_BY}`]: identityRefs || [],
}),
};
return {
'ui:options': {
catalogFilter,
},
};
}
@@ -15,6 +15,7 @@
*/
import { z } from 'zod';
import { makeFieldSchemaFromZod } from '../utils';
import { entityQueryFilterExpressionSchema } from '../EntityPicker/schema';
/**
* @public
@@ -25,7 +26,9 @@ export const OwnedEntityPickerFieldSchema = makeFieldSchemaFromZod(
allowedKinds: z
.array(z.string())
.optional()
.describe('List of kinds of entities to derive options from'),
.describe(
'DEPRECATED: Use `catalogFilter` instead. List of kinds of entities to derive options from',
),
defaultKind: z
.string()
.optional()
@@ -42,6 +45,11 @@ export const OwnedEntityPickerFieldSchema = makeFieldSchemaFromZod(
.describe(
'The default namespace. Options with this namespace will not be prefixed.',
),
catalogFilter: z
.array(entityQueryFilterExpressionSchema)
.or(entityQueryFilterExpressionSchema)
.optional()
.describe('List of key-value filter expression for entities'),
}),
);