import React, { useEffect, useMemo, useState } from 'react' import CardBox from './CardBox' import ConnectedEntityCard from './ConnectedEntityCard' import TenantStatusChip from './TenantStatusChip' import { useAppSelector } from '../stores/hooks' import { emptyOrganizationTenantSummary, getOrganizationViewHref, getTenantViewHref, loadLinkedTenantSummary, } from '../helpers/organizationTenants' import { hasPermission } from '../helpers/userPermissions' type MyOrgTenantSummaryProps = { className?: string } const MyOrgTenantSummary = ({ className = '' }: MyOrgTenantSummaryProps) => { const { currentUser } = useAppSelector((state) => state.auth) const [linkedTenantSummary, setLinkedTenantSummary] = useState(emptyOrganizationTenantSummary) const [isLoadingTenants, setIsLoadingTenants] = useState(false) const organizationId = useMemo( () => currentUser?.organizations?.id || currentUser?.organization?.id || currentUser?.organizationsId || currentUser?.organizationId || '', [ currentUser?.organization?.id, currentUser?.organizationId, currentUser?.organizations?.id, currentUser?.organizationsId, ], ) const organizationName = currentUser?.organizations?.name || currentUser?.organization?.name || currentUser?.organizationName || 'No organization assigned yet' const canViewOrganizations = hasPermission(currentUser, 'READ_ORGANIZATIONS') const canViewTenants = hasPermission(currentUser, 'READ_TENANTS') useEffect(() => { let isMounted = true if (!organizationId || !canViewTenants) { setLinkedTenantSummary(emptyOrganizationTenantSummary) setIsLoadingTenants(false) return () => { isMounted = false } } setIsLoadingTenants(true) loadLinkedTenantSummary(organizationId) .then((summary) => { if (!isMounted) { return } setLinkedTenantSummary(summary) }) .catch((error) => { console.error('Failed to load current user org/tenant context:', error) if (!isMounted) { return } setLinkedTenantSummary(emptyOrganizationTenantSummary) }) .finally(() => { if (isMounted) { setIsLoadingTenants(false) } }) return () => { isMounted = false } }, [canViewTenants, organizationId]) if (!currentUser) { return null } const appRoleName = currentUser?.app_role?.name || 'No role surfaced yet' const linkedTenants = Array.isArray(linkedTenantSummary.rows) ? linkedTenantSummary.rows : [] const tenantSummaryValue = !organizationId ? 'No workspace linked' : !canViewTenants ? 'Restricted' : isLoadingTenants ? 'Loading…' : String(linkedTenantSummary.count || 0) const tenantEmptyStateMessage = !organizationId ? 'No organization is attached to this account yet.' : !canViewTenants ? 'Your current role does not include tenant-read access for this organization context.' : 'No tenant link surfaced yet for your organization.' return (
My account context

Your organization and tenant access

This makes it clear which organization your signed-in account belongs to and which tenant links sit behind it.

My tenant links

{isLoadingTenants ? (
Loading tenant context for your organization…
) : linkedTenants.length ? (
{linkedTenants.map((tenant) => ( ]} details={[ { label: 'Slug', value: tenant.slug }, { label: 'Domain', value: tenant.primary_domain }, { label: 'Timezone', value: tenant.timezone }, { label: 'Currency', value: tenant.default_currency }, ]} actions={ canViewTenants && tenant.id ? [ { href: getTenantViewHref(tenant.id, organizationId, organizationName), label: 'View tenant', color: 'info', outline: true, }, ] : [] } helperText="This tenant link is part of the organization context attached to your account." /> ))}
) : (
{tenantEmptyStateMessage}
)}
) } export default MyOrgTenantSummary