@flowselections/scan-module 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"EindcontroleScanner.d.ts","sourceRoot":"","sources":["../../../src/components/scanners/EindcontroleScanner.tsx"],"names":[],"mappings":"AAyBA,wBAAgB,mBAAmB,CAAC,EAClC,IAAI,EACJ,SAAS,GACV,EAAE;IACD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,+BA2HA"}
1
+ {"version":3,"file":"EindcontroleScanner.d.ts","sourceRoot":"","sources":["../../../src/components/scanners/EindcontroleScanner.tsx"],"names":[],"mappings":"AA2BA,wBAAgB,mBAAmB,CAAC,EAClC,IAAI,EACJ,SAAS,GACV,EAAE;IACD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B,+BAiMA"}
@@ -6,7 +6,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
6
6
  // erbij hoort → die ene kar wordt afgemeld bij de Logistiek Engine. Een kar
7
7
  // die al afgehandeld is wordt nooit een tweede keer afgemeld.
8
8
  // ============================================================================
9
- import { useCallback, useRef, useState } from "react";
9
+ import { useCallback, useEffect, useRef, useState } from "react";
10
10
  import { Badge, Button, Card, CardContent, supabase, toast } from "@flowselections/core";
11
11
  import { CheckCircle2, ClipboardCheck, Loader2, XCircle } from "lucide-react";
12
12
  import { ScanInput } from "./ScanInput";
