-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Filter targets by provider (#90)
- Loading branch information
1 parent
aff3fb1
commit e352ed4
Showing
8 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...bservice/src/app/[workspaceSlug]/_components/target-condition/ProviderConditionRender.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import type { ProviderCondition } from "@ctrlplane/validators/targets"; | ||
import { useState } from "react"; | ||
import { useParams } from "next/navigation"; | ||
import { IconSelector } from "@tabler/icons-react"; | ||
|
||
import { cn } from "@ctrlplane/ui"; | ||
import { Button } from "@ctrlplane/ui/button"; | ||
import { | ||
Command, | ||
CommandGroup, | ||
CommandInput, | ||
CommandItem, | ||
CommandList, | ||
} from "@ctrlplane/ui/command"; | ||
import { Popover, PopoverContent, PopoverTrigger } from "@ctrlplane/ui/popover"; | ||
|
||
import type { TargetConditionRenderProps } from "./target-condition-props"; | ||
import { api } from "~/trpc/react"; | ||
|
||
export const ProviderConditionRender: React.FC< | ||
TargetConditionRenderProps<ProviderCondition> | ||
> = ({ condition, onChange, className }) => { | ||
const [commandOpen, setCommandOpen] = useState(false); | ||
const { workspaceSlug } = useParams<{ workspaceSlug: string }>(); | ||
const workspace = api.workspace.bySlug.useQuery(workspaceSlug); | ||
const providers = api.target.provider.byWorkspaceId.useQuery( | ||
workspace.data?.id ?? "", | ||
{ enabled: workspace.isSuccess && workspace.data != null }, | ||
); | ||
|
||
const setProvider = (provider: string) => | ||
onChange({ ...condition, value: provider }); | ||
|
||
const selectedProvider = providers.data?.find( | ||
(provider) => provider.id === condition.value, | ||
); | ||
|
||
return ( | ||
<div className={cn("flex w-full items-center gap-2", className)}> | ||
<div className="grid w-full grid-cols-12"> | ||
<div className="col-span-2 flex items-center rounded-l-md border bg-transparent px-3 text-sm text-muted-foreground"> | ||
Provider | ||
</div> | ||
<div className="col-span-10"> | ||
<Popover open={commandOpen} onOpenChange={setCommandOpen}> | ||
<PopoverTrigger asChild> | ||
<Button | ||
variant="outline" | ||
role="combobox" | ||
aria-expanded={commandOpen} | ||
className="w-full items-center justify-start gap-2 rounded-l-none rounded-r-md bg-transparent px-2 hover:bg-neutral-800/50" | ||
> | ||
<IconSelector className="h-4 w-4 text-muted-foreground" /> | ||
<span | ||
className={cn( | ||
selectedProvider != null && "text-muted-foreground", | ||
)} | ||
> | ||
{selectedProvider != null | ||
? selectedProvider.name | ||
: "Select provider..."} | ||
</span> | ||
</Button> | ||
</PopoverTrigger> | ||
<PopoverContent align="start" className="w-[462px] p-0"> | ||
<Command> | ||
<CommandInput placeholder="Search provider..." /> | ||
<CommandGroup> | ||
<CommandList> | ||
{providers.data?.length === 0 && ( | ||
<CommandItem disabled>No providers to add</CommandItem> | ||
)} | ||
{providers.data?.map((provider) => ( | ||
<CommandItem | ||
key={provider.id} | ||
value={provider.id} | ||
onSelect={() => { | ||
setProvider(provider.id); | ||
setCommandOpen(false); | ||
}} | ||
> | ||
{provider.name} | ||
</CommandItem> | ||
))} | ||
</CommandList> | ||
</CommandGroup> | ||
</Command> | ||
</PopoverContent> | ||
</Popover> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/validators/src/targets/conditions/provider-condition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from "zod"; | ||
|
||
export const providerCondition = z.object({ | ||
type: z.literal("provider"), | ||
operator: z.literal("equals"), | ||
value: z.string().min(1), | ||
}); | ||
|
||
export type ProviderCondition = z.infer<typeof providerCondition>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters