mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-05 08:23:35 +00:00
added qr code scanner
This commit is contained in:
1 parent
97613fc5b2
commit
a432716f47
5 files changed
+285
-6
No files matched your search
+126
-2
@@ -1,11 +1,24 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
import { useState, useCallback, useRef } from 'react';
|
||||
import QRCode from 'qrcode';
|
||||
import { Html5Qrcode } from 'html5-qrcode';
|
||||
import { useToast } from './useToast';
|
||||
|
||||
export type QRScanResult =
|
||||
| { type: 'lightning_invoice'; value: string }
|
||||
| { type: 'lightning_address'; value: string }
|
||||
| { type: 'cashu_token'; value: string }
|
||||
| { type: 'bitcoin_address'; value: string }
|
||||
| { type: 'npub'; value: string }
|
||||
| { type: 'unknown'; value: string };
|
||||
|
||||
interface UseQRCodeReturn {
|
||||
generateQRCode: (text: string) => Promise<string>;
|
||||
isGenerating: boolean;
|
||||
error: string | null;
|
||||
// Scanner functions
|
||||
isScanning: boolean;
|
||||
startScanning: (onResult: (result: QRScanResult) => void) => Promise<void>;
|
||||
stopScanning: () => Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,6 +34,9 @@ interface UseQRCodeReturn {
|
||||
export function useQRCode(): UseQRCodeReturn {
|
||||
const [isGenerating, setIsGenerating] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [isScanning, setIsScanning] = useState(false);
|
||||
const [scanError, setScanError] = useState<string | null>(null);
|
||||
const scannerRef = useRef<Html5Qrcode | null>(null);
|
||||
const { toast } = useToast();
|
||||
|
||||
const generateQRCode = useCallback(async (text: string): Promise<string> => {
|
||||
@@ -54,9 +70,117 @@ export function useQRCode(): UseQRCodeReturn {
|
||||
}
|
||||
}, [toast]);
|
||||
|
||||
const classifyQRCode = useCallback((decodedText: string): QRScanResult => {
|
||||
// Lightning invoice: lnbc... (case insensitive)
|
||||
if (/^lnbc[a-z0-9]+$/i.test(decodedText)) {
|
||||
return { type: 'lightning_invoice', value: decodedText };
|
||||
}
|
||||
|
||||
// Lightning address: [email protected]
|
||||
if (/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(decodedText)) {
|
||||
return { type: 'lightning_address', value: decodedText };
|
||||
}
|
||||
|
||||
// Cashu token: cashu...
|
||||
if (/^cashu[A-Za-z0-9+/=]+$/.test(decodedText)) {
|
||||
return { type: 'cashu_token', value: decodedText };
|
||||
}
|
||||
|
||||
// Bitcoin address: bc1... or legacy (1..., 3...)
|
||||
if (/^(bc1|3|1)[a-zA-Z0-9]{25,62}$/.test(decodedText)) {
|
||||
return { type: 'bitcoin_address', value: decodedText };
|
||||
}
|
||||
|
||||
// Nostr npub: npub1...
|
||||
if (/^npub1[a-z0-9]{58}$/.test(decodedText)) {
|
||||
return { type: 'npub', value: decodedText };
|
||||
}
|
||||
|
||||
return { type: 'unknown', value: decodedText };
|
||||
}, []);
|
||||
|
||||
const startScanning = useCallback(async (onResult: (result: QRScanResult) => void): Promise<void> => {
|
||||
try {
|
||||
setScanError(null);
|
||||
|
||||
// Find available cameras
|
||||
const devices = await Html5Qrcode.getCameras();
|
||||
if (devices.length === 0) {
|
||||
throw new Error('No cameras found');
|
||||
}
|
||||
|
||||
// Use the back camera by default, or first available
|
||||
const cameraId = devices.length > 1
|
||||
? devices.find(d => d.label.includes('back'))?.id || devices[0].id
|
||||
: devices[0].id;
|
||||
|
||||
// Initialize scanner
|
||||
const scanner = new Html5Qrcode('qr-reader');
|
||||
scannerRef.current = scanner;
|
||||
|
||||
// Start scanning
|
||||
await scanner.start(
|
||||
cameraId,
|
||||
{
|
||||
fps: 10,
|
||||
qrbox: { width: 250, height: 250 },
|
||||
},
|
||||
(decodedText) => {
|
||||
const result = classifyQRCode(decodedText);
|
||||
onResult(result);
|
||||
},
|
||||
(errorMessage) => {
|
||||
// Ignore decoding errors, camera is still running
|
||||
// Only log if it's not a "QR code not found" type error
|
||||
if (!errorMessage.includes('qr')) {
|
||||
console.debug('QR scan error:', errorMessage);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
setIsScanning(true);
|
||||
} catch (err) {
|
||||
let errorMessage = 'Failed to start camera';
|
||||
if (err instanceof Error) {
|
||||
errorMessage = err.message;
|
||||
} else if (typeof err === 'string') {
|
||||
errorMessage = err;
|
||||
}
|
||||
|
||||
// Provide more helpful error messages
|
||||
if (errorMessage.includes('NotAllowedError') || errorMessage.includes('Permission denied')) {
|
||||
errorMessage = 'Camera permission denied. Please allow camera access.';
|
||||
} else if (errorMessage.includes('NotFoundError') || errorMessage.includes('No device')) {
|
||||
errorMessage = 'No camera found. Please connect a camera.';
|
||||
}
|
||||
|
||||
setScanError(errorMessage);
|
||||
setIsScanning(false);
|
||||
console.error('Camera error:', err);
|
||||
}
|
||||
}, [classifyQRCode]);
|
||||
|
||||
const stopScanning = useCallback(async (): Promise<void> => {
|
||||
try {
|
||||
if (scannerRef.current) {
|
||||
await scannerRef.current.stop();
|
||||
await scannerRef.current.clear();
|
||||
scannerRef.current = null;
|
||||
}
|
||||
setIsScanning(false);
|
||||
setScanError(null);
|
||||
} catch (err) {
|
||||
console.error('Error stopping scanner:', err);
|
||||
setIsScanning(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
return {
|
||||
generateQRCode,
|
||||
isGenerating,
|
||||
error,
|
||||
error: scanError,
|
||||
isScanning,
|
||||
startScanning,
|
||||
stopScanning,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user