| 171 | } |
| 172 | |
| 173 | function InvitesSection() { |
| 174 | const result = useAtomValue(invitesAtom); |
| 175 | const [createState, doCreate] = useAtom(createInvite, { mode: "promiseExit" }); |
| 176 | const [, doRevoke] = useAtom(revokeInvite, { mode: "promiseExit" }); |
| 177 | const [role, setRole] = useState<string>("member"); |
| 178 | const [label, setLabel] = useState(""); |
| 179 | const creating = AsyncResult.isWaiting(createState); |
| 180 | |
| 181 | const create = async () => { |
| 182 | const exit = await doCreate({ |
| 183 | payload: { role, label: label.trim() || undefined }, |
| 184 | reactivityKeys: inviteWriteKeys, |
| 185 | }); |
| 186 | if (Exit.isSuccess(exit)) { |
| 187 | setLabel(""); |
| 188 | setRole("member"); |
| 189 | toast.success("Invite link created"); |
| 190 | return; |
| 191 | } |
| 192 | toast.error("Failed to create invite"); |
| 193 | }; |
| 194 | |
| 195 | const revoke = async (inviteId: string) => { |
| 196 | const exit = await doRevoke({ params: { inviteId }, reactivityKeys: inviteWriteKeys }); |
| 197 | toast[Exit.isSuccess(exit) ? "success" : "error"]( |
| 198 | Exit.isSuccess(exit) ? "Invite revoked" : "Failed to revoke invite", |
| 199 | ); |
| 200 | }; |
| 201 | |
| 202 | return ( |
| 203 | <section className="space-y-3"> |
| 204 | <h2 className="text-sm font-medium text-foreground">Invite links</h2> |
| 205 | <div className="flex flex-wrap items-end gap-3 rounded-lg border border-border p-3"> |
| 206 | <div className="space-y-1.5"> |
| 207 | <Label htmlFor="invite-label">Label (optional)</Label> |
| 208 | <Input |
| 209 | id="invite-label" |
| 210 | placeholder="e.g. Alex" |
| 211 | value={label} |
| 212 | onChange={(e) => setLabel(e.target.value)} |
| 213 | /> |
| 214 | </div> |
| 215 | <div className="space-y-1.5"> |
| 216 | <Label htmlFor="invite-role">Role</Label> |
| 217 | <NativeSelect id="invite-role" value={role} onChange={(e) => setRole(e.target.value)}> |
| 218 | {ROLES.map((r) => ( |
| 219 | <NativeSelectOption key={r} value={r}> |
| 220 | {r} |
| 221 | </NativeSelectOption> |
| 222 | ))} |
| 223 | </NativeSelect> |
| 224 | </div> |
| 225 | <Button onClick={create} disabled={creating}> |
| 226 | {creating ? "Creating…" : "Create invite"} |
| 227 | </Button> |
| 228 | </div> |
| 229 | |
| 230 | {AsyncResult.match(result, { |