| 1 | "use client"; |
| 2 | |
| 3 | import { Fragment, useState |
| 4 | |
| 5 | import { FromNowHoverCard } |
| 6 | import { SettingCard } |
| 7 | import { RoleSelect } |
| 8 | import { Avatar, AvatarFallback |
| 9 | import { Button } |
| 10 | import { Card } |
| 11 | import { Field, FieldLabel |
| 12 | import { Input } |
| 13 | import { |
| 14 | Item, |
| 15 | ItemActions, |
| 16 | ItemContent, |
| 17 | ItemDescription, |
| 18 | ItemGroup, |
| 19 | ItemMedia, |
| 20 | ItemSeparator, |
| 21 | ItemTitle, |
| 22 | } from "@/components/ui/item"; |
| 23 | import { |
| 24 | type OrganizationRole, |
| 25 | organizationRoleHierarchy, |
| 26 | organizationRoleOf, |
| 27 | } from "@ghost/permissions"; |
| 28 | import { Skeleton } |
| 29 | import { apiClient } |
| 30 | import { authClient } |
| 31 | import { organizationRoleLabels } |
| 32 | |
| 33 | import { useOrganization } |
| 34 | |
| 35 | export function OrganizationPeople({ |
| 36 | slug, |
| 37 | isOwner, |
| 38 | }: { |
| 39 | slug: string |
| 40 | /** Only an owner may make someone else an owner. */ |
| 41 | isOwner: boolean |
| 42 | }) { |
| 43 | const { organization, |
| 44 | const [invitee, |
| 45 | const [role, |
| 46 | const roles: |
| 47 | ? organizationRoleHierarchy |
| 48 | : organizationRoleHierarchy.filter( |
| 49 | const organizationId = |
| 50 | const invitations = |
| 51 | organization?.invitations.filter |
| 52 | (invitation) => |
| 53 | ) ?? []; |
| 54 | |
| 55 | return ( |
| 56 | <div className="flex flex-col gap-8 |
| 57 | <SettingCard |
| 58 | title="Invite a member" |
| 59 | hint="They join once they accept, from their dashboard or the email." |
| 60 | onSubmit={() => |
| 61 | change( |
| 62 | // An address goes to Better Auth as is; a username is resolved by the API, which keeps the account's email to itself. |
| 63 | () => |
| 64 | invitee.includes("@ |
| 65 | ? authClient.organization. |
| 66 | organizationId, |
| 67 | email: invitee.trim |
| 68 | role, |
| 69 | }) |
| 70 | : apiClient.POST( |
| 71 | params: { path: { |
| 72 | body: { username: invitee |
| 73 | }), |
| 74 | `Invitation sent to ${invitee.trim |
| 75 | ).then((ok |
| 76 | } |
| 77 | pending={busy} |
| 78 | canSave={Boolean(organization |
| 79 | submitText="Send invitation" |
| 80 | > |
| 81 | <div className="flex flex-col gap-4 sm:flex-row |
| 82 | <Field className=" |
| 83 | <FieldLabel htmlFor=" |
| 84 | <Input |
| 85 | id="invite-email" |
| 86 | autoComplete="off" |
| 87 | placeholder="octocat or teammate@example.com" |
| 88 | value={invitee} |
| 89 | onChange={(event) |
| 90 | /> |
| 91 | </Field> |
| 92 | <Field className=" |
| 93 | <FieldLabel htmlFor=" |
| 94 | <RoleSelect |
| 95 | id="invite-member-role" |
| 96 | labels={organizationRoleLabels} |
| 97 | roles={roles} |
| 98 | value={role} |
| 99 | onChange={setRole} |
| 100 | /> |
| 101 | </Field> |
| 102 | </div> |
| 103 | </SettingCard> |
| 104 | |
| 105 | <section> |
| 106 | <h2 className="mb-3 text-sm font-semibold |
| 107 | Members |
| 108 | {organization && ( |
| 109 | <span className="ml-2 font-normal text-muted-foreground tabular-nums |
| 110 | {organization.members.length |
| 111 | </span> |
| 112 | )} |
| 113 | </h2> |
| 114 | <Card className=" |
| 115 | {isPending ? ( |
| 116 | <Skeleton className=" |
| 117 | ) : ( |
| 118 | <ItemGroup className=" |
| 119 | {organization?.members.map |
| 120 | const current = |
| 121 | return ( |
| 122 | <Fragment key={ |
| 123 | {index > 0 |
| 124 | <Item className=" |
| 125 | <ItemMedia> |
| 126 | <Avatar className=" |
| 127 | <AvatarImage |
| 128 | src={member.user |
| 129 | alt="" |
| 130 | /> |
| 131 | <AvatarFallback> |
| 132 | {member.user.name |
| 133 | </AvatarFallback> |
| 134 | </Avatar> |
| 135 | </ItemMedia> |
| 136 | <ItemContent className=" |
| 137 | <ItemTitle>{member. |
| 138 | <ItemDescription className=" |
| 139 | {member.user.email |
| 140 | </ItemDescription> |
| 141 | </ItemContent> |
| 142 | <ItemActions> |
| 143 | <RoleSelect |
| 144 | labels={organizationRoleLabels} |
| 145 | roles={ |
| 146 | roles.includes(current |
| 147 | ? roles |
| 148 | : [...roles, |
| 149 | } |
| 150 | value={current} |
| 151 | disabled={busy || |
| 152 | label={`Role for ${member |
| 153 | onChange={(next) |
| 154 | change( |
| 155 | () => |
| 156 | authClient.organization.updateMemberRole |
| 157 | organizationId, |
| 158 | memberId: member.id |
| 159 | role: next, |
| 160 | }), |
| 161 | `${member.user. |
| 162 | ) |
| 163 | } |
| 164 | /> |
| 165 | <Button |
| 166 | size="sm" |
| 167 | variant="ghost" |
| 168 | disabled={busy || |
| 169 | onClick={() => |
| 170 | change( |
| 171 | () => |
| 172 | authClient.organization.removeMember |
| 173 | organizationId, |
| 174 | memberIdOrEmail: member.id |
| 175 | }), |
| 176 | `${member.user. |
| 177 | ) |
| 178 | } |
| 179 | > |
| 180 | Remove |
| 181 | </Button> |
| 182 | </ItemActions> |
| 183 | </Item> |
| 184 | </Fragment> |
| 185 | ); |
| 186 | })} |
| 187 | </ItemGroup> |
| 188 | )} |
| 189 | </Card> |
| 190 | </section> |
| 191 | |
| 192 | {invitations.length > |
| 193 | <section> |
| 194 | <h2 className="mb-3 text-sm font-semibold |
| 195 | <Card className=" |
| 196 | <ItemGroup className=" |
| 197 | {invitations.map(( |
| 198 | <Fragment key={ |
| 199 | {index > 0 |
| 200 | <Item> |
| 201 | <ItemContent> |
| 202 | <ItemTitle>{invitation. |
| 203 | <ItemDescription> |
| 204 | { |
| 205 | organizationRoleLabels[ |
| 206 | organizationRoleOf(invitation.role |
| 207 | ] |
| 208 | }{" "} |
| 209 | · expires{" "} |
| 210 | <FromNowHoverCard date={ |
| 211 | </ItemDescription> |
| 212 | </ItemContent> |
| 213 | <ItemActions> |
| 214 | <Button |
| 215 | size="sm" |
| 216 | variant="ghost" |
| 217 | disabled={busy} |
| 218 | onClick={() => |
| 219 | change( |
| 220 | () => |
| 221 | authClient.organization.cancelInvitation |
| 222 | invitationId: invitation.id |
| 223 | }), |
| 224 | "Invitation cancelled", |
| 225 | ) |
| 226 | } |
| 227 | > |
| 228 | Cancel |
| 229 | </Button> |
| 230 | </ItemActions> |
| 231 | </Item> |
| 232 | </Fragment> |
| 233 | ))} |
| 234 | </ItemGroup> |
| 235 | </Card> |
| 236 | </section> |
| 237 | )} |
| 238 | </div> |
| 239 | ); |
| 240 | } |