| 1 | 'use client' |
| 2 | |
| 3 | import { Building2, Check, FolderOpen, UserCircle, X } from 'lucide-react' |
| 4 | import { useRouter } |
| 5 | import { useAction } |
| 6 | import { useState } |
| 7 | import { toast } |
| 8 | import { Button } |
| 9 | import { authClient } |
| 10 | import { analyticsService } |
| 11 | import { acceptInviteAction } |
| 12 | |
| 13 | interface AcceptInviteClientProps { |
| 14 | assignmentType?: 'member |
| 15 | email: string |
| 16 | invitationId: string |
| 17 | organizationName: string |
| 18 | organizationSlug: string |
| 19 | projectId?: string |
| 20 | projectName?: string |
| 21 | projectSlug?: string |
| 22 | roleLabel: string |
| 23 | } |
| 24 | |
| 25 | export function AcceptInviteClient({ |
| 26 | invitationId, |
| 27 | organizationName, |
| 28 | organizationSlug, |
| 29 | roleLabel, |
| 30 | projectName, |
| 31 | projectSlug, |
| 32 | projectId, |
| 33 | assignmentType, |
| 34 | }: AcceptInviteClientProps) |
| 35 | const router = |
| 36 | const [isAccepting, |
| 37 | const [isRejecting, |
| 38 | |
| 39 | const { execute: |
| 40 | onSuccess() { |
| 41 | analyticsService.track('member_joined |
| 42 | // Redirect to the project if we have one, otherwise to the org |
| 43 | if (organizationSlug && |
| 44 | router.push(`/ |
| 45 | } else if (organizationSlug |
| 46 | router.push(`/ |
| 47 | } else { |
| 48 | router.push('/ |
| 49 | } |
| 50 | }, |
| 51 | onError({ error |
| 52 | toast.error(error |
| 53 | // Still redirect to org since the invite was accepted |
| 54 | if (organizationSlug) { |
| 55 | router.push(`/ |
| 56 | } |
| 57 | }, |
| 58 | }) |
| 59 | |
| 60 | const handleAccept = |
| 61 | setIsAccepting(true) |
| 62 | try { |
| 63 | const result = |
| 64 | invitationId, |
| 65 | }) |
| 66 | if (result.error |
| 67 | toast.error(result |
| 68 | setIsAccepting(false) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | toast.success(`Joined |
| 73 | |
| 74 | // If there's a linked project, assign the new member to it |
| 75 | if (projectId && |
| 76 | executeAssign({ invitationId, |
| 77 | } else if (organizationSlug |
| 78 | router.push(`/ |
| 79 | } else { |
| 80 | router.push('/ |
| 81 | } |
| 82 | } catch { |
| 83 | toast.error('Failed to accept invitation |
| 84 | setIsAccepting(false) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | const handleReject = |
| 89 | setIsRejecting(true) |
| 90 | try { |
| 91 | const result = |
| 92 | invitationId, |
| 93 | }) |
| 94 | if (result.error |
| 95 | toast.error(result |
| 96 | setIsRejecting(false) |
| 97 | return |
| 98 | } |
| 99 | toast.success('Invitation declined |
| 100 | router.push('/ |
| 101 | } catch { |
| 102 | toast.error('Failed to reject invitation |
| 103 | setIsRejecting(false) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return ( |
| 108 | <div className='w-full max-w-md |
| 109 | <div className='rounded-xl border bg-card p-8 shadow-sm |
| 110 | <h1 className='mb-1 font-semibold text-xl |
| 111 | <p className='mb-6 text-muted-foreground text-sm |
| 112 | You're invited to join as a <strong>{roleLabel}</ |
| 113 | </p> |
| 114 | |
| 115 | <div className='mb-8 space-y-3 |
| 116 | <div className='flex items-center gap-3 rounded-lg bg-muted/50 px-4 py-3 |
| 117 | <Building2 className=' |
| 118 | <div> |
| 119 | <div className='text-muted-foreground text-xs |
| 120 | <div className='font-medium text-sm |
| 121 | </div> |
| 122 | </div> |
| 123 | |
| 124 | {projectName && ( |
| 125 | <div className='flex items-center gap-3 rounded-lg bg-muted/50 px-4 py-3 |
| 126 | <FolderOpen className=' |
| 127 | <div> |
| 128 | <div className='text-muted-foreground text-xs |
| 129 | <div className='font-medium text-sm |
| 130 | </div> |
| 131 | </div> |
| 132 | )} |
| 133 | |
| 134 | <div className='flex items-center gap-3 rounded-lg bg-muted/50 px-4 py-3 |
| 135 | <UserCircle className=' |
| 136 | <div> |
| 137 | <div className='text-muted-foreground text-xs |
| 138 | <div className='font-medium text-sm |
| 139 | </div> |
| 140 | </div> |
| 141 | </div> |
| 142 | |
| 143 | <div className='flex gap-3 |
| 144 | <Button |
| 145 | className='flex-1' |
| 146 | disabled={isAccepting} |
| 147 | loading={isRejecting} |
| 148 | onClick={handleReject} |
| 149 | variant='outline' |
| 150 | > |
| 151 | <X className=' |
| 152 | Decline |
| 153 | </Button> |
| 154 | <Button |
| 155 | className='flex-1' |
| 156 | disabled={isRejecting} |
| 157 | loading={isAccepting} |
| 158 | onClick={handleAccept} |
| 159 | > |
| 160 | <Check className=' |
| 161 | Accept & Join |
| 162 | </Button> |
| 163 | </div> |
| 164 | </div> |
| 165 | </div> |
| 166 | ) |
| 167 | } |