@@ -17,7 +17,23 @@ export function EindcontroleScanner({ naam, scannerId, }) {
17
17
  const [bezig, setBezig] = useState(false);
18
18
  const [resultaat, setResultaat] = useState(null);
19
19
  const [historie, setHistorie] = useState([]);
20
+ const [openstaand, setOpenstaand] = useState([]);
21
+ const [laden, setLaden] = useState(true);
20
22
  const bezigRef = useRef(false);
23
+ const laadOpenstaand = useCallback(async () => {
24
+ try {
25
+ const { data, error } = await db.rpc("scan_open_pakbonnen", { p_dagen: null });
26
+ if (error)
27
+ toast.error("Openstaande pakbonnen laden mislukt");
28
+ setOpenstaand(Array.isArray(data) ? data : []);
29
+ }
30
+ finally {
31
+ setLaden(false);
32
+ }
33
+ }, []);
34
+ useEffect(() => {
35
+ void laadOpenstaand();
36
+ }, [laadOpenstaand]);
21
37
  const scan = useCallback(async (barcode) => {
22
38
  const code = barcode.trim();
23
39
  if (!code || bezigRef.current)
@@ -64,6 +80,9 @@ export function EindcontroleScanner({ naam, scannerId, }) {
64
80
  toast.error(uitkomst.melding);
65
81
  setResultaat(uitkomst);
66
82
  setHistorie((vorig) => [uitkomst, ...vorig].slice(0, 10));
67
- }, [scannerId]);
68
- return (_jsxs("main", { className: "mx-auto max-w-2xl space-y-4 p-4", children: [_jsxs("header", { className: "flex items-center gap-3", children: [_jsx("span", { className: "grid h-11 w-11 shrink-0 place-items-center rounded-xl bg-primary/10 text-primary", children: _jsx(ClipboardCheck, { className: "h-6 w-6" }) }), _jsxs("div", { className: "min-w-0", children: [_jsx("h1", { className: "truncate text-lg font-black", children: naam ?? "Eindcontrole scanner" }), _jsx("p", { className: "text-xs text-muted-foreground", children: "Scan de verzendbrief van de kar" })] })] }), _jsx(ScanInput, { onScan: (code) => void scan(code), variant: "zone", autoStart: true, disabled: bezig, label: "Scan de verzendbrief", placeholder: "Barcode verzendbrief" }), bezig ? (_jsx("div", { className: "grid place-items-center py-6", children: _jsx(Loader2, { className: "h-6 w-6 animate-spin text-muted-foreground" }) })) : null, resultaat ? (_jsx(Card, { className: resultaat.ok ? "border-primary" : "border-destructive", children: _jsxs(CardContent, { className: "space-y-2 py-5 text-center", children: [resultaat.ok ? (_jsx(CheckCircle2, { className: "mx-auto h-12 w-12 text-primary" })) : (_jsx(XCircle, { className: "mx-auto h-12 w-12 text-destructive" })), _jsx("p", { className: "text-lg font-black", children: resultaat.karLabel ?? "Kar" }), _jsxs("p", { className: "text-sm text-muted-foreground", children: [resultaat.orderNumber ? `Order ${resultaat.orderNumber}` : "", resultaat.klant ? ` · ${resultaat.klant}` : ""] }), _jsx("p", { className: resultaat.ok ? "font-semibold text-primary" : "font-semibold text-destructive", children: resultaat.melding }), _jsx(Button, { variant: "outline", className: "h-11", onClick: () => setResultaat(null), children: "Volgende kar" })] }) })) : null, historie.length > 1 ? (_jsxs("div", { className: "space-y-2", children: [_jsx("p", { className: "text-xs font-semibold uppercase text-muted-foreground", children: "Laatste scans" }), historie.slice(1).map((h, i) => (_jsxs("div", { className: "flex items-center gap-2 rounded-lg border p-2 text-sm", children: [_jsx(Badge, { variant: h.ok ? "default" : "secondary", children: h.ok ? "OK" : "Let op" }), _jsxs("span", { className: "truncate", children: [h.karLabel ?? "Kar", " ", h.orderNumber ? `· ${h.orderNumber}` : ""] })] }, i)))] })) : null] }));
83
+ void laadOpenstaand();
84
+ }, [scannerId, laadOpenstaand]);
85
+ return (_jsxs("main", { className: "mx-auto max-w-2xl space-y-4 p-4", children: [_jsxs("header", { className: "flex items-center gap-3", children: [_jsx("span", { className: "grid h-11 w-11 shrink-0 place-items-center rounded-xl bg-primary/10 text-primary", children: _jsx(ClipboardCheck, { className: "h-6 w-6" }) }), _jsxs("div", { className: "min-w-0", children: [_jsx("h1", { className: "truncate text-lg font-black", children: naam ?? "Eindcontrole scanner" }), _jsx("p", { className: "text-xs text-muted-foreground", children: "Scan de verzendbrief van de kar" })] })] }), _jsx(ScanInput, { onScan: (code) => void scan(code), variant: "zone", autoStart: true, disabled: bezig, label: "Scan de verzendbrief", placeholder: "Barcode verzendbrief" }), bezig ? (_jsx("div", { className: "grid place-items-center py-6", children: _jsx(Loader2, { className: "h-6 w-6 animate-spin text-muted-foreground" }) })) : null, resultaat ? (_jsx(Card, { className: resultaat.ok ? "border-primary" : "border-destructive", children: _jsxs(CardContent, { className: "space-y-2 py-5 text-center", children: [resultaat.ok ? (_jsx(CheckCircle2, { className: "mx-auto h-12 w-12 text-primary" })) : (_jsx(XCircle, { className: "mx-auto h-12 w-12 text-destructive" })), _jsx("p", { className: "text-lg font-black", children: resultaat.karLabel ?? "Kar" }), _jsxs("p", { className: "text-sm text-muted-foreground", children: [resultaat.orderNumber ? `Order ${resultaat.orderNumber}` : "", resultaat.klant ? ` · ${resultaat.klant}` : ""] }), _jsx("p", { className: resultaat.ok ? "font-semibold text-primary" : "font-semibold text-destructive", children: resultaat.melding }), _jsx(Button, { variant: "outline", className: "h-11", onClick: () => setResultaat(null), children: "Volgende kar" })] }) })) : null, historie.length > 1 ? (_jsxs("div", { className: "space-y-2", children: [_jsx("p", { className: "text-xs font-semibold uppercase text-muted-foreground", children: "Laatste scans" }), historie.slice(1).map((h, i) => (_jsxs("div", { className: "flex items-center gap-2 rounded-lg border p-2 text-sm", children: [_jsx(Badge, { variant: h.ok ? "default" : "secondary", children: h.ok ? "OK" : "Let op" }), _jsxs("span", { className: "truncate", children: [h.karLabel ?? "Kar", " ", h.orderNumber ? `· ${h.orderNumber}` : ""] })] }, i)))] })) : null, _jsxs("section", { className: "space-y-2", children: [_jsxs("div", { className: "flex items-center justify-between gap-3", children: [_jsx("h2", { className: "text-sm font-semibold uppercase text-muted-foreground", children: "Openstaande pakbonnen" }), _jsx(Badge, { variant: "secondary", className: "shrink-0 tabular-nums", children: openstaand.length })] }), laden ? (_jsx("div", { className: "grid place-items-center py-8", children: _jsx(Loader2, { className: "h-6 w-6 animate-spin text-muted-foreground" }) })) : openstaand.length === 0 ? (_jsx(Card, { children: _jsx(CardContent, { className: "py-8 text-center text-sm text-muted-foreground", children: "Geen openstaande pakbonnen. Alles is afgehandeld." }) })) : (_jsx("div", { className: "space-y-2", children: openstaand.map((p) => (_jsxs("button", { type: "button", disabled: bezig, onClick: () => void scan(p.barcode), className: "w-full rounded-xl border p-3 text-left transition hover:border-primary disabled:opacity-50", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("span", { className: "truncate font-bold", children: p.order_number ?? "Order onbekend" }), _jsx(Badge, { variant: p.soort === "hele_kar" ? "default" : "secondary", children: p.soort === "hele_kar" ? "Hele kar" : "Deelkar" })] }), _jsx("p", { className: "mt-1 truncate text-sm text-muted-foreground", children: p.klant_naam ?? "Klant onbekend" }), _jsxs("p", { className: "mt-1 truncate font-mono text-xs text-muted-foreground", children: [p.barcode, p.cart_index != null
86
+ ? ` · Kar ${p.cart_index} van ${p.cart_total ?? "?"}`
87
+ : ""] })] }, p.barcode))) }))] })] }));
69
88
  }
