Spaces:
Paused
Paused
dushyantsingh0 commited on
Commit ·
51de15d
1
Parent(s): ad4fe35
backend stabilization
Browse filesFiles changed (5)
1234514 unmodified lines2021222324254 unmodified lines3031323334353637import { useState } from "react";import { useAuth, useTickets, useDashboardStats, LoginCard, setPortalType, NexusLogo } from "@nexus/shared";setPortalType('customer');import { Sidebar } from "./components/Sidebar";14 unmodified linesconst { user, token, loading: authLoading, error: authError, login, logout, refreshUser } = useAuth();const { tickets, loading: ticketsLoading, createTicket } = useTickets();const { stats, loading: statsLoading } = useDashboardStats();const [active, setActive] = useState<"dashboard" | "tickets" | "kb" | "analytics" | "thread" | "workspace">("dashboard");const [showNewTicket, setShowNewTicket] = useState(false);4 unmodified linesreturn <LoginCard onLogin={login} loading={authLoading} portal="customer" error={authError} />;}// Pre-load logic: Wait for auth, tickets, and stats before revealing the portalconst isInitialLoading = (authLoading && !user) || (user && (ticketsLoading && tickets.length === 0 || statsLoading && !stats));if (isInitialLoading) {return (1234514 unmodified lines20212223242526274 unmodified lines32333435363738394041424344import { useState } from "react";import { useAuth, useTickets, useDashboardStats, useAIInsights, useServiceNarrative, LoginCard, setPortalType, NexusLogo } from "@nexus/shared";setPortalType('customer');import { Sidebar } from "./components/Sidebar";14 unmodified linesconst { user, token, loading: authLoading, error: authError, login, logout, refreshUser } = useAuth();const { tickets, loading: ticketsLoading, createTicket } = useTickets();const { stats, loading: statsLoading } = useDashboardStats();const { insights, loading: aiLoading } = useAIInsights();const { narrative, loading: narrativeLoading } = useServiceNarrative();const [active, setActive] = useState<"dashboard" | "tickets" | "kb" | "analytics" | "thread" | "workspace">("dashboard");const [showNewTicket, setShowNewTicket] = useState(false);4 unmodified linesreturn <LoginCard onLogin={login} loading={authLoading} portal="customer" error={authError} />;}// Pre-load logic: Wait for auth, tickets, stats, and AI before revealing the portalconst isInitialLoading = (authLoading && !user) || (user && ((ticketsLoading && tickets.length === 0) ||(statsLoading && !stats) ||(aiLoading && !insights) ||(narrativeLoading && !narrative)));if (isInitialLoading) {return (
12345613 unmodified lines20212223242515 unmodified lines4142434445464748import { useState, useEffect } from 'react';import { motion, AnimatePresence } from 'framer-motion';import { useAuth, useDashboardStats, LoginCard, NexusLogo } from '@nexus/shared';import { SideNav } from './components/SideNav';import type { TabId } from './components/SideNav';import { TopBar } from './components/TopBar';13 unmodified linesconst { token, user, loading: authLoading, login, refreshUser, hasRole } = useAuth();const [selectedWorkspaceId, setSelectedWorkspaceId] = useState<string | undefined>(undefined);const { stats, loading: statsLoading } = useDashboardStats(selectedWorkspaceId);const [activeTab, setActiveTab] = useState<TabId>('overview');const [selectedTicket, setSelectedTicket] = useState<any>(null);const [showSettings, setShowSettings] = useState(false);15 unmodified lines);}// Pre-load logic: We wait for auth AND for the first set of stats to arriveconst isInitialLoading = (authLoading && !user) || (user && statsLoading && !stats);if (isInitialLoading) {return (1234567891011121313 unmodified lines27282930313233343515 unmodified lines515253545556575859606162import { useState, useEffect } from 'react';import { motion, AnimatePresence } from 'framer-motion';import {useAuth,useDashboardStats,useAIInsights,useServiceNarrative,LoginCard,NexusLogo} from '@nexus/shared';import { SideNav } from './components/SideNav';import type { TabId } from './components/SideNav';import { TopBar } from './components/TopBar';13 unmodified linesconst { token, user, loading: authLoading, login, refreshUser, hasRole } = useAuth();const [selectedWorkspaceId, setSelectedWorkspaceId] = useState<string | undefined>(undefined);const { stats, loading: statsLoading } = useDashboardStats(selectedWorkspaceId);const { insights, loading: aiLoading } = useAIInsights(selectedWorkspaceId);const { narrative, loading: narrativeLoading } = useServiceNarrative(selectedWorkspaceId);const [activeTab, setActiveTab] = useState<TabId>('overview');const [selectedTicket, setSelectedTicket] = useState<any>(null);const [showSettings, setShowSettings] = useState(false);15 unmodified lines);}// Pre-load logic: We wait for auth AND for the first set of stats/AI to arriveconst isInitialLoading = (authLoading && !user) || (user && ((statsLoading && !stats) ||(aiLoading && !insights) ||(narrativeLoading && !narrative)));if (isInitialLoading) {return (
628 unmodified lines629630631632633634635628 unmodified linesreturn (<div key={i} style={{ alignSelf: isMe ? 'flex-end' : 'flex-start', maxWidth: '85%' }}><div style={{ background: isMe ? 'var(--accent)' : 'var(--bg2)', color: isMe ? '#fff' : 'var(--text1)', padding: '12px 16px', borderRadius: isMe ? '12px 12px 2px 12px' : '12px 12px 12px 2px', border: isMe ? 'none' : '1px solid var(--border)', boxShadow: '0 2px 4px rgba(0,0,0,0.05)' }}><NexusMarkdown content={msg.content} /></div><div style={{ fontSize: 10, color: 'var(--text3)', marginTop: 6, textAlign: isMe ? 'right' : 'left' }}>{msg.sender_name} • {formatTime(msg.created_at)}628 unmodified lines629630631632633634635628 unmodified linesreturn (<div key={i} style={{ alignSelf: isMe ? 'flex-end' : 'flex-start', maxWidth: '85%' }}><div style={{ background: isMe ? 'var(--accent)' : 'var(--bg2)', color: isMe ? '#fff' : 'var(--text1)', padding: '12px 16px', borderRadius: isMe ? '12px 12px 2px 12px' : '12px 12px 12px 2px', border: isMe ? 'none' : '1px solid var(--border)', boxShadow: '0 2px 4px rgba(0,0,0,0.05)' }}><NexusMarkdown content={msg.content} isMe={isMe} /></div><div style={{ fontSize: 10, color: 'var(--text3)', marginTop: 6, textAlign: isMe ? 'right' : 'left' }}>{msg.sender_name} • {formatTime(msg.created_at)}
224 unmodified lines225226227228229230231232233234235224 unmodified lines<ahref={decoratedHref}style={{color: isMe ? '#fff' : COLORS.brand,fontWeight: 600,textDecoration: 'none',borderBottom: `1px solid ${isMe ? 'rgba(255,255,255,0.3)' : COLORS.gray200}`,opacity: 1}}target={isDownload ? "_blank" : "_self"}rel={isDownload ? "noopener noreferrer" : undefined}224 unmodified lines225226227228229230231232233234235236237238224 unmodified lines<ahref={decoratedHref}style={{color: isMe ? 'inherit' : COLORS.brand,fontWeight: 600,textDecoration: 'none',borderBottom: `1px solid ${isMe ? 'rgba(255,255,255,0.4)' : COLORS.gray200}`,opacity: 1,display: isDownload ? 'inline-flex' : 'inline',alignItems: 'center',gap: '4px'}}target={isDownload ? "_blank" : "_self"}rel={isDownload ? "noopener noreferrer" : undefined}
325 unmodified lines326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410325 unmodified linesgenerated_at: string;}// Session-level cache for AI insights to prevent redundant regenerationconst insightsCache: Record<string, AIInsights> = {};const narrativeCache: Record<string, ServiceNarrative> = {};export const useAIInsights = (workspaceId?: string) => {const [insights, setInsights] = useState<AIInsights | null>(null);const [loading, setLoading] = useState(false);const [error, setError] = useState<string | null>(null);const fetchInsights = useCallback(async (force: boolean = false) => {const key = workspaceId || 'global';if (!force && insightsCache[key]) {setInsights(insightsCache[key]);return;}setLoading(true);try {let url = workspaceId ? `dashboard/ai-insights/?workspace_id=${workspaceId}` : 'dashboard/ai-insights/';if (force) {url += (url.includes('?') ? '&' : '?') + 'refresh=true';}const response = await api.get(url);insightsCache[key] = response.data;setInsights(response.data);setError(null);} catch (err: any) {setError(err.response?.data?.detail || 'Failed to fetch AI insights');} finally {setLoading(false);}}, [workspaceId]);useEffect(() => {const token = localStorage.getItem('token');if (token && !insightsCache[workspaceId || 'global']) {fetchInsights();} else if (token) {setInsights(insightsCache[workspaceId || 'global']);}}, [fetchInsights, workspaceId]);return { insights, loading, error, refresh: () => fetchInsights(true) };};export const useServiceNarrative = (workspaceId?: string) => {const [narrative, setNarrative] = useState<ServiceNarrative | null>(null);const [loading, setLoading] = useState(false);const [error, setError] = useState<string | null>(null);const fetchNarrative = useCallback(async (force: boolean = false) => {const key = workspaceId || 'global';if (!force && narrativeCache[key]) {setNarrative(narrativeCache[key]);return;}setLoading(true);try {const url = workspaceId ? `reports/insights/?workspace_id=${workspaceId}` : 'reports/insights/';const response = await api.get(url);narrativeCache[key] = response.data;setNarrative(response.data);setError(null);} catch (err: any) {setError(err.response?.data?.detail || 'Failed to fetch Service Narrative');} finally {setLoading(false);}}, [workspaceId]);useEffect(() => {const token = localStorage.getItem('token');if (token && !narrativeCache[workspaceId || 'global']) {fetchNarrative();} else if (token) {setNarrative(narrativeCache[workspaceId || 'global']);}}, [fetchNarrative, workspaceId]);return { narrative, loading, error, refresh: () => fetchNarrative(true) };};325 unmodified lines326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440325 unmodified linesgenerated_at: string;}// Global singleton state for AI insightslet globalAIInsights: AIInsights | null = null;let globalAIInsightsLoading = false;let globalAIInsightsError: string | null = null;const aiInsightsListeners = new Set<(s: AIInsights | null, l: boolean, e: string | null) => void>();const notifyAIInsights = () => {aiInsightsListeners.forEach(l => l(globalAIInsights, globalAIInsightsLoading, globalAIInsightsError));};export const useAIInsights = (workspaceId?: string) => {const [insights, setInsights] = useState<AIInsights | null>(globalAIInsights);const [loading, setLoading] = useState(globalAIInsightsLoading);const [error, setError] = useState<string | null>(globalAIInsightsError);useEffect(() => {const listener = (s: AIInsights | null, l: boolean, e: string | null) => {setInsights(s);setLoading(l);setError(e);};aiInsightsListeners.add(listener);return () => {aiInsightsListeners.delete(listener);};}, []);const fetchInsights = useCallback(async (force: boolean = false) => {if (!force && globalAIInsights) return;globalAIInsightsLoading = true;notifyAIInsights();try {let url = workspaceId ? `dashboard/ai-insights/?workspace_id=${workspaceId}` : 'dashboard/ai-insights/';if (force) {url += (url.includes('?') ? '&' : '?') + 'refresh=true';}const response = await api.get(url);globalAIInsights = response.data;globalAIInsightsError = null;} catch (err: any) {globalAIInsightsError = err.response?.data?.detail || 'Failed to fetch AI insights';} finally {globalAIInsightsLoading = false;notifyAIInsights();}}, [workspaceId]);useEffect(() => {const token = localStorage.getItem('token');if (token && !globalAIInsights) {fetchInsights();}}, [fetchInsights]);return { insights, loading, error, refresh: () => fetchInsights(true) };};// Global singleton state for Service Narrativelet globalNarrative: ServiceNarrative | null = null;let globalNarrativeLoading = false;let globalNarrativeError: string | null = null;const narrativeListeners = new Set<(s: ServiceNarrative | null, l: boolean, e: string | null) => void>();const notifyNarrative = () => {narrativeListeners.forEach(l => l(globalNarrative, globalNarrativeLoading, globalNarrativeError));};export const useServiceNarrative = (workspaceId?: string) => {const [narrative, setNarrative] = useState<ServiceNarrative | null>(globalNarrative);const [loading, setLoading] = useState(globalNarrativeLoading);const [error, setError] = useState<string | null>(globalNarrativeError);useEffect(() => {const listener = (s: ServiceNarrative | null, l: boolean, e: string | null) => {setNarrative(s);setLoading(l);setError(e);};narrativeListeners.add(listener);return () => {narrativeListeners.delete(listener);};}, []);const fetchNarrative = useCallback(async (force: boolean = false) => {if (!force && globalNarrative) return;globalNarrativeLoading = true;notifyNarrative();try {const url = workspaceId ? `reports/insights/?workspace_id=${workspaceId}` : 'reports/insights/';const response = await api.get(url);globalNarrative = response.data;globalNarrativeError = null;} catch (err: any) {globalNarrativeError = err.response?.data?.detail || 'Failed to fetch Service Narrative';} finally {globalNarrativeLoading = false;notifyNarrative();}}, [workspaceId]);useEffect(() => {const token = localStorage.getItem('token');if (token && !globalNarrative) {fetchNarrative();}}, [fetchNarrative]);return { narrative, loading, error, refresh: () => fetchNarrative(true) };};
frontend/apps/customer/src/App.tsx+10-3
frontend/apps/internal/src/App.tsx+17-3
frontend/apps/internal/src/components/TicketDetails.tsx+1-1
frontend/packages/shared/src/components/NexusMarkdown.tsx+6-3
frontend/packages/shared/src/hooks/useDashboard.ts+69-39