"use client"; import React, { useEffect, useState } from "react"; import { Wifi, WifiOff, CloudUpload, Lock } from "lucide-react"; import { apiFetch } from "@/lib/api"; import type { OtherLock } from "./useOfflineScanning"; type Offline = ReturnType; /** * Offline-mode controls above the scanner: shows who holds which section, lets this * device lock the selected section and download its tickets, and shows the sync queue. */ export function OfflineScanPanel({ offline, token, eventId, eventTitle, sectionId, sectionName, onError, onInfo }: { offline: Offline; token: string | null; eventId: string; eventTitle: string; sectionId: string | null; sectionName: string | null; onError: (m: string) => void; onInfo: (m: string) => void; }) { const [locks, setLocks] = useState([]); const [busy, setBusy] = useState(false); const { lock, pending, online, syncing } = offline; useEffect(() => { if (!token || !eventId || eventId === "all" || !online) { setLocks([]); return; } apiFetch(`/api/scan-locks?eventId=${encodeURIComponent(eventId)}`, { authToken: token }) .then(setLocks).catch(() => setLocks([])); }, [token, eventId, online, lock]); const others = locks.filter(l => l.deviceId !== offline.deviceId()); const enable = async () => { setBusy(true); try { const n = await offline.enable(eventId, eventTitle, sectionId, sectionName); onInfo(`Offline mode on — ${n} ticket(s) downloaded. You can keep scanning without a connection.`); } catch (e: any) { onError(e?.message || "Couldn't turn on offline mode"); } finally { setBusy(false); } }; const release = async () => { setBusy(true); try { await offline.release(); onInfo("Offline mode ended and the section is unlocked."); } catch (e: any) { onError(e?.message || "Couldn't end offline mode"); } finally { setBusy(false); } }; const syncNow = async () => { try { const r = await offline.sync(); if (r) onInfo(`Synced ${r.accepted} scan(s)${r.conflicts ? ` — ${r.conflicts} need a supervisor to review` : ""}.`); } catch (e: any) { onError(e?.message || "Sync failed — scans are kept on this device and will retry."); } }; return (
{online ? : } {online ? "Online" : "Offline"} {lock ? ( <> · Offline mode: {lock.eventTitle}{lock.sectionName ? ` — ${lock.sectionName}` : " — whole event"} Ticket list downloaded at {new Date(lock.snapshotAt).toLocaleTimeString()} · {pending} scan(s) waiting to sync
) : eventId && eventId !== "all" ? (
) : ( Pick an event to use offline mode. )}
{others.length > 0 && (
    {others.map(l => (
  • {l.sectionName ? `"${l.sectionName}"` : "The whole event"} is offline-locked by {l.user.name}{l.deviceName ? ` on ${l.deviceName}` : ""} — scan it on that device.
  • ))}
)} {lock && !online &&

Scans are saved on this device and will sync when the connection is back. Tickets bought after the download time won't be on the list.

}
); }