-
Notifications
You must be signed in to change notification settings - Fork 11
fix: approvals should be scoped to an environment #593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
245 changes: 198 additions & 47 deletions
245
.../src/app/[workspaceSlug]/(app)/(deploy)/_components/deployment-version/ApprovalDialog.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -1,87 +1,238 @@ | ||
| "use client"; | ||
|
|
||
| import { useState } from "react"; | ||
| import type * as schema from "@ctrlplane/db/schema"; | ||
| import React, { useState } from "react"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { IconLoader2, IconSelector, IconX } from "@tabler/icons-react"; | ||
|
|
||
| import * as SCHEMA from "@ctrlplane/db/schema"; | ||
| import { Badge } from "@ctrlplane/ui/badge"; | ||
| import { Button } from "@ctrlplane/ui/button"; | ||
| import { | ||
| Command, | ||
| CommandInput, | ||
| CommandItem, | ||
| CommandList, | ||
| } from "@ctrlplane/ui/command"; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| DialogTrigger, | ||
| } from "@ctrlplane/ui/dialog"; | ||
| import { Label } from "@ctrlplane/ui/label"; | ||
| import { Popover, PopoverContent, PopoverTrigger } from "@ctrlplane/ui/popover"; | ||
| import { Textarea } from "@ctrlplane/ui/textarea"; | ||
|
|
||
| import { api } from "~/trpc/react"; | ||
|
|
||
| export const ApprovalDialog: React.FC<{ | ||
| const EnvironmentCombobox: React.FC<{ | ||
| allEnvironments: schema.Environment[]; | ||
| selectedEnvironmentIds: string[]; | ||
| onSelect: (environmentId: string[]) => void; | ||
| onRemove: (environmentId: string) => void; | ||
| }> = ({ allEnvironments, selectedEnvironmentIds, onSelect, onRemove }) => { | ||
| const unselectedEnvironments = allEnvironments.filter( | ||
| (environment) => !selectedEnvironmentIds.includes(environment.id), | ||
| ); | ||
|
|
||
| const selectedEnvironments = allEnvironments.filter((environment) => | ||
| selectedEnvironmentIds.includes(environment.id), | ||
| ); | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-2"> | ||
| <div className="space-y-0.5"> | ||
| <Label>Environments</Label> | ||
| <p className="text-xs text-muted-foreground"> | ||
| Select the environments to approve the release for. | ||
| </p> | ||
| </div> | ||
|
|
||
| <div className="flex items-center gap-2"> | ||
| {selectedEnvironments.map((environment) => ( | ||
| <Badge | ||
| key={environment.id} | ||
| variant="secondary" | ||
| className="flex max-w-36 items-center gap-1 truncate text-xs" | ||
| > | ||
| {environment.name} | ||
| <Button | ||
| variant="ghost" | ||
| size="icon" | ||
| className="h-3 w-3 focus-visible:ring-0" | ||
| onClick={() => onRemove(environment.id)} | ||
| > | ||
| <IconX className="h-3 w-3" /> | ||
| </Button> | ||
| </Badge> | ||
| ))} | ||
| </div> | ||
|
|
||
| <Popover> | ||
| <PopoverTrigger asChild> | ||
| <Button | ||
| variant="outline" | ||
| className="flex w-40 items-center gap-1" | ||
| size="sm" | ||
| > | ||
| <IconSelector className="h-3 w-3" /> | ||
| Select environments | ||
| </Button> | ||
| </PopoverTrigger> | ||
| <PopoverContent className="p-1" side="bottom" align="start"> | ||
| <Command> | ||
| <CommandInput placeholder="Search environments..." /> | ||
| <CommandList> | ||
| <CommandItem | ||
| value="All" | ||
| onSelect={() => onSelect(allEnvironments.map((e) => e.id))} | ||
| > | ||
| <span className="text-sm">All environments</span> | ||
| </CommandItem> | ||
| {unselectedEnvironments.map((environment) => ( | ||
| <CommandItem | ||
| key={environment.id} | ||
| value={environment.name} | ||
| onSelect={() => onSelect([environment.id])} | ||
| > | ||
| <span className="text-sm">{environment.name}</span> | ||
| </CommandItem> | ||
| ))} | ||
| </CommandList> | ||
| </Command> | ||
| </PopoverContent> | ||
| </Popover> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| const ApprovalDialogControl: React.FC<{ | ||
| versionId: string; | ||
| versionTag: string; | ||
| environments: schema.Environment[]; | ||
| environmentId: string; | ||
| children: React.ReactNode; | ||
| onSubmit?: () => void; | ||
| }> = ({ versionId, versionTag, environmentId, children, onSubmit }) => { | ||
| const [open, setOpen] = useState(false); | ||
| const addRecord = api.deployment.version.addApprovalRecord.useMutation(); | ||
| onSubmit: () => void; | ||
| onCancel: () => void; | ||
| }> = ({ versionId, environments, environmentId, onSubmit, onCancel }) => { | ||
| const [environmentIds, setEnvironmentIds] = useState<string[]>([ | ||
| environmentId, | ||
| ]); | ||
|
|
||
| const router = useRouter(); | ||
|
|
||
| const [reason, setReason] = useState(""); | ||
|
|
||
| const addRecord = api.deployment.version.addApprovalRecord.useMutation(); | ||
| const handleSubmit = (status: SCHEMA.ApprovalStatus) => | ||
| addRecord | ||
| .mutateAsync({ | ||
| deploymentVersionId: versionId, | ||
| environmentId, | ||
| environmentIds, | ||
| status, | ||
| reason, | ||
| }) | ||
| .then(() => setOpen(false)) | ||
| .then(() => onSubmit?.()) | ||
| .then(() => onSubmit()) | ||
| .then(() => router.refresh()); | ||
|
|
||
| const setEnvironmentSelected = (environmentIds: string[]) => | ||
| setEnvironmentIds((prev) => [...prev, ...environmentIds]); | ||
|
|
||
| const setEnvironmentUnselected = (environmentId: string) => | ||
| setEnvironmentIds((prev) => prev.filter((id) => id !== environmentId)); | ||
|
|
||
| return ( | ||
| <div className="space-y-6"> | ||
| <EnvironmentCombobox | ||
| allEnvironments={environments} | ||
| selectedEnvironmentIds={environmentIds} | ||
| onSelect={setEnvironmentSelected} | ||
| onRemove={setEnvironmentUnselected} | ||
| /> | ||
|
|
||
| <div className="space-y-2"> | ||
| <div className="space-y-0.5"> | ||
| <Label>Reason</Label> | ||
| <p className="text-xs text-muted-foreground"> | ||
| Provide a reason for the approval or rejection (optional). | ||
| </p> | ||
| </div> | ||
| <Textarea value={reason} onChange={(e) => setReason(e.target.value)} /> | ||
| </div> | ||
|
|
||
| <DialogFooter className="flex w-full flex-row items-center justify-between sm:justify-between"> | ||
| <Button variant="outline" onClick={onCancel}> | ||
| Cancel | ||
| </Button> | ||
| <div className="flex gap-2"> | ||
| <Button | ||
| variant="outline" | ||
| onClick={() => handleSubmit(SCHEMA.ApprovalStatus.Rejected)} | ||
| disabled={addRecord.isPending} | ||
| > | ||
| Reject | ||
| </Button> | ||
| <Button | ||
| onClick={() => handleSubmit(SCHEMA.ApprovalStatus.Approved)} | ||
| disabled={addRecord.isPending} | ||
| > | ||
| Approve | ||
| </Button> | ||
| </div> | ||
| </DialogFooter> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const ApprovalDialog: React.FC<{ | ||
| versionId: string; | ||
| versionTag: string; | ||
| systemId: string; | ||
| environmentId: string; | ||
| children: React.ReactNode; | ||
| onSubmit?: () => void; | ||
| }> = ({ | ||
| versionId, | ||
| versionTag, | ||
| systemId, | ||
| environmentId, | ||
| children, | ||
| onSubmit, | ||
| }) => { | ||
| const [open, setOpen] = useState(false); | ||
|
|
||
| const { data, isLoading } = api.environment.bySystemId.useQuery(systemId); | ||
|
|
||
| return ( | ||
| <Dialog open={open} onOpenChange={setOpen}> | ||
| <DialogTrigger asChild>{children}</DialogTrigger> | ||
| <DialogContent> | ||
| <DialogHeader> | ||
| <DialogTitle>Approve Release</DialogTitle> | ||
| <DialogDescription> | ||
| Are you sure you want to approve version {versionTag}? | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
|
|
||
| <Textarea | ||
| value={reason} | ||
| onChange={(e) => setReason(e.target.value)} | ||
| placeholder="Reason for approval (optional)" | ||
| /> | ||
|
|
||
| <DialogFooter className="flex w-full flex-row items-center justify-between sm:justify-between"> | ||
| <Button variant="outline" onClick={() => setOpen(false)}> | ||
| Cancel | ||
| </Button> | ||
| <div className="flex gap-2"> | ||
| <Button | ||
| variant="outline" | ||
| onClick={() => handleSubmit(SCHEMA.ApprovalStatus.Rejected)} | ||
| disabled={addRecord.isPending} | ||
| > | ||
| Reject | ||
| </Button> | ||
| <Button | ||
| onClick={() => handleSubmit(SCHEMA.ApprovalStatus.Approved)} | ||
| disabled={addRecord.isPending} | ||
| > | ||
| Approve | ||
| </Button> | ||
| {isLoading && ( | ||
| <DialogContent> | ||
| <div className="flex h-full w-full items-center justify-center"> | ||
| <IconLoader2 className="h-4 w-4 animate-spin" /> | ||
| </div> | ||
| </DialogFooter> | ||
| </DialogContent> | ||
| </DialogContent> | ||
| )} | ||
| {!isLoading && ( | ||
| <DialogContent> | ||
| <DialogHeader> | ||
| <DialogTitle className="max-w-96 truncate"> | ||
| Approve Version {versionTag} | ||
| </DialogTitle> | ||
| </DialogHeader> | ||
|
|
||
| <ApprovalDialogControl | ||
| versionId={versionId} | ||
| environments={data ?? []} | ||
| environmentId={environmentId} | ||
| onSubmit={() => { | ||
| setOpen(false); | ||
| onSubmit?.(); | ||
| }} | ||
| onCancel={() => setOpen(false)} | ||
| /> | ||
| </DialogContent> | ||
| )} | ||
| </Dialog> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Potential issue with environment selection logic.
The
setEnvironmentSelectedfunction appends new IDs without checking for duplicates, whilesetEnvironmentUnselectedproperly filters. This could lead to duplicate environment IDs if the same environment is selected multiple times.📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this probably won't happen and even if it does its a noop since its used in an inArray check in the db
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adityachoudhari26 You're absolutely right! I see the logic now:
unselectedEnvironmentsfilter ensures users can only select from environments not already selected, making natural duplicate selection unlikelyinArrayoperations would treat them as identical values, making it effectively a no-opThanks for the clarification - your approach is sound and the additional complexity of duplicate filtering isn't necessary here.