76 lines
3.7 KiB
TypeScript
76 lines
3.7 KiB
TypeScript
import type { MenuItem } from "../../components/PanelMenu";
|
||
import { useAuthStore } from "../../store/auth";
|
||
|
||
// خروج از حساب — پاککردن نشست و بازگشت به صفحهی ورود
|
||
export function useLogout() {
|
||
return () => {
|
||
useAuthStore.getState().logout();
|
||
window.location.href = "/login";
|
||
};
|
||
}
|
||
|
||
const ROLE_LABELS: Record<string, string> = {
|
||
admin: "ادمین",
|
||
procurement_manager: "مدیر تدارکات",
|
||
pm: "مدیر پروژه",
|
||
supervisor: "سرپرست پروژه",
|
||
};
|
||
|
||
// زیرعنوان پویای سربرگ پنل انبار:
|
||
// [نام کاربر] | [نقش] – [پروژه / همه پروژهها]
|
||
// سرپرست پروژه → نام پروژهی خودش؛ مدیران/ادمین → «همه پروژهها».
|
||
export function inventorySubheader(user: { username?: string; casdoor_id?: string; display_name?: string; first_name: string; last_name: string; role: string | null; role_label: string | null; project_name: string | null } | null | undefined): string {
|
||
if (!user) return "در حال بارگذاری…";
|
||
const name = `${user.first_name || ""} ${user.last_name || ""}`.trim() || user.display_name || "کاربر";
|
||
const uname = user.username || user.casdoor_id || "-";
|
||
const role = user.role_label || (user.role ? ROLE_LABELS[user.role] : "بدون نقش");
|
||
const proj = user.role === "supervisor" && user.project_name ? ` – پروژه: ${user.project_name}` : "";
|
||
return `${name} | نام کاربری: ${uname} | نقش: ${role}${proj}`;
|
||
}
|
||
|
||
// منوی همبرگریِ مختصر برای مدیر تدارکات/ادمین: بخشها بالای صفحه هستند و
|
||
// اینجا فقط «بازگشت به دفتر» و «خروج از حساب» میماند.
|
||
export function accountMenuItems(onLogout?: () => void): MenuItem[] {
|
||
const items: MenuItem[] = [{ label: "بازگشت به دفتر", to: "/" }];
|
||
if (onLogout) {
|
||
items.push({ divider: true });
|
||
items.push({ label: "خروج از حساب", onClick: onLogout });
|
||
}
|
||
return items;
|
||
}
|
||
|
||
// منوی همبرگری سرپرست پروژه در پنل انبار
|
||
export function pmMenuItems(onLogout?: () => void): MenuItem[] {
|
||
const items: MenuItem[] = [];
|
||
if (onLogout) {
|
||
items.push({ label: "خروج از حساب", onClick: onLogout });
|
||
}
|
||
return items;
|
||
}
|
||
|
||
// آیتمهای بخشهای داخلی انبار
|
||
export function panelMenuItems(pathname: string, role: string | null | undefined, onLogout?: () => void): MenuItem[] {
|
||
const isAdmin = role === "admin";
|
||
const isManager = role === "procurement_manager" || role === "pm" || isAdmin;
|
||
const isSupervisor = role === "supervisor" || isAdmin;
|
||
|
||
const items: MenuItem[] = [
|
||
{ label: "داشبورد انبار", to: "/inventory", active: pathname === "/inventory" },
|
||
];
|
||
// فقط سرپرست پروژه و ادمین میتوانند تغییر موجودی بدهند
|
||
if (isSupervisor) {
|
||
items.push({ label: "تغییر موجودی", to: "/inventory/change", active: pathname.startsWith("/inventory/change") });
|
||
}
|
||
// کنترل روزانه و انبارها و کالاهای در راه برای مدیران و سرپرست پروژه
|
||
if (isManager || isSupervisor) {
|
||
items.push({ label: "کنترل روزانه", to: "/inventory/daily", active: pathname.startsWith("/inventory/daily") });
|
||
items.push({ label: "انبارها", to: "/inventory/warehouses", active: pathname.startsWith("/inventory/warehouses") });
|
||
items.push({ label: "کالاهای در راه", to: "/inventory/arrivals", active: pathname.startsWith("/inventory/arrivals") });
|
||
}
|
||
if (onLogout) {
|
||
items.push({ divider: true });
|
||
items.push({ label: "خروج از حساب", onClick: onLogout });
|
||
}
|
||
return items;
|
||
}
|