phase-1: data model, auth, permissions, masters, settings, seed

This commit is contained in:
Atharv1705
2026-11-12 12:28:55 +05:30
parent cb2f48620c
commit e58232a657
48 changed files with 5463 additions and 98 deletions
+132 -12
View File
@@ -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>
);
}