mirror of
https://github.com/xtr-dev/rondevu-demo.git
synced 2025-12-10 18:53:24 +00:00
Fix QR code display and camera selection
- Show QR code while waiting for peer to join (step 3) - Use connection ID variable directly instead of state - Prefer back/environment camera for QR scanning - Add camera selection logging for debugging
This commit is contained in:
73
src/App.jsx
73
src/App.jsx
@@ -142,18 +142,35 @@ function App() {
|
|||||||
log('Connecting...', 'info');
|
log('Connecting...', 'info');
|
||||||
|
|
||||||
let connection;
|
let connection;
|
||||||
|
let connId;
|
||||||
|
|
||||||
if (action === 'create') {
|
if (action === 'create') {
|
||||||
if (method === 'connection-id') {
|
if (method === 'connection-id') {
|
||||||
const id = connectionId || `conn-${Date.now()}`;
|
connId = connectionId || `conn-${Date.now()}`;
|
||||||
connection = await rdv.create(id, topic || 'default');
|
connection = await rdv.create(connId, topic || 'default');
|
||||||
setCurrentConnectionId(id);
|
setCurrentConnectionId(connId);
|
||||||
log(`Created connection: ${id}`, 'success');
|
log(`Created connection: ${connId}`, 'success');
|
||||||
} else {
|
} else {
|
||||||
const id = `conn-${Date.now()}`;
|
connId = `conn-${Date.now()}`;
|
||||||
connection = await rdv.create(id, topic);
|
connection = await rdv.create(connId, topic);
|
||||||
setCurrentConnectionId(id);
|
setCurrentConnectionId(connId);
|
||||||
log(`Created connection: ${id}`, 'success');
|
log(`Created connection: ${connId}`, 'success');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate QR code if creating a connection
|
||||||
|
try {
|
||||||
|
const qrUrl = await QRCode.toDataURL(connId, {
|
||||||
|
width: 256,
|
||||||
|
margin: 2,
|
||||||
|
color: {
|
||||||
|
dark: '#667eea',
|
||||||
|
light: '#ffffff'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
setQrCodeUrl(qrUrl);
|
||||||
|
log('QR code generated', 'success');
|
||||||
|
} catch (err) {
|
||||||
|
log(`QR code generation error: ${err.message}`, 'error');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (method === 'topic') {
|
if (method === 'topic') {
|
||||||
@@ -172,23 +189,6 @@ function App() {
|
|||||||
|
|
||||||
setConnectedPeer(connection.remotePeerId || 'Waiting...');
|
setConnectedPeer(connection.remotePeerId || 'Waiting...');
|
||||||
setupConnection(connection);
|
setupConnection(connection);
|
||||||
|
|
||||||
// Generate QR code if creating a connection
|
|
||||||
if (action === 'create' && currentConnectionId) {
|
|
||||||
try {
|
|
||||||
const qrUrl = await QRCode.toDataURL(currentConnectionId, {
|
|
||||||
width: 256,
|
|
||||||
margin: 2,
|
|
||||||
color: {
|
|
||||||
dark: '#667eea',
|
|
||||||
light: '#ffffff'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
setQrCodeUrl(qrUrl);
|
|
||||||
} catch (err) {
|
|
||||||
log(`QR code generation error: ${err.message}`, 'error');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log(`Error: ${error.message}`, 'error');
|
log(`Error: ${error.message}`, 'error');
|
||||||
setConnectionStatus('disconnected');
|
setConnectionStatus('disconnected');
|
||||||
@@ -207,7 +207,20 @@ function App() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const selectedDeviceId = videoInputDevices[0].deviceId;
|
// Prefer back camera (environment-facing)
|
||||||
|
let selectedDeviceId = videoInputDevices[0].deviceId;
|
||||||
|
const backCamera = videoInputDevices.find(device =>
|
||||||
|
device.label.toLowerCase().includes('back') ||
|
||||||
|
device.label.toLowerCase().includes('rear') ||
|
||||||
|
device.label.toLowerCase().includes('environment')
|
||||||
|
);
|
||||||
|
|
||||||
|
if (backCamera) {
|
||||||
|
selectedDeviceId = backCamera.deviceId;
|
||||||
|
log('Using back camera', 'info');
|
||||||
|
} else {
|
||||||
|
log('Back camera not found, using default', 'info');
|
||||||
|
}
|
||||||
|
|
||||||
scannerRef.current.decodeFromVideoDevice(
|
scannerRef.current.decodeFromVideoDevice(
|
||||||
selectedDeviceId,
|
selectedDeviceId,
|
||||||
@@ -648,6 +661,14 @@ function App() {
|
|||||||
{connectionStatus === 'connecting' ? 'Connecting...' : 'Connect'}
|
{connectionStatus === 'connecting' ? 'Connecting...' : 'Connect'}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{qrCodeUrl && connectionStatus === 'connecting' && action === 'create' && (
|
||||||
|
<div className="qr-code-container">
|
||||||
|
<p className="qr-label">Scan to connect:</p>
|
||||||
|
<img src={qrCodeUrl} alt="Connection QR Code" className="qr-code" />
|
||||||
|
<p className="connection-id-display">{currentConnectionId}</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user