()
| 17 | ]; |
| 18 | |
| 19 | export default function App() { |
| 20 | const navigate = useNavigate(); |
| 21 | const [cart, setCart] = useState<CartItem[]>([]); |
| 22 | const [user, setUser] = useState<User | null>(null); |
| 23 | |
| 24 | useEffect(() => { |
| 25 | const stored = localStorage.getItem('op_testbed_user'); |
| 26 | if (stored) { |
| 27 | const u = JSON.parse(stored) as User; |
| 28 | setUser(u); |
| 29 | op.identify({ |
| 30 | profileId: u.id, |
| 31 | firstName: u.firstName, |
| 32 | lastName: u.lastName, |
| 33 | email: u.email, |
| 34 | }); |
| 35 | applyGroups(u); |
| 36 | } |
| 37 | op.ready(); |
| 38 | }, []); |
| 39 | |
| 40 | function applyGroups(u: User) { |
| 41 | op.setGroups(u.groupIds); |
| 42 | for (const id of u.groupIds) { |
| 43 | const meta = PRESET_GROUPS.find((g) => g.id === id); |
| 44 | if (meta) { |
| 45 | op.upsertGroup({ id, ...meta }); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | function login(u: User) { |
| 51 | localStorage.setItem('op_testbed_user', JSON.stringify(u)); |
| 52 | setUser(u); |
| 53 | op.identify({ |
| 54 | profileId: u.id, |
| 55 | firstName: u.firstName, |
| 56 | lastName: u.lastName, |
| 57 | email: u.email, |
| 58 | }); |
| 59 | applyGroups(u); |
| 60 | op.track('user_login', { method: 'form', group_count: u.groupIds.length }); |
| 61 | navigate('/'); |
| 62 | } |
| 63 | |
| 64 | function logout() { |
| 65 | localStorage.removeItem('op_testbed_user'); |
| 66 | op.clear(); |
| 67 | setUser(null); |
| 68 | } |
| 69 | |
| 70 | function addToCart(product: Product) { |
| 71 | setCart((prev) => { |
| 72 | const existing = prev.find((i) => i.id === product.id); |
| 73 | if (existing) { |
| 74 | return prev.map((i) => |
| 75 | i.id === product.id ? { ...i, qty: i.qty + 1 } : i |
| 76 | ); |
nothing calls this directly
no test coverage detected