feat(ui): tidy node-related types

This commit is contained in:
psychedelicious 2023-11-26 23:33:18 +11:00
parent 42370939a8
commit 4309f3bd58
6 changed files with 36 additions and 32 deletions

View File

@ -9,7 +9,7 @@ import {
buildNotesNode, buildNotesNode,
} from '../store/util/buildNodeData'; } from '../store/util/buildNodeData';
import { DRAG_HANDLE_CLASSNAME, NODE_WIDTH } from '../types/constants'; import { DRAG_HANDLE_CLASSNAME, NODE_WIDTH } from '../types/constants';
import { AnyNodeData, InvocationTemplate } from '../types/invocation'; import { AnyNode, InvocationTemplate } from '../types/invocation';
const templatesSelector = createSelector( const templatesSelector = createSelector(
[(state: RootState) => state.nodes], [(state: RootState) => state.nodes],
(nodes) => nodes.nodeTemplates (nodes) => nodes.nodeTemplates
@ -26,7 +26,7 @@ export const useBuildNodeData = () => {
return useCallback( return useCallback(
// string here is "any invocation type" // string here is "any invocation type"
(type: string | 'current_image' | 'notes'): Node<AnyNodeData> => { (type: string | 'current_image' | 'notes'): AnyNode => {
let _x = window.innerWidth / 2; let _x = window.innerWidth / 2;
let _y = window.innerHeight / 2; let _y = window.innerHeight / 2;

View File

@ -28,7 +28,7 @@ import {
appSocketQueueItemStatusChanged, appSocketQueueItemStatusChanged,
} from 'services/events/actions'; } from 'services/events/actions';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { DRAG_HANDLE_CLASSNAME } from '../types/constants'; import { SHARED_NODE_PROPERTIES } from '../types/constants';
import { import {
BoardFieldValue, BoardFieldValue,
BooleanFieldValue, BooleanFieldValue,
@ -50,7 +50,7 @@ import {
VAEModelFieldValue, VAEModelFieldValue,
} from '../types/field'; } from '../types/field';
import { import {
AnyNodeData, AnyNode,
InvocationTemplate, InvocationTemplate,
isInvocationNode, isInvocationNode,
isNotesNode, isNotesNode,
@ -157,7 +157,7 @@ const nodesSlice = createSlice({
} }
state.nodes[nodeIndex] = action.payload.node; state.nodes[nodeIndex] = action.payload.node;
}, },
nodeAdded: (state, action: PayloadAction<Node<AnyNodeData>>) => { nodeAdded: (state, action: PayloadAction<AnyNode>) => {
const node = action.payload; const node = action.payload;
const position = findUnoccupiedPosition( const position = findUnoccupiedPosition(
state.nodes, state.nodes,
@ -520,7 +520,7 @@ const nodesSlice = createSlice({
state.edges = applyEdgeChanges(edgeChanges, state.edges); state.edges = applyEdgeChanges(edgeChanges, state.edges);
} }
}, },
nodesDeleted: (state, action: PayloadAction<Node<AnyNodeData>[]>) => { nodesDeleted: (state, action: PayloadAction<AnyNode[]>) => {
action.payload.forEach((node) => { action.payload.forEach((node) => {
state.workflow.exposedFields = state.workflow.exposedFields.filter( state.workflow.exposedFields = state.workflow.exposedFields.filter(
(f) => f.nodeId !== node.id (f) => f.nodeId !== node.id
@ -731,7 +731,7 @@ const nodesSlice = createSlice({
state.nodes = applyNodeChanges( state.nodes = applyNodeChanges(
nodes.map((node) => ({ nodes.map((node) => ({
item: { ...node, dragHandle: `.${DRAG_HANDLE_CLASSNAME}` }, item: { ...node, ...SHARED_NODE_PROPERTIES },
type: 'add', type: 'add',
})), })),
[] []

View File

@ -1,6 +1,4 @@
import { import {
Edge,
Node,
OnConnectStartParams, OnConnectStartParams,
SelectionMode, SelectionMode,
Viewport, Viewport,
@ -8,16 +6,16 @@ import {
} from 'reactflow'; } from 'reactflow';
import { FieldIdentifier, FieldType } from '../types/field'; import { FieldIdentifier, FieldType } from '../types/field';
import { import {
AnyNodeData, AnyNode,
InvocationEdgeExtra, InvocationNodeEdge,
InvocationTemplate, InvocationTemplate,
NodeExecutionState, NodeExecutionState,
} from '../types/invocation'; } from '../types/invocation';
import { WorkflowV2 } from '../types/workflow'; import { WorkflowV2 } from '../types/workflow';
export type NodesState = { export type NodesState = {
nodes: Node<AnyNodeData>[]; nodes: AnyNode[];
edges: Edge<InvocationEdgeExtra>[]; edges: InvocationNodeEdge[];
nodeTemplates: Record<string, InvocationTemplate>; nodeTemplates: Record<string, InvocationTemplate>;
connectionStartParams: OnConnectStartParams | null; connectionStartParams: OnConnectStartParams | null;
connectionStartFieldType: FieldType | null; connectionStartFieldType: FieldType | null;
@ -37,8 +35,8 @@ export type NodesState = {
isReady: boolean; isReady: boolean;
mouseOverField: FieldIdentifier | null; mouseOverField: FieldIdentifier | null;
mouseOverNode: string | null; mouseOverNode: string | null;
nodesToCopy: Node<AnyNodeData>[]; nodesToCopy: AnyNode[];
edgesToCopy: Edge<InvocationEdgeExtra>[]; edgesToCopy: InvocationNodeEdge[];
isAddNodePopoverOpen: boolean; isAddNodePopoverOpen: boolean;
addNewNodePosition: XYPosition | null; addNewNodePosition: XYPosition | null;
selectionMode: SelectionMode; selectionMode: SelectionMode;

View File

@ -1,4 +1,4 @@
import { DRAG_HANDLE_CLASSNAME } from 'features/nodes/types/constants'; import { SHARED_NODE_PROPERTIES } from 'features/nodes/types/constants';
import { import {
FieldInputInstance, FieldInputInstance,
FieldOutputInstance, FieldOutputInstance,
@ -14,10 +14,6 @@ import { reduce } from 'lodash-es';
import { Node, XYPosition } from 'reactflow'; import { Node, XYPosition } from 'reactflow';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
export const SHARED_NODE_PROPERTIES: Partial<Node> = {
dragHandle: `.${DRAG_HANDLE_CLASSNAME}`,
};
export const buildNotesNode = (position: XYPosition): Node<NotesNodeData> => { export const buildNotesNode = (position: XYPosition): Node<NotesNodeData> => {
const nodeId = uuidv4(); const nodeId = uuidv4();
const node: Node<NotesNodeData> = { const node: Node<NotesNodeData> = {

View File

@ -1,3 +1,5 @@
import { Node } from 'reactflow';
/** /**
* How long to wait before showing a tooltip when hovering a field handle. * How long to wait before showing a tooltip when hovering a field handle.
*/ */
@ -14,6 +16,13 @@ export const NODE_WIDTH = 320;
*/ */
export const DRAG_HANDLE_CLASSNAME = 'node-drag-handle'; export const DRAG_HANDLE_CLASSNAME = 'node-drag-handle';
/**
* reactflow-specifc properties shared between all node types.
*/
export const SHARED_NODE_PROPERTIES: Partial<Node> = {
dragHandle: `.${DRAG_HANDLE_CLASSNAME}`,
};
/** /**
* Helper for getting the kind of a field. * Helper for getting the kind of a field.
*/ */

View File

@ -1,4 +1,4 @@
import { Node } from 'reactflow'; import { Edge, Node } from 'reactflow';
import { z } from 'zod'; import { z } from 'zod';
import { zProgressImage } from './common'; import { zProgressImage } from './common';
import { import {
@ -64,16 +64,16 @@ export type InvocationNodeData = z.infer<typeof zInvocationNodeData>;
export type CurrentImageNodeData = z.infer<typeof zCurrentImageNodeData>; export type CurrentImageNodeData = z.infer<typeof zCurrentImageNodeData>;
export type AnyNodeData = z.infer<typeof zAnyNodeData>; export type AnyNodeData = z.infer<typeof zAnyNodeData>;
export const isInvocationNode = ( export type InvocationNode = Node<InvocationNodeData, 'invocation'>;
node?: Node<AnyNodeData> export type NotesNode = Node<NotesNodeData, 'notes'>;
): node is Node<InvocationNodeData> => export type CurrentImageNode = Node<CurrentImageNodeData, 'current_image'>;
export type AnyNode = Node<AnyNodeData>;
export const isInvocationNode = (node?: AnyNode): node is InvocationNode =>
Boolean(node && node.type === 'invocation'); Boolean(node && node.type === 'invocation');
export const isNotesNode = ( export const isNotesNode = (node?: AnyNode): node is NotesNode =>
node?: Node<AnyNodeData> Boolean(node && node.type === 'notes');
): node is Node<NotesNodeData> => Boolean(node && node.type === 'notes'); export const isCurrentImageNode = (node?: AnyNode): node is CurrentImageNode =>
export const isCurrentImageNode = (
node?: Node<AnyNodeData>
): node is Node<CurrentImageNodeData> =>
Boolean(node && node.type === 'current_image'); Boolean(node && node.type === 'current_image');
export const isInvocationNodeData = ( export const isInvocationNodeData = (
node?: AnyNodeData node?: AnyNodeData
@ -101,8 +101,9 @@ export type NodeStatus = z.infer<typeof zNodeStatus>;
// #endregion // #endregion
// #region Edges // #region Edges
export const zInvocationEdgeExtra = z.object({ export const zInvocationNodeEdgeExtra = z.object({
type: z.union([z.literal('default'), z.literal('collapsed')]), type: z.union([z.literal('default'), z.literal('collapsed')]),
}); });
export type InvocationEdgeExtra = z.infer<typeof zInvocationEdgeExtra>; export type InvocationNodeEdgeExtra = z.infer<typeof zInvocationNodeEdgeExtra>;
export type InvocationNodeEdge = Edge<InvocationNodeEdgeExtra>;
// #endregion // #endregion