@tari-project/tari-extension-query-builder 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +29 -0
- package/README.md +1 -0
- package/components.json +21 -0
- package/dist/App.d.ts +3 -0
- package/dist/App.d.ts.map +1 -0
- package/dist/codegen/BuilderCodegen.d.ts +20 -0
- package/dist/codegen/BuilderCodegen.d.ts.map +1 -0
- package/dist/codegen/sample.d.ts +3 -0
- package/dist/codegen/sample.d.ts.map +1 -0
- package/dist/components/query-builder/edges/button-edge.d.ts +3 -0
- package/dist/components/query-builder/edges/button-edge.d.ts.map +1 -0
- package/dist/components/query-builder/input/call-input-checkbox.d.ts +11 -0
- package/dist/components/query-builder/input/call-input-checkbox.d.ts.map +1 -0
- package/dist/components/query-builder/input/call-input-select.d.ts +14 -0
- package/dist/components/query-builder/input/call-input-select.d.ts.map +1 -0
- package/dist/components/query-builder/input/call-input-text.d.ts +18 -0
- package/dist/components/query-builder/input/call-input-text.d.ts.map +1 -0
- package/dist/components/query-builder/input/call-input.d.ts +13 -0
- package/dist/components/query-builder/input/call-input.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/call-node.types.d.ts +4 -0
- package/dist/components/query-builder/nodes/call-node.types.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/constants.d.ts +5 -0
- package/dist/components/query-builder/nodes/constants.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/enter-connection.d.ts +3 -0
- package/dist/components/query-builder/nodes/enter-connection.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/exit-connection.d.ts +3 -0
- package/dist/components/query-builder/nodes/exit-connection.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/generic/generic-node.d.ts +5 -0
- package/dist/components/query-builder/nodes/generic/generic-node.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/generic/node-icon.d.ts +8 -0
- package/dist/components/query-builder/nodes/generic/node-icon.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/generic-node.types.d.ts +6 -0
- package/dist/components/query-builder/nodes/generic-node.types.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/input/constants.d.ts +2 -0
- package/dist/components/query-builder/nodes/input/constants.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/input/editable-label.d.ts +9 -0
- package/dist/components/query-builder/nodes/input/editable-label.d.ts.map +1 -0
- package/dist/components/query-builder/nodes/input/input-params-node.d.ts +5 -0
- package/dist/components/query-builder/nodes/input/input-params-node.d.ts.map +1 -0
- package/dist/components/query-builder/query-builder.d.ts +13 -0
- package/dist/components/query-builder/query-builder.d.ts.map +1 -0
- package/dist/components/ui/alert-dialog.d.ts +15 -0
- package/dist/components/ui/alert-dialog.d.ts.map +1 -0
- package/dist/components/ui/badge.d.ts +10 -0
- package/dist/components/ui/badge.d.ts.map +1 -0
- package/dist/components/ui/button.d.ts +11 -0
- package/dist/components/ui/button.d.ts.map +1 -0
- package/dist/components/ui/checkbox.d.ts +5 -0
- package/dist/components/ui/checkbox.d.ts.map +1 -0
- package/dist/components/ui/dropdown-menu.d.ts +26 -0
- package/dist/components/ui/dropdown-menu.d.ts.map +1 -0
- package/dist/components/ui/input.d.ts +4 -0
- package/dist/components/ui/input.d.ts.map +1 -0
- package/dist/components/ui/label.d.ts +5 -0
- package/dist/components/ui/label.d.ts.map +1 -0
- package/dist/components/ui/loading-spinner.d.ts +7 -0
- package/dist/components/ui/loading-spinner.d.ts.map +1 -0
- package/dist/components/ui/popover.d.ts +8 -0
- package/dist/components/ui/popover.d.ts.map +1 -0
- package/dist/components/ui/select.d.ts +16 -0
- package/dist/components/ui/select.d.ts.map +1 -0
- package/dist/components/ui/separator.d.ts +5 -0
- package/dist/components/ui/separator.d.ts.map +1 -0
- package/dist/components/ui/sonner.d.ts +4 -0
- package/dist/components/ui/sonner.d.ts.map +1 -0
- package/dist/components/ui/textarea.d.ts +4 -0
- package/dist/components/ui/textarea.d.ts.map +1 -0
- package/dist/components/ui/tooltip.d.ts +8 -0
- package/dist/components/ui/tooltip.d.ts.map +1 -0
- package/dist/execute/AmbiguousOrderError.d.ts +6 -0
- package/dist/execute/AmbiguousOrderError.d.ts.map +1 -0
- package/dist/execute/CycleDetectedError.d.ts +4 -0
- package/dist/execute/CycleDetectedError.d.ts.map +1 -0
- package/dist/execute/ExecutionPlanner.d.ts +30 -0
- package/dist/execute/ExecutionPlanner.d.ts.map +1 -0
- package/dist/execute/ExecutionPlanner.spec.d.ts +2 -0
- package/dist/execute/ExecutionPlanner.spec.d.ts.map +1 -0
- package/dist/execute/MissingDataError.d.ts +5 -0
- package/dist/execute/MissingDataError.d.ts.map +1 -0
- package/dist/execute/types.d.ts +65 -0
- package/dist/execute/types.d.ts.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/lib/get-next-available.d.ts +2 -0
- package/dist/lib/get-next-available.d.ts.map +1 -0
- package/dist/lib/utils.d.ts +3 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/query-builder/tari-type.d.ts +29 -0
- package/dist/query-builder/tari-type.d.ts.map +1 -0
- package/dist/query-builder/template-reader.d.ts +13 -0
- package/dist/query-builder/template-reader.d.ts.map +1 -0
- package/dist/store/persistence/handlers.d.ts +12 -0
- package/dist/store/persistence/handlers.d.ts.map +1 -0
- package/dist/store/persistence/types.d.ts +18 -0
- package/dist/store/persistence/types.d.ts.map +1 -0
- package/dist/store/persistence/v1_0.d.ts +5 -0
- package/dist/store/persistence/v1_0.d.ts.map +1 -0
- package/dist/store/store.d.ts +4 -0
- package/dist/store/store.d.ts.map +1 -0
- package/dist/store/types.d.ts +97 -0
- package/dist/store/types.d.ts.map +1 -0
- package/dist/tari-extension-query-builder.css +1 -0
- package/dist/tari-extension-query-builder.es.js +183368 -0
- package/dist/tari-extension-query-builder.umd.js +620 -0
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -0
- package/eslint.config.js +34 -0
- package/index.html +12 -0
- package/moon.yml +43 -0
- package/package.json +84 -0
- package/src/App.tsx +114 -0
- package/src/assets/react.svg +1 -0
- package/src/codegen/BuilderCodegen.ts +516 -0
- package/src/codegen/sample.ts +58 -0
- package/src/components/query-builder/edges/button-edge.tsx +50 -0
- package/src/components/query-builder/input/call-input-checkbox.tsx +54 -0
- package/src/components/query-builder/input/call-input-select.tsx +87 -0
- package/src/components/query-builder/input/call-input-text.tsx +98 -0
- package/src/components/query-builder/input/call-input.tsx +51 -0
- package/src/components/query-builder/nodes/call-node.types.ts +3 -0
- package/src/components/query-builder/nodes/constants.ts +4 -0
- package/src/components/query-builder/nodes/enter-connection.tsx +16 -0
- package/src/components/query-builder/nodes/exit-connection.tsx +16 -0
- package/src/components/query-builder/nodes/generic/generic-node.tsx +252 -0
- package/src/components/query-builder/nodes/generic/node-icon.tsx +37 -0
- package/src/components/query-builder/nodes/generic-node.types.ts +5 -0
- package/src/components/query-builder/nodes/input/constants.ts +1 -0
- package/src/components/query-builder/nodes/input/editable-label.tsx +142 -0
- package/src/components/query-builder/nodes/input/input-params-node.tsx +190 -0
- package/src/components/query-builder/query-builder.tsx +577 -0
- package/src/components/ui/alert-dialog.tsx +111 -0
- package/src/components/ui/badge.tsx +37 -0
- package/src/components/ui/button.tsx +50 -0
- package/src/components/ui/checkbox.tsx +27 -0
- package/src/components/ui/dropdown-menu.tsx +217 -0
- package/src/components/ui/input.tsx +21 -0
- package/src/components/ui/label.tsx +19 -0
- package/src/components/ui/loading-spinner.tsx +46 -0
- package/src/components/ui/popover.tsx +40 -0
- package/src/components/ui/select.tsx +158 -0
- package/src/components/ui/separator.tsx +26 -0
- package/src/components/ui/sonner.tsx +23 -0
- package/src/components/ui/textarea.tsx +18 -0
- package/src/components/ui/tooltip.tsx +46 -0
- package/src/execute/AmbiguousOrderError.ts +13 -0
- package/src/execute/CycleDetectedError.ts +7 -0
- package/src/execute/ExecutionPlanner.spec.ts +174 -0
- package/src/execute/ExecutionPlanner.ts +445 -0
- package/src/execute/MissingDataError.ts +10 -0
- package/src/execute/types.ts +87 -0
- package/src/index.css +124 -0
- package/src/index.ts +5 -0
- package/src/lib/get-next-available.ts +12 -0
- package/src/lib/utils.ts +6 -0
- package/src/main.tsx +13 -0
- package/src/query-builder/tari-type.ts +185 -0
- package/src/query-builder/template-reader.ts +69 -0
- package/src/root.css +4 -0
- package/src/store/persistence/handlers.ts +16 -0
- package/src/store/persistence/types.ts +14 -0
- package/src/store/persistence/v1_0.ts +35 -0
- package/src/store/store.ts +396 -0
- package/src/store/types.ts +115 -0
- package/src/stories/data/tari-swap-pool.json +317 -0
- package/src/stories/data/wallet-functions.json +580 -0
- package/src/types.ts +7 -0
- package/src/vite-env.d.ts +1 -0
- package/src/xy-theme.css +144 -0
- package/tsconfig.app.json +31 -0
- package/tsconfig.json +15 -0
- package/tsconfig.lib.json +13 -0
- package/tsconfig.node.json +24 -0
- package/vite.config.ts +35 -0
package/src/main.tsx
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { StrictMode } from "react";
|
|
2
|
+
import { createRoot } from "react-dom/client";
|
|
3
|
+
import App from "./App.tsx";
|
|
4
|
+
|
|
5
|
+
const rootElement = document.getElementById("root");
|
|
6
|
+
if (!rootElement) {
|
|
7
|
+
throw new Error("Root element not found");
|
|
8
|
+
}
|
|
9
|
+
createRoot(rootElement).render(
|
|
10
|
+
<StrictMode>
|
|
11
|
+
<App />
|
|
12
|
+
</StrictMode>,
|
|
13
|
+
);
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { Type } from "@tari-project/typescript-bindings";
|
|
2
|
+
import { HTMLInputTypeAttribute } from "react";
|
|
3
|
+
import { SafeParseReturnType, z, ZodFirstPartySchemaTypes } from "zod";
|
|
4
|
+
|
|
5
|
+
export enum InputControlType {
|
|
6
|
+
TextBoxInput = "TextBoxInput",
|
|
7
|
+
CheckBoxInput = "CheckBoxInput",
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export class TariType {
|
|
11
|
+
public props: TypeProps;
|
|
12
|
+
|
|
13
|
+
constructor(public type: Type) {
|
|
14
|
+
this.props = getTypeProps(type);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public getInputControlType(): InputControlType {
|
|
18
|
+
return this.type === "Bool" ? InputControlType.CheckBoxInput : InputControlType.TextBoxInput;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public get prompt(): string {
|
|
22
|
+
return getPrompt(this.type);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public get inputType(): HTMLInputTypeAttribute {
|
|
26
|
+
return this.props.htmlType;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public get min(): bigint | undefined {
|
|
30
|
+
return this.props.minValue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public get max(): bigint | undefined {
|
|
34
|
+
return this.props.maxValue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public validate(data: string): SafeParseReturnType<unknown, unknown> {
|
|
38
|
+
return this.props.validator.safeParse(data);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public isVoid(): boolean {
|
|
42
|
+
return this.type === "Unit";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public isTuple(): boolean {
|
|
46
|
+
return typeof this.type === "object" && "Tuple" in this.type;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public getTupleDetails(): [TariType?, TariType?] {
|
|
50
|
+
if (typeof this.type === "object" && "Tuple" in this.type && this.type.Tuple.length === 2) {
|
|
51
|
+
return [new TariType(this.type.Tuple[0]), new TariType(this.type.Tuple[1])];
|
|
52
|
+
}
|
|
53
|
+
return [undefined, undefined];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function getSignedMin(bits: number): bigint {
|
|
58
|
+
return -(2n ** BigInt(bits - 1));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getSignedMax(bits: number): bigint {
|
|
62
|
+
return 2n ** BigInt(bits - 1) - 1n;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function getUnsignedMax(bits: number): bigint {
|
|
66
|
+
return 2n ** BigInt(bits) - 1n;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getPrompt(type: Type): string {
|
|
70
|
+
if (typeof type === "string") {
|
|
71
|
+
return type;
|
|
72
|
+
} else {
|
|
73
|
+
if ("Vec" in type) {
|
|
74
|
+
const innerType = getPrompt(type.Vec);
|
|
75
|
+
return `Vec<${innerType}>`;
|
|
76
|
+
} else if ("Tuple" in type) {
|
|
77
|
+
const args = type.Tuple.map(getPrompt);
|
|
78
|
+
return `Tuple<${args.join(", ")}>`;
|
|
79
|
+
} else if ("Other" in type) {
|
|
80
|
+
return type.Other.name;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return "Invalid";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface TypeProps {
|
|
87
|
+
htmlType: HTMLInputTypeAttribute;
|
|
88
|
+
minValue?: bigint;
|
|
89
|
+
maxValue?: bigint;
|
|
90
|
+
validator: ZodFirstPartySchemaTypes;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function getTypeProps(type: Type): TypeProps {
|
|
94
|
+
let htmlType: HTMLInputTypeAttribute = "number";
|
|
95
|
+
let minValue: bigint | undefined = undefined;
|
|
96
|
+
let maxValue: bigint | undefined = undefined;
|
|
97
|
+
let validator: ZodFirstPartySchemaTypes = z.any();
|
|
98
|
+
|
|
99
|
+
if (typeof type === "string") {
|
|
100
|
+
switch (type) {
|
|
101
|
+
case "Bool":
|
|
102
|
+
validator = z.boolean();
|
|
103
|
+
break;
|
|
104
|
+
case "I8": {
|
|
105
|
+
minValue = getSignedMin(8);
|
|
106
|
+
maxValue = getSignedMax(8);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case "I16": {
|
|
110
|
+
minValue = getSignedMin(16);
|
|
111
|
+
maxValue = getSignedMax(16);
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "I32": {
|
|
115
|
+
minValue = getSignedMin(32);
|
|
116
|
+
maxValue = getSignedMax(32);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
case "I64": {
|
|
120
|
+
minValue = getSignedMin(64);
|
|
121
|
+
maxValue = getSignedMax(64);
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case "I128": {
|
|
125
|
+
minValue = getSignedMin(128);
|
|
126
|
+
maxValue = getSignedMax(128);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case "U8": {
|
|
130
|
+
minValue = 0n;
|
|
131
|
+
maxValue = getUnsignedMax(8);
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case "U16": {
|
|
135
|
+
minValue = 0n;
|
|
136
|
+
maxValue = getUnsignedMax(16);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case "U32": {
|
|
140
|
+
minValue = 0n;
|
|
141
|
+
maxValue = getUnsignedMax(32);
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case "U64": {
|
|
145
|
+
minValue = 0n;
|
|
146
|
+
maxValue = getUnsignedMax(64);
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case "U128": {
|
|
150
|
+
minValue = 0n;
|
|
151
|
+
maxValue = getUnsignedMax(128);
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
default:
|
|
155
|
+
htmlType = "text";
|
|
156
|
+
}
|
|
157
|
+
} else {
|
|
158
|
+
htmlType = "text";
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (minValue != null && maxValue != null) {
|
|
162
|
+
validator = z
|
|
163
|
+
.string()
|
|
164
|
+
.refine(
|
|
165
|
+
(val) => {
|
|
166
|
+
try {
|
|
167
|
+
BigInt(val);
|
|
168
|
+
return true;
|
|
169
|
+
} catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
{ message: "Invalid bigint string" },
|
|
174
|
+
)
|
|
175
|
+
.transform((val) => BigInt(val))
|
|
176
|
+
.refine((val) => BigInt(val) >= minValue, {
|
|
177
|
+
message: `Value must be greater than or equal to ${minValue.toString()}`,
|
|
178
|
+
})
|
|
179
|
+
.refine((val) => BigInt(val) <= maxValue, {
|
|
180
|
+
message: `Value must be less than or equal to ${maxValue.toString()}`,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return { htmlType, minValue, maxValue, validator };
|
|
185
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { CALL_NODE_RETURN } from "@/components/query-builder/nodes/call-node.types";
|
|
2
|
+
import { GenericNodeType, InputConnectionType, NodeType, QueryBuilderState } from "@/store/types";
|
|
3
|
+
import { TemplateDef } from "@tari-project/typescript-bindings";
|
|
4
|
+
|
|
5
|
+
export const COMPONENT_ADDRESS_NAME = "__component_address___";
|
|
6
|
+
|
|
7
|
+
type GenericNodeDescription = Parameters<QueryBuilderState["addNodeAt"]>[0];
|
|
8
|
+
|
|
9
|
+
export class TemplateReader {
|
|
10
|
+
constructor(
|
|
11
|
+
public templateDef: TemplateDef,
|
|
12
|
+
public templateAddress: string,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
public get templateName(): string {
|
|
16
|
+
return this.templateDef.V1.template_name;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public getGenericNode(functionName: string): GenericNodeDescription | null {
|
|
20
|
+
const fn = this.templateDef.V1.functions.find((f) => f.name === functionName);
|
|
21
|
+
if (!fn) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
const args = fn.arguments;
|
|
25
|
+
if (!args.length) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
const isMethod = args[0].name === "self";
|
|
29
|
+
const inputs = args.map((arg) => {
|
|
30
|
+
if (arg.name === "self") {
|
|
31
|
+
return {
|
|
32
|
+
inputConnectionType: InputConnectionType.ComponentAddress,
|
|
33
|
+
name: COMPONENT_ADDRESS_NAME,
|
|
34
|
+
label: "Component Address",
|
|
35
|
+
type: { Other: { name: "Component" } },
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
inputConnectionType: InputConnectionType.Parameter,
|
|
40
|
+
name: arg.name,
|
|
41
|
+
type: arg.arg_type,
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
type: NodeType.GenericNode,
|
|
47
|
+
data: {
|
|
48
|
+
type: GenericNodeType.CallNode,
|
|
49
|
+
metadata: {
|
|
50
|
+
type: GenericNodeType.CallNode,
|
|
51
|
+
isMethod,
|
|
52
|
+
templateName: this.templateName,
|
|
53
|
+
templateAddress: this.templateAddress,
|
|
54
|
+
fn,
|
|
55
|
+
},
|
|
56
|
+
hasEnterConnection: true,
|
|
57
|
+
hasExitConnection: true,
|
|
58
|
+
icon: isMethod ? "cube" : "home",
|
|
59
|
+
badge: this.templateName,
|
|
60
|
+
title: functionName,
|
|
61
|
+
inputs,
|
|
62
|
+
output: {
|
|
63
|
+
type: fn.output,
|
|
64
|
+
name: CALL_NODE_RETURN,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/root.css
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { QueryBuilderState } from "../types";
|
|
2
|
+
import { AnyPersistedState, ValidationResult } from "./types";
|
|
3
|
+
import { loadStateV1, saveStateV1 } from "./v1_0";
|
|
4
|
+
|
|
5
|
+
interface VersionHandler<T extends AnyPersistedState> {
|
|
6
|
+
version: T["version"];
|
|
7
|
+
save: (state: QueryBuilderState) => T;
|
|
8
|
+
load: (storedState: unknown) => ValidationResult<T>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const versionHandlers: Record<string, VersionHandler<AnyPersistedState>> = {
|
|
12
|
+
"1.0": { version: "1.0", save: saveStateV1, load: loadStateV1 },
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const latestVersion = "1.0";
|
|
16
|
+
export const latestVersionHandler = versionHandlers[latestVersion];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Edge } from "@xyflow/react";
|
|
3
|
+
import { CustomNode } from "../types";
|
|
4
|
+
|
|
5
|
+
export interface PersistedStateV1 {
|
|
6
|
+
version: "1.0";
|
|
7
|
+
nodes: CustomNode[];
|
|
8
|
+
edges: Edge[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type LatestPersistedState = PersistedStateV1;
|
|
12
|
+
export type AnyPersistedState = PersistedStateV1;
|
|
13
|
+
|
|
14
|
+
export type ValidationResult<T> = { success: true; data: T } | { success: false; error: z.ZodError };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { CustomNode, QueryBuilderState } from "../types";
|
|
3
|
+
import { PersistedStateV1, ValidationResult } from "./types";
|
|
4
|
+
import { Edge } from "@xyflow/react";
|
|
5
|
+
|
|
6
|
+
const persistedStateV1Schema = z.object({
|
|
7
|
+
version: z.literal("1.0"),
|
|
8
|
+
nodes: z.unknown(),
|
|
9
|
+
edges: z.unknown(),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const saveStateV1 = (state: QueryBuilderState): PersistedStateV1 => ({
|
|
13
|
+
version: "1.0",
|
|
14
|
+
nodes: state.nodes,
|
|
15
|
+
edges: state.edges,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const loadStateV1 = (storedState: unknown): ValidationResult<PersistedStateV1> => {
|
|
19
|
+
const result = persistedStateV1Schema.safeParse(storedState);
|
|
20
|
+
if (!result.success) {
|
|
21
|
+
return { success: false, error: result.error };
|
|
22
|
+
}
|
|
23
|
+
const { version, nodes, edges } = result.data;
|
|
24
|
+
if (!nodes || !edges) {
|
|
25
|
+
return { success: false, error: new z.ZodError([]) };
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
success: true,
|
|
29
|
+
data: {
|
|
30
|
+
version,
|
|
31
|
+
nodes: nodes as CustomNode[],
|
|
32
|
+
edges: edges as Edge[],
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
};
|