import { useEffect, useState } from 'react' import { CalendarDays } from 'lucide-react' import { ShoppingBag, Store, Users, FileText, Briefcase, Wallet, } from 'lucide-react' import { useLocale } from '@meshkee/dashboard-ui' import { useAuth } from '../context/AuthContext' import { SectionCard } from '../components/SectionCard' import { HomeChartSlot } from '../components/HomeChartSlot' import { useT } from '../i18n/useT' import type { BusinessMessageKey } from '../i18n/messages' import { listProducts } from '../services/productService' import { listStoreItems } from '../services/storeItemService' import { listCustomers } from '../services/customerService' import { listBlogs } from '../services/blogService' import { listPortfolios } from '../services/portfolioService' import { useTenantBranding } from '../context/TenantBrandingContext' import { hasBusinessModule, isActiveHomeChart, type BusinessModuleId, } from '../utils/businessModules' import styles from '../components/PageContent.module.css' type CountKey = 'products' | 'store' | 'customers' | 'blog' | 'portfolios' const sections: { icon: typeof ShoppingBag titleKey: BusinessMessageKey descKey: BusinessMessageKey linkKey: BusinessMessageKey countLabelKey?: BusinessMessageKey href: string countKey?: CountKey /** When set, card is shown only if this optional module is enabled. */ moduleId?: BusinessModuleId }[] = [ { icon: ShoppingBag, titleKey: 'home.card.products.title', descKey: 'home.card.products.desc', linkKey: 'home.card.products.link', countLabelKey: 'home.card.products.count', href: '/products', countKey: 'products', moduleId: 'products', }, { icon: Store, titleKey: 'home.card.store.title', descKey: 'home.card.store.desc', linkKey: 'home.card.store.link', countLabelKey: 'home.card.store.count', href: '/store', countKey: 'store', moduleId: 'store', }, { icon: Users, titleKey: 'home.card.customers.title', descKey: 'home.card.customers.desc', linkKey: 'home.card.customers.link', countLabelKey: 'home.card.customers.count', href: '/customers', countKey: 'customers', }, { icon: Wallet, titleKey: 'home.card.finance.title', descKey: 'home.card.finance.desc', linkKey: 'home.card.finance.link', href: '/finance', }, { icon: FileText, titleKey: 'home.card.blog.title', descKey: 'home.card.blog.desc', linkKey: 'home.card.blog.link', countLabelKey: 'home.card.blog.count', href: '/blog', countKey: 'blog', moduleId: 'blog', }, { icon: Briefcase, titleKey: 'home.card.portfolios.title', descKey: 'home.card.portfolios.desc', linkKey: 'home.card.portfolios.link', countLabelKey: 'home.card.portfolios.count', href: '/portfolios', countKey: 'portfolios', moduleId: 'portfolio', }, ] type SectionCounts = Partial> async function loadSectionCounts(signal: AbortSignal): Promise { const [products, store, customers, blog, portfolios] = await Promise.all([ listProducts(1, 1, signal).then((r) => r.total).catch(() => null), listStoreItems(1, 1, signal).then((r) => r.total).catch(() => null), listCustomers({ page: 1, pageSize: 1 }, signal).then((r) => r.total).catch(() => null), listBlogs(1, 1, signal).then((r) => r.total).catch(() => null), listPortfolios(1, 1, signal).then((r) => r.total).catch(() => null), ]) const counts: SectionCounts = {} if (products !== null) counts.products = products if (store !== null) counts.store = store if (customers !== null) counts.customers = customers if (blog !== null) counts.blog = blog if (portfolios !== null) counts.portfolios = portfolios return counts } export function HomePage() { const { user } = useAuth() const { locale } = useLocale() const { enabledModules, homeCharts } = useTenantBranding() const t = useT() const [counts, setCounts] = useState({}) useEffect(() => { const controller = new AbortController() void loadSectionCounts(controller.signal).then((next) => { if (!controller.signal.aborted) setCounts(next) }) return () => controller.abort() }, []) const visibleSections = sections.filter( (section) => !section.moduleId || hasBusinessModule(enabledModules, section.moduleId), ) const visibleCharts = homeCharts.filter(isActiveHomeChart) const firstName = (locale === 'en' ? user?.firstNameEn?.trim() || user?.firstName : user?.firstName?.trim() || user?.firstNameEn) || t('home.welcomeFallback') const formattedDate = new Intl.DateTimeFormat(locale === 'fa' ? 'fa-IR' : 'en-US', { month: 'long', day: 'numeric', year: 'numeric', weekday: 'long', }).format(new Date()) return (

{t('home.welcome', { name: firstName })}

{t('home.subtitle')}

{formattedDate}
{visibleSections.map((section) => ( ))}
{visibleCharts.length > 0 ? (
{visibleCharts.map((chartId, index) => (
))}
) : null}
) }