From de75235bec3556c91954b48e2e06d7433f1e070c Mon Sep 17 00:00:00 2001 From: Peter Macdonald Date: Thu, 11 May 2023 09:20:52 +0200 Subject: [PATCH] Small refactor Signed-off-by: Peter Macdonald --- .../GroupsImPartOfPicker.tsx | 78 ++++++++++++++++++ .../index.ts | 4 +- .../schema.ts} | 18 +++++ .../OwnedGroupsPicker/OwnedGroupsPicker.tsx | 81 ------------------- .../fields/OwnedGroupsPicker/schema.ts | 47 ----------- .../scaffolder/src/components/fields/index.ts | 2 +- plugins/scaffolder/src/extensions/default.ts | 12 +-- plugins/scaffolder/src/plugin.tsx | 12 +-- 8 files changed, 111 insertions(+), 143 deletions(-) create mode 100644 plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/GroupsImPartOfPicker.tsx rename plugins/scaffolder/src/components/fields/{OwnedGroupsPicker => GroupsImPartOfPicker}/index.ts (90%) rename plugins/scaffolder/src/components/fields/{OwnedGroupsPicker/OwnedGroupsPicker.test.tsx => GroupsImPartOfPicker/schema.ts} (53%) delete mode 100644 plugins/scaffolder/src/components/fields/OwnedGroupsPicker/OwnedGroupsPicker.tsx delete mode 100644 plugins/scaffolder/src/components/fields/OwnedGroupsPicker/schema.ts diff --git a/plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/GroupsImPartOfPicker.tsx b/plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/GroupsImPartOfPicker.tsx new file mode 100644 index 0000000000..eb3d2e423e --- /dev/null +++ b/plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/GroupsImPartOfPicker.tsx @@ -0,0 +1,78 @@ +/* + * Copyright 2023 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 React from 'react'; +import { identityApiRef, useApi } from '@backstage/core-plugin-api'; +import useAsync from 'react-use/lib/useAsync'; +import { + TextField, + MenuItem, + Select, + FormControl, + InputLabel, +} from '@material-ui/core'; +import { + GroupsImPartOfPickerProps, + GroupsImPartOfPickerSchema, +} from './schema'; + +export { GroupsImPartOfPickerSchema }; + +export const GroupsImPartOfPicker = (props: GroupsImPartOfPickerProps) => { + const { + schema: { title = 'Group', description = 'A group you are part of' }, + required, + } = props; + + const identityApi = useApi(identityApiRef); + const { loading, value: identityRefs } = useAsync(async () => { + const identity = await identityApi.getBackstageIdentity(); + return identity.ownershipEntityRefs; + }); + + const [selectedGroup, setSelectedGroup] = React.useState(''); + + const handleChange = (event: React.ChangeEvent<{ value: unknown }>) => { + setSelectedGroup(event.target.value as string); + }; + + if (loading) { + return ( + + ); + } + + return ( + + {title} + + + ); +}; diff --git a/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/index.ts b/plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/index.ts similarity index 90% rename from plugins/scaffolder/src/components/fields/OwnedGroupsPicker/index.ts rename to plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/index.ts index 673213ae65..960f5de5ef 100644 --- a/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/index.ts +++ b/plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/index.ts @@ -15,6 +15,6 @@ */ export { - OwnedGroupsPickerFieldSchema, - type OwnedGroupsPickerUiOptions, + GroupsImPartOfPickerSchema, + type GroupsImPartOfPickerUiOptions, } from './schema'; diff --git a/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/OwnedGroupsPicker.test.tsx b/plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/schema.ts similarity index 53% rename from plugins/scaffolder/src/components/fields/OwnedGroupsPicker/OwnedGroupsPicker.test.tsx rename to plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/schema.ts index eb2f165350..839aa349a4 100644 --- a/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/OwnedGroupsPicker.test.tsx +++ b/plugins/scaffolder/src/components/fields/GroupsImPartOfPicker/schema.ts @@ -13,3 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import { z } from 'zod'; +import { makeFieldSchemaFromZod } from '../utils'; + +export const GroupsImPartOfPickerFieldSchema = makeFieldSchemaFromZod( + z.string(), + z.object({ + title: z.string().optional(), + description: z.string().optional(), + }), +); + +export type GroupsImPartOfPickerUiOptions = + typeof GroupsImPartOfPickerFieldSchema.uiOptionsType; +export type GroupsImPartOfPickerProps = + typeof GroupsImPartOfPickerFieldSchema.type; +export const GroupsImPartOfPickerSchema = + GroupsImPartOfPickerFieldSchema.schema; diff --git a/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/OwnedGroupsPicker.tsx b/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/OwnedGroupsPicker.tsx deleted file mode 100644 index 5123adff84..0000000000 --- a/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/OwnedGroupsPicker.tsx +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2023 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 React from 'react'; -import { identityApiRef, useApi } from '@backstage/core-plugin-api'; -import useAsync from 'react-use/lib/useAsync'; -import { EntityPicker } from '../EntityPicker/EntityPicker'; -import Autocomplete from '@material-ui/lab/Autocomplete'; -import { OwnedGroupsPickerProps } from './schema'; -import { TextField } from '@material-ui/core'; - -export { OwnedGroupsPickerSchema } from './schema'; - -export const OwnedGroupsPicker = (props: OwnedGroupsPickerProps) => { - const { - schema: { title = 'Group', description = 'A group from the catalog' }, - uiSchema, - required, - } = props; - - const identityApi = useApi(identityApiRef); - const { loading, value: identityRefs } = useAsync(async () => { - const identity = await identityApi.getBackstageIdentity(); - return identity.userEntityRef; - }); - - const catalogFilter = { - kind: 'group', - ['relations.hasMember']: identityRefs || [], - }; - - const groupUiSchema = { - ...uiSchema, - 'ui:options': { - catalogFilter, - defaultKind: 'group', - }, - }; - - if (loading) { - return ( - ( - - )} - options={[]} - /> - ); - } - - return ( - - ); -}; diff --git a/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/schema.ts b/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/schema.ts deleted file mode 100644 index 6f771db5e6..0000000000 --- a/plugins/scaffolder/src/components/fields/OwnedGroupsPicker/schema.ts +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2023 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 { z } from 'zod'; -import { makeFieldSchemaFromZod } from '../utils'; -import { entityQueryFilterExpressionSchema } from '../EntityPicker/schema'; - -export const OwnedGroupsPickerFieldSchema = makeFieldSchemaFromZod( - z.string(), - z.object({ - defaultKind: z - .string() - .optional() - .describe( - 'The default entity kind. Options of this kind will not be prefixed.', - ), - defaultNamespace: z - .union([z.string(), z.literal(false)]) - .optional() - .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'), - }), -); - -export type OwnedGroupsPickerUiOptions = - typeof OwnedGroupsPickerFieldSchema.uiOptionsType; -export type OwnedGroupsPickerProps = typeof OwnedGroupsPickerFieldSchema.type; -export const OwnedGroupsPickerSchema = OwnedGroupsPickerFieldSchema.schema; diff --git a/plugins/scaffolder/src/components/fields/index.ts b/plugins/scaffolder/src/components/fields/index.ts index aeab13bd12..c9f23a00b7 100644 --- a/plugins/scaffolder/src/components/fields/index.ts +++ b/plugins/scaffolder/src/components/fields/index.ts @@ -18,5 +18,5 @@ export * from './OwnerPicker'; export * from './RepoUrlPicker'; export * from './OwnedEntityPicker'; export * from './EntityTagsPicker'; -export * from './OwnedGroupsPicker'; +export * from './GroupsImPartOfPicker'; export { type FieldSchema, makeFieldSchemaFromZod } from './utils'; diff --git a/plugins/scaffolder/src/extensions/default.ts b/plugins/scaffolder/src/extensions/default.ts index a6984e5a49..9c027610a5 100644 --- a/plugins/scaffolder/src/extensions/default.ts +++ b/plugins/scaffolder/src/extensions/default.ts @@ -40,9 +40,9 @@ import { OwnedEntityPickerSchema, } from '../components/fields/OwnedEntityPicker/OwnedEntityPicker'; import { - OwnedGroupsPicker, - OwnedGroupsPickerSchema, -} from '../components/fields/OwnedGroupsPicker/OwnedGroupsPicker'; + GroupsImPartOfPicker, + GroupsImPartOfPickerSchema, +} from '../components/fields/GroupsImPartOfPicker/GroupsImPartOfPicker'; export const DEFAULT_SCAFFOLDER_FIELD_EXTENSIONS = [ { @@ -78,8 +78,8 @@ export const DEFAULT_SCAFFOLDER_FIELD_EXTENSIONS = [ schema: OwnedEntityPickerSchema, }, { - component: OwnedGroupsPicker, - name: 'OwnedGroupsPicker', - schema: OwnedGroupsPickerSchema, + component: GroupsImPartOfPicker, + name: 'GroupsImPartOfPicker', + schema: GroupsImPartOfPickerSchema, }, ]; diff --git a/plugins/scaffolder/src/plugin.tsx b/plugins/scaffolder/src/plugin.tsx index 3b921f91f0..2561e6d63c 100644 --- a/plugins/scaffolder/src/plugin.tsx +++ b/plugins/scaffolder/src/plugin.tsx @@ -65,9 +65,9 @@ import { editRouteRef, } from './routes'; import { - OwnedGroupsPicker, - OwnedGroupsPickerSchema, -} from './components/fields/OwnedGroupsPicker/OwnedGroupsPicker'; + GroupsImPartOfPicker, + GroupsImPartOfPickerSchema, +} from './components/fields/GroupsImPartOfPicker/GroupsImPartOfPicker'; /** * The main plugin export for the scaffolder. @@ -169,9 +169,9 @@ export const OwnerPickerFieldExtension = scaffolderPlugin.provide( */ export const OwnedGroupsFieldExtension = scaffolderPlugin.provide( createScaffolderFieldExtension({ - component: OwnedGroupsPicker, - name: 'OwnedGroupsPicker', - schema: OwnedGroupsPickerSchema, + component: GroupsImPartOfPicker, + name: 'GroupsImPartOfPicker', + schema: GroupsImPartOfPickerSchema, }), );