This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathAcceptInviteBanner.tsx
More file actions
174 lines (166 loc) · 7.29 KB
/
AcceptInviteBanner.tsx
File metadata and controls
174 lines (166 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { Button, ButtonLink, H1, Text } from '@sourcegraph/wildcard'
import { CodyProRoutes } from '../codyProRoutes'
import { CodyAlert } from '../components/CodyAlert'
import { useAcceptInvite, useCancelInvite } from '../management/api/react-query/invites'
import { useUserCodySubscription } from '../subscription/useUserCodySubscription'
import { useInviteParams } from './useInviteParams'
import { UserInviteStatus, useInviteState } from './useInviteState'
export const AcceptInviteBanner: React.FC = () => {
const { inviteParams, clearInviteParams } = useInviteParams()
if (!inviteParams) {
return null
}
return (
<AcceptInviteBannerContent
teamId={inviteParams.teamId}
inviteId={inviteParams.inviteId}
clearInviteParams={clearInviteParams}
/>
)
}
const AcceptInviteBannerContent: React.FC<{ teamId: string; inviteId: string; clearInviteParams: () => void }> = ({
teamId,
inviteId,
clearInviteParams,
}) => {
const { refetch } = useUserCodySubscription()
const inviteState = useInviteState(teamId, inviteId)
const acceptInviteMutation = useAcceptInvite()
const cancelInviteMutation = useCancelInvite()
if (inviteState.status === 'loading') {
return null
}
if (
inviteState.status === 'error' ||
inviteState.initialInviteStatus !== 'sent' ||
inviteState.initialUserStatus === UserInviteStatus.Error
) {
return (
<CodyAlert variant="error">
<H1 as="p" className="mb-2">
Issue with invite
</H1>
<Text className="mb-0">The invitation is no longer valid. Contact your team admin.</Text>
</CodyAlert>
)
}
switch (inviteState.initialUserStatus) {
case UserInviteStatus.NoCurrentTeam:
case UserInviteStatus.AnotherTeamMember: {
// Invite has been canceled. Remove the banner.
if (cancelInviteMutation.isSuccess || cancelInviteMutation.isError) {
return null
}
switch (acceptInviteMutation.status) {
case 'error': {
return (
<CodyAlert variant="error">
<H1 as="p" className="mb-2">
Issue with invite
</H1>
<Text className="mb-0">
Accepting invite failed with error: {acceptInviteMutation.error.message}.
</Text>
</CodyAlert>
)
}
case 'success': {
return (
<CodyAlert variant="greenCodyPro">
<H1 as="p" className="mb-2">
Pro team change complete!
</H1>
<Text>
{inviteState.initialUserStatus === UserInviteStatus.NoCurrentTeam
? 'You successfully joined the new Cody Pro team.'
: 'Your pro team has been successfully changed.'}
</Text>
</CodyAlert>
)
}
case 'idle':
case 'pending':
default: {
return (
<CodyAlert variant="purple">
<H1 as="p" className="mb-2">
Join new Cody Pro team?
</H1>
<Text>You've been invited to a new Cody Pro team by {inviteState.sentBy}.</Text>
<Text>
{inviteState.initialUserStatus === UserInviteStatus.NoCurrentTeam
? 'You will get unlimited autocompletions, chat messages and commands.'
: 'This will terminate your current Cody Pro plan, and place you on the new Cody Pro team. You will not lose access to your Cody Pro benefits.'}
</Text>
<div>
<Button
variant="primary"
disabled={acceptInviteMutation.isPending || cancelInviteMutation.isPending}
className="mr-3"
onClick={() =>
acceptInviteMutation.mutate(
{ teamId, inviteId },
{ onSuccess: () => refetch(), onSettled: clearInviteParams }
)
}
>
Accept
</Button>
<Button
variant="link"
disabled={acceptInviteMutation.isPending || cancelInviteMutation.isPending}
onClick={() =>
cancelInviteMutation.mutate(
{ teamId, inviteId },
{ onSettled: clearInviteParams }
)
}
>
Decline
</Button>
</div>
</CodyAlert>
)
}
}
}
case UserInviteStatus.InvitedTeamMember: {
if (cancelInviteMutation.isIdle) {
void cancelInviteMutation.mutate({ teamId, inviteId }, { onSettled: clearInviteParams })
}
return (
<CodyAlert variant="error">
<H1 as="p" className="mb-2">
Issue with invite
</H1>
<Text className="mb-0">
You've been invited to a Cody Pro team by {inviteState.sentBy}.<br />
You cannot accept this invite as as you are already on this team.
</Text>
</CodyAlert>
)
}
case UserInviteStatus.AnotherTeamSoleAdmin: {
return (
<CodyAlert variant="error">
<H1 as="p" className="mb-2">
Issue with invite
</H1>
<Text className="mb-0">You've been invited to a new Cody Pro team by {inviteState.sentBy}.</Text>
<Text>
To accept this invite you need to transfer your administrative role to another member of your
team and click the invite link again.
</Text>
<div>
<ButtonLink variant="primary" to={CodyProRoutes.ManageTeam}>
Manage team
</ButtonLink>
</div>
</CodyAlert>
)
}
default: {
return null
}
}
}