diff --git a/copperc/src/app/u/pipeline/_nodes/constant.tsx b/copperc/src/app/u/pipeline/_nodes/constant.tsx index 0f806133..f094e13c 100644 --- a/copperc/src/app/u/pipeline/_nodes/constant.tsx +++ b/copperc/src/app/u/pipeline/_nodes/constant.tsx @@ -2,8 +2,8 @@ import { NumberInput, Select, TextInput } from "@mantine/core"; import { Node, NodeProps, useReactFlow } from "@xyflow/react"; import { BaseNode } from "./base"; import { NodeDef } from "."; +import { stringUnionToArray } from "@/lib/util"; -const types = ["Text", "Integer", "Float"] as const; type ValueType = | { type: "Text"; @@ -12,8 +12,25 @@ type ValueType = | { type: "Integer"; value: number; + } + | { + type: "Float"; + value: number; + } + | { + type: "Boolean"; + value: boolean; }; +type ConstType = ValueType["type"]; + +export const constTypes = stringUnionToArray()( + "Text", + "Integer", + "Float", + "Boolean", +); + type ConstantNodeType = Node< { value: ValueType; @@ -45,9 +62,9 @@ function ConstantNodeElement({ data, id }: NodeProps) { input = ( { - if (typeof value === "string") { + if (typeof value !== "number") { return; } @@ -62,6 +79,44 @@ function ConstantNodeElement({ data, id }: NodeProps) { allowDecimal={false} /> ); + } else if (data.value.type === "Float") { + input = ( + { + if (typeof value !== "number") { + return; + } + + updateNodeData(id, { + value: { + type: "Float", + value, + }, + }); + }} + value={data.value.value} + allowDecimal={true} + /> + ); + } else if (data.value.type === "Boolean") { + input = ( + { + data={constTypes} + onChange={(v) => { + const value = v as ConstType | null; + if (value === null || value == data.value.type) { return; } @@ -102,6 +159,20 @@ function ConstantNodeElement({ data, id }: NodeProps) { value: 0, }, }); + } else if (value === "Float") { + updateNodeData(id, { + value: { + type: "Float", + value: 0, + }, + }); + } else if (value === "Boolean") { + updateNodeData(id, { + value: { + type: "Boolean", + value: false, + }, + }); } }} value={data.value.type} diff --git a/copperc/src/app/u/pipeline/_nodes/input.tsx b/copperc/src/app/u/pipeline/_nodes/input.tsx index b0d836b2..3db2208d 100644 --- a/copperc/src/app/u/pipeline/_nodes/input.tsx +++ b/copperc/src/app/u/pipeline/_nodes/input.tsx @@ -1,18 +1,11 @@ import { Select } from "@mantine/core"; import { Node, NodeProps, useReactFlow } from "@xyflow/react"; import { BaseNode } from "./base"; -import { dataTypes } from "@/lib/attributes"; +import { DataType, dataTypes } from "@/lib/attributes"; import { NodeDef } from "."; import { components } from "@/lib/api/openapi"; -const types = ["Text", "Integer", "Float"] as const; - -type InputNodeType = Node< - { - type: (typeof types)[number]; - }, - "pipelineinput" ->; +type InputNodeType = Node<{ type: DataType }, "pipelineinput">; function InputNodeElement({ data, id }: NodeProps) { const { deleteElements, getEdges, updateNodeData } = useReactFlow(); @@ -92,8 +85,6 @@ export const InputNode: NodeDef = { return null; } - return { - type: data_type.value as (typeof types)[number], - }; + return { type: data_type.value as DataType }; }, }; diff --git a/copperc/src/lib/attributes/index.ts b/copperc/src/lib/attributes/index.ts index 4bec11ca..021e7322 100644 --- a/copperc/src/lib/attributes/index.ts +++ b/copperc/src/lib/attributes/index.ts @@ -8,18 +8,11 @@ import { _integerAttrType } from "./impls/integer"; import { _hashAttrType } from "./impls/hash"; import { _referenceAttrType } from "./impls/reference"; import { components } from "../api/openapi"; +import { stringUnionToArray } from "../util"; export type DataType = components["schemas"]["AttributeInfo"]["data_type"]["type"]; -// Hack types to enforce `attrTypes` -type NonEmptyArray = [T, ...T[]]; -type MustInclude = [T] extends [U[keyof U]] ? U : never; -function stringUnionToArray() { - return >(...elements: MustInclude) => - elements; -} - export const dataTypes = stringUnionToArray()( "Text", "Integer", diff --git a/copperc/src/lib/util.ts b/copperc/src/lib/util.ts new file mode 100644 index 00000000..6f85de8c --- /dev/null +++ b/copperc/src/lib/util.ts @@ -0,0 +1,13 @@ +// Hack types to enforce `attrTypes` +type NonEmptyArray = [T, ...T[]]; +type MustInclude = [T] extends [U[keyof U]] ? U : never; + +/** + * Hack that allows us to create a type-checked array + * that contains every variant of a string union. + */ +// TODO: uniqueness +export function stringUnionToArray() { + return >(...elements: MustInclude) => + elements; +}