Update Layout.tsx

This commit is contained in:
2025-12-11 22:27:46 +01:00
committed by GitHub
parent 669f55a14d
commit ffebddaf33

View File

@@ -8,7 +8,10 @@ import { Condo, Notice, AppSettings } from '../types';
export const Layout: React.FC = () => {
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
const user = CondoService.getCurrentUser();
const isAdmin = user?.role === 'admin' || user?.role === 'poweruser';
// Logic: "isPrivileged" includes Admin AND PowerUser.
// This allows PowerUsers to see Reports and other admin-like features.
const isPrivileged = user?.role === 'admin' || user?.role === 'poweruser';
const [condos, setCondos] = useState<Condo[]>([]);
const [activeCondo, setActiveCondo] = useState<Condo | undefined>(undefined);
@@ -28,10 +31,10 @@ export const Layout: React.FC = () => {
const globalSettings = await CondoService.getSettings();
setSettings(globalSettings);
if (isAdmin && globalSettings.features.multiCondo) {
if (isPrivileged && globalSettings.features.multiCondo) {
const list = await CondoService.getCondos();
setCondos(list);
} else if (isAdmin) {
} else if (isPrivileged) {
const list = await CondoService.getCondos();
setCondos(list);
}
@@ -55,18 +58,13 @@ export const Layout: React.FC = () => {
const isTicketNew = ticketDate > lastViewedTickets;
const isArchived = t.status === 'RESOLVED' || t.status === 'CLOSED';
if (isAdmin) {
// Admin: Count new unarchived tickets OR tickets with new comments from users
if (isPrivileged) {
// Admin/PowerUser: Count new unarchived tickets OR tickets with new comments from users
if (isTicketNew && !isArchived) {
count++;
} else {
// Check for new comments from users
// Optimization: In a real app we'd need a lighter query.
// Here we iterate because we have the data or fetch lightly.
// Assuming getTickets includes basic info or we need to check updatedAt
const updatedDate = new Date(t.updatedAt).getTime();
if (updatedDate > lastViewedTickets) {
// Deep check: fetch comments only if recently updated
const comments = await CondoService.getTicketComments(t.id);
const hasNewUserReply = comments.some(c => new Date(c.createdAt).getTime() > lastViewedTickets && c.userId !== user?.id);
if (hasNewUserReply) count++;
@@ -87,8 +85,8 @@ export const Layout: React.FC = () => {
} catch(e) { console.error("Error calc ticket badges", e); }
// Check for notices & expenses for User
if (!isAdmin && active && user) {
// Check for notices & expenses for User (non-privileged mostly, but logic works for all if needed)
if (!isPrivileged && active && user) {
try {
// 2. Check Notices
const unread = await CondoService.getUnreadNoticesForUser(user.id, active.id);
@@ -122,7 +120,7 @@ export const Layout: React.FC = () => {
window.removeEventListener('expenses-viewed', handleUpdate);
window.removeEventListener('tickets-viewed', handleUpdate);
};
}, [isAdmin]);
}, [isPrivileged]);
const handleCondoSwitch = (condoId: string) => {
CondoService.setActiveCondo(condoId);
@@ -229,8 +227,8 @@ export const Layout: React.FC = () => {
<h1 className="font-bold text-xl text-slate-800 tracking-tight">CondoPay</h1>
</div>
{/* Condo Switcher (Admin Only & MultiCondo Enabled) */}
{isAdmin && settings?.features.multiCondo && (
{/* Condo Switcher (Privileged Only & MultiCondo Enabled) */}
{isPrivileged && settings?.features.multiCondo && (
<div className="relative mt-2">
<div className="flex items-center gap-1.5 mb-2 text-xs font-bold text-slate-400 uppercase tracking-wider">
<LayoutDashboard className="w-3 h-3" />
@@ -260,8 +258,8 @@ export const Layout: React.FC = () => {
)}
</div>
)}
{/* Static info if not multi-condo or not admin */}
{(!isAdmin || (isAdmin && !settings?.features.multiCondo)) && activeCondo && (
{/* Static info if not multi-condo or not privileged */}
{(!isPrivileged || (isPrivileged && !settings?.features.multiCondo)) && activeCondo && (
<div className="px-3 py-2 bg-slate-50 border border-slate-100 rounded-lg text-sm text-slate-500 truncate">
{activeCondo.name}
</div>
@@ -275,7 +273,7 @@ export const Layout: React.FC = () => {
</div>
{/* Mobile Condo Switcher */}
{isAdmin && settings?.features.multiCondo && (
{isPrivileged && settings?.features.multiCondo && (
<div className="lg:hidden px-4 py-2 border-b border-slate-100">
<p className="text-xs font-semibold text-slate-400 uppercase tracking-wider mb-2 flex items-center gap-2">
<LayoutDashboard className="w-3 h-3" />
@@ -301,8 +299,8 @@ export const Layout: React.FC = () => {
<span className="font-medium">Famiglie</span>
</NavLink>
{/* Condo Financials - Admin or Enabled User */}
{(isAdmin || settings?.features.condoFinancialsView) && (
{/* Condo Financials - Admin/PowerUser or Enabled User */}
{(isPrivileged || settings?.features.condoFinancialsView) && (
<NavLink to="/financials" className={navClass} onClick={closeMenu}>
<ReceiptEuro className="w-5 h-5" />
<span className="font-medium">Spese Condominio</span>
@@ -315,7 +313,7 @@ export const Layout: React.FC = () => {
<div className="flex items-center justify-between w-full">
<div className="flex items-center gap-3">
<Briefcase className="w-5 h-5" />
<span className="font-medium">{isAdmin ? 'Spese Straordinarie' : 'Le Mie Spese Extra'}</span>
<span className="font-medium">{isPrivileged ? 'Spese Straordinarie' : 'Le Mie Spese Extra'}</span>
</div>
{newExpensesCount > 0 && (
<span className="bg-red-500 text-white text-[10px] font-bold px-1.5 h-5 min-w-[20px] rounded-full flex items-center justify-center shadow-sm">
@@ -326,8 +324,8 @@ export const Layout: React.FC = () => {
</NavLink>
)}
{/* Privileged Links */}
{isAdmin && settings?.features.reports && (
{/* Privileged Links (Admin & PowerUser) */}
{isPrivileged && settings?.features.reports && (
<NavLink to="/reports" className={navClass} onClick={closeMenu}>
<PieChart className="w-5 h-5" />
<span className="font-medium">Reportistica</span>