Files
Convivo/apps/convivo/src/App.tsx
T
Lago 692df23149 Add Playwright E2E suite (46 tests) + app testids + test_cases.md
- e2e/: full Playwright suite covering every spec case (staff auth/UI,
  welcome + session check-in, attendee access/gating, connections,
  profile visibility, speaker material, sponsor, feedback, E2E journeys,
  role security matrix). 46/46 passing.
- apps/convivo: add data-testid attributes (gate, tabs, scanner, toggles)
  to support the E2E suite.
- test_cases.md: spec-case-to-test coverage map (all cases fully covered).
- .gitignore: ignore e2e node_modules/reports/test-results.
2026-09-09 10:13:26 +02:00

172 lines
6.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useCallback, useEffect, useState } from 'react';
import { AuthProvider, useAuth } from './context/AuthContext';
import { ToastProvider } from './context/ToastContext';
import { Login } from './screens/Login';
import { Home } from './screens/Home';
import { Agenda } from './screens/Agenda';
import { Scan } from './screens/Scan';
import { Connections } from './screens/Connections';
import { Leaderboard } from './screens/Leaderboard';
import { Profile } from './screens/Profile';
import { Feedback } from './screens/Feedback';
import { QrImage, Spinner } from './components/ui';
import { getMyConnections } from './lib/api';
import { usePolling } from './lib/usePolling';
type Tab = 'home' | 'agenda' | 'scan' | 'connections' | 'profile' | 'leaderboard';
const TABS: { id: Tab; ico: string; label: string }[] = [
{ id: 'home', ico: '🏠', label: 'Home' },
{ id: 'agenda', ico: '📅', label: 'Agenda' },
{ id: 'scan', ico: '📷', label: 'Scan' },
{ id: 'connections', ico: '🤝', label: 'Connect' },
{ id: 'profile', ico: '👤', label: 'Profile' },
];
function Shell() {
const { session, contact, registration, loading, refresh, signOut } = useAuth();
const [tab, setTab] = useState<Tab>('home');
const [feedbackSession, setFeedbackSession] = useState<string | null>(null);
const [pendingIncoming, setPendingIncoming] = useState(0);
const email = contact?.email ?? '';
const loadPending = useCallback(async () => {
try {
const conns = await getMyConnections();
setPendingIncoming(conns.filter((c) => c.status === 'pending' && c.to_email === email).length);
} catch {
/* non-fatal */
}
}, [email]);
useEffect(() => {
if (email) void loadPending();
}, [email, loadPending]);
// Keep the Connect red-dot fresh: poll every 5s while visible.
usePolling(() => void loadPending(), 5000, !!email);
// Gate: a non-staff member must be checked in by staff before using the app.
// (Staff are never gated — they're the ones doing the checking in.)
const isGated = !!contact && !contact.is_admin && !registration?.checked_in;
// While gated, poll so the gate lifts as soon as staff checks them in.
usePolling(() => { if (isGated) void refresh(); }, 4000, isGated);
// Recalc immediately when a connection is accepted/declined/sent.
useEffect(() => {
const onChanged = () => void loadPending();
window.addEventListener('connections-changed', onChanged);
return () => window.removeEventListener('connections-changed', onChanged);
}, [loadPending]);
if (loading) {
return (
<div className="app">
<div className="center" style={{ flex: 1 }}>
<Spinner />
</div>
</div>
);
}
if (!session || !contact) {
return (
<div className="app">
<Login />
</div>
);
}
// Not checked in yet: a non-staff member must be checked in by staff before
// they can use the app. The gate lifts automatically (4s poll) once staff
// checks them in.
if (isGated) {
return (
<div className="app">
<div className="center" data-testid="checkin-required-gate" style={{ flex: 1, flexDirection: 'column', gap: 14, padding: 24, textAlign: 'center' }}>
<div style={{ fontSize: 56 }}>🏷️</div>
<div className="h1" style={{ textAlign: 'center' }}>Please see a staff member</div>
<div className="muted" style={{ textAlign: 'center', maxWidth: 320 }}>
You haven’t been checked in yet. Find a staff member and have them
scan your lanyard to get checked in — then you can start using the app.
</div>
{registration?.qr_token && (
<div className="card qr-wrap" style={{ marginTop: 4 }}>
<div className="h2" style={{ textAlign: 'center' }}>Your QR code</div>
<QrImage value={registration.qr_token} size={180} />
<div className="muted small" style={{ textAlign: 'center' }}>
Show this to <b>staff</b> to get checked in.
</div>
</div>
)}
<div className="muted small" style={{ opacity: 0.6 }}>
Checking in… this screen updates automatically.
</div>
<button className="btn ghost" onClick={() => void refresh()}>
I’ve been checked in — refresh
</button>
<button className="btn ghost" onClick={() => void signOut()}>
Log out
</button>
</div>
</div>
);
}
// Feedback is a full-screen takeover (no tab bar).
if (feedbackSession) {
return (
<div className="app">
<Feedback sessionId={feedbackSession} onDone={() => { setFeedbackSession(null); setTab('home'); }} />
</div>
);
}
// The home page (incl. the tab bar) is identical for every role. Staff keep
// their check-in capability inside the Scan screen (Attendee/Staff toggle).
const scanLabel = 'Scan';
return (
<div className="app">
{tab === 'home' && <Home go={(t) => setTab(t as Tab)} />}
{tab === 'agenda' && <Agenda openFeedback={(id) => setFeedbackSession(id)} />}
{tab === 'scan' && <Scan />}
{tab === 'connections' && <Connections />}
{tab === 'profile' && <Profile />}
{tab === 'leaderboard' && <Leaderboard />}
<nav className="tabbar">
{TABS.map((t) => (
<button
key={t.id}
data-testid={`tab-${t.id}`}
className={`tab ${t.id === 'scan' ? 'tab-scan' : ''} ${tab === t.id ? 'active' : ''}`}
onClick={() => setTab(t.id)}
>
{t.id === 'scan' ? (
<span className="scan-btn">📷</span>
) : (
<span className="ico">
{t.ico}
{t.id === 'connections' && pendingIncoming > 0 && <span className="dot" />}
</span>
)}
<span>{t.id === 'scan' ? scanLabel : t.label}</span>
</button>
))}
</nav>
</div>
);
}
export default function App() {
return (
<ToastProvider>
<AuthProvider>
<Shell />
</AuthProvider>
</ToastProvider>
);
}