| 1 | "use client"; |
| 2 | |
| 3 | import { useMutation, useQuery |
| 4 | import { toast } |
| 5 | |
| 6 | import { Badge } |
| 7 | import { Button } |
| 8 | import { Skeleton } |
| 9 | import { Spinner } |
| 10 | import { apiClient, unwrap |
| 11 | |
| 12 | /** The viewer's organizations that restrict OAuth apps, and whether this app reaches each; a member asks an organization's admins for an app it keeps out. Renders nothing when no organization restricts apps. */ |
| 13 | export function OrganizationAccess({ |
| 14 | const queryKey = |
| 15 | const queryClient = |
| 16 | const { data, |
| 17 | queryKey, |
| 18 | queryFn: () => |
| 19 | unwrap( |
| 20 | apiClient.GET("/api/oauth-apps/{clientId}/organizations |
| 21 | params: { path: { |
| 22 | }), |
| 23 | ), |
| 24 | }); |
| 25 | const ask = |
| 26 | mutationFn: (slug: |
| 27 | unwrap( |
| 28 | apiClient.POST( |
| 29 | "/api/organizations/{slug}/oauth-app-access/{clientId}/request", |
| 30 | { params: { path: |
| 31 | ), |
| 32 | ), |
| 33 | onSuccess: () => |
| 34 | toast.success("Asked the organization's admins |
| 35 | return queryClient.invalidateQueries |
| 36 | }, |
| 37 | onError: (error) |
| 38 | }); |
| 39 | |
| 40 | if (isPending) return |
| 41 | const restricting = |
| 42 | (organization) => |
| 43 | ); |
| 44 | if (restricting.length |
| 45 | |
| 46 | return ( |
| 47 | <div className="flex flex-col gap-2 |
| 48 | <h3 className="text-sm font-medium |
| 49 | <ul className="flex flex-col gap-2 |
| 50 | {restricting.map(( |
| 51 | <li |
| 52 | key={organization.slug |
| 53 | className="flex items-center justify-between gap-3 text-sm" |
| 54 | > |
| 55 | <span className="min-w-0 truncate |
| 56 | {organization.allowed ? |
| 57 | <Badge>Allowed</Badge |
| 58 | ) : organization.state |
| 59 | <Button |
| 60 | size="sm" |
| 61 | variant="outline" |
| 62 | disabled={ask.isPending |
| 63 | onClick={() => |
| 64 | > |
| 65 | {ask.isPending && |
| 66 | <Spinner /> |
| 67 | )} |
| 68 | Request access |
| 69 | </Button> |
| 70 | ) : ( |
| 71 | <Badge variant=" |
| 72 | {organization.state === |
| 73 | </Badge> |
| 74 | )} |
| 75 | </li> |
| 76 | ))} |
| 77 | </ul> |
| 78 | </div> |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | export function OrganizationAccessSkeleton() |
| 83 | return ( |
| 84 | <div className="flex flex-col gap-2 |
| 85 | <Skeleton className=" |
| 86 | <Skeleton className=" |
| 87 | </div> |
| 88 | ); |
| 89 | } |