|
| 1 | +import { useFetcher } from "@remix-run/react"; |
| 2 | +import { useEffect } from "react"; |
| 3 | +import { Button } from "~/components/primitives/Buttons"; |
| 4 | +import { Callout } from "~/components/primitives/Callout"; |
| 5 | +import { ClipboardField } from "~/components/primitives/ClipboardField"; |
| 6 | +import { RelativeDateTime } from "~/components/primitives/DateTime"; |
| 7 | +import { |
| 8 | + Dialog, |
| 9 | + DialogContent, |
| 10 | + DialogDescription, |
| 11 | + DialogFooter, |
| 12 | + DialogHeader, |
| 13 | +} from "~/components/primitives/Dialog"; |
| 14 | +import { FormError } from "~/components/primitives/FormError"; |
| 15 | +import { Hint } from "~/components/primitives/Hint"; |
| 16 | +import { Label } from "~/components/primitives/Label"; |
| 17 | +import { TextArea } from "~/components/primitives/TextArea"; |
| 18 | +import type { |
| 19 | + SupportAccessDialogActionData, |
| 20 | + SupportAccessDialogData, |
| 21 | +} from "~/routes/admin.api.v2.orgs.$organizationId.support-access"; |
| 22 | + |
| 23 | +type SupportAccessRequestDialogProps = { |
| 24 | + org: { id: string; title: string } | null; |
| 25 | + open: boolean; |
| 26 | + onOpenChange: (open: boolean) => void; |
| 27 | +}; |
| 28 | + |
| 29 | +export function SupportAccessRequestDialog({ |
| 30 | + org, |
| 31 | + open, |
| 32 | + onOpenChange, |
| 33 | +}: SupportAccessRequestDialogProps) { |
| 34 | + const loadFetcher = useFetcher<SupportAccessDialogData>(); |
| 35 | + const submitFetcher = useFetcher<SupportAccessDialogActionData>(); |
| 36 | + const load = loadFetcher.load; |
| 37 | + const endpoint = org ? `/admin/api/v2/orgs/${org.id}/support-access` : undefined; |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + if (open && endpoint) { |
| 41 | + load(endpoint); |
| 42 | + } |
| 43 | + }, [load, open, endpoint]); |
| 44 | + |
| 45 | + if (!org) return null; |
| 46 | + |
| 47 | + const result = submitFetcher.data; |
| 48 | + const created = result?.success ? result : undefined; |
| 49 | + const error = result && !result.success ? result.error : undefined; |
| 50 | + const isSubmitting = submitFetcher.state !== "idle"; |
| 51 | + |
| 52 | + if (created) { |
| 53 | + return ( |
| 54 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 55 | + <DialogContent className="sm:max-w-lg"> |
| 56 | + <DialogHeader>Request created - {org.title}</DialogHeader> |
| 57 | + <DialogDescription> |
| 58 | + We don't email the org. Send this link to an Owner or Admin of {org.title} so they can |
| 59 | + approve it. Once approved, click Support Access again. Access lasts 7 days. |
| 60 | + </DialogDescription> |
| 61 | + <ClipboardField value={created.link} variant="secondary/medium" /> |
| 62 | + <DialogFooter className="justify-end"> |
| 63 | + <Button variant="primary/small" onClick={() => onOpenChange(false)}> |
| 64 | + Done |
| 65 | + </Button> |
| 66 | + </DialogFooter> |
| 67 | + </DialogContent> |
| 68 | + </Dialog> |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + const pending = loadFetcher.data?.pending ?? []; |
| 73 | + |
| 74 | + return ( |
| 75 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 76 | + <DialogContent className="sm:max-w-lg"> |
| 77 | + <DialogHeader>Support Access - {org.title}</DialogHeader> |
| 78 | + <DialogDescription> |
| 79 | + This organization requires approval before Trigger.dev staff can access its dashboard. |
| 80 | + </DialogDescription> |
| 81 | + <submitFetcher.Form method="post" action={endpoint} className="flex flex-col gap-4"> |
| 82 | + <Callout variant="warning"> |
| 83 | + No active approval. An Owner or Admin of this org has to approve a request first. |
| 84 | + Approval lasts 7 days and covers any Trigger.dev staff member. |
| 85 | + </Callout> |
| 86 | + <div> |
| 87 | + <Label htmlFor="support-access-reason" required> |
| 88 | + Reason |
| 89 | + </Label> |
| 90 | + <TextArea |
| 91 | + id="support-access-reason" |
| 92 | + name="reason" |
| 93 | + rows={4} |
| 94 | + required |
| 95 | + placeholder="e.g. Investigating stuck runs in production" |
| 96 | + /> |
| 97 | + <Hint>Shown to the org's admins on their Support Access page.</Hint> |
| 98 | + {error && <FormError>{error}</FormError>} |
| 99 | + </div> |
| 100 | + {pending.length > 0 && ( |
| 101 | + <div className="flex flex-col gap-1.5"> |
| 102 | + <Label>Pending requests</Label> |
| 103 | + {pending.map((request) => ( |
| 104 | + <div key={request.id} className="rounded-md bg-background-hover px-3 py-2.5"> |
| 105 | + <div className="truncate text-sm text-text-bright">{request.reason}</div> |
| 106 | + <div className="text-xs text-text-dimmed"> |
| 107 | + {request.requestedBy} · <RelativeDateTime date={new Date(request.createdAt)} /> |
| 108 | + </div> |
| 109 | + </div> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + )} |
| 113 | + <DialogFooter> |
| 114 | + <Button type="button" variant="tertiary/small" onClick={() => onOpenChange(false)}> |
| 115 | + Cancel |
| 116 | + </Button> |
| 117 | + <Button type="submit" variant="primary/small" disabled={isSubmitting}> |
| 118 | + {isSubmitting ? "Creating..." : "Create request"} |
| 119 | + </Button> |
| 120 | + </DialogFooter> |
| 121 | + </submitFetcher.Form> |
| 122 | + </DialogContent> |
| 123 | + </Dialog> |
| 124 | + ); |
| 125 | +} |
0 commit comments