sozsoft-platform/ui/src/components/shared/BarcodeScanner/BarcodeScannerHost.tsx
2026-09-21 18:01:40 +03:00

33 lines
895 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useEffect, useState } from 'react'
import BarcodeScannerDialog from './BarcodeScannerDialog'
import { registerBarcodeScanner } from './scanBarcode'
/**
* `scanBarcode()` çağrılarını karşılayan tekil diyalog. Uygulama kökünde bir
* kez mount edilir; React ağacı dışından açılabilmesinin tek yolu budur.
*/
const BarcodeScannerHost = () => {
const [resolve, setResolve] = useState<((code: string | null) => void) | null>(null)
useEffect(() => {
registerBarcodeScanner((next) => setResolve(() => next))
return () => registerBarcodeScanner(undefined)
}, [])
const finish = (code: string | null) => {
resolve?.(code)
setResolve(null)
}
if (!resolve) return null
return (
<BarcodeScannerDialog
open
onDetect={(code) => finish(code)}
onDialogClose={() => finish(null)}
/>
)
}
export default BarcodeScannerHost