Spaces:
Paused

nexus / frontend /apps /internal /src /App.tsx
dushyantsingh0
fix: allow staff cross-workspace live events via WebSockets
bc1617c
Raw History Blame Contribute Delete
8.57 kB
import { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import {
useAuth,
useDashboardStats,
useAIInsights,
useServiceNarrative,
LoginCard,
NexusLogo,
setPortalType
} from '@nexus/shared';
setPortalType('internal');
import { SideNav } from './components/SideNav';
import type { TabId } from './components/SideNav';
import { TopBar } from './components/TopBar';
import { TicketDetails } from './components/TicketDetails';
import { DashboardOverview } from './components/DashboardOverview';
import { TicketBoard } from './components/TicketBoard';
import { KnowledgeBase } from './components/KnowledgeBase';
import { EscalationCentre } from './components/EscalationCentre';
import { AnalyticsDashboard } from './components/AnalyticsDashboard';
import { WorkspaceSettings } from './components/WorkspaceSettings';
import { UserSettingsModal } from './components/UserSettingsModal';
import { NexusMasterAgent } from './components/NexusMasterAgent';
import InfraMonitor from './components/InfraMonitor';
import './App.css';
function App() {
const { token, user, loading: authLoading, login, logout, refreshUser, hasRole } = useAuth();
const [selectedWorkspaceId, setSelectedWorkspaceId] = useState<string | undefined>(undefined);
const { stats, loading: statsLoading } = useDashboardStats(selectedWorkspaceId);
useAIInsights(selectedWorkspaceId);
useServiceNarrative(selectedWorkspaceId);
const [activeTab, setActiveTab] = useState<TabId>('overview');
const [selectedTicket, setSelectedTicket] = useState<any>(null);
const [showSettings, setShowSettings] = useState(false);
// Sync selected workspace with user's home workspace on login
useEffect(() => {
if (user?.workspace_id && !selectedWorkspaceId) {
setSelectedWorkspaceId(user.workspace_id);
}
}, [user, selectedWorkspaceId]);
const isAdminOrLead = hasRole(['3sc_admin', '3sc_lead']);
if (!token) {
return (
<div className="login-wrapper">
<LoginCard onLogin={login} loading={authLoading} portal="internal" />
</div>
);
}
// --- STRICT PORTAL GUARD: Internal Staff Only ---
const role = user?.role?.toLowerCase() || '';
const isStaff = role.startsWith('3sc_') || role.startsWith('sc_') || role === 'admin' || role === 'staff';
if (user && !isStaff) {
return (
<div className="nexus-unauthorized-overlay">
<div className="nexus-unauthorized-box">
<NexusLogo subtitle="SECURITY CLEARANCE DENIED" />
<h1>Access Forbidden</h1>
<p>This portal is reserved for 3SC Operations Staff. Your account identity is restricted to the Customer Portal.</p>
<div className="nexus-unauthorized-actions">
<button onClick={() => window.location.href = '/'} className="nexus-btn-primary">Return to Customer Portal</button>
<button onClick={logout} className="nexus-btn-secondary">Logout and Switch Account</button>
</div>
</div>
</div>
);
}
// Pre-load logic: We wait for auth AND for the first set of stats to arrive.
// AI/Narrative loading is now "soft" to avoid navigation flickers.
// SAFETY: 5-second hard timeout prevents infinite loading if a backend endpoint 500s.
const [bootstrapComplete, setBootstrapComplete] = useState(false);
useEffect(() => {
const timer = setTimeout(() => setBootstrapComplete(true), 5000);
return () => clearTimeout(timer);
}, []);
useEffect(() => {
if (stats || (!statsLoading && user)) {
setBootstrapComplete(true);
}
}, [stats, statsLoading, user]);
const isInitialLoading = !bootstrapComplete && (
(authLoading && !user) || (user && (statsLoading && !stats))
);
if (isInitialLoading) {
return (
<div className="nexus-loading-screen">
<div className="nexus-loading-box">
<NexusLogo subtitle={user ? "SYNCHRONIZING ENVIRONMENT" : "SECURITY CLEARANCE GRANTED"} />
<p className="nexus-loading-msg">{user ? "Hydrating workspace intelligence..." : "Initializing Workspace Environment..."}</p>
<div className="nexus-loading-bar"><div className="nexus-loading-progress" /></div>
</div>
</div>
);
}
const renderContent = () => {
// 403 Forbidden check for direct navigation or state manipulation
const forbidden = <div className="forbidden-view">403: Forbidden — Insufficient Permissions</div>;
switch (activeTab) {
case 'overview':
return <DashboardOverview workspaceId={selectedWorkspaceId} />;
case 'workspace':
return isAdminOrLead ? <WorkspaceSettings workspaceId={selectedWorkspaceId} /> : forbidden;
case 'queue':
return (
<TicketBoard
onSelect={(t) => {
setSelectedTicket(t);
setActiveTab('nexus');
}}
/>
);
case 'nexus':
return (
<TicketDetails
initialTicket={selectedTicket}
onBack={() => setActiveTab('queue')}
/>
);
case 'kb':
return <KnowledgeBase workspaceId={selectedWorkspaceId} />;
case 'escalation':
return isAdminOrLead ? <EscalationCentre /> : forbidden;
case 'analytics':
return (
<AnalyticsDashboard
workspaceId={selectedWorkspaceId}
readOnly={!isAdminOrLead}
onTicketSelect={(t) => {
setSelectedTicket(t);
setActiveTab('nexus');
}}
/>
);
case 'infra':
return isAdminOrLead ? <InfraMonitor /> : forbidden;
default:
return <DashboardOverview />;
}
};
const getPageInfo = () => {
const openCount = (stats?.by_status?.OPEN || 0) + (stats?.by_status?.ACKNOWLEDGED || 0) + (stats?.by_status?.IN_PROGRESS || 0);
const breachCount = stats?.performance?.sla_breached_count || 0;
const pages: Record<TabId, { title: string; subtitle: string }> = {
overview: {
title: 'Operations Overview',
subtitle: `WORKSPACE: ${stats?.workspace_id?.slice(0,8).toUpperCase() || 'SNOWBALL'} · ${openCount} OPEN · ${breachCount} SLA BREACH`
},
workspace: {
title: 'Workspace Settings',
subtitle: 'ORGANIZATION IDENTITY, TEAM MEMBERS, AND ROLE-BASED ACCESS'
},
queue: { title: 'Operational Board', subtitle: 'KANBAN & SCRUM MANAGEMENT VIEWS' },
nexus: { title: 'Nexus AI', subtitle: selectedTicket ? `${selectedTicket.id?.slice(0,8).toUpperCase()} — ${selectedTicket.title.toUpperCase()}` : 'AGENTIC SUPPORT ASSISTANT' },
kb: { title: 'Knowledge Library', subtitle: 'INTERNAL DOCUMENTATION & GUIDES' },
escalation: { title: 'Escalation Centre', subtitle: 'SLA MONITORING & ESCALATION RULES' },
analytics: { title: 'Team Analytics', subtitle: 'PERFORMANCE METRICS — LAST 30 DAYS' },
infra: { title: 'Nexus Control Center', subtitle: 'INFRASTRUCTURE TOPOLOGY & AI REASONING AUDIT' },
};
return pages[activeTab] || pages.overview;
};
const { title, subtitle } = getPageInfo();
return (
<div className="admin-layout">
<SideNav
activeTab={activeTab}
onTabChange={setActiveTab}
selectedWorkspaceId={selectedWorkspaceId}
onWorkspaceChange={setSelectedWorkspaceId}
onSettingsClick={() => setShowSettings(true)}
/>
<main className="admin-main">
<TopBar
title={title}
subtitle={subtitle}
onTicketSelect={(t) => {
setSelectedTicket(t);
setActiveTab('nexus');
}}
/>
<section className="admin-content">
<AnimatePresence mode="wait">
<motion.div
key={activeTab}
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.2 }}
style={{ minHeight: '100%' }}
>
{renderContent()}
</motion.div>
</AnimatePresence>
</section>
</main>
{showSettings && (
<UserSettingsModal
user={user}
onClose={() => setShowSettings(false)}
onUpdate={refreshUser}
/>
)}
{/* Persistent AI Master Hub */}
<NexusMasterAgent workspaceId={selectedWorkspaceId} />
</div>
);
}
export default App;