|
| 1 | +// Copyright 2026 Synnax Labs, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License included in the file |
| 4 | +// licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with the Business Source |
| 7 | +// License, use of this software will be governed by the Apache License, Version 2.0, |
| 8 | +// included in the file licenses/APL.txt. |
| 9 | + |
| 10 | +// Code generated by Oracle. DO NOT EDIT. |
| 11 | +import { caseconv, control, record, spatial, zod } from "@synnaxlabs/x"; |
| 12 | +import { produce } from "immer"; |
| 13 | +import { z } from "zod"; |
| 14 | + |
| 15 | +import { |
| 16 | + handleAddNode, |
| 17 | + handleRemoveEdge, |
| 18 | + handleRemoveNode, |
| 19 | + handleSetAuthority, |
| 20 | + handleSetEdge, |
| 21 | + handleSetLegend, |
| 22 | + handleSetNodePosition, |
| 23 | + handleSetProps, |
| 24 | +} from "@/schematic/actions"; |
| 25 | +import { edgeZ, legendZ, nodeZ, type Schematic } from "@/schematic/types.gen"; |
| 26 | + |
| 27 | +/** SetNodePosition moves a node to a new position. */ |
| 28 | +export const setNodePositionPayloadZ = z.object({ |
| 29 | + key: z.string(), |
| 30 | + position: spatial.xyZ, |
| 31 | +}); |
| 32 | + |
| 33 | +export type SetNodePositionPayload = z.infer<typeof setNodePositionPayloadZ>; |
| 34 | + |
| 35 | +/** |
| 36 | + * AddNode appends a node to the schematic. If props is non-empty it is stored |
| 37 | + * under the node's key in the schematic props map. |
| 38 | + */ |
| 39 | +export const addNodePayloadZ = z.object({ |
| 40 | + node: nodeZ, |
| 41 | + props: caseconv.preserveCase(zod.nullToUndefined(record.unknownZ())), |
| 42 | +}); |
| 43 | + |
| 44 | +export type AddNodePayload = z.infer<typeof addNodePayloadZ>; |
| 45 | + |
| 46 | +/** RemoveNode removes a node and any props stored under its key. */ |
| 47 | +export const removeNodePayloadZ = z.object({ |
| 48 | + key: z.string(), |
| 49 | +}); |
| 50 | + |
| 51 | +export type RemoveNodePayload = z.infer<typeof removeNodePayloadZ>; |
| 52 | + |
| 53 | +/** |
| 54 | + * SetEdge inserts the edge if no edge with the same key exists, otherwise |
| 55 | + * replaces the existing edge with the same key. |
| 56 | + */ |
| 57 | +export const setEdgePayloadZ = z.object({ |
| 58 | + edge: edgeZ, |
| 59 | +}); |
| 60 | + |
| 61 | +export type SetEdgePayload = z.infer<typeof setEdgePayloadZ>; |
| 62 | + |
| 63 | +/** RemoveEdge removes the edge with the given key, if present. */ |
| 64 | +export const removeEdgePayloadZ = z.object({ |
| 65 | + key: z.string(), |
| 66 | +}); |
| 67 | + |
| 68 | +export type RemoveEdgePayload = z.infer<typeof removeEdgePayloadZ>; |
| 69 | + |
| 70 | +/** SetProps sets the props entry for the given node or edge key. */ |
| 71 | +export const setPropsPayloadZ = z.object({ |
| 72 | + key: z.string(), |
| 73 | + props: caseconv.preserveCase(record.nullishToEmpty()), |
| 74 | +}); |
| 75 | + |
| 76 | +export type SetPropsPayload = z.infer<typeof setPropsPayloadZ>; |
| 77 | + |
| 78 | +/** SetAuthority sets the control authority level for this schematic. */ |
| 79 | +export const setAuthorityPayloadZ = z.object({ |
| 80 | + value: control.authorityZ, |
| 81 | +}); |
| 82 | + |
| 83 | +export type SetAuthorityPayload = z.infer<typeof setAuthorityPayloadZ>; |
| 84 | + |
| 85 | +/** SetLegend replaces the schematic's control-legend overlay configuration. */ |
| 86 | +export const setLegendPayloadZ = z.object({ |
| 87 | + legend: legendZ, |
| 88 | +}); |
| 89 | + |
| 90 | +export type SetLegendPayload = z.infer<typeof setLegendPayloadZ>; |
| 91 | + |
| 92 | +export const ACTION_TYPES = { |
| 93 | + set_node_position: "set_node_position", |
| 94 | + add_node: "add_node", |
| 95 | + remove_node: "remove_node", |
| 96 | + set_edge: "set_edge", |
| 97 | + remove_edge: "remove_edge", |
| 98 | + set_props: "set_props", |
| 99 | + set_authority: "set_authority", |
| 100 | + set_legend: "set_legend", |
| 101 | +} as const; |
| 102 | + |
| 103 | +export const actionZ = z.discriminatedUnion("type", [ |
| 104 | + z.object({ |
| 105 | + type: z.literal("set_node_position"), |
| 106 | + setNodePosition: setNodePositionPayloadZ, |
| 107 | + }), |
| 108 | + z.object({ type: z.literal("add_node"), addNode: addNodePayloadZ }), |
| 109 | + z.object({ type: z.literal("remove_node"), removeNode: removeNodePayloadZ }), |
| 110 | + z.object({ type: z.literal("set_edge"), setEdge: setEdgePayloadZ }), |
| 111 | + z.object({ type: z.literal("remove_edge"), removeEdge: removeEdgePayloadZ }), |
| 112 | + z.object({ type: z.literal("set_props"), setProps: setPropsPayloadZ }), |
| 113 | + z.object({ type: z.literal("set_authority"), setAuthority: setAuthorityPayloadZ }), |
| 114 | + z.object({ type: z.literal("set_legend"), setLegend: setLegendPayloadZ }), |
| 115 | +]); |
| 116 | + |
| 117 | +export type Action = z.infer<typeof actionZ>; |
| 118 | + |
| 119 | +export const setNodePosition = (payload: SetNodePositionPayload): Action => ({ |
| 120 | + type: "set_node_position", |
| 121 | + setNodePosition: payload, |
| 122 | +}); |
| 123 | + |
| 124 | +export const addNode = (payload: AddNodePayload): Action => ({ |
| 125 | + type: "add_node", |
| 126 | + addNode: payload, |
| 127 | +}); |
| 128 | + |
| 129 | +export const removeNode = (payload: RemoveNodePayload): Action => ({ |
| 130 | + type: "remove_node", |
| 131 | + removeNode: payload, |
| 132 | +}); |
| 133 | + |
| 134 | +export const setEdge = (payload: SetEdgePayload): Action => ({ |
| 135 | + type: "set_edge", |
| 136 | + setEdge: payload, |
| 137 | +}); |
| 138 | + |
| 139 | +export const removeEdge = (payload: RemoveEdgePayload): Action => ({ |
| 140 | + type: "remove_edge", |
| 141 | + removeEdge: payload, |
| 142 | +}); |
| 143 | + |
| 144 | +export const setProps = (payload: SetPropsPayload): Action => ({ |
| 145 | + type: "set_props", |
| 146 | + setProps: payload, |
| 147 | +}); |
| 148 | + |
| 149 | +export const setAuthority = (payload: SetAuthorityPayload): Action => ({ |
| 150 | + type: "set_authority", |
| 151 | + setAuthority: payload, |
| 152 | +}); |
| 153 | + |
| 154 | +export const setLegend = (payload: SetLegendPayload): Action => ({ |
| 155 | + type: "set_legend", |
| 156 | + setLegend: payload, |
| 157 | +}); |
| 158 | + |
| 159 | +export const reduce = (state: Schematic, action: Action): Schematic => { |
| 160 | + switch (action.type) { |
| 161 | + case "set_node_position": |
| 162 | + handleSetNodePosition(state, action.setNodePosition); |
| 163 | + break; |
| 164 | + case "add_node": |
| 165 | + handleAddNode(state, action.addNode); |
| 166 | + break; |
| 167 | + case "remove_node": |
| 168 | + handleRemoveNode(state, action.removeNode); |
| 169 | + break; |
| 170 | + case "set_edge": |
| 171 | + handleSetEdge(state, action.setEdge); |
| 172 | + break; |
| 173 | + case "remove_edge": |
| 174 | + handleRemoveEdge(state, action.removeEdge); |
| 175 | + break; |
| 176 | + case "set_props": |
| 177 | + handleSetProps(state, action.setProps); |
| 178 | + break; |
| 179 | + case "set_authority": |
| 180 | + handleSetAuthority(state, action.setAuthority); |
| 181 | + break; |
| 182 | + case "set_legend": |
| 183 | + handleSetLegend(state, action.setLegend); |
| 184 | + break; |
| 185 | + } |
| 186 | + return state; |
| 187 | +}; |
| 188 | + |
| 189 | +export const reduceAll = (state: Schematic, actions: Action[]): Schematic => |
| 190 | + produce(state, (draft) => actions.forEach((action) => reduce(draft, action))); |
0 commit comments