Hi POS team,
I think that I found a bug in useStatefulSubscribableScannerData
: a colleague noticed that the camera scanner adds a leading 0
to UPC-A barcodes, It seems that the camera scanner might be interpreting them as EAN-13.
Here’s a simplified version of how I’m using the hook with the <CameraScanner />
component. I added a toast to log the actual value:
import { useEffect, useState } from "react";
import {
useStatefulSubscribableScannerData,
useApi,
} from "@shopify/ui-extensions-react/point-of-sale";
export function usePosScanner() {
const statefulSubscribableScanner = useStatefulSubscribableScannerData();
const [lastScanData, setLastScanData] = useState<ScanData>();
const { toast } = useApi<"pos.home.modal.render">();
useEffect(() => {
const initialDate = new Date();
return statefulSubscribableScanner.subscribe((x) => {
const { data, source } = x;
if (!source || !data) return;
toast.show(JSON.stringify({ data, source }));
if (initialDate.getTime() === Date.now()) {
// The subscribe callback fires immediately with the last scan result.
// This is undesired, as this would happen even closing and reopening an extension.
return;
}
// Debounce if using the camera and scanning the same data.
if (
lastScanData &&
source === "camera" &&
data === lastScanData.value &&
lastScanData.date.getTime() + DUPLICATED_SCAN_INTERVAL > Date.now()
) {
return;
}
setLastScanData({ value: data, source, date: new Date() });
});
}, [statefulSubscribableScanner, lastScanData]);
return lastScanData;
}