chore: hooks -> decorators

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-10-14 09:06:16 +02:00
parent aee334c14c
commit 88746203aa
15 changed files with 91 additions and 85 deletions
@@ -35,7 +35,7 @@ import { ReviewStepProps } from '@backstage/plugin-scaffolder-react';
import { useTemplateTimeSavedMinutes } from '../../hooks/useTemplateTimeSaved';
import { JsonValue } from '@backstage/types';
// eslint-disable-next-line @backstage/no-relative-monorepo-imports
import { useFormHooks } from '../../../../../scaffolder/src/alpha/hooks/useFormHooks';
import { useFormDecorators } from '../../../../../scaffolder/src/alpha/hooks/useFormDecorators';
const useStyles = makeStyles({
markdown: {
@@ -89,24 +89,24 @@ export const Workflow = (workflowProps: WorkflowProps): JSX.Element | null => {
const sortedManifest = useFilteredSchemaProperties(manifest);
const minutesSaved = useTemplateTimeSavedMinutes(templateRef);
const { setSecrets } = useTemplateSecrets();
const formHooks = useFormHooks();
const formDecorators = useFormDecorators();
const workflowOnCreate = useCallback(
async (formState: Record<string, JsonValue>) => {
if (manifest?.EXPERIMENTAL_formHooks && formHooks?.size) {
// for each of the form hooks, go and call the hook with the context
if (manifest?.EXPERIMENTAL_formDecorators && formDecorators?.size) {
// for each of the form decorators, go and call the decorator with the context
await Promise.all(
manifest.EXPERIMENTAL_formHooks.map(async hook => {
const formHook = formHooks.get(hook.id);
if (!formHook) {
manifest.EXPERIMENTAL_formDecorators.map(async decorator => {
const formDecorator = formDecorators.get(decorator.id);
if (!formDecorator) {
// eslint-disable-next-line no-console
console.error('Failed to find form hook', hook.id);
console.error('Failed to find form decorator', decorator.id);
return;
}
await formHook.fn({
await formDecorator.fn({
setSecrets,
input: hook.input,
input: decorator.input,
});
}),
);
@@ -121,8 +121,8 @@ export const Workflow = (workflowProps: WorkflowProps): JSX.Element | null => {
});
},
[
manifest?.EXPERIMENTAL_formHooks,
formHooks,
manifest?.EXPERIMENTAL_formDecorators,
formDecorators,
onCreate,
analytics,
templateName,
@@ -13,21 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createScaffolderFormHook } from './createScaffolderFormHook';
import { createScaffolderFormDecorator } from './createScaffolderFormDecorator';
describe('createScaffolderFormHook', () => {
it('should return a hook', () => {
const hook = createScaffolderFormHook({
describe('createScaffolderFormDecorator', () => {
it('should return a decorator', () => {
const decorator = createScaffolderFormDecorator({
id: 'test',
deps: {},
fn: async () => {},
});
expect(hook).toMatchInlineSnapshot();
expect(decorator).toMatchInlineSnapshot();
});
it('should allow passing schema and be typesafe', () => {
const hook = createScaffolderFormHook({
const decorator = createScaffolderFormDecorator({
id: 'test',
deps: {},
schema: {
@@ -46,6 +46,6 @@ describe('createScaffolderFormHook', () => {
},
});
expect(hook).toMatchInlineSnapshot();
expect(decorator).toMatchInlineSnapshot();
});
});
@@ -16,12 +16,12 @@
import { AnyApiRef } from '@backstage/core-plugin-api';
import { z } from 'zod';
export type ScaffolderFormHookContext<TInput> = {
export type ScaffolderFormDecoratorContext<TInput> = {
input: TInput;
setSecrets: (input: Record<string, string>) => void;
};
export type ScaffolderFormHook<
export type ScaffolderFormDecorator<
TInputSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType } = {},
TDeps extends { [key in string]: AnyApiRef } = { [key in string]: AnyApiRef },
TInput extends {} = {
@@ -35,7 +35,7 @@ export type ScaffolderFormHook<
};
deps?: TDeps;
fn: (
ctx: ScaffolderFormHookContext<TInput>,
ctx: ScaffolderFormDecoratorContext<TInput>,
deps: TDeps extends { [key in string]: AnyApiRef }
? { [key in keyof TDeps]: TDeps[key]['T'] }
: never,
@@ -43,11 +43,11 @@ export type ScaffolderFormHook<
};
/**
* Method for creating hooks which can be used to collect
* Method for creating decorators which can be used to collect
* secrets from the user before submitting to the backend.
* @public
*/
export function createScaffolderFormHook<
export function createScaffolderFormDecorator<
TDeps extends { [key in string]: AnyApiRef },
TInputSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType },
TInput extends {} = {
@@ -60,12 +60,12 @@ export function createScaffolderFormHook<
};
deps?: TDeps;
fn: (
ctx: ScaffolderFormHookContext<TInput>,
ctx: ScaffolderFormDecoratorContext<TInput>,
deps: TDeps extends { [key in string]: AnyApiRef }
? { [key in keyof TDeps]: TDeps[key]['T'] }
: never,
) => Promise<void>;
}): ScaffolderFormHook<TInputSchema, TDeps, TInput> {
}): ScaffolderFormDecorator<TInputSchema, TDeps, TInput> {
return {
...options,
version: 'v1',
@@ -13,4 +13,4 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './createScaffolderFormHook';
export * from './createScaffolderFormDecorator';
+1 -1
View File
@@ -33,5 +33,5 @@ export type TemplateParameterSchema = {
description?: string;
schema: JsonObject;
}>;
EXPERIMENTAL_formHooks?: { id: string; input?: JsonObject }[];
EXPERIMENTAL_formDecorators?: { id: string; input?: JsonObject }[];
};