Skip to content

Commit

Permalink
fix: ios safari typeMessage events, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nzambello committed Oct 19, 2023
1 parent c9bf892 commit f7d4d4d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 80 deletions.
64 changes: 23 additions & 41 deletions src/components/MemoriWidget/MemoriWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,18 @@ const typeMessage = (
hasBatchQueued,
},
});

document.dispatchEvent(e);

const isSafariIOS =
window.navigator.userAgent.includes('Safari') &&
!window.navigator.userAgent.includes('Chrome') &&
/iPad|iPhone|iPod/.test(navigator.userAgent);

if (isSafariIOS) {
setTimeout(() => {
document.dispatchEvent(new CustomEvent('MemoriEndSpeak'));
}, 300);
}
};
const typeMessageHidden = (
message: string,
Expand Down Expand Up @@ -189,10 +199,10 @@ const typeBatchMessages = (
?.hasAttribute('disabled');
}

const isSafari =
const isSafariIOS =
window.navigator.userAgent.includes('Safari') &&
!window.navigator.userAgent.includes('Chrome');
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
!window.navigator.userAgent.includes('Chrome') &&
/iPad|iPhone|iPod/.test(navigator.userAgent);

const stepsGenerator = (function* () {
yield* messages;
Expand All @@ -209,17 +219,21 @@ const typeBatchMessages = (
disableInputs();
}

let waitForPrevious = step.waitForPrevious;
if (isSafariIOS) waitForPrevious = false;

typeMessage(
step.message,
step.waitForPrevious,
waitForPrevious,
step.hidden,
step.typingText,
step.useLoaderTextAsMsg,
!next.done
);

if (isIOS && isSafari) {
if (isSafariIOS) {
setTimeout(() => {
document.dispatchEvent(new CustomEvent('MemoriEndSpeak'));
reEnableInputs();
}, 3000);
}
Expand Down Expand Up @@ -1561,35 +1575,6 @@ const MemoriWidget = ({
audioContext.resume().then(() => speak(text));
return;
}
if (isIOS && isSafari) {
if (isPlayingAudio || memoriSpeaking) {
// try {
// memoriSpeaking = false;
// if (speechSynthesizer) {
// speechSynthesizer.close();
// speechSynthesizer = null;
// }
// if (audioDestination) {
// audioDestination.pause();
// audioDestination.close();
// }
// if (audioContext) {
// // audioContext.close().then(() => {
// // audioContext = new AudioContext();
// // let buffer = audioContext.createBuffer(1, 10000, 22050);
// // let source = audioContext.createBufferSource();
// // source.buffer = buffer;
// // source.connect(audioContext.destination);
// // });
// audioContext.destination.disconnect();
// }
// } catch (e) {
// console.error('stopAudio error: ', e);
// emitEndSpeakEvent();
// }
// stopAudio();
}
}
if (audioContext.state === 'closed') {
audioContext = new AudioContext();
let buffer = audioContext.createBuffer(1, 10000, 22050);
Expand Down Expand Up @@ -1647,9 +1632,6 @@ const MemoriWidget = ({
memoriSpeaking = true;

try {
// if (audioContext.destination.context.state === 'running') {
// audioContext.destination.disconnect();
// }
audioContext.decodeAudioData(result.audioData, function (buffer) {
source.buffer = buffer;
source.connect(audioContext.destination);
Expand Down Expand Up @@ -1688,21 +1670,21 @@ const MemoriWidget = ({
speechSynthesizer.close();
speechSynthesizer = null;
}
// emitEndSpeakEvent();
emitEndSpeakEvent();
}
} else {
audioContext.resume();
setIsPlayingAudio(false);
memoriSpeaking = false;
// emitEndSpeakEvent();
emitEndSpeakEvent();
}
},
error => {
console.error('speak:', error);
window.speechSynthesis.speak(new SpeechSynthesisUtterance(text));
setIsPlayingAudio(false);
memoriSpeaking = false;
// emitEndSpeakEvent();
emitEndSpeakEvent();
}
);

Expand Down
85 changes: 46 additions & 39 deletions src/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,52 @@ Localhost.args = {
integrationID: '82f017cc-450b-4c47-acf5-47910d336ce9',
};

const TemplateWithBatchButton: Story<Props> = args => (
<div>
<button
style={{
position: 'fixed',
top: '0.5rem',
left: '0.5rem',
zIndex: 99999,
}}
onClick={() => {
window.typeBatchMessages([
{
message: 'uno di tre',
hidden: false,
waitForPrevious: true,
},
{
message: 'due di tre',
hidden: false,
waitForPrevious: true,
},
{
message: 'tre di tre',
hidden: false,
waitForPrevious: true,
},
]);
}}
>
Start Batch
</button>
<Memori {...args} />
</div>
);
export const WithBatch = TemplateWithBatchButton.bind({});
WithBatch.args = {
ownerUserName: 'nzambello',
memoriName: 'Nicola',
tenantID: 'app.memorytwin.com',
apiURL: 'https://backend.memori.ai',
baseURL: 'https://app.memorytwin.com',
uiLang: 'it',
showShare: true,
showSettings: true,
};

// export const Instruction = Template.bind({});
// Instruction.args = {
// ownerUserName: 'nzambello',
Expand All @@ -65,42 +111,3 @@ Localhost.args = {
// pin: 'giver pin',
// authToken: 'your login token',
// };

export const DemoSophia = Template.bind({});
DemoSophia.args = {
layout: 'TOTEM',
memoriName: 'Sophia',
ownerUserName: 'francescoimpellizzeri',
memoriID: '57405959-a2c5-4499-adb8-bb8dcc848ad5',
ownerUserID: '44ccefdb-d7a2-447e-9851-2a76be055a40',
tenantID: 'nextpresent.aclambda.online',
apiURL: 'https://backend.memori.ai',
baseURL: 'https://nextpresent.aclambda.online',
uiLang: 'it',
lang: 'it',
showShare: true,
showSettings: true,
integrationID: 'd10bbffd-26f3-4614-a092-15b215d04ffa',
initialQuestion: 'Welcome Totem',
contextVars: 'CONTEXT:TOTEM',
// https://assets.memori.ai/api/v2/asset/6af08bf1-f011-40ea-92a2-699b7b53a53c.glb
};

export const DemoStefano = Template.bind({});
DemoStefano.args = {
layout: 'TOTEM',
memoriName: 'Stefano Zingoni',
ownerUserName: 'roberta.bianchi',
memoriID: 'd6df4c75-8dfb-4e6d-b19f-193fa8a9bcce',
ownerUserID: 'b242160e-c19c-468f-b7f8-b2dc1ce51045',
tenantID: 'gruppoe.aclambda.online',
apiURL: 'https://backend.memori.ai',
baseURL: 'https://gruppoe.aclambda.online',
uiLang: 'it',
lang: 'it',
showShare: true,
showSettings: true,
contextVars: 'CONTEXT:TOTEM',
integrationID: '5712601d-6174-45e7-bfb1-b4ac71c4ff80',
// https://assets.memori.ai/api/v2/asset/ed50c6a4-37cd-458b-9209-8cabfa256b30.glb
};

0 comments on commit f7d4d4d

Please sign in to comment.