feat(waiter): native app finds the venue server again after its IP changes

When the saved server address stops answering, the native app re-checks it
once and then probes every address in the same /24 (same scheme and port,
nearest first, 48 in parallel, 1.5s timeout) for /api/system/identity,
switching only to the server that reports THIS venue's site_id - never to
"any Xenia server". A found address is saved on the venue and the app
reloads onto the same screen (/offline → start page) after a short
"Ο server άλλαξε διεύθυνση" notice; the offline queue syncs after reload.

- src/native/rediscovery.js: subnet candidates + scan (pure, injectable)
- src/native/autoRediscover.js: re-check first, one scan at a time,
  automatic attempts at most once a minute; skipped for dev / site-less venues
- ServerRediscovery: triggers when the connection is confirmed offline
  (retry every 2 min) or any request fails with a network error
- Offline page: manual "Αναζήτηση server στο δίκτυο" with progress and
  "not found → scan the QR" guidance

Tests: 11 unit checks (ordering, port kept, non-IP hosts, other venue never
chosen, early stop, progress). Emulator E2E with a real address change
(.99 → .2): logged in → found in ~3.5s, back on /tables with live WS;
logged out → found in ~6.5s, waiter list loads; server really down →
address untouched, manual search reports not found. Web modes unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-28 17:00:44 +03:00
co-authored by Claude Opus 5.5
parent cae5d75005
commit ab82598859
5 changed files with 232 additions and 1 deletions
@@ -0,0 +1,65 @@
import { useEffect, useState } from 'react'
import useConnectionStore from '../store/connectionStore'
import { canRediscover, rediscover } from '../native/autoRediscover'
const RETRY_WHILE_OFFLINE_MS = 2 * 60_000
const RELOAD_DELAY_MS = 1500
/**
* Native app: when the venue server stops answering, look for it at a new
* address on the same network and reconnect automatically (plan step 5).
*
* Triggers: the connection is confirmed offline (logged in), or any request
* fails with a network error (login / offline screens). Retries every 2 minutes
* while offline. On success it shows a short notice and reloads into the new
* address — the offline queue lives in IndexedDB and syncs after the reload.
*/
export default function ServerRediscovery() {
const status = useConnectionStore(s => s.status)
const [movedTo, setMovedTo] = useState(null)
const enabled = canRediscover()
useEffect(() => {
if (!enabled) return
let cancelled = false
async function attempt() {
const result = await rediscover()
if (cancelled || result.status !== 'moved') return
setMovedTo(result.baseUrl)
// Stay on the waiter's current screen — except /offline, which only
// exists because the server was unreachable; start over from there.
const target = window.location.pathname === '/offline' ? '/' : window.location.href
setTimeout(() => window.location.replace(target), RELOAD_DELAY_MS)
}
window.addEventListener('backend-offline', attempt)
let timer = null
if (status === 'offline') {
attempt()
timer = setInterval(attempt, RETRY_WHILE_OFFLINE_MS)
}
return () => {
cancelled = true
window.removeEventListener('backend-offline', attempt)
clearInterval(timer)
}
}, [enabled, status])
if (!movedTo) return null
return (
<div style={{
position: 'fixed', inset: 0, zIndex: 10001, background: 'rgba(15,23,42,0.92)',
display: 'flex', alignItems: 'center', justifyContent: 'center', padding: 32, textAlign: 'center',
}}>
<div>
<div style={{ fontSize: 44, marginBottom: 12 }}>🔄</div>
<p style={{ color: '#e2e8f0', fontSize: 17, fontWeight: 600, marginBottom: 6 }}>
Ο server άλλαξε διεύθυνση
</p>
<p style={{ color: '#94a3b8', fontSize: 14, fontFamily: 'monospace' }}>{movedTo}</p>
<p style={{ color: '#94a3b8', fontSize: 14, marginTop: 10 }}>Επανασύνδεση…</p>
</div>
</div>
)
}