6c86f0f571
- integrated request-module into module registry - moved backend into modules/request-module - added manifest.json and module.py - implemented dynamic router mounting - implemented dynamic static mounting - removed direct dependencies from platform core - verified startup with request-module - verified startup without request-module - added implementation_report_003.md
204 lines
8.2 KiB
TypeScript
204 lines
8.2 KiB
TypeScript
import { useState, useEffect, useRef } from 'react'
|
||
import { Outlet, Link, useLocation, useNavigate } from 'react-router-dom'
|
||
import { useAuth } from '../contexts/AuthContext'
|
||
import { notificationApi } from '../api/client'
|
||
import {
|
||
LayoutDashboard, Ticket, Users, Settings, LogOut, Plus, Cog, Bell, Check, Trash2
|
||
} from 'lucide-react'
|
||
|
||
const navigation = [
|
||
{ name: 'Панель', href: '/', icon: LayoutDashboard },
|
||
{ name: 'Заявки', href: '/tickets', icon: Ticket },
|
||
{ name: 'Пользователи', href: '/users', icon: Users },
|
||
{ name: 'Справочники', href: '/reference', icon: Settings },
|
||
{ name: 'Шаблоны', href: '/templates', icon: Bell },
|
||
{ name: 'Настройки', href: '/settings', icon: Cog },
|
||
]
|
||
|
||
export default function Layout() {
|
||
const { user, logout } = useAuth()
|
||
const location = useLocation()
|
||
const navigate = useNavigate()
|
||
const [notifications, setNotifications] = useState<any[]>([])
|
||
const [unreadCount, setUnreadCount] = useState(0)
|
||
const [showDropdown, setShowDropdown] = useState(false)
|
||
const dropdownRef = useRef<HTMLDivElement>(null)
|
||
|
||
const loadNotifications = () => {
|
||
notificationApi.getNotifications().then(res => {
|
||
setNotifications(res.data.notifications)
|
||
setUnreadCount(res.data.unread_count)
|
||
}).catch(() => {})
|
||
}
|
||
|
||
useEffect(() => {
|
||
loadNotifications()
|
||
const interval = setInterval(loadNotifications, 30000)
|
||
return () => clearInterval(interval)
|
||
}, [])
|
||
|
||
useEffect(() => {
|
||
const handleClick = (e: MouseEvent) => {
|
||
if (dropdownRef.current && !dropdownRef.current.contains(e.target as Node)) {
|
||
setShowDropdown(false)
|
||
}
|
||
}
|
||
document.addEventListener('mousedown', handleClick)
|
||
return () => document.removeEventListener('mousedown', handleClick)
|
||
}, [])
|
||
|
||
const handleMarkRead = async (id: number) => {
|
||
await notificationApi.markAsRead(id)
|
||
loadNotifications()
|
||
}
|
||
|
||
const handleMarkAllRead = async () => {
|
||
await notificationApi.markAllAsRead()
|
||
loadNotifications()
|
||
}
|
||
|
||
const handleDelete = async (id: number) => {
|
||
await notificationApi.deleteNotification(id)
|
||
loadNotifications()
|
||
}
|
||
|
||
const handleLogout = () => {
|
||
logout()
|
||
navigate('/login')
|
||
}
|
||
|
||
return (
|
||
<div className="flex h-screen bg-gray-100">
|
||
{/* Sidebar */}
|
||
<div className="w-64 bg-white shadow-lg">
|
||
<div className="p-6">
|
||
<h1 className="text-2xl font-bold text-gray-800">Сервис Деск</h1>
|
||
<p className="text-sm text-gray-500 mt-1">Система обработки заявок</p>
|
||
</div>
|
||
|
||
<nav className="mt-6">
|
||
{navigation.map((item) => {
|
||
const isActive = location.pathname === item.href ||
|
||
(item.href !== '/' && location.pathname.startsWith(item.href))
|
||
|
||
return (
|
||
<Link
|
||
key={item.name}
|
||
to={item.href}
|
||
className={`flex items-center px-6 py-3 text-sm font-medium transition-colors ${
|
||
isActive
|
||
? 'bg-blue-50 text-blue-700 border-r-2 border-blue-700'
|
||
: 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'
|
||
}`}
|
||
>
|
||
<item.icon className="w-5 h-5 mr-3" />
|
||
{item.name}
|
||
</Link>
|
||
)
|
||
})}
|
||
</nav>
|
||
|
||
<div className="absolute bottom-0 w-64 p-4 border-t">
|
||
<div className="flex items-center mb-3">
|
||
<div className="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center text-white text-sm font-medium">
|
||
{user?.first_name?.[0]}{user?.last_name?.[0]}
|
||
</div>
|
||
<div className="ml-3">
|
||
<p className="text-sm font-medium text-gray-700">{user?.first_name} {user?.last_name}</p>
|
||
<p className="text-xs text-gray-500">{user?.email}</p>
|
||
</div>
|
||
</div>
|
||
<button
|
||
onClick={handleLogout}
|
||
className="flex items-center w-full px-3 py-2 text-sm text-gray-600 hover:bg-gray-100 rounded-lg"
|
||
>
|
||
<LogOut className="w-4 h-4 mr-2" />
|
||
Выйти
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
{/* Main content */}
|
||
<div className="flex-1 overflow-auto">
|
||
<div className="p-8">
|
||
{/* Header */}
|
||
<div className="flex items-center justify-between mb-8">
|
||
<div>
|
||
<h2 className="text-2xl font-semibold text-gray-800">
|
||
{navigation.find(n => n.href === location.pathname ||
|
||
(n.href !== '/' && location.pathname.startsWith(n.href)))?.name || 'Панель'}
|
||
</h2>
|
||
</div>
|
||
<div className="flex items-center gap-3">
|
||
{/* Notification bell */}
|
||
<div className="relative" ref={dropdownRef}>
|
||
<button
|
||
onClick={() => setShowDropdown(!showDropdown)}
|
||
className="relative p-2 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded-lg"
|
||
>
|
||
<Bell className="w-5 h-5" />
|
||
{unreadCount > 0 && (
|
||
<span className="absolute -top-1 -right-1 bg-red-500 text-white text-xs rounded-full w-5 h-5 flex items-center justify-center">
|
||
{unreadCount > 9 ? '9+' : unreadCount}
|
||
</span>
|
||
)}
|
||
</button>
|
||
|
||
{showDropdown && (
|
||
<div className="absolute right-0 mt-2 w-96 bg-white rounded-lg shadow-lg border z-50 max-h-96 overflow-hidden">
|
||
<div className="flex items-center justify-between px-4 py-3 border-b">
|
||
<span className="font-semibold text-sm">Уведомления</span>
|
||
{unreadCount > 0 && (
|
||
<button onClick={handleMarkAllRead} className="text-xs text-blue-600 hover:text-blue-800">
|
||
Прочитать все
|
||
</button>
|
||
)}
|
||
</div>
|
||
<div className="overflow-y-auto max-h-80">
|
||
{notifications.length === 0 ? (
|
||
<p className="text-gray-500 text-sm text-center py-8">Нет уведомлений</p>
|
||
) : (
|
||
notifications.map(n => (
|
||
<div key={n.id} className={`px-4 py-3 border-b hover:bg-gray-50 flex gap-3 ${n.is_read === 0 ? 'bg-blue-50/50' : ''}`}>
|
||
<div className="flex-1 min-w-0">
|
||
<p className="text-sm font-medium text-gray-900 truncate">{n.title}</p>
|
||
<p className="text-xs text-gray-500 mt-1 line-clamp-2">{n.message}</p>
|
||
<p className="text-xs text-gray-400 mt-1">
|
||
{n.created_at ? new Date(n.created_at).toLocaleString('ru-RU') : ''}
|
||
</p>
|
||
</div>
|
||
<div className="flex flex-col gap-1">
|
||
{n.is_read === 0 && (
|
||
<button onClick={() => handleMarkRead(n.id)} className="p-1 text-blue-500 hover:text-blue-700" title="Прочитано">
|
||
<Check className="w-4 h-4" />
|
||
</button>
|
||
)}
|
||
<button onClick={() => handleDelete(n.id)} className="p-1 text-gray-400 hover:text-red-500" title="Удалить">
|
||
<Trash2 className="w-4 h-4" />
|
||
</button>
|
||
</div>
|
||
</div>
|
||
))
|
||
)}
|
||
</div>
|
||
</div>
|
||
)}
|
||
</div>
|
||
|
||
<Link
|
||
to="/tickets/new"
|
||
className="flex items-center px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
|
||
>
|
||
<Plus className="w-4 h-4 mr-2" />
|
||
Новая заявка
|
||
</Link>
|
||
</div>
|
||
</div>
|
||
|
||
<Outlet />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)
|
||
}
|