|
| 1 | +import React, {useEffect, useCallback, useState, useRef} from 'react'; |
| 2 | +import classNames from 'classnames'; |
| 3 | +// eslint-disable-next-line import/no-unresolved |
| 4 | +import {driver} from 'driver.js'; |
| 5 | +import 'driver.js/dist/driver.css'; |
| 6 | +import {defineMessages, injectIntl, intlShape} from 'react-intl'; |
| 7 | +import PropTypes from 'prop-types'; |
| 8 | + |
| 9 | +import Box from '../box/box.jsx'; |
| 10 | +import {BLOCKS_TAB_INDEX} from '../../reducers/editor-tab'; |
| 11 | +import {getLocalStorageValue, setLocalStorageValue} from '../../lib/local-storage.js'; |
| 12 | +import addExtensionIcon from '../gui/icon--extensions.svg'; |
| 13 | +import styles from './extension-button.css'; |
| 14 | +import './extension-button.raw.css'; |
| 15 | + |
| 16 | +const messages = defineMessages({ |
| 17 | + addExtension: { |
| 18 | + id: 'gui.gui.addExtension', |
| 19 | + description: 'Button to add an extension in the target pane', |
| 20 | + defaultMessage: 'Add Extension' |
| 21 | + }, |
| 22 | + faceSensingCalloutTitle: { |
| 23 | + id: 'gui.gui.faceSensingCalloutTitle', |
| 24 | + description: 'Hey there! \u{1F44B}', |
| 25 | + defaultMessage: 'Hey there! \u{1F44B}' |
| 26 | + }, |
| 27 | + faceSensingCalloutDescription: { |
| 28 | + id: 'gui.gui.faceSensingCalloutDescription', |
| 29 | + description: 'There is a new extension!', |
| 30 | + defaultMessage: 'There is a new extension!' |
| 31 | + } |
| 32 | +}); |
| 33 | + |
| 34 | +const localStorageAvailable = |
| 35 | + 'localStorage' in window && window.localStorage !== null; |
| 36 | + |
| 37 | +// Default to true to make sure we don't end up showing the feature |
| 38 | +// callouts multiple times if localStorage isn't available. |
| 39 | +const hasIntroducedFaceSensing = (username = 'guest') => { |
| 40 | + if (!localStorageAvailable) return true; |
| 41 | + return getLocalStorageValue('hasIntroducedFaceSensing', username) === true; |
| 42 | +}; |
| 43 | + |
| 44 | +const setHasIntroducedFaceSensing = (username = 'guest') => { |
| 45 | + if (!localStorageAvailable) return; |
| 46 | + setLocalStorageValue('hasIntroducedFaceSensing', username, true); |
| 47 | +}; |
| 48 | + |
| 49 | +const hasUsedFaceSensing = (username = 'guest') => { |
| 50 | + if (!localStorageAvailable) return true; |
| 51 | + return getLocalStorageValue('hasUsedFaceSensing', username) === true; |
| 52 | +}; |
| 53 | + |
| 54 | +const ExtensionButton = props => { |
| 55 | + const { |
| 56 | + activeTabIndex, |
| 57 | + intl, |
| 58 | + showNewFeatureCallouts, |
| 59 | + onExtensionButtonClick, |
| 60 | + username |
| 61 | + } = props; |
| 62 | + |
| 63 | + const driverRef = useRef(null); |
| 64 | + // Keep in a state to avoid reads from localStorage on every render. |
| 65 | + const [shouldShowFaceSensingCallouts, setShouldShowFaceSensingCallouts] = |
| 66 | + useState(showNewFeatureCallouts && !hasIntroducedFaceSensing(username) && !hasUsedFaceSensing(username)); |
| 67 | + const [clicked, setClicked] = useState(false); |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + if (!shouldShowFaceSensingCallouts) return; |
| 71 | + |
| 72 | + const onFirstClick = () => { |
| 73 | + const isExtensionButtonVisible = document.querySelector('div[class*="extension-button-container"]'); |
| 74 | + if (!isExtensionButtonVisible) return; |
| 75 | + |
| 76 | + const tooltip = driver({ |
| 77 | + allowClose: false, |
| 78 | + allowInteraction: true, |
| 79 | + overlayColor: 'transparent', |
| 80 | + popoverOffset: -3, |
| 81 | + steps: [{ |
| 82 | + element: 'div[class*="extension-button-container"]', |
| 83 | + popover: { |
| 84 | + title: intl.formatMessage(messages.faceSensingCalloutTitle), |
| 85 | + description: intl.formatMessage(messages.faceSensingCalloutDescription), |
| 86 | + side: 'right', |
| 87 | + align: 'center', |
| 88 | + popoverClass: 'tooltip-face-sensing', |
| 89 | + showButtons: [] |
| 90 | + } |
| 91 | + }] |
| 92 | + }); |
| 93 | + setClicked(true); |
| 94 | + driverRef.current = tooltip; |
| 95 | + tooltip.drive(); |
| 96 | + }; |
| 97 | + window.addEventListener('click', onFirstClick, {once: true}); |
| 98 | + |
| 99 | + return () => { |
| 100 | + if (driverRef.current) { |
| 101 | + driverRef.current.destroy(); |
| 102 | + driverRef.current = null; |
| 103 | + } |
| 104 | + }; |
| 105 | + }, []); |
| 106 | + |
| 107 | + useEffect(() => { |
| 108 | + if (!driverRef.current) return; |
| 109 | + |
| 110 | + if (!shouldShowFaceSensingCallouts && driverRef.current) { |
| 111 | + driverRef.current.destroy(); |
| 112 | + } |
| 113 | + |
| 114 | + if (!shouldShowFaceSensingCallouts || !clicked) return; |
| 115 | + |
| 116 | + const isExtensionButtonVisible = document.querySelector('div[class*="extension-button-container"]'); |
| 117 | + |
| 118 | + if (!isExtensionButtonVisible || activeTabIndex !== BLOCKS_TAB_INDEX) { |
| 119 | + driverRef.current.destroy(); |
| 120 | + } |
| 121 | + |
| 122 | + if (isExtensionButtonVisible && activeTabIndex === BLOCKS_TAB_INDEX) { |
| 123 | + driverRef.current.drive(); |
| 124 | + } |
| 125 | + }, [shouldShowFaceSensingCallouts, activeTabIndex, clicked]); |
| 126 | + |
| 127 | + const handleExtensionButtonClick = useCallback(() => { |
| 128 | + if (driverRef.current) { |
| 129 | + driverRef.current.destroy(); |
| 130 | + driverRef.current = null; |
| 131 | + } |
| 132 | + |
| 133 | + if (shouldShowFaceSensingCallouts) { |
| 134 | + setHasIntroducedFaceSensing(username); |
| 135 | + setShouldShowFaceSensingCallouts(false); |
| 136 | + } |
| 137 | + onExtensionButtonClick?.(); |
| 138 | + }, [shouldShowFaceSensingCallouts]); |
| 139 | + |
| 140 | + return ( |
| 141 | + <Box className={styles.extensionButtonContainer}> |
| 142 | + <button |
| 143 | + className={ |
| 144 | + classNames(styles.extensionButton, |
| 145 | + shouldShowFaceSensingCallouts && styles.radiate |
| 146 | + )} |
| 147 | + title={intl.formatMessage(messages.addExtension)} |
| 148 | + onClick={handleExtensionButtonClick} |
| 149 | + > |
| 150 | + <img |
| 151 | + className={styles.extensionButtonIcon} |
| 152 | + draggable={false} |
| 153 | + src={addExtensionIcon} |
| 154 | + /> |
| 155 | + </button> |
| 156 | + </Box> |
| 157 | + ); |
| 158 | +}; |
| 159 | + |
| 160 | +ExtensionButton.propTypes = { |
| 161 | + activeTabIndex: PropTypes.number, |
| 162 | + intl: intlShape.isRequired, |
| 163 | + onExtensionButtonClick: PropTypes.func, |
| 164 | + showNewFeatureCallouts: PropTypes.bool, |
| 165 | + username: PropTypes.string |
| 166 | +}; |
| 167 | + |
| 168 | +const ExtensionButtonIntl = injectIntl(ExtensionButton); |
| 169 | + |
| 170 | +export default ExtensionButtonIntl; |
0 commit comments