import SealCard from "./SealCard"; import LetterTemplatesTable from "./LetterTemplates/LetterTemplatesTable"; import RecentActivitiesCard from "./RecentActivitiesCard"; import { CommonRemarks } from "./CommonRemarks"; import { useAuth } from "@/shared/context/AuthContext"; import { useUnit } from "@/user-management/hooks/useUnit"; import HeaderAndFooter from "./HeaderAndFooter"; import { useState, useEffect, useCallback, useMemo } from "react"; import { useSearchParams } from "react-router-dom"; import { UnitDto } from "@/user-management/dto/unit/unitDto"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/shared/common/ui/select"; import { PrefixAndSuffix } from "./PrefixAndSuffixCard"; import { t } from "i18next"; import i18n from "@/i18n"; import AdminSetupAlert from "@/layout/components/AlertShow"; import RecordTagsManagement from "./record-tags/RecordTags"; import { TemplateSamplePage } from "@/user-management/TemplateSample/TemplateSamplePage"; import { BookImage, ClipboardType, ChevronRight, Menu, PencilRuler, SquareActivity, Stamp, Tag, X, Building, ClipboardList, FileText, } from "lucide-react"; import { cn } from "@/shared/lib/utils"; import { useDebounce } from "../content/useDebounce"; import { SingleSelect } from "@/shared/common/ui/single-select"; export interface FilePreview { type: "header" | "footer" | "seal"; url: string; file: File; uploadedAt: Date; id: string | null; } export interface Activity { id: string; type: string; description: string; timestamp: Date; } interface MenuItem { label: string; icon: React.ReactNode; element?: React.ReactNode; } const ContentManagement = () => { const { user } = useAuth(); const lang = i18n.language; const [searchParams, setSearchParams] = useSearchParams(); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [isSidebarCollapsed, setIsSidebarCollapsed] = useState(false); const [selectedUnitId, setSelectedUnitId] = useState(""); const debouncedUnitId = useDebounce(selectedUnitId, 300); const organizationId = user?.employee && user.employee.length > 0 ? user.employee[0].organizationId : undefined; const { getAccessibleList } = useUnit(); const { data: unitsResponse, isLoading: unitsLoading } = organizationId ? getAccessibleList(organizationId, { take: 300, skip: 0 }) : { data: undefined, isLoading: false }; // Notify listeners (e.g. AdminSetupAlert) after the unit selection debounces. useEffect(() => { if (!debouncedUnitId) return; window.dispatchEvent( new CustomEvent("unitChanged", { detail: debouncedUnitId }), ); }, [debouncedUnitId]); const handleUnitChange = useCallback( (value: string) => { setSelectedUnitId(value); setSearchParams( (prev) => { const next = new URLSearchParams(prev); next.set("unit", value); return next; }, { replace: true }, ); }, [setSearchParams], ); const selectMenuItem = useCallback( (index: number, label: string) => { setSearchParams( (prev) => { const next = new URLSearchParams(prev); next.set("tab", label); if (selectedUnitId) { next.set("unit", selectedUnitId); } return next; }, { replace: true }, ); setIsMobileMenuOpen(false); }, [selectedUnitId, setSearchParams], ); // Close mobile menu when resizing to desktop useEffect(() => { const handleResize = () => { if (window.innerWidth >= 768) { setIsMobileMenuOpen(false); } }; window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, []); useEffect(() => { const unitFromUrl = searchParams.get("unit"); const items = unitsResponse?.data?.items; if ( unitFromUrl && items?.some((unit: UnitDto) => unit.id === unitFromUrl) && unitFromUrl !== selectedUnitId ) { setSelectedUnitId(unitFromUrl); return; } if ( !unitsLoading && items?.length && !selectedUnitId && !unitFromUrl ) { const firstUnitId = items[0].id; setSelectedUnitId(firstUnitId); setSearchParams( (prev) => { const next = new URLSearchParams(prev); next.set("unit", firstUnitId); if (!next.get("tab")) { next.set("tab", "contentManagement.seal"); } return next; }, { replace: true }, ); } }, [ unitsLoading, unitsResponse, selectedUnitId, searchParams, setSearchParams, ]); // Sidebar menu items with dynamic loading states const menuItems: MenuItem[] = [ { label: "contentManagement.seal", icon: , element: , }, { label: "contentManagement.letterTemplate", icon: , element: , }, { label: "recordTag.title", icon: , element: , }, { label: "contentManagement.prefix", icon: , element: ( ), }, { label: "contentManagement.commonRemarks", icon: , element: , }, { label: "contentManagement.headerAndFooter", icon: , element: , }, { label: "template.sample", icon: , element: , }, { label: "dashboard.recentActivities", icon: , element: , }, ]; const activeMenuIndex = useMemo(() => { const tab = searchParams.get("tab"); if (!tab) return 0; const index = menuItems.findIndex((item) => item.label === tab); return index >= 0 ? index : 0; }, [menuItems, searchParams]); const activeMenuItem = menuItems[activeMenuIndex]; const isTemplateSampleActive = activeMenuItem?.label === "template.sample"; useEffect(() => { if (!searchParams.get("tab") && menuItems.length > 0) { setSearchParams( (prev) => { const next = new URLSearchParams(prev); next.set("tab", menuItems[0].label); return next; }, { replace: true }, ); } }, [menuItems, searchParams, setSearchParams]); return (
{/* Header Section */}

{t("organization.contentManagement")}

{t("organization.contentMsg")}

{/* Admin Alert */}
{/* Unit Selection Card */} {unitsResponse?.data?.items?.length > 0 && (
({ value: u.id, label: lang === "en" ? u.name.en : u.name.am, }))} onValueChange={handleUnitChange} value={selectedUnitId ?? ""} placeholder={t("delegation.selectDelegatedPosition")} /> {/* */}
)} {/* Main Content Grid */}
{/* Mobile Overlay */} {isMobileMenuOpen && (
setIsMobileMenuOpen(false)} /> )} {/* Sidebar */}
{/* Sidebar Header */}
{!isSidebarCollapsed && (

{t("organization.contentManagement")}

)}
{/* Collapse Toggle - Desktop */} {/* Close Button - Mobile */}
{/* Navigation Menu */}
{/* Main Content Area */}
{/* Mobile Header */}

{t(activeMenuItem?.label || "contentmanagement")}

{/* Page Content */}
{/* Breadcrumb Navigation */}
{t("content")} {activeMenuItem && ( <> {t(activeMenuItem.label)} )}
{/* Render the active element with loading state */}
{activeMenuItem?.element}
); }; export default ContentManagement;