Skip to content

Commit

Permalink
Merge pull request #301 from elie222/match-any-all
Browse files Browse the repository at this point in the history
Match all / all conditions toggle
  • Loading branch information
elie222 authored Jan 9, 2025
2 parents b2f991a + 4367d34 commit 3b9cb9b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
47 changes: 44 additions & 3 deletions apps/web/app/(app)/automation/RuleForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { toast } from "sonner";
import TextareaAutosize from "react-textarea-autosize";
import { capitalCase } from "capital-case";
import { usePostHog } from "posthog-js/react";
import { ExternalLinkIcon, PlusIcon } from "lucide-react";
import { ExternalLinkIcon, PlusIcon, FilterIcon } from "lucide-react";
import { Card } from "@/components/Card";
import { Button } from "@/components/ui/button";
import { ErrorMessage, Input, Label } from "@/components/Input";
Expand All @@ -28,7 +28,12 @@ import {
SectionDescription,
TypographyH3,
} from "@/components/Typography";
import { ActionType, CategoryFilterType, RuleType } from "@prisma/client";
import {
ActionType,
CategoryFilterType,
LogicalOperator,
RuleType,
} from "@prisma/client";
import { createRuleAction, updateRuleAction } from "@/utils/actions/rule";
import {
type CreateRuleBody,
Expand Down Expand Up @@ -58,6 +63,13 @@ import { hasVariables } from "@/utils/template";
import { getEmptyCondition } from "@/utils/condition";
import { AlertError } from "@/components/Alert";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";

export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {
const {
Expand Down Expand Up @@ -189,7 +201,36 @@ export function RuleForm({ rule }: { rule: CreateRuleBody & { id?: string } }) {
/>
</div>

<TypographyH3 className="mt-6">Conditions</TypographyH3>
<div className="mt-6 flex items-end justify-between">
<TypographyH3>Conditions</TypographyH3>

<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="sm">
<FilterIcon className="mr-2 h-4 w-4" />
Match{" "}
{watch("conditionalOperator") === LogicalOperator.AND
? "all"
: "any"}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuRadioGroup
value={watch("conditionalOperator")}
onValueChange={(value) =>
setValue("conditionalOperator", value as LogicalOperator)
}
>
<DropdownMenuRadioItem value={LogicalOperator.AND}>
Match all conditions
</DropdownMenuRadioItem>
<DropdownMenuRadioItem value={LogicalOperator.OR}>
Match any condition
</DropdownMenuRadioItem>
</DropdownMenuRadioGroup>
</DropdownMenuContent>
</DropdownMenu>
</div>

{errors.conditions?.root?.message && (
<div className="mt-4">
Expand Down
4 changes: 4 additions & 0 deletions apps/web/utils/actions/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getGmailAccessToken, getGmailClient } from "@/utils/gmail/client";
import { aiFindExampleMatches } from "@/utils/ai/example-matches/find-example-matches";
import { withActionInstrumentation } from "@/utils/actions/middleware";
import { flattenConditions } from "@/utils/condition";
import { LogicalOperator } from "@prisma/client";

export const createRuleAction = withActionInstrumentation(
"createRule",
Expand Down Expand Up @@ -58,6 +59,7 @@ export const createRuleAction = withActionInstrumentation(
}
: undefined,
userId: session.user.id,
conditionalOperator: body.conditionalOperator || LogicalOperator.AND,
// conditions
instructions: conditions.instructions || null,
from: conditions.from || null,
Expand Down Expand Up @@ -124,6 +126,8 @@ export const updateRuleAction = withActionInstrumentation(
automate: body.automate ?? undefined,
runOnThreads: body.runOnThreads ?? undefined,
name: body.name || undefined,
conditionalOperator:
body.conditionalOperator || LogicalOperator.AND,
// conditions
instructions: conditions.instructions || null,
from: conditions.from || null,
Expand Down
9 changes: 8 additions & 1 deletion apps/web/utils/actions/validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { z } from "zod";
import { CategoryFilterType, GroupItemType } from "@prisma/client";
import {
CategoryFilterType,
GroupItemType,
LogicalOperator,
} from "@prisma/client";
import { ActionType, RuleType } from "@prisma/client";

// groups
Expand Down Expand Up @@ -134,6 +138,9 @@ export const createRuleBody = z.object({
message: "You can't have two conditions with the same type.",
},
),
conditionalOperator: z
.enum([LogicalOperator.AND, LogicalOperator.OR])
.default(LogicalOperator.AND),
});
export type CreateRuleBody = z.infer<typeof createRuleBody>;

Expand Down

0 comments on commit 3b9cb9b

Please sign in to comment.