File size: 21,208 Bytes
46463e1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | /**
* @license
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'motion/react';
import {
LayoutDashboard,
ShoppingCart,
Package,
Users,
Megaphone,
Settings,
Activity,
CloudLightning,
Wifi,
WifiOff,
Clock,
PhoneCall,
UserCircle
} from 'lucide-react';
import { TireProduct, StockHistoryItem, StaffUser, Invoice, SystemSettings, StaffRole } from './types';
import {
INITIAL_PRODUCTS,
INITIAL_HISTORY,
INITIAL_STAFF,
INITIAL_INVOICES,
INITIAL_SETTINGS
} from './initialData';
// Subcomponents
import Dashboard from './components/Dashboard';
import InvoiceGenerator from './components/InvoiceGenerator';
import InventoryManager from './components/InventoryManager';
import StaffManager from './components/StaffManager';
import SocialPromoter from './components/SocialPromoter';
import SettingsComponent from './components/Settings';
export default function App() {
const [activeTab, setActiveTab] = useState('dashboard');
const [isOnline, setIsOnline] = useState(true);
const [currentTime, setCurrentTime] = useState(new Date());
// Core database state hooks
const [settings, setSettings] = useState<SystemSettings>(INITIAL_SETTINGS);
const [products, setProducts] = useState<TireProduct[]>(INITIAL_PRODUCTS);
const [invoices, setInvoices] = useState<Invoice[]>(INITIAL_INVOICES);
const [history, setHistory] = useState<StockHistoryItem[]>(INITIAL_HISTORY);
const [staffList, setStaffList] = useState<StaffUser[]>(INITIAL_STAFF);
const [currentStaff, setCurrentStaff] = useState<StaffUser>(INITIAL_STAFF[1]); // Zin Brother as default cashier
// Invoice review overlay trigger state
const [selectedInvoiceForView, setSelectedInvoiceForView] = useState<Invoice | null>(null);
// Initialize and load from local storage if existing
useEffect(() => {
// Clock tick
const timer = setInterval(() => setCurrentTime(new Date()), 1000);
const storedSettings = localStorage.getItem('HBT_SETTINGS');
const storedProducts = localStorage.getItem('HBT_PRODUCTS');
const storedInvoices = localStorage.getItem('HBT_INVOICES');
const storedHistory = localStorage.getItem('HBT_HISTORY');
const storedStaff = localStorage.getItem('HBT_STAFF');
const storedCurrent = localStorage.getItem('HBT_CURRENT_STAFF');
if (storedSettings) setSettings(JSON.parse(storedSettings));
if (storedProducts) setProducts(JSON.parse(storedProducts));
if (storedInvoices) setInvoices(JSON.parse(storedInvoices));
if (storedHistory) setHistory(JSON.parse(storedHistory));
if (storedStaff) setStaffList(JSON.parse(storedStaff));
if (storedCurrent) {
const parsedCurrent = JSON.parse(storedCurrent);
// Ensure currentStaff matches the parsed currentStaff safely
setCurrentStaff(parsedCurrent);
} else {
setCurrentStaff(INITIAL_STAFF[1]); // Zain Brother manager
}
return () => clearInterval(timer);
}, []);
// Sync utilities to persistence
const syncToLocalStorage = (key: string, data: any) => {
localStorage.setItem(key, JSON.stringify(data));
};
// State Adjustments multipliers
const handleUpdateSettings = (updated: SystemSettings) => {
setSettings(updated);
syncToLocalStorage('HBT_SETTINGS', updated);
};
// Triggered from Dashboard replenishment alarms
const handleQuickAddStock = (productId: string, amount: number) => {
const adjustedProducts = products.map(p => {
if (p.id === productId) {
const resultingStock = p.stock + amount;
// Append history log immediately
const logItem: StockHistoryItem = {
id: 'log_' + Math.random().toString(36).substr(2, 9),
productId: p.id,
productLabel: `${p.brand} ${p.model} (${p.size})`,
dateTime: new Date().toISOString(),
type: 'STOCK_IN',
quantity: amount,
resultingStock,
adjustedBy: `${currentStaff.name} (${currentStaff.role})`,
reason: `Quick Restock of Alarm: Restocked cargo pallet.`
};
const updatedHistory = [logItem, ...history];
setHistory(updatedHistory);
syncToLocalStorage('HBT_HISTORY', updatedHistory);
return { ...p, stock: resultingStock };
}
return p;
});
setProducts(adjustedProducts);
syncToLocalStorage('HBT_PRODUCTS', adjustedProducts);
};
// Triggered from InventoryManager manual configurations
const handleAdjustProductStock = (
productId: string,
quantityChange: number,
type: StockHistoryItem['type'],
reason: string
) => {
const adjustedProducts = products.map(p => {
if (p.id === productId) {
const resultingStock = p.stock + quantityChange;
// Create log item
const logItem: StockHistoryItem = {
id: 'log_' + Math.random().toString(36).substr(2, 9),
productId: p.id,
productLabel: `${p.brand} ${p.model} (${p.size})`,
dateTime: new Date().toISOString(),
type,
quantity: Math.abs(quantityChange),
resultingStock,
adjustedBy: `${currentStaff.name} (${currentStaff.role})`,
reason
};
const updatedHistory = [logItem, ...history];
setHistory(updatedHistory);
syncToLocalStorage('HBT_HISTORY', updatedHistory);
return { ...p, stock: resultingStock };
}
return p;
});
setProducts(adjustedProducts);
syncToLocalStorage('HBT_PRODUCTS', adjustedProducts);
};
const handleAddNewProduct = (prod: TireProduct) => {
const updatedProducts = [prod, ...products];
setProducts(updatedProducts);
syncToLocalStorage('HBT_PRODUCTS', updatedProducts);
// Write initial STOCK_IN history entry
const logItem: StockHistoryItem = {
id: 'log_' + Math.random().toString(36).substr(2, 9),
productId: prod.id,
productLabel: `${prod.brand} ${prod.model} (${prod.size})`,
dateTime: new Date().toISOString(),
type: 'STOCK_IN',
quantity: prod.stock,
resultingStock: prod.stock,
adjustedBy: `${currentStaff.name} (${currentStaff.role})`,
reason: `First inventory system initialization of tyre profile SKU.`
};
const updatedHistory = [logItem, ...history];
setHistory(updatedHistory);
syncToLocalStorage('HBT_HISTORY', updatedHistory);
};
const handleDeleteProduct = (productId: string) => {
const updatedProducts = products.filter(p => p.id !== productId);
setProducts(updatedProducts);
syncToLocalStorage('HBT_PRODUCTS', updatedProducts);
};
// Adding Client Invoice automatically reduces matching tire quantities and writes logs
const handleAddInvoice = (newInv: Invoice) => {
// Append invoice
const updatedInvoices = [...invoices, newInv];
setInvoices(updatedInvoices);
syncToLocalStorage('HBT_INVOICES', updatedInvoices);
// Iterate items and decrease physical stock counts
let revisedProducts = [...products];
let newLogs: StockHistoryItem[] = [];
newInv.items.forEach((item) => {
revisedProducts = revisedProducts.map(p => {
if (p.id === item.productId) {
const resultingStock = p.stock - item.quantity;
// Construct checkout history block
const logItem: StockHistoryItem = {
id: 'log_' + Math.random().toString(36).substr(2, 9),
productId: p.id,
productLabel: `${p.brand} ${p.model} (${p.size})`,
dateTime: new Date().toISOString(),
type: 'STOCK_OUT',
quantity: item.quantity,
resultingStock,
adjustedBy: `${currentStaff.name} (${currentStaff.role})`,
reason: `Retail Invoice checkout sales: Ref ${newInv.invoiceNumber}`
};
newLogs.push(logItem);
return { ...p, stock: resultingStock };
}
return p;
});
});
const revisedHistory = [...newLogs, ...history];
setHistory(revisedHistory);
setProducts(revisedProducts);
syncToLocalStorage('HBT_PRODUCTS', revisedProducts);
syncToLocalStorage('HBT_HISTORY', revisedHistory);
};
// Staff managers
const handleAddStaffMember = (newUser: StaffUser) => {
const updated = [...staffList, newUser];
setStaffList(updated);
syncToLocalStorage('HBT_STAFF', updated);
};
const handleToggleStaffStatus = (staffId: string) => {
const updated = staffList.map(s =>
s.id === staffId ? { ...s, active: !s.active } : s
);
setStaffList(updated);
syncToLocalStorage('HBT_STAFF', updated);
};
const handleUpdateStaffRole = (staffId: string, role: StaffRole) => {
const updated = staffList.map(s =>
s.id === staffId ? { ...s, role } : s
);
setStaffList(updated);
syncToLocalStorage('HBT_STAFF', updated);
};
const handleSetCurrentStaff = (staff: StaffUser) => {
setCurrentStaff(staff);
syncToLocalStorage('HBT_CURRENT_STAFF', staff);
};
// Decrypted Restore logic
const handleRestoreDatabase = (decoded: {
settings: SystemSettings;
products: any[];
invoices: any[];
history: any[];
staff: any[];
}) => {
setSettings(decoded.settings);
setProducts(decoded.products);
setInvoices(decoded.invoices);
setHistory(decoded.history);
setStaffList(decoded.staff);
// Save and persist all entries
syncToLocalStorage('HBT_SETTINGS', decoded.settings);
syncToLocalStorage('HBT_PRODUCTS', decoded.products);
syncToLocalStorage('HBT_INVOICES', decoded.invoices);
syncToLocalStorage('HBT_HISTORY', decoded.history);
syncToLocalStorage('HBT_STAFF', decoded.staff);
// Set Owner as current operator safely if there's any owner in the backup list
const firstOwner = decoded.staff.find(s => s.role === 'owner' && s.active);
if (firstOwner) {
setCurrentStaff(firstOwner);
syncToLocalStorage('HBT_CURRENT_STAFF', firstOwner);
}
};
// Factory defaults
const handleClearCacheToDefault = () => {
localStorage.clear();
};
return (
<div className="min-h-screen bg-[#F8FAFC] flex flex-col md:flex-row antialiased select-none font-sans" id="application-shell">
{/* 1. COLLAPSIBLE OR RESPONSIVE STATIC SIDEBAR */}
<aside className="w-full md:w-64 bg-white border-b md:border-r border-slate-200 text-slate-800 flex flex-col justify-between shrink-0 shadow-sm" id="sidebar-panel">
<div className="space-y-6">
{/* Top Shop Brand Logo */}
<div className="p-6 border-b border-slate-100 flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="w-9 h-9 rounded-lg bg-slate-900 flex items-center justify-center font-bold text-white text-base shadow-sm">
HBT
</div>
<div className="space-y-0.5">
<h2 className="text-xs font-bold tracking-tight text-slate-900 uppercase">Haider Brothers</h2>
<span className="text-[10px] text-slate-400 font-medium block">Tire Shop Manager</span>
</div>
</div>
</div>
{/* Navigation Links list */}
<nav className="px-3 space-y-1">
<button
onClick={() => setActiveTab('dashboard')}
className={`w-full flex items-center gap-3 px-3.5 py-2.5 rounded-lg text-xs font-medium font-sans transition cursor-pointer ${
activeTab === 'dashboard'
? 'bg-slate-900 text-white shadow-sm font-semibold'
: 'text-slate-500 hover:text-slate-900 hover:bg-slate-50'
}`}
>
<LayoutDashboard className="w-4 h-4 text-inherit" />
Reporting Cockpit
</button>
<button
onClick={() => {
setActiveTab('invoices');
setSelectedInvoiceForView(null);
}}
className={`w-full flex items-center gap-3 px-3.5 py-2.5 rounded-lg text-xs font-medium font-sans transition cursor-pointer ${
activeTab === 'invoices'
? 'bg-slate-900 text-white shadow-sm font-semibold'
: 'text-slate-500 hover:text-slate-900 hover:bg-slate-50'
}`}
>
<ShoppingCart className="w-4 h-4 text-inherit" />
Invoice Generator
</button>
<button
onClick={() => setActiveTab('inventory')}
className={`w-full flex items-center gap-3 px-3.5 py-2.5 rounded-lg text-xs font-medium font-sans transition cursor-pointer ${
activeTab === 'inventory'
? 'bg-slate-900 text-white shadow-sm font-semibold'
: 'text-slate-500 hover:text-slate-900 hover:bg-slate-50'
}`}
>
<Package className="w-4 h-4 text-inherit" />
Stock Inventory
</button>
<button
onClick={() => setActiveTab('staff')}
className={`w-full flex items-center gap-3 px-3.5 py-2.5 rounded-lg text-xs font-medium font-sans transition cursor-pointer ${
activeTab === 'staff'
? 'bg-slate-900 text-white shadow-sm font-semibold'
: 'text-slate-500 hover:text-slate-900 hover:bg-slate-50'
}`}
>
<Users className="w-4 h-4 text-inherit" />
Cashiers & RBAC
</button>
<button
onClick={() => setActiveTab('social')}
className={`w-full flex items-center gap-3 px-3.5 py-2.5 rounded-lg text-xs font-medium font-sans transition cursor-pointer ${
activeTab === 'social'
? 'bg-slate-900 text-white shadow-sm font-semibold'
: 'text-slate-500 hover:text-slate-900 hover:bg-slate-50'
}`}
>
<Megaphone className="w-4 h-4 text-inherit" />
Outreach Promos
</button>
<button
onClick={() => setActiveTab('settings')}
className={`w-full flex items-center gap-3 px-3.5 py-2.5 rounded-lg text-xs font-medium font-sans transition cursor-pointer ${
activeTab === 'settings'
? 'bg-slate-900 text-white shadow-sm font-semibold'
: 'text-slate-500 hover:text-slate-900 hover:bg-slate-50'
}`}
>
<Settings className="w-4 h-4 text-inherit" />
Settings & Vault
</button>
</nav>
</div>
{/* Bottom Operator profile card in sidebar */}
<div className="p-4 border-t border-slate-100 bg-slate-50 space-y-2">
<div className="flex items-center gap-2.5 text-xs">
<UserCircle className="w-8 h-8 text-slate-400" />
<div className="flex-1 min-w-0">
<p className="font-semibold text-slate-950 truncate text-[11px]">{currentStaff.name}</p>
<p className="text-[10px] text-slate-400 uppercase tracking-wider font-medium">{currentStaff.role}</p>
</div>
</div>
<div className="bg-white rounded-lg p-2 flex justify-between items-center text-[10px] border border-slate-200">
<span className="text-slate-400 font-medium">Node secure:</span>
<span className="font-mono text-emerald-600 font-bold flex items-center gap-1.5">
<span className="w-1.5 h-1.5 bg-emerald-500 rounded-full"></span>
ONLINE
</span>
</div>
</div>
</aside>
{/* 2. CORE WORKSPACE: HEADER + SCROLLABLE PAGE VIEWS */}
<main className="flex-1 flex flex-col min-w-0 overflow-y-auto h-screen" id="workspace-viewport">
{/* Top Navbar */}
<header className="bg-white border-b border-slate-200 px-6 md:px-8 py-4 flex flex-col sm:flex-row justify-between items-start sm:items-center gap-3 shrink-0">
{/* Left info */}
<div className="flex items-center gap-2.5">
<span className="w-2 h-2 bg-slate-900 rounded-full animate-pulse"></span>
<span className="text-[11px] font-bold text-slate-800 font-sans tracking-wider uppercase">
SECURE CASHIER LEDGER
</span>
<span className="text-slate-200">|</span>
<span className="text-[11px] text-slate-500 flex items-center gap-1">
Store Support: {settings.shopPhone}
</span>
</div>
{/* Right systems specs */}
<div className="flex items-center gap-3.5 text-xs">
{/* Real-time Clock */}
<div className="text-slate-600 flex items-center gap-1.5 bg-[#F8FAFC] px-2.5 py-1.5 rounded-lg border border-slate-200/80">
<Clock className="w-3.5 h-3.5 text-slate-400" />
<span className="font-mono font-medium text-[11px] text-slate-700">{currentTime.toLocaleTimeString()}</span>
</div>
{/* Offline vs Online Beacons and alarms */}
{isOnline ? (
<span className="bg-emerald-50 text-emerald-800 border border-emerald-200/60 font-semibold font-sans px-2.5 py-1.5 rounded-lg flex items-center gap-1.5 text-[10px]">
<Wifi className="w-3.5 h-3.5 text-emerald-600" />
VAULT OK
</span>
) : (
<span className="bg-amber-50 text-amber-800 border border-amber-200/60 font-semibold font-sans px-2.5 py-1.5 rounded-lg flex items-center gap-1.5 text-[10px] animate-pulse">
<WifiOff className="w-3.5 h-3.5 text-amber-600" />
AIR-GAPPED LOCAL
</span>
)}
</div>
</header>
{/* Tab Pages dispatcher Router panel */}
<div className="flex-1 p-6 md:p-8 overflow-y-auto">
<AnimatePresence mode="wait">
<motion.div
key={activeTab}
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.15 }}
className="h-full"
>
{activeTab === 'dashboard' && (
<Dashboard
products={products}
invoices={invoices}
currentStaff={currentStaff}
settings={settings}
setActiveTab={setActiveTab}
onQuickAddStock={handleQuickAddStock}
setSelectedInvoiceForView={(inv) => {
setSelectedInvoiceForView(inv);
setActiveTab('invoices');
}}
/>
)}
{activeTab === 'invoices' && (
<InvoiceGenerator
products={products}
invoices={invoices}
currentStaff={currentStaff}
settings={settings}
isOnline={isOnline}
onAddInvoice={handleAddInvoice}
selectedInvoiceForView={selectedInvoiceForView}
setSelectedInvoiceForView={setSelectedInvoiceForView}
/>
)}
{activeTab === 'inventory' && (
<InventoryManager
products={products}
history={history}
currentStaff={currentStaff}
settings={settings}
onAdjustProductStock={handleAdjustProductStock}
onAddNewProduct={handleAddNewProduct}
onDeleteProduct={handleDeleteProduct}
/>
)}
{activeTab === 'staff' && (
<StaffManager
staffList={staffList}
currentStaff={currentStaff}
onSetCurrentStaff={handleSetCurrentStaff}
onAddStaffMember={handleAddStaffMember}
onToggleStaffStatus={handleToggleStaffStatus}
onUpdateStaffRole={handleUpdateStaffRole}
/>
)}
{activeTab === 'social' && (
<SocialPromoter
products={products}
settings={settings}
/>
)}
{activeTab === 'settings' && (
<SettingsComponent
settings={settings}
isOnline={isOnline}
onUpdateSettings={handleUpdateSettings}
onToggleOnlineMode={() => setIsOnline(!isOnline)}
onRestoreDatabase={handleRestoreDatabase}
onClearCacheToDefault={handleClearCacheToDefault}
activeProducts={products}
activeInvoices={invoices}
activeHistory={history}
activeStaff={staffList}
/>
)}
</motion.div>
</AnimatePresence>
</div>
</main>
</div>
);
}
|