|
| 1 | +import { useState } from "react"; |
| 2 | +import { format } from "date-fns"; |
| 3 | +import { ShieldOff, X } from "lucide-react"; |
| 4 | +import { toast } from "sonner"; |
| 5 | + |
| 6 | +import { trpc } from "~/api/trpc"; |
| 7 | +import { Button } from "~/components/ui/button"; |
| 8 | +import { DateTimePicker } from "~/components/ui/datetime-picker"; |
| 9 | +import { |
| 10 | + Popover, |
| 11 | + PopoverContent, |
| 12 | + PopoverTrigger, |
| 13 | +} from "~/components/ui/popover"; |
| 14 | +import { useWorkspace } from "~/components/WorkspaceProvider"; |
| 15 | + |
| 16 | +function RemoveSkipButton({ skipId }: { skipId: string }) { |
| 17 | + const { workspace } = useWorkspace(); |
| 18 | + const utils = trpc.useUtils(); |
| 19 | + |
| 20 | + const deleteSkip = trpc.policySkips.delete.useMutation({ |
| 21 | + onSuccess: () => { |
| 22 | + toast.success("Skip removal queued for this release target"); |
| 23 | + utils.policySkips.forTarget.invalidate(); |
| 24 | + utils.releaseTargets.evaluations.invalidate(); |
| 25 | + }, |
| 26 | + onError: (error) => |
| 27 | + toast.error("Failed to remove skip", { description: error.message }), |
| 28 | + }); |
| 29 | + |
| 30 | + return ( |
| 31 | + <Button |
| 32 | + variant="outline" |
| 33 | + size="sm" |
| 34 | + className="h-6 shrink-0 gap-1.5 rounded-full px-2.5 text-xs" |
| 35 | + disabled={deleteSkip.isPending} |
| 36 | + onClick={() => deleteSkip.mutate({ workspaceId: workspace.id, skipId })} |
| 37 | + > |
| 38 | + <X className="size-3" /> |
| 39 | + Remove skip |
| 40 | + </Button> |
| 41 | + ); |
| 42 | +} |
| 43 | + |
| 44 | +type TargetSkipsProps = { |
| 45 | + environmentId: string; |
| 46 | + resourceId: string; |
| 47 | + versionId: string; |
| 48 | +}; |
| 49 | + |
| 50 | +export function TargetSkips({ |
| 51 | + environmentId, |
| 52 | + resourceId, |
| 53 | + versionId, |
| 54 | +}: TargetSkipsProps) { |
| 55 | + const { data: skips = [] } = trpc.policySkips.forTarget.useQuery({ |
| 56 | + environmentId, |
| 57 | + resourceId, |
| 58 | + versionId, |
| 59 | + }); |
| 60 | + |
| 61 | + if (skips.length === 0) return null; |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="space-y-2 rounded-lg border border-dashed p-4"> |
| 65 | + <p className="text-sm font-medium">Active skips</p> |
| 66 | + {skips.map((skip) => ( |
| 67 | + <div key={skip.id} className="flex items-center gap-2"> |
| 68 | + <span className="text-sm">{skip.policyName ?? "Unknown policy"}</span> |
| 69 | + <span className="text-xs text-muted-foreground"> |
| 70 | + {skip.reason} |
| 71 | + {skip.expiresAt != null |
| 72 | + ? ` · expires ${format(new Date(skip.expiresAt), "MMM d, yyyy h:mm a")}` |
| 73 | + : ""} |
| 74 | + </span> |
| 75 | + <div className="grow" /> |
| 76 | + <RemoveSkipButton skipId={skip.id} /> |
| 77 | + </div> |
| 78 | + ))} |
| 79 | + </div> |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +type SkipTarget = { |
| 84 | + environmentId: string; |
| 85 | + resourceId: string; |
| 86 | + versionId: string; |
| 87 | + ruleId: string; |
| 88 | +}; |
| 89 | + |
| 90 | +export function SkipRuleButton({ target }: { target: SkipTarget }) { |
| 91 | + const { workspace } = useWorkspace(); |
| 92 | + const utils = trpc.useUtils(); |
| 93 | + const [open, setOpen] = useState(false); |
| 94 | + const [expiresAt, setExpiresAt] = useState<Date | undefined>(undefined); |
| 95 | + |
| 96 | + const createSkip = trpc.policySkips.createForTarget.useMutation({ |
| 97 | + onSuccess: () => { |
| 98 | + toast.success("Skip queued for this release target"); |
| 99 | + utils.policySkips.forTarget.invalidate(); |
| 100 | + utils.releaseTargets.evaluations.invalidate(); |
| 101 | + setOpen(false); |
| 102 | + setExpiresAt(undefined); |
| 103 | + }, |
| 104 | + onError: (error) => |
| 105 | + toast.error("Failed to skip rule", { description: error.message }), |
| 106 | + }); |
| 107 | + |
| 108 | + return ( |
| 109 | + <Popover open={open} onOpenChange={setOpen}> |
| 110 | + <PopoverTrigger asChild> |
| 111 | + <Button |
| 112 | + variant="outline" |
| 113 | + size="sm" |
| 114 | + className="h-6 shrink-0 gap-1.5 rounded-full px-2.5 text-xs" |
| 115 | + > |
| 116 | + <ShieldOff className="size-3" /> |
| 117 | + Skip for this target |
| 118 | + </Button> |
| 119 | + </PopoverTrigger> |
| 120 | + <PopoverContent align="end" className="w-72 space-y-3"> |
| 121 | + <div className="space-y-1"> |
| 122 | + <p className="text-sm font-medium">Skip this rule</p> |
| 123 | + <p className="text-xs text-muted-foreground"> |
| 124 | + Bypass this policy rule for this release target only. |
| 125 | + </p> |
| 126 | + </div> |
| 127 | + <div className="space-y-1"> |
| 128 | + <span className="text-xs font-medium">Expires at (optional)</span> |
| 129 | + <DateTimePicker |
| 130 | + value={expiresAt} |
| 131 | + onChange={setExpiresAt} |
| 132 | + placeholder="" |
| 133 | + /> |
| 134 | + </div> |
| 135 | + <Button |
| 136 | + size="sm" |
| 137 | + className="w-full" |
| 138 | + disabled={createSkip.isPending} |
| 139 | + onClick={() => |
| 140 | + createSkip.mutate({ |
| 141 | + workspaceId: workspace.id, |
| 142 | + environmentId: target.environmentId, |
| 143 | + resourceId: target.resourceId, |
| 144 | + versionId: target.versionId, |
| 145 | + ruleId: target.ruleId, |
| 146 | + expiresAt, |
| 147 | + }) |
| 148 | + } |
| 149 | + > |
| 150 | + Add skip |
| 151 | + </Button> |
| 152 | + </PopoverContent> |
| 153 | + </Popover> |
| 154 | + ); |
| 155 | +} |
0 commit comments