import { useState, useRef, useEffect } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import useAuthStore from '../store/useAuthStore'; import { getCompanySlug } from '../utils/slug'; import api from '../api/axios'; export default function Navbar() { const { user, logout } = useAuthStore(); const [open, setOpen] = useState(false); const ref = useRef(null); const navigate = useNavigate(); const location = useLocation(); const isOwner = user?.role === 'owner'; const slug = getCompanySlug(user?.company_name); const [balance, setBalance] = useState(null); const loadWallet = () => { if (user?.role === 'owner' || user?.role === 'kasir') { api.get('/api/companies/me').then(res => { setBalance(res.data.wallet_balance); }).catch(e => console.error("Gagal load wallet", e)); } }; useEffect(() => { loadWallet(); const handleWalletUpdate = () => loadWallet(); window.addEventListener('refresh_wallet', handleWalletUpdate); return () => window.removeEventListener('refresh_wallet', handleWalletUpdate); }, [user]); const formatRp = (num) => 'Rp ' + (num || 0).toLocaleString('id-ID'); const getNavPath = (p) => { if (user?.role === 'super_admin') return p; return `/${slug}${p}`; }; const isOnPOSArea = location.pathname.includes('/pos') || location.pathname.includes('/kasir'); const isOnReportPage = location.pathname.endsWith('/kasir/report'); useEffect(() => { const fn = (e) => { if (ref.current && !ref.current.contains(e.target)) setOpen(false); }; document.addEventListener('mousedown', fn); return () => document.removeEventListener('mousedown', fn); }, []); return (
calendar_today {new Date().toLocaleDateString('id-ID', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' })}
{balance !== null && (
account_balance_wallet {formatRp(balance)}
)} {open && (

{user?.name}

{user?.email}

{/* Laporan Harian - muncul di halaman kasir/POS area */} {isOnPOSArea && ( <> {user?.company_settings?.enable_member !== false && ( )} )} {/* Buka Kasir - hanya owner di luar POS area */} {isOwner && !isOnPOSArea && ( )} {/* Kembali ke Dashboard - hanya owner di POS area */} {isOwner && isOnPOSArea && ( )}
)}
); }