139 lines
6.0 KiB
TypeScript
139 lines
6.0 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import { tenantStore, TenantMembershipInfo } from '../../stores/tenantStore';
|
|
import { apiClient } from '../../services/api';
|
|
import { Building2, ChevronDown } from 'lucide-react';
|
|
|
|
/**
|
|
* Header Tenant Switcher Dropdown Component.
|
|
*
|
|
* Fulfills FR-TENANTMEMBERSHIPCONTEXT-002 (UC-019 / SCREEN-TENANT-003).
|
|
* Styled per DESIGN.md: AgriTrade Mercantile tokens.
|
|
*/
|
|
export const HeaderTenantSwitcher: React.FC = () => {
|
|
const [activeTenantId, setActiveTenantId] = useState<string | null>(tenantStore.getState().activeTenantId);
|
|
const [activeBusinessName, setActiveBusinessName] = useState<string | null>(tenantStore.getState().activeBusinessName);
|
|
const [effectiveRoles, setEffectiveRoles] = useState<string[]>(tenantStore.getState().effectiveRoles);
|
|
const [myTenants, setMyTenants] = useState<TenantMembershipInfo[]>(tenantStore.getState().myTenants);
|
|
const [isOpen, setIsOpen] = useState(false);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
const unsubscribe = tenantStore.subscribe(() => {
|
|
const state = tenantStore.getState();
|
|
setActiveTenantId(state.activeTenantId);
|
|
setActiveBusinessName(state.activeBusinessName);
|
|
setEffectiveRoles(state.effectiveRoles);
|
|
setMyTenants(state.myTenants);
|
|
});
|
|
|
|
loadTenants();
|
|
|
|
return () => unsubscribe();
|
|
}, []);
|
|
|
|
const loadTenants = async () => {
|
|
setLoading(true);
|
|
const resp = await apiClient<{ tenants: TenantMembershipInfo[] }>('/api/v1/auth/my-tenants');
|
|
setLoading(false);
|
|
if (resp.data && resp.data.tenants) {
|
|
tenantStore.setMyTenants(resp.data.tenants);
|
|
if (!tenantStore.getState().activeTenantId && resp.data.tenants.length > 0) {
|
|
const first = resp.data.tenants[0];
|
|
handleSwitch(first.tenant_id, first.business_name, [first.role]);
|
|
}
|
|
}
|
|
};
|
|
|
|
const handleSwitch = async (tenantId: string, businessName: string, roles: string[]) => {
|
|
setError(null);
|
|
setLoading(true);
|
|
const resp = await apiClient('/api/v1/session/active-tenant', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ tenant_id: tenantId }),
|
|
});
|
|
setLoading(false);
|
|
|
|
if (resp.status === 200) {
|
|
tenantStore.setActiveTenant(tenantId, businessName, roles);
|
|
setIsOpen(false);
|
|
} else {
|
|
setError(resp.error || 'Failed to switch tenant.');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="relative inline-block text-left" data-testid="tenant-switcher">
|
|
<div>
|
|
<button
|
|
type="button"
|
|
onClick={() => setIsOpen(!isOpen)}
|
|
className="inline-flex items-center justify-between w-64 rounded border border-card-border px-3 py-1.5 bg-white hover:bg-surface-low focus:outline-none focus:border-sidebar-surface focus:ring-2 focus:ring-secondary-amber/20 transition shadow-none"
|
|
id="tenant-menu-button"
|
|
aria-expanded={isOpen}
|
|
aria-haspopup="true"
|
|
>
|
|
<div className="flex items-center space-x-2 truncate mr-2">
|
|
<Building2 className="w-4 h-4 text-text-muted flex-shrink-0" />
|
|
<div className="flex flex-col text-left truncate">
|
|
<span className="text-[10px] text-text-muted uppercase tracking-wider font-mono font-semibold">Active Firm</span>
|
|
<span className="font-semibold text-xs text-text-primary truncate">
|
|
{activeBusinessName || 'Select Trading Firm...'}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center space-x-1 flex-shrink-0">
|
|
<span className="inline-flex items-center px-1.5 py-0.5 rounded-full text-[10px] font-mono font-medium bg-status-normal-bg text-status-normal-text">
|
|
{effectiveRoles[0] || 'Member'}
|
|
</span>
|
|
<ChevronDown className="w-3.5 h-3.5 text-text-muted" />
|
|
</div>
|
|
</button>
|
|
</div>
|
|
|
|
{isOpen && (
|
|
<div
|
|
className="origin-top-right absolute right-0 mt-1.5 w-72 rounded bg-card-surface border border-card-border shadow-tactile focus:outline-none z-50 divide-y divide-card-border"
|
|
role="menu"
|
|
aria-orientation="vertical"
|
|
aria-labelledby="tenant-menu-button"
|
|
>
|
|
<div className="px-4 py-2.5 bg-surface-low">
|
|
<p className="text-[10px] text-text-muted uppercase tracking-wider font-mono font-semibold">Authorized Firms Directory</p>
|
|
{error && <p className="text-xs text-error mt-1">{error}</p>}
|
|
</div>
|
|
|
|
<div className="py-1 max-h-60 overflow-y-auto" role="none">
|
|
{myTenants.length === 0 ? (
|
|
<div className="px-4 py-2 text-xs text-text-muted font-mono">No authorized enterprises found.</div>
|
|
) : (
|
|
myTenants.map((t) => (
|
|
<button
|
|
key={t.membership_id}
|
|
onClick={() => handleSwitch(t.tenant_id, t.business_name, [t.role])}
|
|
disabled={loading}
|
|
className={`w-full text-left px-4 py-2.5 text-xs hover:bg-surface-low flex items-center justify-between transition-colors ${
|
|
t.tenant_id === activeTenantId
|
|
? 'bg-status-normal-bg/40 text-text-primary font-semibold border-l-2 border-accent-ochre'
|
|
: 'text-text-primary'
|
|
}`}
|
|
role="menuitem"
|
|
>
|
|
<div className="truncate pr-2">
|
|
<p className="font-medium truncate">{t.business_name}</p>
|
|
<p className="text-[10px] text-text-muted font-mono">{t.legal_identifier}</p>
|
|
</div>
|
|
<span className="inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-mono bg-surface-container text-text-muted">
|
|
{t.role}
|
|
</span>
|
|
</button>
|
|
))
|
|
)}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|