chore: adding some more useful state to the context
Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import React, { useCallback, useEffect } from 'react';
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import {
|
||||
Content,
|
||||
InfoCard,
|
||||
@@ -28,7 +28,7 @@ import { useTemplateParameterSchema } from '../../hooks/useTemplateParameterSche
|
||||
import { Stepper, type StepperProps } from '../Stepper/Stepper';
|
||||
import {
|
||||
SecretsContextProvider,
|
||||
useTemplateSecrets,
|
||||
useInternalTemplateSecrets,
|
||||
} from '../../../secrets/SecretsContext';
|
||||
import { useFilteredSchemaProperties } from '../../hooks/useFilteredSchemaProperties';
|
||||
import { ReviewStepProps } from '@backstage/plugin-scaffolder-react';
|
||||
@@ -88,11 +88,14 @@ export const Workflow = (workflowProps: WorkflowProps): JSX.Element | null => {
|
||||
const { loading, manifest, error } = useTemplateParameterSchema(templateRef);
|
||||
const sortedManifest = useFilteredSchemaProperties(manifest);
|
||||
const minutesSaved = useTemplateTimeSavedMinutes(templateRef);
|
||||
const { setSecrets } = useTemplateSecrets();
|
||||
const { setSecrets } = useInternalTemplateSecrets();
|
||||
const formDecorators = useFormDecorators();
|
||||
const [formState, setFormState] = useState<Record<string, JsonValue>>({});
|
||||
|
||||
const workflowOnCreate = useCallback(
|
||||
async (formState: Record<string, JsonValue>) => {
|
||||
async (originalFormState: Record<string, JsonValue>) => {
|
||||
setFormState(originalFormState);
|
||||
|
||||
if (manifest?.EXPERIMENTAL_formDecorators && formDecorators?.size) {
|
||||
// for each of the form decorators, go and call the decorator with the context
|
||||
await Promise.all(
|
||||
@@ -106,6 +109,8 @@ export const Workflow = (workflowProps: WorkflowProps): JSX.Element | null => {
|
||||
|
||||
await formDecorator.fn({
|
||||
setSecrets,
|
||||
setFormState,
|
||||
formState,
|
||||
input: decorator.input,
|
||||
});
|
||||
}),
|
||||
@@ -124,6 +129,7 @@ export const Workflow = (workflowProps: WorkflowProps): JSX.Element | null => {
|
||||
manifest?.EXPERIMENTAL_formDecorators,
|
||||
formDecorators,
|
||||
onCreate,
|
||||
formState,
|
||||
analytics,
|
||||
templateName,
|
||||
minutesSaved,
|
||||
|
||||
@@ -14,11 +14,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { AnyApiRef } from '@backstage/core-plugin-api';
|
||||
import { JsonValue } from '@backstage/types';
|
||||
import { Dispatch, SetStateAction } from 'react';
|
||||
import { z } from 'zod';
|
||||
|
||||
export type ScaffolderFormDecoratorContext<TInput> = {
|
||||
input: TInput;
|
||||
setSecrets: (input: Record<string, string>) => void;
|
||||
formState: Record<string, JsonValue>;
|
||||
|
||||
setFormState: Dispatch<SetStateAction<Record<string, JsonValue>>>;
|
||||
setSecrets: Dispatch<SetStateAction<Record<string, string>>>;
|
||||
};
|
||||
|
||||
export type ScaffolderFormDecorator<
|
||||
|
||||
@@ -94,3 +94,15 @@ export const useTemplateSecrets = (): ScaffolderUseTemplateSecrets => {
|
||||
|
||||
return { setSecrets, secrets };
|
||||
};
|
||||
|
||||
export const useInternalTemplateSecrets = () => {
|
||||
const value = useContext(SecretsContext)?.atVersion(1);
|
||||
|
||||
if (!value) {
|
||||
throw new Error(
|
||||
'useTemplateSecrets must be used within a SecretsContextProvider',
|
||||
);
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user