| 1 | "use client"; |
| 2 | |
| 3 | import { useRouter } from |
| 4 | import { useState } |
| 5 | import { toast } |
| 6 | |
| 7 | import { Button } |
| 8 | import { Spinner } |
| 9 | import { apiClient, apiErrorMessage |
| 10 | import { authClient } |
| 11 | |
| 12 | /** A member's own controls on the organization's page: show or hide their membership, and leave. The last owner cannot leave; the API says so. */ |
| 13 | export function MembershipActions({ |
| 14 | slug, |
| 15 | isPublic, |
| 16 | }: { |
| 17 | slug: string |
| 18 | isPublic: boolean |
| 19 | }) { |
| 20 | const router = |
| 21 | const [pending, |
| 22 | |
| 23 | async function toggleVisibility |
| 24 | setPending("visibility"); |
| 25 | const params = |
| 26 | const { error |
| 27 | ? await apiClient. |
| 28 | "/api/organizations/{slug}/public-members", |
| 29 | params, |
| 30 | ) |
| 31 | : await apiClient. |
| 32 | setPending(null); |
| 33 | if (error) return |
| 34 | toast.success( |
| 35 | isPublic ? "Your membership is private" |
| 36 | ); |
| 37 | router.refresh(); |
| 38 | } |
| 39 | |
| 40 | async function leave |
| 41 | setPending("leave"); |
| 42 | const { data: |
| 43 | await authClient.organization |
| 44 | query: { organizationSlug: slug |
| 45 | }); |
| 46 | const { error |
| 47 | ? await authClient. |
| 48 | : { error: { |
| 49 | setPending(null); |
| 50 | if (error) return |
| 51 | toast.success(`You left |
| 52 | router.push("/dashboard |
| 53 | } |
| 54 | |
| 55 | return ( |
| 56 | <div className="flex flex-col gap-2 |
| 57 | <p className="text-xs text-muted-foreground |
| 58 | {isPublic |
| 59 | ? "Your membership is public." |
| 60 | : "Your membership is private: only members see it."} |
| 61 | </p> |
| 62 | <div className="flex gap-2 |
| 63 | <Button |
| 64 | size="sm" |
| 65 | variant="outline" |
| 66 | className="flex-1" |
| 67 | disabled={pending !== |
| 68 | onClick={toggleVisibility} |
| 69 | > |
| 70 | {pending === "visibility |
| 71 | {isPublic ? "Make private |
| 72 | </Button> |
| 73 | <Button |
| 74 | size="sm" |
| 75 | variant="ghost" |
| 76 | className="text-destructive" |
| 77 | disabled={pending !== |
| 78 | onClick={() => |
| 79 | if ( |
| 80 | window.confirm( |
| 81 | `Leave ${slug}? You lose access to its repositories.` |
| 82 | ) |
| 83 | ) { |
| 84 | void leave(); |
| 85 | } |
| 86 | }} |
| 87 | > |
| 88 | {pending === "leave |
| 89 | Leave |
| 90 | </Button> |
| 91 | </div> |
| 92 | </div> |
| 93 | ); |
| 94 | } |