|
| 1 | +import React from 'react' |
| 2 | +import Radium from "radium"; |
| 3 | +import Typist from "react-typist"; |
| 4 | + |
| 5 | +import {getState, setState} from "@ml/oceans/state"; |
| 6 | +import guide from "@ml/oceans/models/guide"; |
| 7 | +import soundLibrary from "@ml/oceans/models/soundLibrary"; |
| 8 | +import styles from "@ml/oceans/styles"; |
| 9 | +import colors from "@ml/oceans/styles/colors"; |
| 10 | +import I18n from "@ml/oceans/i18n"; |
| 11 | +import {Button} from "@ml/oceans/components/common"; |
| 12 | +import arrowDownImage from "@public/images/arrow-down.png"; |
| 13 | + |
| 14 | +let UnwrappedGuide = class Guide extends React.Component { |
| 15 | + onShowing() { |
| 16 | + clearInterval(getState().guideTypingTimer); |
| 17 | + setState({guideShowing: true, guideTypingTimer: null}); |
| 18 | + } |
| 19 | + |
| 20 | + dismissGuideClick() { |
| 21 | + const dismissed = guide.dismissCurrentGuide(); |
| 22 | + if (dismissed) { |
| 23 | + soundLibrary.playSound('other'); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + render() { |
| 28 | + const state = getState(); |
| 29 | + const currentGuide = guide.getCurrentGuide(); |
| 30 | + |
| 31 | + let guideBgStyle = [styles.guideBackground]; |
| 32 | + if (currentGuide) { |
| 33 | + if (currentGuide.noDimBackground) { |
| 34 | + guideBgStyle = [styles.guideBackgroundHidden]; |
| 35 | + } |
| 36 | + |
| 37 | + // Info guides should have a darker background color. |
| 38 | + if (currentGuide.style === 'Info') { |
| 39 | + guideBgStyle.push({backgroundColor: colors.transparentBlack}); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Start playing the typing sounds. |
| 44 | + if (!state.guideShowing && !state.guideTypingTimer && currentGuide) { |
| 45 | + const guideTypingTimer = setInterval(() => { |
| 46 | + soundLibrary.playSound('no', 0.5); |
| 47 | + }, 1000 / 10); |
| 48 | + setState({guideTypingTimer}); |
| 49 | + } |
| 50 | + |
| 51 | + return ( |
| 52 | + <div> |
| 53 | + {currentGuide && currentGuide.image && ( |
| 54 | + <img |
| 55 | + src={currentGuide.image} |
| 56 | + style={[styles.guideImage, currentGuide.imageStyle || {}]} |
| 57 | + /> |
| 58 | + )} |
| 59 | + {!!currentGuide && ( |
| 60 | + <div> |
| 61 | + <div |
| 62 | + key={currentGuide.id} |
| 63 | + style={guideBgStyle} |
| 64 | + onClick={this.dismissGuideClick} |
| 65 | + id="uitest-dismiss-guide" |
| 66 | + > |
| 67 | + <div |
| 68 | + style={{ |
| 69 | + ...styles.guide, |
| 70 | + ...styles[`guide${currentGuide.style}`] |
| 71 | + }} |
| 72 | + > |
| 73 | + <div> |
| 74 | + {currentGuide.style === 'Info' && ( |
| 75 | + <div style={styles.guideHeading}> |
| 76 | + {I18n.t('didYouKnow')} |
| 77 | + </div> |
| 78 | + )} |
| 79 | + <div style={styles.guideTypingText}> |
| 80 | + <Typist |
| 81 | + avgTypingDelay={35} |
| 82 | + stdTypingDelay={15} |
| 83 | + cursor={{show: false}} |
| 84 | + onTypingDone={this.onShowing} |
| 85 | + > |
| 86 | + {currentGuide.textFn(getState())} |
| 87 | + </Typist> |
| 88 | + </div> |
| 89 | + <div |
| 90 | + style={ |
| 91 | + currentGuide.style === 'Info' |
| 92 | + ? styles.guideFinalTextInfoContainer |
| 93 | + : styles.guideFinalTextContainer |
| 94 | + } |
| 95 | + > |
| 96 | + <div style={styles.guideFinalText}> |
| 97 | + {currentGuide.textFn(getState())} |
| 98 | + </div> |
| 99 | + </div> |
| 100 | + {currentGuide.style === 'Info' && ( |
| 101 | + <Button style={styles.infoGuideButton} onClick={() => {}}> |
| 102 | + {I18n.t('continue')} |
| 103 | + </Button> |
| 104 | + )} |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + {currentGuide.arrow && ( |
| 109 | + <img |
| 110 | + src={arrowDownImage} |
| 111 | + style={{ |
| 112 | + ...styles.guideArrow, |
| 113 | + ...styles[`arrow${currentGuide.arrow}`] |
| 114 | + }} |
| 115 | + /> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + )} |
| 119 | + </div> |
| 120 | + ); |
| 121 | + } |
| 122 | +}; |
| 123 | +export default Radium(UnwrappedGuide); |
0 commit comments