feat(ui): add notes state to fields

This commit is contained in:
psychedelicious
2025-01-17 16:31:08 +11:00
parent b44415415a
commit cfb63c1b81
2 changed files with 23 additions and 6 deletions

View File

@@ -95,23 +95,30 @@ type FieldValueAction<T extends FieldValue> = PayloadAction<{
value: T;
}>;
const getField = (nodeId: string, fieldName: string, state: NodesState) => {
const nodeIndex = state.nodes.findIndex((n) => n.id === nodeId);
const node = state.nodes?.[nodeIndex];
if (!isInvocationNode(node)) {
return;
}
return node.data?.inputs[fieldName];
};
const fieldValueReducer = <T extends FieldValue>(
state: NodesState,
action: FieldValueAction<T>,
schema: z.ZodTypeAny
) => {
const { nodeId, fieldName, value } = action.payload;
const nodeIndex = state.nodes.findIndex((n) => n.id === nodeId);
const node = state.nodes?.[nodeIndex];
if (!isInvocationNode(node)) {
const field = getField(nodeId, fieldName, state);
if (!field) {
return;
}
const input = node.data?.inputs[fieldName];
const result = schema.safeParse(value);
if (!input || nodeIndex < 0 || !result.success) {
if (!result.success) {
return;
}
input.value = result.data;
field.value = result.data;
};
export const nodesSlice = createSlice({
@@ -409,6 +416,14 @@ export const nodesSlice = createSlice({
fieldStringGeneratorValueChanged: (state, action: FieldValueAction<StringGeneratorFieldValue>) => {
fieldValueReducer(state, action, zStringGeneratorFieldValue);
},
fieldNotesChanged: (state, action: PayloadAction<{ nodeId: string; fieldName: string; val?: string }>) => {
const { nodeId, fieldName, val } = action.payload;
const field = getField(nodeId, fieldName, state);
if (!field) {
return;
}
field.notes = val;
},
notesNodeValueChanged: (state, action: PayloadAction<{ nodeId: string; value: string }>) => {
const { nodeId, value } = action.payload;
const nodeIndex = state.nodes.findIndex((n) => n.id === nodeId);
@@ -476,6 +491,7 @@ export const {
fieldFloatGeneratorValueChanged,
fieldIntegerGeneratorValueChanged,
fieldStringGeneratorValueChanged,
fieldNotesChanged,
nodeEditorReset,
nodeIsIntermediateChanged,
nodeIsOpenChanged,

View File

@@ -38,6 +38,7 @@ const zFieldUIComponent = z.enum(['none', 'textarea', 'slider']);
const zFieldInputInstanceBase = z.object({
name: z.string().trim().min(1),
label: z.string().nullish(),
notes: z.string().nullish(),
});
const zFieldTemplateBase = z.object({
name: z.string().min(1),