| 1 | "use client"; |
| 2 | |
| 3 | import type { OrganizationAuthClient } from "@better-auth-ui/core/plugins/organization"; |
| 4 | import { useAuth, useAuthPlugin |
| 5 | import { |
| 6 | useActiveOrganization, |
| 7 | useListOrganizationMembers, |
| 8 | } from "@better-auth-ui/react/plugins/organization"; |
| 9 | import type { Organization |
| 10 | import type { ComponentProps |
| 11 | |
| 12 | import { Badge } |
| 13 | import { organizationPlugin } |
| 14 | import { cn } |
| 15 | import { |
| 16 | OrganizationLogo, |
| 17 | type OrganizationLogoSize, |
| 18 | } from "./organization-logo"; |
| 19 | import { OrganizationViewSkeleton } |
| 20 | |
| 21 | export type OrganizationViewProps |
| 22 | className?: string |
| 23 | isPending?: boolean |
| 24 | size?: OrganizationLogoSize |
| 25 | hideRole?: boolean |
| 26 | hideSlug?: boolean |
| 27 | organization?: Partial |
| 28 | }; |
| 29 | |
| 30 | /** Compact organization row: logo, primary name, secondary slug — analogous to `UserView`. */ |
| 31 | export function OrganizationView({ |
| 32 | className, |
| 33 | isPending, |
| 34 | size = "md" |
| 35 | hideSlug: hideSlugProp, |
| 36 | hideRole, |
| 37 | organization, |
| 38 | ...props |
| 39 | }: OrganizationViewProps |
| 40 | const { authClient |
| 41 | const { |
| 42 | roles, |
| 43 | slugPrefix, |
| 44 | hideSlug: pluginHideSlug, |
| 45 | } = useAuthPlugin(organizationPlugin |
| 46 | const hideSlug = |
| 47 | |
| 48 | const { data: |
| 49 | |
| 50 | const { data: |
| 51 | useActiveOrganization(authClient, { |
| 52 | enabled: !organization && |
| 53 | }); |
| 54 | |
| 55 | const resolvedOrganization = |
| 56 | |
| 57 | const { data: |
| 58 | useListOrganizationMembers(authClient, { |
| 59 | query: { |
| 60 | organizationId: resolvedOrganization?.id |
| 61 | }, |
| 62 | enabled: !!resolvedOrganization?.id |
| 63 | }); |
| 64 | |
| 65 | const membership = |
| 66 | (member) => |
| 67 | ); |
| 68 | |
| 69 | if ( |
| 70 | isPending || |
| 71 | (!organization && activeOrganizationPending |
| 72 | (!hideRole && !! |
| 73 | ) { |
| 74 | return ( |
| 75 | <OrganizationViewSkeleton |
| 76 | className={className} |
| 77 | hideSlug={hideSlug} |
| 78 | size={size} |
| 79 | {...props} |
| 80 | /> |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | return ( |
| 85 | <div |
| 86 | className={cn(" |
| 87 | {...props} |
| 88 | > |
| 89 | <OrganizationLogo |
| 90 | organization={resolvedOrganization} |
| 91 | className={size === |
| 92 | size={size === |
| 93 | /> |
| 94 | |
| 95 | <div className="flex min-w-0 flex-col |
| 96 | <div className="flex min-w-0 items-center gap-2 |
| 97 | <p className="truncate text-sm font-medium leading-tight text-foreground |
| 98 | {resolvedOrganization?.name} |
| 99 | </p> |
| 100 | |
| 101 | {!hideRole && !! |
| 102 | <Badge variant=" |
| 103 | {roles?.[membership. |
| 104 | </Badge> |
| 105 | )} |
| 106 | </div> |
| 107 | |
| 108 | {!hideSlug && !! |
| 109 | <p className="truncate overflow-x-hidden text-muted-foreground text-xs font-mono leading-tight |
| 110 | {slugPrefix} |
| 111 | {resolvedOrganization.slug} |
| 112 | </p> |
| 113 | )} |
| 114 | </div> |
| 115 | </div> |
| 116 | ); |
| 117 | } |