feat(waiter): native pairing, venue switcher, QR scanner, Android back handling

- First run in the native app shows only the pairing screen (VenuesPage):
  scan the manager's pairing QR or type the server address; the server must
  answer /api/system/identity as xenia-pos with a supported api_version, and
  a QR's ?pair=<site_id> must match - a code from another venue is refused
- /venues (native only): list of paired venues, switch (reloads into that
  venue's own token/IndexedDB), remove (data kept - may hold unsynced
  orders). Reachable from the login screen, the user menu and the offline page
- QrScannerModal: WebView camera via qr-scanner (no Google Play Services
  dependency, carries over to iOS); requests the camera once up front so the
  Android prompt appears a single time and "denied" gets its own message
- Hardware back: @capacitor/app listener - no-op on root screens (/, /tables,
  /login), otherwise history back. Path-based because Chrome's history
  intervention makes the WebView report canGoBack=false for the sentinel
  entry AndroidBackGuard pushes
- Before pairing no IndexedDB is opened, so nothing is created under the
  un-namespaced name
- InstallAppBanner now shows in plain-HTTP browser mode only when the venue
  server actually serves the APK (HEAD /downloads/xenia-waiter.apk)

Verified on an Android 15 emulator (API 35) with real taps via adb and state
via WebView DevTools: wrong address -> error; other venue's QR -> refused;
pairing -> venue-namespaced storage, live WebSocket; back on /tables keeps the
app open; restart keeps venue + session; second venue pairs logged-out and
switching back keeps venue 1's session; camera deny -> one prompt + message;
allow -> live preview. Signed release APK installed and paired. Web modes
(same-origin, plain-HTTP LAN IP, VITE_SERVER_URL) re-verified in Edge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-28 13:31:23 +03:00
co-authored by Claude Opus 5.5
parent 0b0f5c60c3
commit a486e3ff63
10 changed files with 440 additions and 12 deletions
+9 -1
View File
@@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'
import PinPad from '../components/PinPad'
import useAuthStore from '../store/authStore'
import client from '../api/client'
import { assetUrl } from '../config/server'
import { assetUrl, deliveryMode, getActiveVenue } from '../config/server'
// ─── Waiter card ──────────────────────────────────────────────────────────────
@@ -128,6 +128,14 @@ export default function LoginPage() {
<div style={{ flexShrink: 0, padding: '40px 16px 20px', textAlign: 'center' }}>
<h1 className="app-title" style={{ marginBottom: 10 }}>Xenia POS</h1>
<p className="app-subtitle">Ποιος είσαι;</p>
{deliveryMode() === 'native' && (
<button
onClick={() => navigate('/venues')}
style={{ marginTop: 10, background: 'none', border: 'none', color: 'var(--muted)', fontSize: 13, cursor: 'pointer' }}
>
🏪 {getActiveVenue()?.name} · <span style={{ color: 'var(--accent)' }}>Αλλαγή</span>
</button>
)}
</div>
{/* Scrollable card list */}
+10
View File
@@ -1,4 +1,5 @@
import client from '../api/client'
import { deliveryMode } from '../config/server'
import { useState } from 'react'
export default function OfflinePage() {
@@ -25,6 +26,15 @@ export default function OfflinePage() {
<button className="btn btn--primary btn--lg" onClick={retry} disabled={checking}>
{checking ? 'Έλεγχος…' : 'Επανάληψη'}
</button>
{deliveryMode() === 'native' && (
<button
className="btn btn--secondary btn--lg"
style={{ marginTop: 12 }}
onClick={() => { window.location.href = '/venues' }}
>
Αλλαγή καταστήματος
</button>
)}
</div>
</div>
)
+151
View File
@@ -0,0 +1,151 @@
import { useCallback, useEffect, useState } from 'react'
import useThemeStore from '../store/themeStore'
import QrScannerModal from '../components/QrScannerModal'
import { pairWith } from '../native/pairing'
import { getActiveVenue, listVenues, removeVenue, switchVenue } from '../config/server'
/**
* Native app: pair with a venue server and switch between paired venues.
*
* `firstRun` — rendered on its own (no router, no auth) when the app has no
* active venue yet. Otherwise it's the /venues route, reachable from the login
* screen, the user menu and the offline screen.
*/
export default function VenuesPage({ firstRun = false }) {
const dark = useThemeStore(s => s.dark)
const [venues, setVenues] = useState(listVenues)
const [address, setAddress] = useState('')
const [scanning, setScanning] = useState(false)
const [busy, setBusy] = useState(false)
const [error, setError] = useState(null)
const active = getActiveVenue()
// First run renders outside <App>'s ThemeApplier
useEffect(() => {
document.documentElement.setAttribute('data-theme', dark ? 'dark' : 'light')
}, [dark])
const connect = useCallback(async raw => {
setScanning(false)
setBusy(true)
setError(null)
try {
const venue = await pairWith(raw)
switchVenue(venue.siteId) // reloads the app into the venue
} catch (e) {
setError(e.message || 'Αποτυχία σύνδεσης.')
setBusy(false)
}
}, [])
function handleRemove(venue) {
const msg = venue.siteId === active?.siteId
? `Αφαίρεση του ενεργού καταστήματος «${venue.name}»; Θα χρειαστεί νέα σύνδεση.`
: `Αφαίρεση του καταστήματος «${venue.name}» από αυτό το κινητό;`
if (!window.confirm(msg)) return
removeVenue(venue.siteId)
if (venue.siteId === active?.siteId) {
window.location.replace('/')
} else {
setVenues(listVenues())
}
}
return (
<div className="page" style={{ padding: '24px 20px', gap: 24, display: 'flex', flexDirection: 'column', overflowY: 'auto' }}>
{!firstRun && (
<button
onClick={() => window.history.length > 1 ? window.history.back() : window.location.replace('/')}
style={{ alignSelf: 'flex-start', background: 'none', border: 'none', color: 'var(--muted)', fontSize: 15, cursor: 'pointer', padding: 0 }}
>
← Πίσω
</button>
)}
<div style={{ textAlign: 'center' }}>
<div style={{ fontSize: 44, marginBottom: 8 }}>🏪</div>
<h1 style={{ fontSize: 22, fontWeight: 700, color: 'var(--text)', marginBottom: 6 }}>
{firstRun ? 'Σύνδεση με κατάστημα' : 'Καταστήματα'}
</h1>
<p style={{ fontSize: 14, color: 'var(--muted)', lineHeight: 1.5 }}>
Σαρώστε το QR από τον διαχειριστή ή πληκτρολογήστε τη διεύθυνση του server.
Το κινητό πρέπει να είναι στο WiFi του καταστήματος.
</p>
</div>
{venues.length > 0 && (
<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
{venues.map(v => {
const isActive = v.siteId === active?.siteId
return (
<div key={v.siteId} style={{
display: 'flex', alignItems: 'center', gap: 12,
background: 'var(--bg2)', border: `1px solid ${isActive ? 'var(--accent)' : 'var(--border)'}`,
borderRadius: 14, padding: '12px 14px',
}}>
<button
onClick={() => !isActive && switchVenue(v.siteId)}
disabled={busy}
style={{ flex: 1, textAlign: 'left', background: 'none', border: 'none', padding: 0, cursor: isActive ? 'default' : 'pointer' }}
>
<div style={{ fontSize: 16, fontWeight: 600, color: 'var(--text)' }}>
{v.name}
{isActive && (
<span style={{ marginLeft: 8, fontSize: 11, fontWeight: 700, color: 'var(--accent)' }}>ΕΝΕΡΓΟ</span>
)}
</div>
<div style={{ fontSize: 12, color: 'var(--muted)', fontFamily: 'monospace' }}>{v.baseUrl}</div>
</button>
<button
onClick={() => handleRemove(v)}
aria-label="Αφαίρεση"
style={{ background: 'none', border: 'none', color: 'var(--muted)', fontSize: 22, cursor: 'pointer', padding: 4 }}
>
×
</button>
</div>
)
})}
</div>
)}
<div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
<button className="btn btn--accent btn--lg" onClick={() => { setError(null); setScanning(true) }} disabled={busy}>
📷 Σάρωση QR
</button>
<div style={{ display: 'flex', gap: 8 }}>
<input
value={address}
onChange={e => setAddress(e.target.value)}
onKeyDown={e => e.key === 'Enter' && address && connect(address)}
placeholder="π.χ. 192.168.1.50"
inputMode="url"
autoCapitalize="off"
autoCorrect="off"
disabled={busy}
style={{
flex: 1, minWidth: 0, background: 'var(--bg3)', border: '1px solid var(--border)',
borderRadius: 12, padding: '0 14px', height: 52, color: 'var(--text)', fontSize: 16, outline: 'none',
}}
/>
<button className="btn btn--secondary" onClick={() => connect(address)} disabled={busy || !address}
style={{ height: 52, padding: '0 18px' }}>
{busy ? 'Έλεγχος…' : 'Σύνδεση'}
</button>
</div>
{error && (
<p style={{
fontSize: 14, color: 'var(--danger)', background: 'var(--danger-dim)',
borderRadius: 10, padding: '10px 12px', lineHeight: 1.45,
}}>
{error}
</p>
)}
</div>
{scanning && <QrScannerModal onResult={connect} onClose={() => setScanning(false)} />}
</div>
)
}