fix(custom-widgets): remove stale form helpers
This commit is contained in:
1 file changed
-170
@@ -32,176 +32,6 @@ interface CustomWidgetFormProps {
|
|||||||
definitionId?: string;
|
definitionId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const displayConfigBuilders: Record<string, (values: z.infer<typeof formSchema>) => Record<string, unknown>> = {
|
|
||||||
singleValue: (v) => ({
|
|
||||||
type: "singleValue",
|
|
||||||
jsonPath: v.jsonPath,
|
|
||||||
label: v.label,
|
|
||||||
unit: v.unit,
|
|
||||||
valueSize: v.valueSize,
|
|
||||||
labelPosition: v.labelPosition,
|
|
||||||
}),
|
|
||||||
keyValue: (v) => ({
|
|
||||||
type: "keyValue",
|
|
||||||
mappings: v.mappings,
|
|
||||||
layout: v.kvLayout,
|
|
||||||
columns: v.kvColumns,
|
|
||||||
}),
|
|
||||||
table: (v) => ({
|
|
||||||
type: "table",
|
|
||||||
tablePath: v.tablePath,
|
|
||||||
columns: v.columns,
|
|
||||||
striped: v.striped,
|
|
||||||
compact: v.compact,
|
|
||||||
}),
|
|
||||||
statGrid: (v) => ({
|
|
||||||
type: "statGrid",
|
|
||||||
items: v.statGridItems,
|
|
||||||
columns: v.statGridColumns,
|
|
||||||
cardStyle: v.cardStyle,
|
|
||||||
}),
|
|
||||||
progressBars: (v) => ({
|
|
||||||
type: "progressBars",
|
|
||||||
bars: v.progressBars.map((b) => ({
|
|
||||||
...b,
|
|
||||||
maxPath: b.maxPath || undefined,
|
|
||||||
})),
|
|
||||||
showPercentage: v.showPercentage,
|
|
||||||
barSize: v.barSize,
|
|
||||||
}),
|
|
||||||
statusIndicator: (v) => ({
|
|
||||||
type: "statusIndicator",
|
|
||||||
items: v.statusItems.map((item) => ({
|
|
||||||
...item,
|
|
||||||
goodValues: item.goodValues
|
|
||||||
.split(",")
|
|
||||||
.map((s) => s.trim())
|
|
||||||
.filter(Boolean),
|
|
||||||
})),
|
|
||||||
layout: v.statusLayout,
|
|
||||||
dotSize: v.dotSize,
|
|
||||||
}),
|
|
||||||
countGrid: (v) => ({
|
|
||||||
type: "countGrid",
|
|
||||||
items: v.countGridItems,
|
|
||||||
columns: v.countGridColumns,
|
|
||||||
valueSize: v.countValueSize,
|
|
||||||
}),
|
|
||||||
raw: (v) => ({
|
|
||||||
type: "raw",
|
|
||||||
jsonPath: v.rawJsonPath,
|
|
||||||
maxHeight: v.rawMaxHeight,
|
|
||||||
}),
|
|
||||||
actionButton: (v) => ({
|
|
||||||
type: "actionButton",
|
|
||||||
buttonLabel: v.buttonLabel,
|
|
||||||
buttonColor: v.buttonColor,
|
|
||||||
confirmText: v.confirmText || undefined,
|
|
||||||
successMessage: v.successMessage || undefined,
|
|
||||||
}),
|
|
||||||
customJsx: (v) => ({ type: "customJsx", template: v.template }),
|
|
||||||
};
|
|
||||||
|
|
||||||
const serverToFormFieldMap: Record<string, Record<string, string>> = {
|
|
||||||
statGrid: { items: "statGridItems" },
|
|
||||||
countGrid: { items: "countGridItems" },
|
|
||||||
statusIndicator: { items: "statusItems" },
|
|
||||||
progressBars: { bars: "progressBars" },
|
|
||||||
keyValue: { mappings: "mappings" },
|
|
||||||
table: { columns: "columns", tablePath: "tablePath" },
|
|
||||||
singleValue: { jsonPath: "jsonPath", label: "label", unit: "unit" },
|
|
||||||
raw: { jsonPath: "rawJsonPath" },
|
|
||||||
actionButton: { buttonLabel: "buttonLabel" },
|
|
||||||
customJsx: { template: "template" },
|
|
||||||
};
|
|
||||||
|
|
||||||
function extractServerErrors(err: unknown, displayType: string): Record<string, string> {
|
|
||||||
const errors: Record<string, string> = {};
|
|
||||||
const trpcErr = err as {
|
|
||||||
data?: { zodError?: { fieldErrors?: Record<string, string[]> } };
|
|
||||||
message?: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (trpcErr?.data?.zodError?.fieldErrors) {
|
|
||||||
for (const [field, messages] of Object.entries(trpcErr.data.zodError.fieldErrors)) {
|
|
||||||
if (messages?.[0]) {
|
|
||||||
errors[field] = messages[0];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errors;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const issues = JSON.parse(trpcErr?.message ?? "[]") as Array<{
|
|
||||||
path: (string | number)[];
|
|
||||||
message: string;
|
|
||||||
}>;
|
|
||||||
const fieldMap = serverToFormFieldMap[displayType] ?? {};
|
|
||||||
|
|
||||||
for (const issue of issues) {
|
|
||||||
const path = [...issue.path];
|
|
||||||
if (path[0] === "displayConfig") {
|
|
||||||
path.shift();
|
|
||||||
const serverField = String(path[0]);
|
|
||||||
const formField = fieldMap[serverField] ?? serverField;
|
|
||||||
path[0] = formField;
|
|
||||||
}
|
|
||||||
errors[path.join(".")] = issue.message;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// not parseable, ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors;
|
|
||||||
}
|
|
||||||
|
|
||||||
const listItemDefaults = {
|
|
||||||
mapping: { label: "", jsonPath: "$", unit: "" },
|
|
||||||
column: { header: "", jsonPath: "$" },
|
|
||||||
statGridItem: { label: "", jsonPath: "$", unit: "", color: "blue" },
|
|
||||||
progressBar: {
|
|
||||||
label: "",
|
|
||||||
valuePath: "$",
|
|
||||||
maxPath: "",
|
|
||||||
unit: "",
|
|
||||||
color: "blue",
|
|
||||||
},
|
|
||||||
statusItem: { label: "", jsonPath: "$", goodValues: "online,true" },
|
|
||||||
countGridItem: { label: "", jsonPath: "$", unit: "" },
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
function cloneLast<T extends Record<string, unknown>>(arr: T[], fallback: T): T {
|
|
||||||
const last = arr[arr.length - 1];
|
|
||||||
return last ? { ...last } : { ...fallback };
|
|
||||||
}
|
|
||||||
|
|
||||||
const ALL_DISPLAY_TYPES = [
|
|
||||||
"singleValue",
|
|
||||||
"keyValue",
|
|
||||||
"table",
|
|
||||||
"statGrid",
|
|
||||||
"progressBars",
|
|
||||||
"statusIndicator",
|
|
||||||
"countGrid",
|
|
||||||
"raw",
|
|
||||||
"actionButton",
|
|
||||||
"customJsx",
|
|
||||||
] as const;
|
|
||||||
const MANTINE_COLORS = [
|
|
||||||
"blue",
|
|
||||||
"teal",
|
|
||||||
"green",
|
|
||||||
"red",
|
|
||||||
"orange",
|
|
||||||
"yellow",
|
|
||||||
"violet",
|
|
||||||
"pink",
|
|
||||||
"cyan",
|
|
||||||
"grape",
|
|
||||||
"indigo",
|
|
||||||
"lime",
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
export function CustomWidgetForm({ mode, initialValues, definitionId }: CustomWidgetFormProps) {
|
export function CustomWidgetForm({ mode, initialValues, definitionId }: CustomWidgetFormProps) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const globalT = useI18n();
|
const globalT = useI18n();
|
||||||
|
|||||||
Reference in new issue
Block a user