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:
@@ -1,10 +1,13 @@
|
||||
import { useState } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { deliveryMode } from '../config/server'
|
||||
|
||||
// Shown only in plain-HTTP browser mode (http://<LAN IP>), where the page can't
|
||||
// work offline after a reload and can't be installed as a real PWA. Hidden until
|
||||
// an app download link exists (VITE_APP_DOWNLOAD_URL at build time).
|
||||
const DOWNLOAD_URL = import.meta.env.VITE_APP_DOWNLOAD_URL || ''
|
||||
// work offline after a reload and can't be installed as a real PWA.
|
||||
//
|
||||
// The APK is served by the venue's own server (scripts/build-apk.mjs bundles the
|
||||
// release APK into the web build), so the banner appears only when that file is
|
||||
// actually there. VITE_APP_DOWNLOAD_URL overrides the location if ever needed.
|
||||
const DOWNLOAD_URL = import.meta.env.VITE_APP_DOWNLOAD_URL || '/downloads/xenia-waiter.apk'
|
||||
const DISMISS_KEY = 'install-banner-dismissed-until'
|
||||
const DISMISS_DAYS = 7
|
||||
|
||||
@@ -17,9 +20,18 @@ function isDismissed() {
|
||||
}
|
||||
|
||||
export default function InstallAppBanner() {
|
||||
const [hidden, setHidden] = useState(isDismissed)
|
||||
const eligible = deliveryMode() === 'plain-web' && !isDismissed()
|
||||
const [available, setAvailable] = useState(false)
|
||||
const [hidden, setHidden] = useState(false)
|
||||
|
||||
if (!DOWNLOAD_URL || hidden || deliveryMode() !== 'plain-web') return null
|
||||
useEffect(() => {
|
||||
if (!eligible) return
|
||||
fetch(DOWNLOAD_URL, { method: 'HEAD' })
|
||||
.then(r => setAvailable(r.ok && !(r.headers.get('content-type') || '').includes('text/html')))
|
||||
.catch(() => {})
|
||||
}, [eligible])
|
||||
|
||||
if (!eligible || !available || hidden) return null
|
||||
|
||||
function dismiss() {
|
||||
try {
|
||||
@@ -51,8 +63,7 @@ export default function InstallAppBanner() {
|
||||
</span>
|
||||
<a
|
||||
href={DOWNLOAD_URL}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
download
|
||||
style={{
|
||||
background: 'var(--accent)',
|
||||
color: '#0f172a',
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import QrScanner from 'qr-scanner'
|
||||
|
||||
const MSG_DENIED = 'Δεν δόθηκε άδεια κάμερας. Επιτρέψτε την από τις ρυθμίσεις του κινητού ή πληκτρολογήστε τη διεύθυνση.'
|
||||
const MSG_NO_CAMERA = 'Δεν ήταν δυνατή η ενεργοποίηση της κάμερας. Πληκτρολογήστε τη διεύθυνση.'
|
||||
|
||||
/**
|
||||
* Full-screen camera view that resolves the first QR code it sees.
|
||||
* Runs in the WebView (getUserMedia), so it needs no Google Play Services and
|
||||
* works the same on iOS later. Capacitor turns the WebView's camera request into
|
||||
* the normal Android runtime permission prompt.
|
||||
*/
|
||||
export default function QrScannerModal({ onResult, onClose }) {
|
||||
const videoRef = useRef(null)
|
||||
const [error, setError] = useState(null)
|
||||
|
||||
useEffect(() => {
|
||||
let done = false
|
||||
let scanner = null
|
||||
|
||||
async function start() {
|
||||
// Ask for the camera ONCE ourselves: qr-scanner retries with another camera
|
||||
// after a refusal, which would show the Android permission prompt twice, and
|
||||
// its errors don't tell "denied" apart from "no camera".
|
||||
try {
|
||||
const probe = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } })
|
||||
probe.getTracks().forEach(t => t.stop())
|
||||
} catch (err) {
|
||||
if (!done) setError(err?.name === 'NotAllowedError' ? MSG_DENIED : MSG_NO_CAMERA)
|
||||
return
|
||||
}
|
||||
if (done) return
|
||||
|
||||
scanner = new QrScanner(
|
||||
videoRef.current,
|
||||
result => {
|
||||
if (done) return
|
||||
done = true
|
||||
scanner.stop()
|
||||
onResult(result.data)
|
||||
},
|
||||
{ preferredCamera: 'environment', returnDetailedScanResult: true, highlightScanRegion: true },
|
||||
)
|
||||
scanner.start().catch(() => {
|
||||
if (!done) setError(MSG_NO_CAMERA)
|
||||
})
|
||||
}
|
||||
|
||||
start()
|
||||
return () => {
|
||||
done = true
|
||||
scanner?.destroy()
|
||||
}
|
||||
}, [onResult])
|
||||
|
||||
return (
|
||||
<div style={{
|
||||
position: 'fixed', inset: 0, zIndex: 10000, background: '#000',
|
||||
display: 'flex', flexDirection: 'column',
|
||||
}}>
|
||||
<div style={{ flex: 1, position: 'relative', overflow: 'hidden' }}>
|
||||
{/* 1px transparent poster hides the WebView's big "play" placeholder before the stream starts */}
|
||||
<video
|
||||
ref={videoRef}
|
||||
poster="data:image/gif;base64,R0lGODlhAQABAAAAACw="
|
||||
style={{ width: '100%', height: '100%', objectFit: 'cover' }}
|
||||
muted
|
||||
playsInline
|
||||
/>
|
||||
{error && (
|
||||
<div style={{
|
||||
position: 'absolute', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center',
|
||||
padding: 32, textAlign: 'center', color: '#e2e8f0', fontSize: 15, lineHeight: 1.5,
|
||||
background: 'rgba(15,23,42,0.92)',
|
||||
}}>
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div style={{ padding: 16, display: 'flex', flexDirection: 'column', gap: 10, background: '#0f172a' }}>
|
||||
{!error && (
|
||||
<p style={{ color: '#94a3b8', fontSize: 13, textAlign: 'center' }}>
|
||||
Σκοπεύστε το QR από τον διαχειριστή (Ρυθμίσεις → Γενικά → Σερβιτόροι WiFi)
|
||||
</p>
|
||||
)}
|
||||
<button className="btn btn--secondary btn--lg" onClick={onClose} style={{ width: '100%' }}>Ακύρωση</button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import useAuthStore from '../store/authStore'
|
||||
import useShiftStore from '../store/shiftStore'
|
||||
import useThemeStore from '../store/themeStore'
|
||||
import client from '../api/client'
|
||||
import { deliveryMode, getActiveVenue } from '../config/server'
|
||||
|
||||
function formatTime(iso) {
|
||||
if (!iso) return ''
|
||||
@@ -168,6 +169,14 @@ export default function UserMenu() {
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* ── Venue switch (native app only) ───────────────── */}
|
||||
{deliveryMode() === 'native' && (
|
||||
<button className="user-menu-item" onClick={() => { setOpen(false); navigate('/venues') }}>
|
||||
<span className="user-menu-item__icon">🏪</span>
|
||||
<span>Κατάστημα: {getActiveVenue()?.name ?? '—'}</span>
|
||||
</button>
|
||||
)}
|
||||
|
||||
{/* ── Theme toggle ──────────────────────────────────── */}
|
||||
<button className="user-menu-item" onClick={() => { toggle(); setOpen(false) }}>
|
||||
<span className="user-menu-item__icon">{dark ? '☀️' : '🌙'}</span>
|
||||
|
||||
Reference in New Issue
Block a user