Add URL parameter ingestion and postMessage parent bridge handler
This commit is contained in:
+54
@@ -36,6 +36,60 @@ export default function App() {
|
|||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// Ingest Patient Data from URL Search Params or Parent postMessage (WebView Bridge)
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window === 'undefined') return;
|
||||||
|
|
||||||
|
// Method 1: Check URL Query Parameters (e.g. ?first_name=Ramesh&phone=9876543210&autostart=true)
|
||||||
|
const urlParams = new URLSearchParams(window.location.search);
|
||||||
|
const autostart = urlParams.get('autostart') === 'true' || urlParams.get('autoStart') === 'true';
|
||||||
|
const firstName = urlParams.get('first_name') || urlParams.get('firstName') || urlParams.get('name');
|
||||||
|
|
||||||
|
if (autostart && firstName) {
|
||||||
|
const patient = {
|
||||||
|
first_name: firstName,
|
||||||
|
last_name: urlParams.get('last_name') || urlParams.get('lastName') || '',
|
||||||
|
age: urlParams.get('age') || '30',
|
||||||
|
gender: urlParams.get('gender') || 'Male',
|
||||||
|
phone: urlParams.get('phone') || '',
|
||||||
|
email: urlParams.get('email') || '',
|
||||||
|
};
|
||||||
|
const category = {
|
||||||
|
type: 'general',
|
||||||
|
code: urlParams.get('category') || 'general_medicine',
|
||||||
|
name: urlParams.get('category_name') || 'General Medicine / Primary Care',
|
||||||
|
};
|
||||||
|
const language = urlParams.get('language') || urlParams.get('lang') || 'ta-IN';
|
||||||
|
const mode = urlParams.get('mode') || 'voice';
|
||||||
|
|
||||||
|
handleStartSession({ patient, category, language, mode });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method 2: Listen for postMessage from Parent App (Web iFrame / WebView)
|
||||||
|
const handleParentBridgeMessage = (event) => {
|
||||||
|
if (!event.data) return;
|
||||||
|
let payload = event.data;
|
||||||
|
if (typeof payload === 'string') {
|
||||||
|
try { payload = JSON.parse(payload); } catch (_) { return; }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (payload.type === 'INIT_PATIENT_ONBOARDING' && payload.data) {
|
||||||
|
const { patient, category, language, mode } = payload.data;
|
||||||
|
if (patient) {
|
||||||
|
handleStartSession({
|
||||||
|
patient,
|
||||||
|
category: category || { type: 'general', code: 'general_medicine', name: 'General Medicine / Primary Care' },
|
||||||
|
language: language || 'ta-IN',
|
||||||
|
mode: mode || 'voice',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', handleParentBridgeMessage);
|
||||||
|
return () => window.removeEventListener('message', handleParentBridgeMessage);
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Handle Session Initialization (Start Voice / Text Session)
|
// Handle Session Initialization (Start Voice / Text Session)
|
||||||
const handleStartSession = async ({ patient, category, language, mode }) => {
|
const handleStartSession = async ({ patient, category, language, mode }) => {
|
||||||
// Prime audio context synchronously inside user click gesture event
|
// Prime audio context synchronously inside user click gesture event
|
||||||
|
|||||||
Reference in New Issue
Block a user