| 1 | "use client"; |
| 2 | |
| 3 | import type { OrganizationAuthClient } |
| 4 | import { useAuth, useAuthPlugin |
| 5 | import { useListOrganizations } |
| 6 | import { Fragment, useState |
| 7 | |
| 8 | import { Button } |
| 9 | import { Card, CardContent |
| 10 | import { Item, ItemGroup |
| 11 | import { organizationPlugin } |
| 12 | import { CreateOrganizationDialog } |
| 13 | import { OrganizationRow } |
| 14 | import { OrganizationViewSkeleton } |
| 15 | import { OrganizationsEmpty } |
| 16 | |
| 17 | export type OrganizationsProps |
| 18 | className?: string |
| 19 | }; |
| 20 | |
| 21 | export function Organizations({ |
| 22 | const { authClient |
| 23 | const { |
| 24 | allowOrganizationCreation, |
| 25 | localization: organizationLocalization, |
| 26 | organizationLimit, |
| 27 | } = useAuthPlugin(organizationPlugin |
| 28 | |
| 29 | const [createOpen, |
| 30 | |
| 31 | const { data: |
| 32 | useListOrganizations(authClient); |
| 33 | const canCreate = |
| 34 | allowOrganizationCreation && |
| 35 | (organizationLimit === undefined |
| 36 | (organizations?.length ?? |
| 37 | |
| 38 | return ( |
| 39 | <> |
| 40 | <div className={className |
| 41 | <div className="flex flex-col gap-3 |
| 42 | <div className="flex items-end justify-between gap-3 |
| 43 | <h2 className="truncate text-sm font-semibold |
| 44 | {organizationLocalization.organizations} |
| 45 | </h2> |
| 46 | |
| 47 | {allowOrganizationCreation && ( |
| 48 | <Button |
| 49 | className="shrink-0" |
| 50 | size="sm" |
| 51 | disabled={organizationsPending || |
| 52 | onClick={() => |
| 53 | > |
| 54 | {organizationLocalization.createOrganization} |
| 55 | </Button> |
| 56 | )} |
| 57 | </div> |
| 58 | |
| 59 | <Card className=" |
| 60 | <CardContent className=" |
| 61 | {organizationsPending ? ( |
| 62 | <ItemGroup> |
| 63 | <Item> |
| 64 | <OrganizationViewSkeleton /> |
| 65 | </Item> |
| 66 | </ItemGroup> |
| 67 | ) : !organizations?. |
| 68 | <OrganizationsEmpty |
| 69 | canCreate={canCreate} |
| 70 | onCreatePress={() => |
| 71 | /> |
| 72 | ) : ( |
| 73 | <ItemGroup className=" |
| 74 | {organizations.map(( |
| 75 | <Fragment key={ |
| 76 | {index > 0 |
| 77 | <OrganizationRow organization={ |
| 78 | </Fragment> |
| 79 | ))} |
| 80 | </ItemGroup> |
| 81 | )} |
| 82 | </CardContent> |
| 83 | </Card> |
| 84 | </div> |
| 85 | </div> |
| 86 | |
| 87 | {canCreate && ( |
| 88 | <CreateOrganizationDialog |
| 89 | open={createOpen} |
| 90 | onOpenChange={setCreateOpen} |
| 91 | /> |
| 92 | )} |
| 93 | </> |
| 94 | ); |
| 95 | } |