Skip to content

Instantly share code, notes, and snippets.

@eduardocalazansjr
Created September 17, 2020 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardocalazansjr/32594d1ebd71e4050c594bcae9695b94 to your computer and use it in GitHub Desktop.
Save eduardocalazansjr/32594d1ebd71e4050c594bcae9695b94 to your computer and use it in GitHub Desktop.
webchat-rn
const url = "https://privateboturl"
const token = "uniqueUserId"
const onClose = () => {
//Callback de fechamento do Chat
}
//Habilita input de áudio e configura o UserId
const runFirst = `
window.supportsAudioRecording = true;
setPid("${token}");
`
//Interface Javascript para comunicação entre chat e app, 100% personalizável
const handleOnMessage = (event: WebViewMessageEvent): void => {
const messageData = JSON.parse(event.nativeEvent.data)
//Tipos de eventos personalizáveis
switch (messageData.type) {
case 'startRecording':
onStartMicRecording()
return
case 'stopRecording':
onStopMicRecording()
.then((r) => {
webView.current!.injectJavaScript(`
window.sendAudio("${r}");
true;
`)
})
.catch((r) =>
webView.current!.injectJavaScript(`
window.supportsAudioRecording = false;
true;
`),
)
return
default:
console.log('Unsupported')
}
}
return (
<ModalRN
visible={visible}
animationType="fade"
transparent
onRequestClose={onClose}>
<WebView
ref={webView}
source={{ uri: url }}
injectedJavaScriptBeforeContentLoaded={`localStorage.setItem("pid", "${token}");`}
injectedJavaScript={runFirst}
onMessage={(event): void => handleOnMessage(event)}
startInLoadingState
javaScriptEnabled
domStorageEnabled
renderLoading={(): React.ReactElement => <Loading />}
onShouldStartLoadWithRequest={(event): boolean => {
//Configura abertura de links externos/PWA
if (!event.url.includes(url)) {
Linking.openURL(event.url)
return false
}
return true
}}
onError={chat.open}
mediaPlaybackRequiresUserAction={false}
/>
</ModalRN>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment