c_agri_trade_code/frontend/src/App.tsx

268 lines
10 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import { authStore } from './stores/authStore';
import { tenantStore } from './stores/tenantStore';
import { LoginView } from './features/auth/LoginView';
import { SessionView } from './features/auth/SessionView';
import { AccessDeniedView } from './features/auth/AccessDeniedView';
import { HeaderTenantSwitcher } from './features/tenant/HeaderTenantSwitcher';
import { PlatformTenantsView } from './features/tenant/PlatformTenantsView';
import { TenantMembersView } from './features/tenant/TenantMembersView';
import { AuditLogView } from './features/tenant/AuditLogView';
import { MarketDeskView } from './features/market/MarketDeskView';
import { SaudaProcurementView } from './features/procurement/SaudaProcurementView';
import {
Shield,
Building2,
Users,
FileText,
LogOut,
Layers,
TrendingUp,
ShoppingCart,
UserCheck
} from 'lucide-react';
export type TabId = 'market' | 'procurement' | 'members' | 'tenants' | 'audit' | 'sessions';
interface NavItemConfig {
id: TabId;
label: string;
icon: React.ComponentType<{ className?: string }>;
allowedRoles: string[];
}
const ALL_NAV_ITEMS: NavItemConfig[] = [
{
id: 'market',
label: 'Market Desk',
icon: TrendingUp,
allowedRoles: [
'Mandi Buyer',
'Purchase Manager',
'Partner / MD',
'Tenant Administrator',
'Desk Trader',
'Procurement Officer',
'Sales Executive',
'Sales Manager',
'Accounts Executive'
],
},
{
id: 'procurement',
label: 'Sauda Booking',
icon: ShoppingCart,
allowedRoles: [
'Mandi Buyer',
'Purchase Manager',
'Partner / MD',
'Tenant Administrator',
'Procurement Officer',
'Accounts Executive'
],
},
{
id: 'members',
label: 'Tenant Members',
icon: Users,
allowedRoles: ['Tenant Administrator', 'Partner / MD'],
},
{
id: 'tenants',
label: 'Platform Tenants',
icon: Building2,
allowedRoles: ['System Administrator'],
},
{
id: 'audit',
label: 'Security Audit',
icon: FileText,
allowedRoles: ['Tenant Administrator', 'System Administrator'],
},
{
id: 'sessions',
label: 'Session Governance',
icon: Shield,
allowedRoles: ['*'], // All authenticated roles
},
];
const resolveDefaultTab = (roles: string[]): TabId => {
if (roles.includes('Mandi Buyer') || roles.includes('Purchase Manager') || roles.includes('Partner / MD')) {
return 'market';
}
if (roles.includes('Tenant Administrator')) {
return 'members';
}
if (roles.includes('System Administrator')) {
return 'tenants';
}
return 'sessions';
};
export const App: React.FC = () => {
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(authStore.getState().isAuthenticated);
const [currentUser, setCurrentUser] = useState(authStore.getState().user);
const userRoles = currentUser?.roles || [];
const [activeTab, setActiveTab] = useState<TabId>(() => resolveDefaultTab(userRoles));
useEffect(() => {
return authStore.subscribe(() => {
const state = authStore.getState();
setIsAuthenticated(state.isAuthenticated);
setCurrentUser(state.user);
const roles = state.user?.roles || [];
setActiveTab((prev) => {
const allowed = ALL_NAV_ITEMS.filter((item) =>
item.allowedRoles.includes('*') || item.allowedRoles.some((r) => roles.includes(r))
).map((i) => i.id);
if (!allowed.includes(prev)) {
return resolveDefaultTab(roles);
}
return prev;
});
});
}, []);
if (!isAuthenticated) {
return <LoginView />;
}
// Filter navigation items strictly against current role claims (BRD-FR-040 RBAC)
const isRoleAllowed = (allowedRoles: string[]) => {
if (allowedRoles.includes('*')) return true;
return allowedRoles.some((role) => userRoles.includes(role));
};
const authorizedNavItems = ALL_NAV_ITEMS.filter((item) => isRoleAllowed(item.allowedRoles));
const currentTabConfig = ALL_NAV_ITEMS.find((item) => item.id === activeTab);
const isCurrentTabAuthorized = currentTabConfig ? isRoleAllowed(currentTabConfig.allowedRoles) : false;
return (
<div className="min-h-screen bg-surface flex text-on-surface font-sans">
{/* 240px Persistent Dark Pine Navigation Drawer per DESIGN.md */}
<aside className="w-60 bg-sidebar-surface text-white flex-shrink-0 flex flex-col justify-between border-r border-sidebar-active select-none">
<div>
{/* Brand Header */}
<div className="p-5 border-b border-sidebar-active flex items-center space-x-3">
<div className="w-8 h-8 rounded bg-sidebar-active flex items-center justify-center text-accent-ochre">
<Layers className="w-5 h-5" />
</div>
<div>
<div className="text-sm font-bold tracking-tight text-white flex items-center gap-1.5">
<span>AgriTrade</span>
<span className="text-[10px] font-mono uppercase bg-sidebar-active text-accent-ochre px-1.5 py-0.2 rounded">ATB</span>
</div>
<p className="text-[11px] text-gray-400 font-mono">Tactile Mercantile Desk</p>
</div>
</div>
{/* Active Persona Context Badge */}
<div className="px-4 py-3 border-b border-sidebar-active/60 bg-sidebar-active/30">
<div className="flex items-center space-x-2 text-[10px] uppercase font-mono tracking-wider text-accent-ochre mb-1">
<UserCheck className="w-3 h-3" />
<span>Active Persona</span>
</div>
<div className="text-xs font-semibold text-white truncate">
{currentUser?.name || currentUser?.email?.split('@')[0] || 'Authenticated User'}
</div>
<div className="text-[11px] font-mono text-gray-300 truncate">
{userRoles.length > 0 ? userRoles.join(', ') : 'Standard User'}
</div>
</div>
{/* Role-Gated Navigation Items */}
<nav className="p-3 space-y-1">
<div className="px-3 py-1.5 text-[10px] font-semibold uppercase tracking-wider text-gray-400 font-mono">
Operational Portals
</div>
{authorizedNavItems.map((item) => {
const Icon = item.icon;
const isActive = activeTab === item.id;
return (
<button
key={item.id}
onClick={() => setActiveTab(item.id)}
className={`w-full flex items-center space-x-3 px-3 py-2 rounded text-xs font-medium transition duration-150 ${
isActive
? 'bg-sidebar-active text-white border-l-2 border-accent-ochre shadow-none font-semibold'
: 'text-gray-300 hover:bg-sidebar-active/60 hover:text-white'
}`}
>
<Icon className={`w-4 h-4 ${isActive ? 'text-accent-ochre' : 'text-gray-400'}`} />
<span>{item.label}</span>
</button>
);
})}
</nav>
</div>
{/* Drawer Bottom Context & Logout */}
<div className="p-4 border-t border-sidebar-active space-y-3">
<button
onClick={() => {
authStore.logout();
tenantStore.clearTenant();
}}
className="w-full flex items-center justify-center space-x-2 px-3 py-2 text-xs font-medium text-gray-300 hover:text-white hover:bg-sidebar-active/80 rounded transition border border-sidebar-active"
>
<LogOut className="w-3.5 h-3.5 text-gray-400" />
<span>Sign Out</span>
</button>
<div className="text-[10px] text-gray-400 text-center font-mono">
UP-AI v1.5 Standard Desk
</div>
</div>
</aside>
{/* Main Operational Canvas */}
<div className="flex-1 flex flex-col min-w-0">
{/* Top Control Bar */}
<header className="bg-card-surface border-b border-card-border px-8 py-3.5 flex items-center justify-between sticky top-0 z-10 shadow-none">
<div className="flex items-center space-x-3">
<h1 className="text-base font-semibold text-text-primary capitalize tracking-tight">
{activeTab === 'market' && 'Mandi Market Desk & Daily Rates (SCREEN-MKT-001)'}
{activeTab === 'procurement' && 'Purchase Sauda Contracts Register (SCREEN-PROC-001)'}
{activeTab === 'members' && 'Tenant Membership & Role Matrix'}
{activeTab === 'tenants' && 'Platform Trading Tenants'}
{activeTab === 'audit' && 'Security Audit Trail & Mutations'}
{activeTab === 'sessions' && 'Session & Token Governance'}
</h1>
<span className="text-xs text-text-muted">|</span>
<span className="text-xs font-mono text-text-muted uppercase">240px Asymmetric Canvas</span>
</div>
<div className="flex items-center space-x-4">
<HeaderTenantSwitcher />
</div>
</header>
{/* View Content with Strict RBAC Access Guard */}
<main className="flex-1 p-8 max-w-7xl w-full mx-auto">
{!isCurrentTabAuthorized ? (
<AccessDeniedView />
) : (
<>
{activeTab === 'market' && (
<MarketDeskView onNavigateToSauda={() => setActiveTab('procurement')} />
)}
{activeTab === 'procurement' && <SaudaProcurementView />}
{activeTab === 'members' && <TenantMembersView />}
{activeTab === 'tenants' && <PlatformTenantsView />}
{activeTab === 'audit' && <AuditLogView />}
{activeTab === 'sessions' && <SessionView />}
</>
)}
</main>
{/* Footer */}
<footer className="bg-card-surface border-t border-card-border px-8 py-3 text-xs text-text-muted flex justify-between items-center">
<span>Agriculture Trading Backoffice (ATB) &copy; 2026. Tactile Corporate Minimalism.</span>
<span className="font-mono text-[11px]">DESIGN.md Tokenized</span>
</footer>
</div>
</div>
);
};