Files
leocrm/frontend/src/components/layout/AISidebar.tsx
T

323 lines
14 KiB
TypeScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
import { ChatWindow } from '@/components/ai/ChatWindow';
import { ResizablePanel } from '@/components/ui/ResizablePanel';
import { SuggestionList } from '@/components/ai/SuggestionSidebar';
import { createSession, fetchSessions } from '@/api/ai';
import { useUIStore } from '@/store/uiStore';
import { useTranslation } from 'react-i18next';
import { useUsers, useGroups } from '@/api/hooks';
import { Avatar } from '@/components/ui/Avatar';
const robotIcon = (className: string) => (
<svg className={className} fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 2a4 4 0 014 4v1h1a3 3 0 013 3v6a3 3 0 01-3 3h-1v1a4 4 0 01-4 4H8a4 4 0 01-4-4v-1H3a3 3 0 01-3-3V10a3 3 0 013-3h1V6a4 4 0 014-4z M9 10h.01M15 10h.01M9 15h6" />
</svg>
);
const bellIcon = (className: string) => (
<svg className={className} fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" />
</svg>
);
const bulbIcon = (className: string) => (
<svg className={className} fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z" />
</svg>
);
const teamIcon = (className: string) => (
<svg className={className} fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6-3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
</svg>
);
const chatBubbleIcon = (className: string) => (
<svg className={className} fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
</svg>
);
const chevronRightIcon = (
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
</svg>
);
interface TabDef {
key: 'proactive' | 'chat' | 'notifications' | 'team' | 'chatroom';
label: string;
icon: (cls: string) => React.ReactNode;
testId: string;
}
function TeamPanel() {
const { data: usersData, isLoading: usersLoading } = useUsers();
const { data: groupsData, isLoading: groupsLoading } = useGroups();
const users: any[] = usersData?.items || [];
const groups: any[] = groupsData?.items || [];
return (
<div className="flex flex-col h-full overflow-y-auto p-3 gap-3" data-testid="team-panel">
<div>
<h3 className="text-xs font-semibold text-secondary-500 uppercase tracking-wide mb-2">Mitarbeiter</h3>
{usersLoading ? (
<p className="text-sm text-secondary-400">Laden...</p>
) : users.length === 0 ? (
<p className="text-sm text-secondary-400">Keine Mitarbeiter</p>
) : (
<div className="space-y-1">
{users.map((u) => (
<div key={u.id} className="flex items-center gap-2 px-2 py-1.5 rounded-lg hover:bg-secondary-50 transition-colors">
<div className="relative flex-shrink-0">
<Avatar name={u.name} size="sm" />
<span className="absolute bottom-0 right-0 w-2.5 h-2.5 rounded-full border-2 border-white bg-secondary-300" aria-label="offline" />
</div>
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-secondary-700 truncate">{u.name}</p>
<p className="text-xs text-secondary-400 truncate">{u.email}</p>
</div>
<span className="text-xs text-secondary-400">{u.role}</span>
</div>
))}
</div>
)}
</div>
<div>
<h3 className="text-xs font-semibold text-secondary-500 uppercase tracking-wide mb-2">Gruppen</h3>
{groupsLoading ? (
<p className="text-sm text-secondary-400">Laden...</p>
) : groups.length === 0 ? (
<p className="text-sm text-secondary-400">Keine Gruppen</p>
) : (
<div className="space-y-1">
{groups.map((g) => (
<div key={g.id} className="flex items-center gap-2 px-2 py-1.5 rounded-lg hover:bg-secondary-50 transition-colors">
<div className="w-8 h-8 rounded-full bg-secondary-200 flex items-center justify-center flex-shrink-0">
<svg className="w-4 h-4 text-secondary-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
</div>
<div className="flex-1 min-w-0">
<p className="text-sm font-medium text-secondary-700 truncate">{g.name}</p>
{g.description && <p className="text-xs text-secondary-400 truncate">{g.description}</p>}
</div>
</div>
))}
</div>
)}
</div>
</div>
);
}
function ChatPanel() {
return (
<div className="flex flex-col h-full" data-testid="chat-panel">
<div className="flex-1 overflow-y-auto p-3">
<p className="text-sm text-secondary-400 text-center py-8">Chat-Sidebar - Coming Soon</p>
</div>
</div>
);
}
export function AISidebar() {
const { t } = useTranslation();
const { aiSidebarCollapsed, toggleAISidebar, aiSidebarTab, setAISidebarTab, notifications, removeNotification } = useUIStore();
const [sessionId, setSessionId] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
(async () => {
try {
const sessions = await fetchSessions(true);
if (sessions.length > 0) {
setSessionId(sessions[0].id);
} else {
const session = await createSession({ is_sidebar: true, title: 'Sidebar Chat' });
setSessionId(session.id);
}
} catch (e) {
console.error('AISidebar init error:', e);
} finally {
setLoading(false);
}
})();
}, []);
const tabs: TabDef[] = [
{ key: 'chat', label: 'KI Chat', icon: robotIcon, testId: 'ai-sidebar-tab-chat' },
{ key: 'proactive', label: 'Live KI', icon: bulbIcon, testId: 'ai-sidebar-tab-proactive' },
{ key: 'notifications', label: t('topbar.notifications'), icon: bellIcon, testId: 'ai-sidebar-tab-notifications' },
{ key: 'team', label: 'Team', icon: teamIcon, testId: 'ai-sidebar-tab-team' },
{ key: 'chatroom', label: 'Chat', icon: chatBubbleIcon, testId: 'ai-sidebar-tab-chatroom' },
];
const activeTab = tabs.find((tab) => tab.key === aiSidebarTab) || tabs[0];
// Collapsed: narrow icon strip (desktop only, hidden on mobile)
if (aiSidebarCollapsed) {
return (
<div
className="hidden md:flex flex-shrink-0 w-12 flex-col items-center border-l border-secondary-200 bg-white py-3 gap-2"
data-testid="ai-sidebar-collapsed"
>
{tabs.map((tab) => (
<button
key={tab.key}
onClick={() => { setAISidebarTab(tab.key); toggleAISidebar(); }}
className="p-2 rounded-md hover:bg-secondary-100 min-h-touch min-w-touch flex items-center justify-center focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500 relative"
aria-label={tab.label}
title={tab.label}
data-testid={tab.testId}
>
{tab.icon('w-5 h-5 text-secondary-600')}
{tab.key === 'notifications' && notifications.length > 0 && (
<span className="absolute -top-0.5 -right-0.5 w-4 h-4 bg-danger-500 text-white text-[10px] font-bold rounded-full flex items-center justify-center">
{notifications.length}
</span>
)}
</button>
))}
</div>
);
}
const renderTabContent = () => {
if (aiSidebarTab === 'proactive') {
return <SuggestionList />;
}
if (aiSidebarTab === 'notifications') {
return (
<div className="flex flex-col h-full overflow-y-auto p-3 gap-2" data-testid="notification-list">
{notifications.length === 0 && (
<p className="text-sm text-secondary-400 text-center py-4">Keine Benachrichtigungen</p>
)}
{notifications.map((msg, i) => (
<div key={i} className="flex items-start justify-between gap-2 px-3 py-2 rounded-lg border border-secondary-200 bg-secondary-50 hover:border-secondary-300 hover:bg-secondary-100 transition-colors text-sm text-secondary-700 group">
<span className="flex-1 leading-snug">{msg}</span>
<button
className="p-1 rounded text-secondary-300 hover:text-danger-600 hover:bg-danger-50 opacity-0 group-hover:opacity-100 transition-opacity flex-shrink-0 mt-0.5"
aria-label="Benachrichtigung löschen"
onClick={() => removeNotification(i)}
>
<svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
))}
</div>
);
}
if (aiSidebarTab === 'team') {
return <TeamPanel />;
}
if (aiSidebarTab === 'chatroom') {
return <ChatPanel />;
}
// chat tab
if (loading) {
return (
<div className="flex items-center justify-center h-full text-sm text-secondary-400">Laden...</div>
);
}
if (sessionId) {
return <ChatWindow sessionId={sessionId} compact className="h-full" />;
}
return (
<div className="flex items-center justify-center h-full text-sm text-red-500">
Session konnte nicht erstellt werden
</div>
);
};
const iconStrip = (
<div className="flex items-center gap-1 px-2 h-[58px] border-b border-secondary-200" role="tablist">
{tabs.map((tab) => (
<button
key={tab.key}
onClick={() => setAISidebarTab(tab.key)}
className={`p-2 rounded-md min-h-touch min-w-touch flex items-center justify-center transition-colors relative ${
aiSidebarTab === tab.key
? 'bg-primary-100 text-primary-600'
: 'text-secondary-500 hover:bg-secondary-100 hover:text-secondary-700'
}`}
role="tab"
aria-selected={aiSidebarTab === tab.key}
aria-label={tab.label}
data-testid={tab.testId}
>
{tab.icon('w-5 h-5')}
{tab.key === 'notifications' && notifications.length > 0 && (
<span className="absolute -top-0.5 -right-0.5 w-4 h-4 bg-danger-500 text-white text-[10px] font-bold rounded-full flex items-center justify-center">
{notifications.length}
</span>
)}
</button>
))}
<div className="flex-1" />
<button
onClick={toggleAISidebar}
className="p-1.5 rounded-md text-secondary-400 hover:text-secondary-600 hover:bg-secondary-100 min-h-touch min-w-touch flex items-center justify-center"
title="Einklappen"
aria-label="KI Assistent einklappen"
>
{chevronRightIcon}
</button>
</div>
);
// Expanded: Mobile = full width overlay, Desktop = resizable panel
return (
<>
{/* Mobile: full width overlay with back button */}
<div
className="md:hidden fixed inset-0 z-50 bg-white flex flex-col"
data-testid="ai-sidebar-mobile"
>
<div className="h-14 flex items-center gap-2 px-3 border-b border-secondary-200">
<button
onClick={toggleAISidebar}
className="inline-flex items-center gap-1 px-2 py-1 rounded text-sm text-secondary-700 hover:bg-secondary-100 min-h-touch"
aria-label="Zurück"
data-testid="ai-sidebar-back"
>
<svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 19l-7-7 7-7" />
</svg>
<span className="text-sm font-medium">Zurück</span>
</button>
</div>
{iconStrip}
<div className="px-3 py-2 border-b border-secondary-200">
<span className="text-sm font-medium text-secondary-700">{activeTab.label}</span>
</div>
<div className="flex-1 overflow-hidden">
{renderTabContent()}
</div>
</div>
{/* Desktop: resizable panel */}
<ResizablePanel
initialWidth={320}
minWidth={240}
maxWidth={600}
handleSide="left"
className="hidden md:flex border-l border-secondary-200 bg-white"
data-testid="ai-sidebar-pane"
>
<div className="h-full flex flex-col">
{iconStrip}
<div className="px-3 py-2 border-b border-secondary-200">
<span className="text-sm font-medium text-secondary-700">{activeTab.label}</span>
</div>
<div className="flex-1 overflow-hidden">
{renderTabContent()}
</div>
</div>
</ResizablePanel>
</>
);
}