phase-1: data model, auth, permissions, masters, settings, seed
This commit is contained in:
@@ -11,27 +11,79 @@ import {
|
||||
Building2,
|
||||
BarChart3,
|
||||
Bell,
|
||||
Database,
|
||||
Settings,
|
||||
ChevronRight,
|
||||
type LucideIcon,
|
||||
} from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { can, type Capability } from "@/lib/permissions";
|
||||
import { useAuthStore } from "@/hooks/useAuth";
|
||||
|
||||
const NAV_ITEMS = [
|
||||
{ to: "/", label: "Dashboard", icon: LayoutDashboard, phase: 6 },
|
||||
{ to: "/assets", label: "Assets", icon: Package, phase: 2 },
|
||||
{ to: "/complaints", label: "Complaints", icon: AlertCircle, phase: 3 },
|
||||
{ to: "/pm", label: "Preventive Maintenance", icon: Wrench, phase: 4 },
|
||||
{ to: "/calibration", label: "Calibration", icon: FlaskConical, phase: 4 },
|
||||
{ to: "/contracts", label: "Contracts & Warranty", icon: FileText, phase: 5 },
|
||||
{ to: "/insurance", label: "Insurance", icon: Shield, phase: 5 },
|
||||
{ to: "/demo-equipment", label: "Demo Equipment", icon: Truck, phase: 5 },
|
||||
{ to: "/visits", label: "Department Visits", icon: Building2, phase: 5 },
|
||||
{ to: "/reports", label: "Reports", icon: BarChart3, phase: 7 },
|
||||
{ to: "/notifications", label: "Notifications", icon: Bell, phase: 6 },
|
||||
{ to: "/settings", label: "Settings", icon: Settings, phase: 1, adminOnly: true },
|
||||
interface NavItem {
|
||||
to: string;
|
||||
label: string;
|
||||
icon: LucideIcon;
|
||||
capability?: Capability;
|
||||
anyCapabilities?: Capability[];
|
||||
}
|
||||
|
||||
const NAV_ITEMS: NavItem[] = [
|
||||
{ to: "/", label: "Dashboard", icon: LayoutDashboard },
|
||||
{ to: "/assets", label: "Assets", icon: Package, capability: "view_assets" },
|
||||
{ to: "/complaints", label: "Complaints", icon: AlertCircle },
|
||||
{ to: "/pm", label: "Preventive Maintenance", icon: Wrench, capability: "view_pm" },
|
||||
{
|
||||
to: "/calibration",
|
||||
label: "Calibration",
|
||||
icon: FlaskConical,
|
||||
capability: "view_calibration_contracts",
|
||||
},
|
||||
{
|
||||
to: "/contracts",
|
||||
label: "Contracts & Warranty",
|
||||
icon: FileText,
|
||||
capability: "view_calibration_contracts",
|
||||
},
|
||||
{
|
||||
to: "/insurance",
|
||||
label: "Insurance",
|
||||
icon: Shield,
|
||||
capability: "view_calibration_contracts",
|
||||
},
|
||||
{
|
||||
to: "/demo-equipment",
|
||||
label: "Demo Equipment",
|
||||
icon: Truck,
|
||||
capability: "view_demo_equipment",
|
||||
},
|
||||
{
|
||||
to: "/visits",
|
||||
label: "Department Visits",
|
||||
icon: Building2,
|
||||
capability: "view_visits",
|
||||
},
|
||||
{
|
||||
to: "/reports",
|
||||
label: "Reports",
|
||||
icon: BarChart3,
|
||||
anyCapabilities: ["view_all_reports", "view_dept_reports"],
|
||||
},
|
||||
{ to: "/notifications", label: "Notifications", icon: Bell },
|
||||
{ to: "/masters", label: "Masters", icon: Database, capability: "manage_admin" },
|
||||
{ to: "/settings", label: "Settings", icon: Settings, capability: "manage_admin" },
|
||||
];
|
||||
|
||||
export function Sidebar() {
|
||||
const user = useAuthStore((s) => s.user);
|
||||
const role = user?.role;
|
||||
|
||||
const visibleItems = NAV_ITEMS.filter((item) => {
|
||||
if (item.capability) return can(role, item.capability);
|
||||
if (item.anyCapabilities) return item.anyCapabilities.some((c) => can(role, c));
|
||||
return true;
|
||||
});
|
||||
|
||||
return (
|
||||
<aside className="w-56 lg:w-64 shrink-0 min-h-screen bg-[#1e3a8a] text-white flex flex-col">
|
||||
{/* Logo */}
|
||||
@@ -50,7 +102,7 @@ export function Sidebar() {
|
||||
|
||||
{/* Navigation */}
|
||||
<nav className="flex-1 px-2 py-4 space-y-0.5 overflow-y-auto">
|
||||
{NAV_ITEMS.map(({ to, label, icon: Icon }) => (
|
||||
{visibleItems.map(({ to, label, icon: Icon }) => (
|
||||
<NavLink
|
||||
key={to}
|
||||
to={to}
|
||||
@@ -73,7 +125,7 @@ export function Sidebar() {
|
||||
|
||||
{/* Footer */}
|
||||
<div className="px-4 py-3 border-t border-blue-700 text-xs text-blue-300">
|
||||
Phase 0 — Scaffold
|
||||
Phase 1 — Foundation
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
|
||||
@@ -1,18 +1,58 @@
|
||||
import { Search, Bell, ChevronDown, User } from "lucide-react";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useQueryClient } from "@tanstack/react-query";
|
||||
import { Search, Bell, ChevronDown, User, LogOut, CalendarClock, RotateCcw } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { formatDate } from "@/lib/format";
|
||||
import { ROLE_LABELS } from "@/lib/permissions";
|
||||
import { useAuthStore } from "@/hooks/useAuth";
|
||||
import { settingsApi } from "@/api/settings";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
|
||||
interface TopBarProps {
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export function TopBar({ title }: TopBarProps) {
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
const { user, demoAsOfDate, setDemoAsOfDate, logout } = useAuthStore();
|
||||
|
||||
const [menuOpen, setMenuOpen] = useState(false);
|
||||
const [dateInput, setDateInput] = useState(demoAsOfDate ?? "");
|
||||
const [savingDate, setSavingDate] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
setDateInput(demoAsOfDate ?? "");
|
||||
}, [demoAsOfDate]);
|
||||
|
||||
const isAdmin = user?.role === "system_admin";
|
||||
|
||||
async function handleDemoDateUpdate(newDate: string | null) {
|
||||
setSavingDate(true);
|
||||
try {
|
||||
const res = await settingsApi.setDemoDate(newDate);
|
||||
setDemoAsOfDate(res.demo_as_of_date ?? null);
|
||||
setDateInput(res.demo_as_of_date ?? "");
|
||||
await queryClient.invalidateQueries();
|
||||
} finally {
|
||||
setSavingDate(false);
|
||||
}
|
||||
}
|
||||
|
||||
function handleSignOut() {
|
||||
setMenuOpen(false);
|
||||
logout();
|
||||
navigate("/login", { replace: true });
|
||||
}
|
||||
|
||||
return (
|
||||
<header className="h-14 bg-white border-b border-gray-200 flex items-center px-4 gap-4 sticky top-0 z-10">
|
||||
<header className="min-h-14 bg-white border-b border-gray-200 flex items-center flex-wrap px-4 py-2 gap-3 sticky top-0 z-10">
|
||||
{/* Page title */}
|
||||
{title && <h1 className="font-semibold text-gray-800 text-sm truncate mr-auto">{title}</h1>}
|
||||
|
||||
{/* Global search placeholder */}
|
||||
<div className={cn("relative ml-auto", title ? "" : "flex-1 max-w-md")}>
|
||||
<div className={cn("relative", title ? "" : "flex-1 max-w-xs sm:max-w-sm")}>
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400" />
|
||||
<input
|
||||
type="text"
|
||||
@@ -21,20 +61,100 @@ export function TopBar({ title }: TopBarProps) {
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Active Demo Date Pill */}
|
||||
{demoAsOfDate && (
|
||||
<span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-amber-100 text-amber-800 border border-amber-300">
|
||||
<CalendarClock className="h-3.5 w-3.5" />
|
||||
Demo Date: {formatDate(demoAsOfDate)}
|
||||
</span>
|
||||
)}
|
||||
|
||||
{/* Admin Demo Clock Picker */}
|
||||
{isAdmin && (
|
||||
<div className="flex items-center gap-1.5 bg-gray-50 border border-gray-200 rounded-md px-2 py-1 text-xs">
|
||||
<span className="text-gray-500 font-medium hidden md:inline">As of Date:</span>
|
||||
<input
|
||||
type="date"
|
||||
aria-label="As of Date"
|
||||
value={dateInput}
|
||||
onChange={(e) => setDateInput(e.target.value)}
|
||||
className="border border-gray-200 rounded px-1.5 py-0.5 text-xs bg-white focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
disabled={savingDate || !dateInput}
|
||||
onClick={() => handleDemoDateUpdate(dateInput || null)}
|
||||
className="px-2 py-0.5 rounded bg-[#1e3a8a] text-white hover:bg-[#1e3480] disabled:opacity-40"
|
||||
>
|
||||
Apply
|
||||
</button>
|
||||
{demoAsOfDate && (
|
||||
<button
|
||||
type="button"
|
||||
disabled={savingDate}
|
||||
onClick={() => handleDemoDateUpdate(null)}
|
||||
title="Reset to Today"
|
||||
className="inline-flex items-center gap-1 px-1.5 py-0.5 rounded border border-gray-300 bg-white text-gray-600 hover:bg-gray-100 disabled:opacity-40"
|
||||
>
|
||||
<RotateCcw className="h-3 w-3" />
|
||||
<span className="hidden lg:inline">Reset</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Notification bell placeholder */}
|
||||
<button className="relative p-2 rounded-md hover:bg-gray-100 text-gray-500">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => navigate("/notifications")}
|
||||
className="relative p-2 rounded-md hover:bg-gray-100 text-gray-500 ml-auto"
|
||||
aria-label="Notifications"
|
||||
>
|
||||
<Bell className="h-5 w-5" />
|
||||
<span className="absolute top-1 right-1 h-2 w-2 rounded-full bg-red-500" />
|
||||
</button>
|
||||
|
||||
{/* User menu placeholder */}
|
||||
<button className="flex items-center gap-2 px-3 py-1.5 rounded-md hover:bg-gray-100 text-sm text-gray-700">
|
||||
<div className="h-7 w-7 rounded-full bg-[#1e3a8a] flex items-center justify-center text-white text-xs font-bold">
|
||||
<User className="h-4 w-4" />
|
||||
</div>
|
||||
<span className="hidden sm:block">Admin</span>
|
||||
<ChevronDown className="h-4 w-4 text-gray-400" />
|
||||
</button>
|
||||
{/* User menu */}
|
||||
<div className="relative">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setMenuOpen((o) => !o)}
|
||||
className="flex items-center gap-2 px-2.5 py-1.5 rounded-md hover:bg-gray-100 text-sm text-gray-700"
|
||||
>
|
||||
<div className="h-7 w-7 rounded-full bg-[#1e3a8a] flex items-center justify-center text-white text-xs font-bold shrink-0">
|
||||
<User className="h-4 w-4" />
|
||||
</div>
|
||||
<div className="hidden sm:flex flex-col items-start leading-tight">
|
||||
<span className="font-medium text-gray-800 text-xs">{user?.name ?? "User"}</span>
|
||||
<span className="text-[10px] text-gray-500">
|
||||
{user?.role ? ROLE_LABELS[user.role] : ""}
|
||||
{user?.department_name ? ` • ${user.department_name}` : ""}
|
||||
</span>
|
||||
</div>
|
||||
<ChevronDown className="h-4 w-4 text-gray-400" />
|
||||
</button>
|
||||
|
||||
{menuOpen && (
|
||||
<div className="absolute right-0 mt-1 w-60 bg-white border border-gray-200 rounded-md shadow-lg z-30 py-2">
|
||||
<div className="px-3 py-2 border-b border-gray-100 space-y-1">
|
||||
<div className="text-sm font-medium text-gray-900 truncate">{user?.name}</div>
|
||||
<div className="text-xs text-gray-500 truncate">{user?.email}</div>
|
||||
<div className="flex items-center gap-1.5 flex-wrap pt-0.5">
|
||||
{user?.role && <Badge variant="info">{ROLE_LABELS[user.role]}</Badge>}
|
||||
{user?.department_name && <Badge variant="outline">{user.department_name}</Badge>}
|
||||
</div>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleSignOut}
|
||||
className="w-full flex items-center gap-2 px-3 py-2 text-sm text-red-600 hover:bg-red-50 text-left"
|
||||
>
|
||||
<LogOut className="h-4 w-4" />
|
||||
Sign Out
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</header>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
||||
import { X } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface ModalProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
title: string;
|
||||
description?: string;
|
||||
children: React.ReactNode;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function Modal({
|
||||
open,
|
||||
onOpenChange,
|
||||
title,
|
||||
description,
|
||||
children,
|
||||
className,
|
||||
}: ModalProps) {
|
||||
return (
|
||||
<DialogPrimitive.Root open={open} onOpenChange={onOpenChange}>
|
||||
<DialogPrimitive.Portal>
|
||||
<DialogPrimitive.Overlay className="fixed inset-0 z-40 bg-black/40 backdrop-blur-[1px]" />
|
||||
<DialogPrimitive.Content
|
||||
className={cn(
|
||||
"fixed left-1/2 top-1/2 z-50 w-full max-w-lg -translate-x-1/2 -translate-y-1/2 rounded-lg bg-white p-6 shadow-xl border border-gray-200 max-h-[90vh] overflow-y-auto",
|
||||
className
|
||||
)}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-4 mb-4">
|
||||
<div>
|
||||
<DialogPrimitive.Title className="text-lg font-semibold text-gray-900">
|
||||
{title}
|
||||
</DialogPrimitive.Title>
|
||||
{description && (
|
||||
<DialogPrimitive.Description className="text-sm text-gray-500 mt-0.5">
|
||||
{description}
|
||||
</DialogPrimitive.Description>
|
||||
)}
|
||||
</div>
|
||||
<DialogPrimitive.Close
|
||||
aria-label="Close"
|
||||
className="rounded p-1 text-gray-400 hover:bg-gray-100 hover:text-gray-600"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</DialogPrimitive.Close>
|
||||
</div>
|
||||
{children}
|
||||
</DialogPrimitive.Content>
|
||||
</DialogPrimitive.Portal>
|
||||
</DialogPrimitive.Root>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user