Spaces:
Sleeping
Sleeping
File size: 17,387 Bytes
8314cf4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | import { ReactNode, useState, useRef, useEffect } from "react";
import { Link, useLocation } from "wouter";
import { useAuth } from "../context/auth-context";
import {
useListNotifications,
useMarkNotificationRead,
useMarkAllNotificationsRead,
getListNotificationsQueryKey,
} from "@workspace/api-client-react";
import { useQueryClient } from "@tanstack/react-query";
import {
LayoutDashboard,
CheckSquare,
Users2,
Bell,
Users,
Building2,
ShieldCheck,
X,
ExternalLink,
LogOut,
Menu,
} from "lucide-react";
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { useToast } from "@/hooks/use-toast";
// Role helpers
function isAdmin(role: string) { return role === "admin"; }
function isHeadOfTeam(role: string) { return role === "head_of_team"; }
function isTeamLead(role: string) { return role === "team_lead"; }
function isMember(role: string) { return role === "member"; }
function canManageUsers(role: string) { return isAdmin(role) || isHeadOfTeam(role) || isTeamLead(role); }
function getNavLinks(role: string) {
const roles_all = ["admin", "head_of_team", "team_lead", "member"];
const roles_privileged = ["admin", "head_of_team", "team_lead"];
const links = [
{ href: "/", label: "لوحة التحكم", icon: LayoutDashboard, roles: roles_all },
{ href: "/tasks", label: "المهام", icon: CheckSquare, roles: roles_all },
{ href: "/clients", label: "العملاء", icon: Building2, roles: roles_privileged },
{ href: "/notifications", label: "الإشعارات", icon: Bell, roles: roles_all },
{ href: "/users", label: "الأعضاء", icon: Users, roles: roles_privileged },
{ href: "/teams", label: "الفرق", icon: Users2, roles: ["admin"] },
{ href: "/admin", label: "الإدارة", icon: ShieldCheck, roles: ["admin"] },
];
return links.filter(l => l.roles.includes(role));
}
function getMobileTabs(role: string) {
const roles_all = ["admin", "head_of_team", "team_lead", "member"];
const roles_privileged = ["admin", "head_of_team", "team_lead"];
const all = [
{ href: "/", label: "الرئيسية", icon: LayoutDashboard, roles: roles_all },
{ href: "/tasks", label: "المهام", icon: CheckSquare, roles: roles_all },
{ href: "/clients", label: "العملاء", icon: Building2, roles: roles_privileged },
{ href: "/users", label: "الأعضاء", icon: Users, roles: roles_privileged },
{ href: "/admin", label: "الإدارة", icon: ShieldCheck, roles: ["admin"] },
];
return all.filter(l => l.roles.includes(role)).slice(0, 5);
}
function NotificationsDropdown({ userId }: { userId: number }) {
const [open, setOpen] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const queryClient = useQueryClient();
const { toast } = useToast();
const { data: notifications } = useListNotifications(
{ userId, unreadOnly: false },
{ query: { enabled: !!userId, queryKey: getListNotificationsQueryKey({ userId, unreadOnly: false }) } }
);
const { data: unreadNotifs } = useListNotifications(
{ userId, unreadOnly: true },
{ query: { enabled: !!userId, queryKey: getListNotificationsQueryKey({ userId, unreadOnly: true }) } }
);
const markRead = useMarkNotificationRead();
const markAllRead = useMarkAllNotificationsRead();
const unreadCount = unreadNotifs?.length ?? 0;
const recent = notifications?.slice(0, 12) ?? [];
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
};
document.addEventListener("mousedown", handleClickOutside);
return () => document.removeEventListener("mousedown", handleClickOutside);
}, []);
useEffect(() => {
const interval = setInterval(() => {
queryClient.invalidateQueries({ queryKey: getListNotificationsQueryKey({ userId, unreadOnly: true }) });
}, 60_000);
return () => clearInterval(interval);
}, [queryClient, userId]);
const handleMarkRead = (id: number) => {
markRead.mutate({ id }, {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: getListNotificationsQueryKey({ userId, unreadOnly: true }) });
queryClient.invalidateQueries({ queryKey: getListNotificationsQueryKey({ userId, unreadOnly: false }) });
},
});
};
const handleMarkAllRead = () => {
markAllRead.mutate({ data: { userId } }, {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: getListNotificationsQueryKey({ userId, unreadOnly: true }) });
queryClient.invalidateQueries({ queryKey: getListNotificationsQueryKey({ userId, unreadOnly: false }) });
toast({ title: "تم تعليم جميع الإشعارات كمقروءة" });
setOpen(false);
},
});
};
const notifTypeIcon: Record<string, string> = {
task_assigned: "📋",
status_changed: "🔄",
new_comment: "💬",
comment_added: "💬",
deadline_soon: "⏰",
};
return (
<div className="relative" ref={ref}>
<button
onClick={() => setOpen(o => !o)}
className="relative p-2 rounded-lg hover:bg-slate-100 transition-colors"
aria-label="الإشعارات"
>
<Bell className="h-5 w-5 text-slate-600" />
{unreadCount > 0 && (
<span className="absolute -top-0.5 -right-0.5 bg-red-500 text-white text-[10px] font-bold rounded-full w-4 h-4 flex items-center justify-center">
{unreadCount > 9 ? "9+" : unreadCount}
</span>
)}
</button>
{open && (
<div className="absolute left-0 top-full mt-2 w-96 max-w-[calc(100vw-2rem)] bg-white border rounded-xl shadow-2xl z-50 overflow-hidden">
<div className="flex items-center justify-between px-4 py-3 border-b bg-slate-50">
<div className="flex items-center gap-2">
<Bell className="h-4 w-4 text-slate-500" />
<span className="font-semibold text-sm">الإشعارات</span>
{unreadCount > 0 && (
<Badge className="bg-red-100 text-red-700 text-[10px] px-1.5 py-0 hover:bg-red-100">{unreadCount} جديد</Badge>
)}
</div>
<div className="flex items-center gap-1">
{unreadCount > 0 && (
<button onClick={handleMarkAllRead} className="text-xs text-primary hover:underline font-medium">
تعليم الكل كمقروء
</button>
)}
<button onClick={() => setOpen(false)} className="p-1 rounded hover:bg-slate-200 transition-colors ml-1">
<X className="h-3.5 w-3.5 text-slate-500" />
</button>
</div>
</div>
<div className="max-h-80 overflow-y-auto">
{recent.length === 0 ? (
<div className="flex flex-col items-center justify-center py-10 text-center">
<div className="text-4xl mb-2">✅</div>
<p className="text-sm font-medium text-slate-600">أنت محدّث!</p>
<p className="text-xs text-slate-400 mt-1">لا توجد إشعارات جديدة</p>
</div>
) : (
recent.map(n => (
<div
key={n.id}
className={`flex items-start gap-3 px-4 py-3 border-b last:border-0 hover:bg-slate-50 transition-colors ${!n.isRead ? "bg-blue-50/40" : ""}`}
>
<span className="text-lg flex-shrink-0 mt-0.5">{notifTypeIcon[n.type] ?? "🔔"}</span>
<div className="flex-1 min-w-0">
<p className={`text-sm leading-snug ${!n.isRead ? "font-medium text-slate-800" : "text-slate-600"}`}>
{n.message}
</p>
<p className="text-[10px] text-slate-400 mt-1">
{new Date(n.createdAt).toLocaleDateString("ar", { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" })}
</p>
</div>
<div className="flex items-center gap-1 flex-shrink-0">
{n.taskId && (
<Link href={`/tasks/${n.taskId}`} onClick={() => setOpen(false)}>
<button className="p-1 rounded hover:bg-slate-200 transition-colors" title="عرض المهمة">
<ExternalLink className="h-3 w-3 text-slate-400" />
</button>
</Link>
)}
{!n.isRead && (
<button
onClick={() => handleMarkRead(n.id)}
className="w-2 h-2 rounded-full bg-blue-500 hover:bg-blue-700 transition-colors flex-shrink-0 mt-1"
title="تعليم كمقروء"
/>
)}
</div>
</div>
))
)}
</div>
<div className="border-t px-4 py-2 bg-slate-50">
<Link href="/notifications" onClick={() => setOpen(false)}>
<button className="text-xs text-primary hover:underline w-full text-center">
عرض جميع الإشعارات
</button>
</Link>
</div>
</div>
)}
</div>
);
}
function RoleBadge({ role }: { role: string }) {
const labels: Record<string, { label: string; cls: string }> = {
admin: { label: "مدير النظام", cls: "bg-red-100 text-red-700" },
head_of_team: { label: "رئيس الفرق", cls: "bg-orange-100 text-orange-700" },
team_lead: { label: "قائد فريق", cls: "bg-blue-100 text-blue-700" },
member: { label: "عضو", cls: "bg-slate-100 text-slate-600" },
};
const r = labels[role] ?? { label: role, cls: "bg-slate-100 text-slate-500" };
return (
<span className={`text-[9px] font-semibold px-1.5 py-0.5 rounded-full ${r.cls}`}>{r.label}</span>
);
}
function Header({ onMenuToggle }: { onMenuToggle: () => void }) {
const { user, logout } = useAuth();
return (
<header className="h-16 border-b bg-white flex items-center justify-between px-4 md:px-6 flex-shrink-0">
<div className="flex items-center gap-3">
<button
onClick={onMenuToggle}
className="md:hidden p-2 rounded-lg hover:bg-slate-100 transition-colors"
aria-label="القائمة"
>
<Menu className="h-5 w-5 text-slate-600" />
</button>
<h1 className="font-bold text-xl text-primary tracking-tight">TeamTasker</h1>
<Badge variant="outline" className="text-xs bg-slate-50 text-slate-500 font-normal hidden sm:flex">Command Center</Badge>
</div>
<div className="flex items-center gap-2 md:gap-3">
{user && <NotificationsDropdown userId={user.id} />}
{user && (
<div className="flex items-center gap-2">
<div className="hidden md:flex items-center gap-2">
<Avatar className="h-8 w-8 border border-slate-200">
<AvatarFallback style={{ backgroundColor: user.avatarColor || "#ccc", color: "#fff", fontSize: "12px" }}>
{user.name.substring(0, 2).toUpperCase()}
</AvatarFallback>
</Avatar>
<div className="hidden lg:flex flex-col gap-0.5">
<span className="text-sm font-medium text-slate-800 leading-none">{user.name}</span>
<div className="flex items-center gap-1">
<span className="text-xs text-slate-500">{user.team}</span>
<RoleBadge role={user.role} />
</div>
</div>
</div>
<Button
variant="ghost"
size="sm"
onClick={logout}
className="text-slate-500 hover:text-red-600 hover:bg-red-50 gap-1.5"
title="تسجيل الخروج"
>
<LogOut className="h-4 w-4" />
<span className="hidden md:inline text-sm">خروج</span>
</Button>
</div>
)}
</div>
</header>
);
}
function Sidebar({ open, onClose }: { open: boolean; onClose: () => void }) {
const [location] = useLocation();
const { user, logout } = useAuth();
const navLinks = user ? getNavLinks(user.role) : [];
return (
<>
{open && (
<div className="fixed inset-0 bg-black/30 z-20 md:hidden" onClick={onClose} />
)}
<aside
className={`
fixed md:relative z-30 md:z-auto
top-0 md:top-auto right-0 md:right-auto
h-full md:h-auto
w-64 border-r bg-slate-50/95 md:bg-slate-50/50 flex flex-col flex-shrink-0
transition-transform duration-300 md:translate-x-0
${open ? "translate-x-0" : "translate-x-full md:translate-x-0"}
`}
>
<div className="flex items-center justify-between p-4 border-b md:hidden">
<span className="font-bold text-primary">TeamTasker</span>
<button onClick={onClose} className="p-1 rounded hover:bg-slate-200">
<X className="h-4 w-4 text-slate-500" />
</button>
</div>
{user && (
<div className="p-4 border-b hidden md:block">
<div className="flex items-center gap-3">
<Avatar className="h-10 w-10 border border-slate-200 shadow-sm">
<AvatarFallback style={{ backgroundColor: user.avatarColor || "#ccc", color: "#fff" }}>
{user.name.substring(0, 2).toUpperCase()}
</AvatarFallback>
</Avatar>
<div className="flex flex-col min-w-0 gap-0.5">
<span className="text-sm font-medium truncate">{user.name}</span>
<span className="text-xs text-slate-500 truncate">{user.team}</span>
<RoleBadge role={user.role} />
</div>
</div>
</div>
)}
<div className="p-4 flex-1 overflow-y-auto">
<nav className="space-y-1">
{navLinks.map(link => {
const Icon = link.icon;
const isActive = location === link.href || (link.href !== "/" && location.startsWith(link.href));
return (
<Link
key={link.href}
href={link.href}
onClick={onClose}
className={`flex items-center gap-3 px-3 py-2.5 rounded-md text-sm font-medium transition-colors ${
isActive ? "bg-primary/10 text-primary" : "text-slate-600 hover:bg-slate-100 hover:text-slate-900"
}`}
>
<Icon className={`h-4 w-4 ${isActive ? "text-primary" : "text-slate-400"}`} />
{link.label}
</Link>
);
})}
</nav>
</div>
<div className="p-4 border-t">
<button
onClick={() => { logout(); onClose(); }}
className="flex items-center gap-2 w-full px-3 py-2 text-sm text-red-600 hover:bg-red-50 rounded-md transition-colors"
>
<LogOut className="h-4 w-4" />
تسجيل الخروج
</button>
</div>
</aside>
</>
);
}
function MobileBottomBar() {
const [location] = useLocation();
const { user } = useAuth();
const tabs = user ? getMobileTabs(user.role) : [];
return (
<nav className="md:hidden fixed bottom-0 left-0 right-0 z-20 bg-white border-t shadow-lg">
<div className="flex items-center justify-around h-16 px-2">
{tabs.map(tab => {
const Icon = tab.icon;
const isActive = location === tab.href || (tab.href !== "/" && location.startsWith(tab.href));
return (
<Link
key={tab.href}
href={tab.href}
className={`flex flex-col items-center gap-0.5 px-3 py-2 rounded-lg transition-colors ${
isActive ? "text-primary" : "text-slate-400"
}`}
>
<Icon className="h-5 w-5" />
<span className="text-[9px] font-medium">{tab.label}</span>
</Link>
);
})}
</div>
</nav>
);
}
export function AppLayout({ children }: { children: ReactNode }) {
const [sidebarOpen, setSidebarOpen] = useState(false);
return (
<div className="min-h-screen flex flex-col bg-background text-foreground h-screen overflow-hidden" dir="rtl">
<Header onMenuToggle={() => setSidebarOpen(o => !o)} />
<div className="flex flex-1 overflow-hidden">
<Sidebar open={sidebarOpen} onClose={() => setSidebarOpen(false)} />
<main className="flex-1 overflow-auto bg-slate-50/30 pb-16 md:pb-0">
{children}
</main>
</div>
<MobileBottomBar />
</div>
);
}
// Export role helpers for use in other components
export { isAdmin, isHeadOfTeam, isTeamLead, isMember, canManageUsers };
|