Skip to content

Commit

Permalink
fix: Rename condition stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Nov 13, 2024
1 parent 4cef8d9 commit 5352cdd
Show file tree
Hide file tree
Showing 28 changed files with 189 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TargetCondition } from "@ctrlplane/validators/targets";
import type { ResourceCondition } from "@ctrlplane/validators/targets";
import type { Metadata } from "next";
import Link from "next/link";
import { notFound } from "next/navigation";
Expand All @@ -21,7 +21,7 @@ import {
TooltipProvider,
TooltipTrigger,
} from "@ctrlplane/ui/tooltip";
import { TargetFilterType } from "@ctrlplane/validators/targets";
import { ResourceFilterType } from "@ctrlplane/validators/targets";

import { api } from "~/trpc/server";
import { ProviderActionsDropdown } from "./ProviderActionsDropdown";
Expand All @@ -45,8 +45,8 @@ export default async function TargetProvidersPage({
if (targetProviders.length === 0) return <TargetProvidersGettingStarted />;

const providers = targetProviders.map((provider) => {
const filter: TargetCondition = {
type: TargetFilterType.Provider,
const filter: ResourceCondition = {
type: ResourceFilterType.Provider,
value: provider.id,
operator: "equals",
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import type * as schema from "@ctrlplane/db/schema";
import type { TargetCondition } from "@ctrlplane/validators/targets";
import type { ResourceCondition } from "@ctrlplane/validators/targets";
import React from "react";
import { IconDots, IconFilter, IconLoader2 } from "@tabler/icons-react";
import range from "lodash/range";
Expand Down Expand Up @@ -42,7 +42,7 @@ export const TargetPageContent: React.FC<{
{ placeholderData: (prev) => prev },
);

const onFilterChange = (condition: TargetCondition | undefined) => {
const onFilterChange = (condition: ResourceCondition | undefined) => {
const cond = condition ?? defaultCondition;
if (isEmptyCondition(cond)) setFilter(undefined);
if (!isEmptyCondition(cond)) setFilter(cond);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as SCHEMA from "@ctrlplane/db/schema";
import type { TargetCondition } from "@ctrlplane/validators/targets";
import type { ResourceCondition } from "@ctrlplane/validators/targets";
import { useState } from "react";
import Link from "next/link";
import { useParams } from "next/navigation";
Expand Down Expand Up @@ -37,7 +37,7 @@ import {
import {
defaultCondition,
isComparisonCondition,
targetCondition,
resourceCondition,
} from "@ctrlplane/validators/targets";

import { api } from "~/trpc/react";
Expand All @@ -46,7 +46,7 @@ import { TargetIcon } from "../TargetIcon";

const TargetViewsCombobox: React.FC<{
workspaceId: string;
onChange: (targetCondition: TargetCondition) => void;
onChange: (targetCondition: ResourceCondition) => void;
}> = ({ workspaceId, onChange }) => {
const targetViewsQ = api.target.view.list.useQuery(workspaceId, {
enabled: workspaceId !== "",
Expand Down Expand Up @@ -99,12 +99,12 @@ const TargetViewsCombobox: React.FC<{
};

const filterForm = z.object({
targetFilter: targetCondition.optional(),
targetFilter: resourceCondition.optional(),
});

const getFilter = (
targetFilter: TargetCondition | null,
): TargetCondition | undefined => {
targetFilter: ResourceCondition | null,
): ResourceCondition | undefined => {
if (targetFilter == null) return undefined;
if (!isComparisonCondition(targetFilter))
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { JobTargetCondition } from "@ctrlplane/validators/jobs";
import type { TargetCondition } from "@ctrlplane/validators/targets";
import type { ResourceCondition } from "@ctrlplane/validators/targets";
import { useState } from "react";
import { useParams } from "next/navigation";
import { IconLoader2, IconSelector } from "@tabler/icons-react";
Expand All @@ -21,8 +21,8 @@ import {
FilterType,
} from "@ctrlplane/validators/conditions";
import {
TargetFilterType,
TargetOperator,
ResourceFilterType,
ResourceOperator,
} from "@ctrlplane/validators/targets";

import type { JobConditionRenderProps } from "./job-condition-props";
Expand All @@ -47,9 +47,9 @@ export const JobTargetConditionRender: React.FC<
const workspaceQ = api.workspace.bySlug.useQuery(workspaceSlug);
const workspace = workspaceQ.data;

const searchFilter: TargetCondition = {
type: TargetFilterType.Name,
operator: TargetOperator.Like,
const searchFilter: ResourceCondition = {
type: ResourceFilterType.Name,
operator: ResourceOperator.Like,
value: `%${searchDebounced}%`,
};

Expand All @@ -62,13 +62,13 @@ export const JobTargetConditionRender: React.FC<
system?.environments.map((env) => env.resourceFilter).filter(isPresent) ??
[];

const systemFilter: TargetCondition = {
const systemFilter: ResourceCondition = {
type: FilterType.Comparison,
operator: ComparisonOperator.Or,
conditions: envFilters,
};

const systemTargetsFilter: TargetCondition | undefined =
const systemTargetsFilter: ResourceCondition | undefined =
system != null
? {
type: FilterType.Comparison,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
ComparisonCondition,
TargetCondition,
ResourceCondition,
} from "@ctrlplane/validators/targets";
import {
IconChevronDown,
Expand Down Expand Up @@ -34,8 +34,8 @@ import { ColumnOperator } from "@ctrlplane/validators/conditions";
import {
doesConvertingToComparisonRespectMaxDepth,
isComparisonCondition,
TargetFilterType,
TargetOperator,
ResourceFilterType,
ResourceOperator,
} from "@ctrlplane/validators/targets";

import type { TargetConditionRenderProps } from "./target-condition-props";
Expand All @@ -44,18 +44,21 @@ import { TargetConditionRender } from "./TargetConditionRender";
export const ComparisonConditionRender: React.FC<
TargetConditionRenderProps<ComparisonCondition>
> = ({ condition, onChange, depth = 0, className }) => {
const setOperator = (operator: TargetOperator.And | TargetOperator.Or) =>
const setOperator = (operator: ResourceOperator.And | ResourceOperator.Or) =>
onChange({ ...condition, operator });

const updateCondition = (index: number, changedCondition: TargetCondition) =>
const updateCondition = (
index: number,
changedCondition: ResourceCondition,
) =>
onChange({
...condition,
conditions: condition.conditions.map((c, i) =>
i === index ? changedCondition : c,
),
});

const addCondition = (changedCondition: TargetCondition) =>
const addCondition = (changedCondition: ResourceCondition) =>
onChange({
...condition,
conditions: [...condition.conditions, changedCondition],
Expand All @@ -72,8 +75,8 @@ export const ComparisonConditionRender: React.FC<
if (!cond) return;

const newComparisonCondition: ComparisonCondition = {
type: TargetFilterType.Comparison,
operator: TargetOperator.And,
type: ResourceFilterType.Comparison,
operator: ResourceOperator.And,
conditions: [cond],
};

Expand Down Expand Up @@ -107,8 +110,8 @@ export const ComparisonConditionRender: React.FC<
}

const newNotComparisonCondition: ComparisonCondition = {
type: TargetFilterType.Comparison,
operator: TargetOperator.And,
type: ResourceFilterType.Comparison,
operator: ResourceOperator.And,
not: true,
conditions: [cond],
};
Expand Down Expand Up @@ -165,8 +168,8 @@ export const ComparisonConditionRender: React.FC<
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem value={TargetOperator.And}>And</SelectItem>
<SelectItem value={TargetOperator.Or}>Or</SelectItem>
<SelectItem value={ResourceOperator.And}>And</SelectItem>
<SelectItem value={ResourceOperator.Or}>Or</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
Expand Down Expand Up @@ -261,8 +264,8 @@ export const ComparisonConditionRender: React.FC<
<DropdownMenuItem
onClick={() =>
addCondition({
type: TargetFilterType.Metadata,
operator: TargetOperator.Equals,
type: ResourceFilterType.Metadata,
operator: ResourceOperator.Equals,
key: "",
value: "",
})
Expand All @@ -273,8 +276,8 @@ export const ComparisonConditionRender: React.FC<
<DropdownMenuItem
onClick={() =>
addCondition({
type: TargetFilterType.Kind,
operator: TargetOperator.Equals,
type: ResourceFilterType.Kind,
operator: ResourceOperator.Equals,
value: "",
})
}
Expand All @@ -284,8 +287,8 @@ export const ComparisonConditionRender: React.FC<
<DropdownMenuItem
onClick={() =>
addCondition({
type: TargetFilterType.Name,
operator: TargetOperator.Like,
type: ResourceFilterType.Name,
operator: ResourceOperator.Like,
value: "",
})
}
Expand All @@ -295,7 +298,7 @@ export const ComparisonConditionRender: React.FC<
<DropdownMenuItem
onClick={() =>
addCondition({
type: TargetFilterType.Identifier,
type: ResourceFilterType.Identifier,
operator: ColumnOperator.Like,
value: "",
})
Expand All @@ -306,8 +309,8 @@ export const ComparisonConditionRender: React.FC<
<DropdownMenuItem
onClick={() =>
addCondition({
type: TargetFilterType.Provider,
operator: TargetOperator.Equals,
type: ResourceFilterType.Provider,
operator: ResourceOperator.Equals,
value: "",
})
}
Expand All @@ -318,8 +321,8 @@ export const ComparisonConditionRender: React.FC<
<DropdownMenuItem
onClick={() =>
addCondition({
type: TargetFilterType.Comparison,
operator: TargetOperator.And,
type: ResourceFilterType.Comparison,
operator: ResourceOperator.And,
conditions: [],
not: false,
})
Expand All @@ -332,8 +335,8 @@ export const ComparisonConditionRender: React.FC<
<DropdownMenuItem
onClick={() =>
addCondition({
type: TargetFilterType.Comparison,
operator: TargetOperator.And,
type: ResourceFilterType.Comparison,
operator: ResourceOperator.And,
not: true,
conditions: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
KindCondition,
NameCondition,
ProviderCondition,
TargetCondition,
ResourceCondition,
} from "@ctrlplane/validators/targets";
import React from "react";
import _ from "lodash";
Expand All @@ -24,22 +24,22 @@ import {
isMetadataCondition,
isNameCondition,
isProviderCondition,
TargetOperator,
ResourceOperator,
} from "@ctrlplane/validators/targets";

import { api } from "~/trpc/react";

const operatorVerbs = {
[TargetOperator.And]: "and",
[TargetOperator.Or]: "or",
[TargetOperator.Equals]: "is",
[TargetOperator.Null]: (
[ResourceOperator.And]: "and",
[ResourceOperator.Or]: "or",
[ResourceOperator.Equals]: "is",
[ResourceOperator.Null]: (
<span>
is <span className="text-orange-500">null</span>
</span>
),
[TargetOperator.Regex]: "matches",
[TargetOperator.Like]: "contains",
[ResourceOperator.Regex]: "matches",
[ResourceOperator.Like]: "contains",
};

const ConditionBadge: React.FC<{
Expand Down Expand Up @@ -205,7 +205,7 @@ const StringifiedProviderCondition: React.FC<{
};

const StringifiedTargetCondition: React.FC<{
condition: TargetCondition;
condition: ResourceCondition;
depth?: number;
truncate?: boolean;
tabbed?: boolean;
Expand Down Expand Up @@ -241,7 +241,7 @@ const StringifiedTargetCondition: React.FC<{
};

export const TargetConditionBadge: React.FC<{
condition: TargetCondition;
condition: ResourceCondition;
tabbed?: boolean;
}> = ({ condition, tabbed = false }) => (
<HoverCard>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type * as schema from "@ctrlplane/db/schema";
import type { TargetCondition } from "@ctrlplane/validators/targets";
import type { ResourceCondition } from "@ctrlplane/validators/targets";
import React, { useState } from "react";
import { useRouter } from "next/navigation";

Expand All @@ -26,8 +26,8 @@ import { TargetConditionRender } from "./TargetConditionRender";
import { TargetViewForm, targetViewFormSchema } from "./TargetViewForm";

type TargetConditionDialogProps = {
condition?: TargetCondition;
onChange: (condition: TargetCondition | undefined) => void;
condition?: ResourceCondition;
onChange: (condition: ResourceCondition | undefined) => void;
children: React.ReactNode;
};

Expand Down Expand Up @@ -94,7 +94,7 @@ export const TargetConditionDialog: React.FC<TargetConditionDialogProps> = ({

type CreateTargetViewDialogProps = {
workspaceId: string;
filter?: TargetCondition;
filter?: ResourceCondition;
onSubmit?: (view: schema.ResourceView) => void;
children: React.ReactNode;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TargetCondition } from "@ctrlplane/validators/targets";
import type { ResourceCondition } from "@ctrlplane/validators/targets";
import React from "react";

import {
Expand All @@ -23,7 +23,7 @@ import { TargetMetadataConditionRender } from "./TargetMetadataConditionRender";
* to render this component properly.
*/
export const TargetConditionRender: React.FC<
TargetConditionRenderProps<TargetCondition>
TargetConditionRenderProps<ResourceCondition>
> = ({ condition, onChange, depth = 0, className }) => {
if (isComparisonCondition(condition))
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import { Textarea } from "@ctrlplane/ui/textarea";
import {
defaultCondition,
isValidTargetCondition,
targetCondition,
resourceCondition,
} from "@ctrlplane/validators/targets";

import { TargetConditionRender } from "./TargetConditionRender";

export const targetViewFormSchema = z.object({
name: z.string().min(1),
filter: targetCondition.refine((data) => isValidTargetCondition(data), {
filter: resourceCondition.refine((data) => isValidTargetCondition(data), {
message: "Invalid target condition",
}),
description: z.string().optional(),
Expand Down
Loading

0 comments on commit 5352cdd

Please sign in to comment.