The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Improve group move rules UX and workflow condition validation messages.
Kiran Kf43303f27a90
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-additional-settings.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-additional-settings.tsxindex 293cbb8ac3..50657d92c4 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-additional-settings.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-additional-settings.tsx@@ -8,6 +8,7 @@ import useGroup from "@/lib/swr/use-group";import { useGroupMoveRules } from "@/lib/swr/use-group-move-rules";import { GroupProps } from "@/lib/types";import { updateGroupSchema } from "@/lib/zod/schemas/groups";+import { workflowConditionsSchema } from "@/lib/zod/schemas/workflows";import { GroupSettingsRow } from "@/ui/partners/groups/group-settings-row";import { Button, Checkbox, Modal, Switch } from "@dub/ui";import { pluralize } from "@dub/utils";@@ -117,8 +118,15 @@ function GroupAdditionalSettingsForm({const onSubmit = async (data: FormData) => {if (!group) return;if (data.moveRules && data.moveRules.length > 0) {- // TODO- // Add a client-only validation for the move rules+ const parsed = workflowConditionsSchema.safeParse(data.moveRules);++ if (!parsed.success) {+ toast.error(+ parsed.error.issues[0]?.message ??+ "Please complete the group move rule.",+ );+ return;+ }if (groups) {const groupsWithMatchingRules = findGroupsWithMatchingRules({diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-move-rules.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-move-rules.tsxindex 76576da532..027523f593 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-move-rules.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/settings/group-move-rules.tsx@@ -29,7 +29,7 @@ import { Fragment, useMemo } from "react";import { Controller, useFieldArray, useFormContext } from "react-hook-form";// Draft form value shapes (partial ranges allowed while editing)-type RangeValue = { min: number; max?: number };+type RangeValue = { min?: number; max?: number };type ValueType = number | RangeValue | string | string[] | undefined;type MetricAttributeKey = (typeof GROUP_MOVE_METRIC_ATTRIBUTE_KEYS)[number];@@ -48,6 +48,26 @@ const isMultiGroupOperator = (const isPartnerGroupRule = (rule: WorkflowCondition | undefined) =>rule?.attribute === "partnerGroup";+const isMetricRuleComplete = (rule: WorkflowCondition | undefined): boolean => {+ if (!rule?.attribute || isPartnerGroupRule(rule)) {+ return false;+ }++ const min = getMinValue(rule.value as ValueType);+ if (min == null || min === 0) {+ return false;+ }++ if (rule.operator === "between") {+ const max = getMaxValue(rule.value as ValueType);+ if (max == null || max === 0) {+ return false;+ }+ }++ return true;+};+export function GroupMoveRules() {const { plan } = useWorkspace();@@ -95,9 +115,11 @@ export function GroupMoveRules() {const canAddMetricRule =usedMetricAttributes.length < GROUP_MOVE_METRIC_ATTRIBUTE_KEYS.length;- // Source group is only an additional condition after at least one metric rule+ // Source group is only an additional condition after a complete metric ruleconst canAddPartnerGroupCondition =- metricRuleIndexes.length > 0 && !hasPartnerGroupCondition;+ metricRuleIndexes.length > 0 &&+ !hasPartnerGroupCondition &&+ isMetricRuleComplete(metricRuleIndexes[0].rule);const { canUseGroupMoveRule } = getPlanCapabilities(plan);@@ -370,7 +392,7 @@ function MetricGroupRule({</div>{nestedCondition && (- <div className="rounded-xl border border-neutral-200 bg-neutral-100 px-2.5 pb-2.5">+ <div className="-mx-px rounded-xl border-x border-t border-neutral-200 bg-neutral-100 px-2.5 pb-2.5"><div className="pt-2.5">{"onAdd" in nestedCondition ? (<Buttondiff --git a/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts b/apps/web/lib/api/workflows/evaluate-workflow-conditions.tsindex fda2561078..fa26baa9af 100644--- a/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts+++ b/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts@@ -26,6 +26,11 @@ export function evaluateWorkflowConditions({return false;}+ if (condition.value == null) {+ console.error(`Value is required for ${condition.attribute}.`);+ return false;+ }+if (!operator.evaluate(attributeValue, condition.value)) {return false;}diff --git a/apps/web/lib/api/workflows/operator-definitions.ts b/apps/web/lib/api/workflows/operator-definitions.tsindex 5ee8319a0d..33e6db7fd3 100644--- a/apps/web/lib/api/workflows/operator-definitions.ts+++ b/apps/web/lib/api/workflows/operator-definitions.ts@@ -1,6 +1,6 @@export type ConditionValue =| number- | { min: number; max?: number }+ | { min?: number; max?: number }| string| string[];diff --git a/apps/web/lib/api/workflows/validate-workflow-conditions.ts b/apps/web/lib/api/workflows/validate-workflow-conditions.tsindex 38b2d9e420..239354236a 100644--- a/apps/web/lib/api/workflows/validate-workflow-conditions.ts+++ b/apps/web/lib/api/workflows/validate-workflow-conditions.ts@@ -22,7 +22,7 @@ const WORKFLOW_TYPE_ATTRIBUTES = {type WorkflowConditionInput = {attribute: string;operator: string;- value: unknown;+ value?: unknown;};export async function validateWorkflowConditions({diff --git a/apps/web/lib/zod/schemas/workflows.ts b/apps/web/lib/zod/schemas/workflows.tsindex a358c780c2..c84bd6aeb1 100644--- a/apps/web/lib/zod/schemas/workflows.ts+++ b/apps/web/lib/zod/schemas/workflows.ts@@ -2,7 +2,10 @@ import {WORKFLOW_ATTRIBUTE_KEYS,WorkflowAttributeKey,} from "@/lib/api/workflows/attribute-definitions";-import { WORKFLOW_OPERATOR_KEYS } from "@/lib/api/workflows/operator-definitions";+import {+ WORKFLOW_OPERATOR_KEYS,+ WORKFLOW_OPERATORS,+} from "@/lib/api/workflows/operator-definitions";import { WorkflowTrigger } from "@prisma/client";import * as z from "zod/v4";@@ -34,19 +37,54 @@ export enum WORKFLOW_ACTION_TYPES {}// Individual condition-export const workflowConditionSchema = z.object({- attribute: z.enum(WORKFLOW_ATTRIBUTE_KEYS),- operator: z.enum(WORKFLOW_OPERATOR_KEYS).default("gte"),- value: z.union([- z.number(),- z.object({- min: z.number(),- max: z.number(),+export const workflowConditionSchema = z+ .object({+ attribute: z.enum(WORKFLOW_ATTRIBUTE_KEYS, {+ error: "Please select an activity for this rule.",}),- z.string(),- z.array(z.string()).min(1),- ]),-});+ operator: z.enum(WORKFLOW_OPERATOR_KEYS).default("gte"),+ value: z+ .union([+ z.number(),+ z.object({+ min: z.number().optional(),+ max: z.number().optional(),+ }),+ z.string(),+ z.array(z.string()),+ ])+ .optional(),+ })+ .superRefine((data, ctx) => {+ if (data.value == null) {+ ctx.addIssue({+ code: "custom",+ message:+ data.attribute === "partnerGroup"+ ? "Please select a partner group."+ : "Please enter a threshold value for this rule.",+ path: ["value"],+ });+ return;+ }++ const operatorDefinition =+ WORKFLOW_OPERATORS[data.operator as keyof typeof WORKFLOW_OPERATORS];++ if (!operatorDefinition) {+ return;+ }++ try {+ operatorDefinition.validate(data.value as any);+ } catch (error) {+ ctx.addIssue({+ code: "custom",+ message: error instanceof Error ? error.message : "Invalid value.",+ path: ["value"],+ });+ }+ });// Array of conditionsexport const workflowConditionsSchema = z.array(workflowConditionSchema);