File size: 3,399 Bytes
78d4e18
312fdff
78d4e18
 
312fdff
 
 
 
 
 
 
 
 
 
78d4e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312fdff
 
 
 
 
 
 
 
78d4e18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312fdff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useState } from 'react';
import { NavLink, Outlet } from 'react-router-dom';
import { getSelectedStoreId, setSelectedStoreId } from '../config';
import { getHealth } from '../api/client';

const navItems = [
  { to: '/', label: 'Dashboard', end: true },
  { to: '/heatmap', label: 'Heatmap' },
  { to: '/funnel', label: 'Funnel' },
  { to: '/anomalies', label: 'Anomalies' },
  { to: '/health', label: 'System Health' },
] as const;

export function Layout() {
  const [currentStore, setCurrentStore] = useState(getSelectedStoreId());
  const [availableStores, setAvailableStores] = useState<string[]>(['ST1008', 'ST_STORE2']);

  useEffect(() => {
    getHealth()
      .then((data) => {
        if (data && data.stores) {
          const ids = data.stores.map((s) => s.store_id);
          if (ids.length > 0) {
            setAvailableStores(ids);
          }
        }
      })
      .catch((err) => {
        console.warn('Failed to load active stores list from health endpoint, using defaults:', err);
      });
  }, []);

  const handleStoreChange = (storeId: string) => {
    setSelectedStoreId(storeId);
    setCurrentStore(storeId);
    // Reload the page to reset websockets and fetch new metrics
    window.location.reload();
  };

  return (
    <div className="flex min-h-screen">
      <aside className="flex w-56 shrink-0 flex-col border-r border-[var(--color-border)] bg-[var(--color-surface-raised)]">
        <div className="border-b border-[var(--color-border)] px-5 py-6">
          <p className="text-xs font-semibold uppercase tracking-wider text-[var(--color-text-muted)]">
            Store Intelligence
          </p>
          <h1 className="mt-1 text-lg font-semibold text-[var(--color-text)]">Apex Retail</h1>
          
          <div className="mt-4">
            <label htmlFor="store-selector" className="sr-only">Select Store</label>
            <select
              id="store-selector"
              value={currentStore}
              onChange={(e) => handleStoreChange(e.target.value)}
              className="w-full rounded-md border border-[var(--color-border)] bg-[var(--color-surface-overlay)] px-2 py-1.5 font-sans text-sm text-[var(--color-text)] outline-none focus:border-sky-400"
            >
              {availableStores.map((id) => (
                <option key={id} value={id}>
                  {id === 'ST1008' ? 'ST1008 (Brigade)' : id === 'ST_STORE2' ? 'ST_STORE2 (Store 2)' : id}
                </option>
              ))}
            </select>
          </div>
        </div>
        <nav className="flex flex-1 flex-col gap-1 p-3">
          {navItems.map((item) => (
            <NavLink
              key={item.to}
              to={item.to}
              end={'end' in item ? item.end : false}
              className={({ isActive }) =>
                [
                  'rounded-lg px-3 py-2.5 text-sm font-medium transition-colors',
                  isActive
                    ? 'bg-[var(--color-accent-muted)] text-sky-300'
                    : 'text-[var(--color-text-muted)] hover:bg-[var(--color-surface-overlay)] hover:text-[var(--color-text)]',
                ].join(' ')
              }
            >
              {item.label}
            </NavLink>
          ))}
        </nav>
      </aside>
      <main className="flex min-w-0 flex-1 flex-col">
        <Outlet />
      </main>
    </div>
  );
}