@@ -1 +1 @@
1
- {"version":3,"file":"useScanEngine.d.ts","sourceRoot":"","sources":["../../src/hooks/useScanEngine.ts"],"names":[],"mappings":"AAoBA,UAAU,iBAAiB;IACzB,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B,6EAA6E;IAC7E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAOD,wBAAgB,aAAa,CAAC,EAC5B,MAAM,EACN,UAAiB,EACjB,OAAc,GACf,EAAE,iBAAiB;;;;;;;kBAwBV,MAAM;EAqOf;AAED,oDAAoD;AACpD,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,GAAG,MAAM,QAqBzD"}
1
+ {"version":3,"file":"useScanEngine.d.ts","sourceRoot":"","sources":["../../src/hooks/useScanEngine.ts"],"names":[],"mappings":"AAoBA,UAAU,iBAAiB;IACzB,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC/B,6EAA6E;IAC7E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAOD,wBAAgB,aAAa,CAAC,EAC5B,MAAM,EACN,UAAiB,EACjB,OAAc,GACf,EAAE,iBAAiB;;;;;;;kBAwBV,MAAM;EAwQf;AAED,oDAAoD;AACpD,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,GAAG,MAAM,QAqBzD"}
@@ -123,7 +123,7 @@ export function useScanEngine({ onScan, debounceMs = 1200, enabled = true, }) {
123
123
  if (!cameraActive)
124
124
  return;
125
125
  let cancelled = false;
126
- let timer = 0;
126
+ let gelukt = false;
127
127
  let detector = null;
128
128
  try {
129
129
  const Ctor = window.BarcodeDetector;
@@ -133,98 +133,59 @@ export function useScanEngine({ onScan, debounceMs = 1200, enabled = true, }) {
133
133
  catch {
134
134
  detector = null;
135
135
  }
136
- // Bijhouden welke lezers echt draaien: pas als er niets werkt tonen we een
137
- // melding. Anders blijft de camera gewoon bruikbaar via de andere sporen.
136
+ // Werkcanvas: we knippen het kadergebied uit en vergroten het. Een
137
+ // streepjescode die maar een deel van het beeld vult wordt zo veel
138
+ // betrouwbaarder gelezen dan het volledige camerabeeld.
139
+ const canvas = document.createElement("canvas");
140
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
141
+ const knipKader = (video, marge) => {
142
+ const bw = video.videoWidth;
143
+ const bh = video.videoHeight;
144
+ if (!bw || !bh || !ctx)
145
+ return null;
146
+ const x = Math.round(bw * marge);
147
+ const b = Math.round(bw * (1 - marge * 2));
148
+ const y = Math.round(bh * (0.5 - 0.22));
149
+ const h = Math.round(bh * 0.44);
150
+ const schaal = b < 900 ? 2 : 1;
151
+ canvas.width = b * schaal;
152
+ canvas.height = h * schaal;
153
+ ctx.drawImage(video, x, y, b, h, 0, 0, canvas.width, canvas.height);
154
+ return canvas;
155
+ };
138
156
  let nativeOk = false;
139
157
  let ocrOk = false;
140
158
  let zxingOk = false;
141
159
  const run = async () => {
142
- if (detector) {
143
- nativeOk = true;
144
- const tick = async () => {
145
- if (cancelled)
146
- return;
147
- const video = videoRef.current;
148
- if (video && video.readyState >= 2) {
149
- try {
150
- const results = await detector.detect(video);
151
- if (results?.[0]?.rawValue)
152
- submit(results[0].rawValue);
153
- }
154
- catch {
155
- /* frame overslaan */
156
- }
157
- }
158
- timer = window.setTimeout(tick, 120);
159
- };
160
- void tick();
161
- }
162
- // Tweede spoor: de code die in tekst ONDER de streepjescode staat.
163
- // Loopt parallel aan de barcodelezers — wat het eerst een geldige code
164
- // oplevert, wint (dubbele treffers vangt de debounce op).
165
- void (async () => {
166
- const lezer = await tekstLezer();
167
- if (!lezer || cancelled)
168
- return;
169
- ocrOk = true;
170
- while (!cancelled) {
171
- const video = videoRef.current;
172
- if (video && video.readyState >= 2) {
173
- try {
174
- const strook = knipStrook(video);
175
- if (strook) {
176
- const { data } = await lezer.recognize(strook);
177
- const code = codeUitTekst(data?.text ?? "");
178
- if (code && !cancelled)
179
- submit(code);
180
- }
181
- }
182
- catch {
183
- /* frame overslaan */
184
- }
185
- }
186
- await new Promise((r) => window.setTimeout(r, 250));
187
- }
188
- })();
189
- // ZXing draait altijd mee: naast de native BarcodeDetector als extra
190
- // decoder (die herkent niet elk formaat/conditie), of als enige fallback.
160
+ // ── Spoor 1: ZXing op het uitgeknipte, vergrote kader ──────────────
161
+ let leesKader = null;
191
162
  const startZxing = async () => {
192
163
  const { BrowserMultiFormatReader } = await import("@zxing/browser");
193
164
  const { DecodeHintType, BarcodeFormat } = await import("@zxing/library");
194
165
  if (cancelled)
195
166
  return;
196
- // TRY_HARDER: meer moeite doen per frame (lichtelijk trager, veel betrouwbaarder)
197
167
  const hints = new Map();
198
168
  hints.set(DecodeHintType.TRY_HARDER, true);
199
- // Alleen de formaten die op onze pakbonnen staan, zodat de decoder niet
200
- // naar een ander formaat gokt en een halve code teruggeeft.
201
169
  hints.set(DecodeHintType.POSSIBLE_FORMATS, [
202
170
  BarcodeFormat.CODE_128,
203
171
  BarcodeFormat.QR_CODE,
204
172
  ]);
205
- const reader = new BrowserMultiFormatReader(hints, {
206
- delayBetweenScanAttempts: 150,
207
- });
208
- const video = videoRef.current;
209
- if (!video)
210
- throw new Error("videoElement ontbreekt");
211
- const controls = await reader.decodeFromVideoElement(video, (result) => {
212
- if (!cancelled && result)
213
- submit(result.getText());
214
- });
215
- if (cancelled)
216
- controls.stop();
217
- else
218
- zxingStopRef.current = () => controls.stop();
173
+ const reader = new BrowserMultiFormatReader(hints);
174
+ leesKader = (bron) => {
175
+ try {
176
+ return reader.decodeFromCanvas(bron)?.getText() ?? null;
177
+ }
178
+ catch {
179
+ return null; // geen code in dit beeld
180
+ }
181
+ };
219
182
  zxingOk = true;
220
183
  };
221
184
  try {
222
185
  await startZxing();
223
186
  }
224
187
  catch (eerste) {
225
- // Eén keer opnieuw: een hapering in het netwerk of een verlopen versie
226
- // van het losse codeleesbestand is meestal bij de tweede poging weg.
227
- console.warn("[scan] ZXing starten mislukt (poging 1)", eerste);
188
+ console.warn("[scan] ZXing laden mislukt (poging 1)", eerste);
228
189
  await new Promise((r) => window.setTimeout(r, 600));
229
190
  if (cancelled)
230
191
  return;
@@ -233,19 +194,86 @@ export function useScanEngine({ onScan, debounceMs = 1200, enabled = true, }) {
233
194
  }
234
195
  catch (tweede) {
235
196
  const melding = tweede instanceof Error ? tweede.message : String(tweede);
236
- console.warn("[scan] ZXing starten mislukt (poging 2)", tweede);
197
+ console.warn("[scan] ZXing laden mislukt (poging 2)", tweede);
237
198
  void logScan("", "zxing_start_mislukt", melding, null);
238
- if (!cancelled && !nativeOk && !ocrOk) {
239
- setCameraError("Codes herkennen lukt niet in deze browser. Typ de code of gebruik een scanner.");
199
+ }
200
+ }
201
+ nativeOk = !!detector;
202
+ // Eén lus over de beelden: eerst het kader, dan wat ruimer. Beide
203
+ // decoders krijgen hetzelfde beeld, dus de telefoon doet het werk maar
204
+ // één keer per frame.
205
+ const marges = [0.08, 0.02, 0.2];
206
+ let beurt = 0;
207
+ const lus = async () => {
208
+ while (!cancelled) {
209
+ const video = videoRef.current;
210
+ if (video && video.readyState >= 2) {
211
+ const bron = knipKader(video, marges[beurt % marges.length]);
212
+ beurt += 1;
213
+ if (bron) {
214
+ if (detector) {
215
+ try {
216
+ const results = await detector.detect(bron);
217
+ const waarde = results?.[0]?.rawValue;
218
+ if (waarde) {
219
+ gelukt = true;
220
+ submit(waarde);
221
+ }
222
+ }
223
+ catch {
224
+ /* frame overslaan */
225
+ }
226
+ }
227
+ if (!cancelled && leesKader) {
228
+ const tekst = leesKader(bron);
229
+ if (tekst) {
230
+ gelukt = true;
231
+ submit(tekst);
232
+ }
233
+ }
234
+ }
235
+ }
236
+ await new Promise((r) => window.setTimeout(r, 90));
237
+ }
238
+ };
239
+ void lus();
240
+ // ── Spoor 2: de tekstregel onder de streepjescode ───────────────────
241
+ // Pas na een paar seconden zonder treffer, want tekstherkenning is zwaar
242
+ // en zou anders rekenkracht van de streepjescodelezer afsnoepen.
243
+ void (async () => {
244
+ await new Promise((r) => window.setTimeout(r, 4000));
245
+ if (cancelled || gelukt)
246
+ return;
247
+ const lezer = await tekstLezer();
248
+ if (!lezer || cancelled)
249
+ return;
250
+ ocrOk = true;
251
+ while (!cancelled) {
252
+ const video = videoRef.current;
253
+ if (video && video.readyState >= 2) {
254
+ try {
255
+ const strook = knipStrook(video);
256
+ if (strook) {
257
+ const { data } = await lezer.recognize(strook);
258
+ const code = codeUitTekst(data?.text ?? "");
259
+ if (code && !cancelled)
260
+ submit(code);
261
+ }
262
+ }
263
+ catch {
264
+ /* frame overslaan */
265
+ }
240
266
  }
267
+ await new Promise((r) => window.setTimeout(r, 900));
241
268
  }
269
+ })();
270
+ if (!cancelled && !zxingOk && !nativeOk) {
271
+ setCameraError("Codes herkennen lukt niet in deze browser. Typ de code of gebruik een scanner.");
242
272
  }
243
- void zxingOk;
244
273
  };
245
274
  void run();
246
275
  return () => {
247
276
  cancelled = true;
248
- window.clearTimeout(timer);
249
277
  zxingStopRef.current?.();
250
278
  zxingStopRef.current = null;
251
279
  };