import React, { Component } from 'react'; import PropTypes from 'prop-types'; import cx from 'classnames'; import qs from 'query-string'; import CommaAuth, { config as AuthConfig } from '@commaai/my-comma-auth'; import { EXPLORER_URL } from '../../config'; import Modal from './baseModal'; export default class OnboardingModal extends Component { static propTypes = { handlePandaConnect: PropTypes.func, routes: PropTypes.array }; static instructionalImages = { step2: require('../../images/webusb-enable-experimental-features.png'), step3: require('../../images/webusb-enable-webusb.png') }; constructor(props) { super(props); this.state = { webUsbEnabled: !!navigator.usb, viewingUsbInstructions: false, pandaConnected: false }; this.attemptPandaConnection = this.attemptPandaConnection.bind(this); this.toggleUsbInstructions = this.toggleUsbInstructions.bind(this); this.navigateToExplorer = this.navigateToExplorer.bind(this); } componentDidMount() { const script = document.createElement("script"); document.body.appendChild(script); script.onload = () => { window.AppleID.auth.init({ clientId : AuthConfig.APPLE_CLIENT_ID, scope : AuthConfig.APPLE_SCOPES, redirectURI : AuthConfig.APPLE_REDIRECT_URI, state : AuthConfig.APPLE_STATE, }); }; script.src = "https://appleid.cdn-apple.com/appleauth/static/jsapi/appleid/1/en_US/appleid.auth.js"; script.async = true; document.addEventListener('AppleIDSignInOnSuccess', (data) => { const { code, state } = data.detail.authorization; window.location = [AuthConfig.APPLE_REDIRECT_PATH, qs.stringify({ code, state })].join('?'); }); document.addEventListener('AppleIDSignInOnFailure', console.log); } attemptPandaConnection() { if (!this.state.webUsbEnabled) { return; } this.props.handlePandaConnect(); } toggleUsbInstructions() { this.setState({ viewingUsbInstructions: !this.state.viewingUsbInstructions }); } navigateToExplorer() { window.location.href = EXPLORER_URL; } filterRoutesWithCan(drive) { return drive.can === true; } renderPandaEligibility() { const { webUsbEnabled, pandaConnected } = this.state; const { attemptingPandaConnection } = this.props; if (!webUsbEnabled) { return (

WebUSB is not enabled in your Chrome settings

); } if (!pandaConnected && attemptingPandaConnection) { return (

Waiting for panda USB connection

); } } renderLogin() { if (CommaAuth.isAuthenticated()) { return ( ); } else { return <> Sign in with Google Sign in with GitHub ; } } renderOnboardingOptions() { return (
{this.renderLogin()}
); } renderUsbInstructions() { return (

Follow these directions to enable WebUSB:

  1. Open your Chrome settings:

    chrome://flags/#enable-experimental-web-platform-features
  2. Enable Experimental Platform features:

    Screenshot of Google Chrome Experimental Platform features
  3. Enable WebUSB:

    Screenshot of Google Chrome enable WebUSB
  4. Relaunch your Chrome browser and try enabling live mode again.

); } renderModalContent() { if (this.state.viewingUsbInstructions) { return this.renderUsbInstructions(); } return this.renderOnboardingOptions(); } renderModalFooter() { return (

Don't have a {' '} panda ? {' '} Get one here {' '} or {' '} try the demo .

); } render() { return ( {this.renderModalContent()} ); } }