|
| 1 | +import { Loader2, Users } from "lucide-react"; |
| 2 | +import { GithubBrandIcon } from "@/components/brand-icon"; |
| 3 | +import { CachedAvatar } from "@/components/cached-avatar"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { |
| 6 | + Dialog, |
| 7 | + DialogContent, |
| 8 | + DialogDescription, |
| 9 | + DialogFooter, |
| 10 | + DialogHeader, |
| 11 | + DialogTitle, |
| 12 | +} from "@/components/ui/dialog"; |
| 13 | +import { initialsFor } from "@/lib/initials"; |
| 14 | +import type { ParsedInvite } from "@/lib/team-mode"; |
| 15 | +import { useInviteAccept } from "./use-invite-accept"; |
| 16 | +import { useTeamIdentity } from "./use-team-identity"; |
| 17 | + |
| 18 | +/** |
| 19 | + * Headline invite-accept UX. Shown when the app is opened with a team invite |
| 20 | + * (`?invite=<token>`). Confirms the GitHub identity we'll register, then |
| 21 | + * redeems the token — on success the hook persists the config, flips team |
| 22 | + * mode on, and reloads. |
| 23 | + */ |
| 24 | +export function InviteAcceptDialog({ |
| 25 | + invite, |
| 26 | + onDismiss, |
| 27 | +}: { |
| 28 | + invite: ParsedInvite; |
| 29 | + onDismiss: () => void; |
| 30 | +}) { |
| 31 | + const { identity, isLoading } = useTeamIdentity(); |
| 32 | + const { status, errorMessage, accept } = useInviteAccept(); |
| 33 | + const accepting = status === "accepting"; |
| 34 | + const displayName = identity?.displayName?.trim() || identity?.login || ""; |
| 35 | + |
| 36 | + return ( |
| 37 | + <Dialog |
| 38 | + open |
| 39 | + onOpenChange={(next) => { |
| 40 | + // Don't let an outside-click cancel mid-accept (the reload is |
| 41 | + // imminent); otherwise dismissing just closes the prompt. |
| 42 | + if (!next && !accepting) onDismiss(); |
| 43 | + }} |
| 44 | + > |
| 45 | + <DialogContent className="max-w-[420px]"> |
| 46 | + <DialogHeader> |
| 47 | + <div className="mb-1 flex size-10 items-center justify-center rounded-full bg-accent"> |
| 48 | + <Users className="size-5 text-foreground" strokeWidth={2} /> |
| 49 | + </div> |
| 50 | + <DialogTitle>Join the team workspace</DialogTitle> |
| 51 | + <DialogDescription> |
| 52 | + You've been invited to a shared Helmor cloud workspace at{" "} |
| 53 | + <span className="font-medium text-foreground">{invite.url}</span>. |
| 54 | + </DialogDescription> |
| 55 | + </DialogHeader> |
| 56 | + |
| 57 | + {isLoading ? ( |
| 58 | + <div className="flex items-center justify-center gap-2 py-6 text-small text-muted-foreground"> |
| 59 | + <Loader2 className="size-3.5 animate-spin" /> |
| 60 | + Detecting your GitHub account… |
| 61 | + </div> |
| 62 | + ) : identity ? ( |
| 63 | + <div className="flex items-center gap-3 rounded-lg border border-border/50 bg-muted/30 p-3"> |
| 64 | + <div className="relative shrink-0"> |
| 65 | + <CachedAvatar |
| 66 | + size="lg" |
| 67 | + className="size-9" |
| 68 | + src={identity.avatarUrl} |
| 69 | + alt={identity.login} |
| 70 | + fallback={initialsFor(displayName)} |
| 71 | + fallbackClassName="bg-muted text-ui font-semibold uppercase text-muted-foreground" |
| 72 | + /> |
| 73 | + <span className="absolute -right-1 -bottom-1 flex size-[16px] items-center justify-center rounded-full bg-background ring-2 ring-background"> |
| 74 | + <GithubBrandIcon size={10} /> |
| 75 | + </span> |
| 76 | + </div> |
| 77 | + <div className="min-w-0"> |
| 78 | + <div className="truncate text-ui font-semibold text-foreground"> |
| 79 | + {displayName} |
| 80 | + </div> |
| 81 | + <div className="truncate text-small text-muted-foreground"> |
| 82 | + @{identity.login} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + </div> |
| 86 | + ) : ( |
| 87 | + <div className="rounded-lg border border-destructive/40 bg-destructive/5 p-3 text-small text-muted-foreground"> |
| 88 | + No GitHub account is connected. Sign in to GitHub in Settings → |
| 89 | + Accounts, then reopen the invite link. |
| 90 | + </div> |
| 91 | + )} |
| 92 | + |
| 93 | + {errorMessage ? ( |
| 94 | + <p className="text-small text-destructive">{errorMessage}</p> |
| 95 | + ) : null} |
| 96 | + |
| 97 | + <DialogFooter> |
| 98 | + <Button |
| 99 | + variant="ghost" |
| 100 | + onClick={onDismiss} |
| 101 | + disabled={accepting} |
| 102 | + className="cursor-pointer" |
| 103 | + > |
| 104 | + Not now |
| 105 | + </Button> |
| 106 | + <Button |
| 107 | + onClick={() => { |
| 108 | + if (identity) void accept(invite, identity); |
| 109 | + }} |
| 110 | + disabled={!identity || accepting} |
| 111 | + className="cursor-pointer" |
| 112 | + > |
| 113 | + {accepting ? ( |
| 114 | + <> |
| 115 | + <Loader2 className="size-3.5 animate-spin" /> |
| 116 | + Joining… |
| 117 | + </> |
| 118 | + ) : ( |
| 119 | + "Join team" |
| 120 | + )} |
| 121 | + </Button> |
| 122 | + </DialogFooter> |
| 123 | + </DialogContent> |
| 124 | + </Dialog> |
| 125 | + ); |
| 126 | +} |
0 commit comments