Skip to content

Commit

Permalink
Fix smart category not saving
Browse files Browse the repository at this point in the history
  • Loading branch information
elie222 committed Dec 22, 2024
1 parent 3319d15 commit cc22a83
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 55 deletions.
20 changes: 5 additions & 15 deletions apps/web/app/(app)/automation/RuleForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import type { LabelsResponse } from "@/app/api/google/labels/route";
import { MultiSelectFilter } from "@/components/MultiSelectFilter";
import { useCategories } from "@/hooks/useCategories";
import { hasVariables } from "@/utils/template";
import { getEmptyConditions } from "@/utils/condition";
import { getEmptyCondition } from "@/utils/condition";
import { AlertError } from "@/components/Alert";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";

Expand Down Expand Up @@ -155,12 +155,8 @@ export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {

const conditions = watch("conditions");
const unusedCondition = useMemo(() => {
const usedConditions = conditions?.map(({ type }) => type);
const emptyConditions = getEmptyConditions();
const unusedCondition = emptyConditions.find(
(condition) => !usedConditions?.includes(condition.type),
);
return unusedCondition;
const usedConditions = new Set(conditions?.map(({ type }) => type));
return Object.values(RuleType).find((type) => !usedConditions.has(type));
}, [conditions]);

// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
Expand Down Expand Up @@ -224,9 +220,7 @@ export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {
});
}

const emptyCondition = getEmptyConditions().find(
(condition) => condition.type === selectedType,
);
const emptyCondition = getEmptyCondition(selectedType);
if (emptyCondition) {
setValue(`conditions.${index}`, emptyCondition);
}
Expand Down Expand Up @@ -439,7 +433,7 @@ export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {
type="button"
variant="secondary"
className="w-full"
onClick={() => appendCondition(unusedCondition)}
onClick={() => appendCondition(getEmptyCondition(unusedCondition))}
>
<PlusIcon className="mr-2 h-4 w-4" />
Add Condition
Expand Down Expand Up @@ -477,10 +471,6 @@ export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {
</div>
<div className="space-y-4 sm:col-span-3">
{actionInputs[action.type].fields.map((field) => {
console.log(
"🚀 ~ {actionInputs[action.type].fields.map ~ field:",
field,
);
const isAiGenerated = action[field.name]?.ai;

const value = watch(`actions.${i}.${field.name}.value`);
Expand Down
1 change: 0 additions & 1 deletion apps/web/app/(app)/automation/TestRules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export function TestRulesContent() {
description: result.error,
});
} else {
console.log("🚀 ~ onTest ~ result:", result);
setTestResults((prev) => ({ ...prev, [message.id]: result }));
}
setIsTesting((prev) => ({ ...prev, [message.id]: false }));
Expand Down
56 changes: 17 additions & 39 deletions apps/web/utils/condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,62 +94,40 @@ export function getConditionTypes(
);
}

const aiEmptyCondition = {
type: RuleType.AI,
instructions: "",
};

const groupEmptyCondition = {
type: RuleType.GROUP,
groupId: "",
};

const staticEmptyCondition = {
type: RuleType.STATIC,
from: null,
to: null,
subject: null,
body: null,
};

const categoryEmptyCondition = {
type: RuleType.CATEGORY,
categoryFilterType: null,
categoryFilters: null,
};

export function getEmptyConditions(): ZodCondition[] {
return [
aiEmptyCondition,
groupEmptyCondition,
staticEmptyCondition,
categoryEmptyCondition,
];
}

export function getEmptyCondition(
type: RuleType,
groupId?: string,
category?: string,
): ZodCondition {
switch (type) {
case RuleType.AI:
return aiEmptyCondition;
return {
type: RuleType.AI,
instructions: "",
};
case RuleType.GROUP:
return {
...groupEmptyCondition,
type: RuleType.GROUP,
groupId: groupId || "",
};
case RuleType.STATIC:
return staticEmptyCondition;
return {
type: RuleType.STATIC,
from: null,
to: null,
subject: null,
body: null,
};
case RuleType.CATEGORY:
return {
...categoryEmptyCondition,
categoryFilters: category ? [category] : null,
type: RuleType.CATEGORY,
categoryFilterType: CategoryFilterType.INCLUDE,
categoryFilters: category ? [category] : null,
};
default:
throw new Error(`Invalid condition type: ${type}`);
// biome-ignore lint/correctness/noSwitchDeclarations: intentional exhaustive check
const exhaustiveCheck: never = type;
return exhaustiveCheck;
}
}

Expand Down

1 comment on commit cc22a83

@vercel
Copy link

@vercel vercel bot commented on cc22a83 Dec 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.