|
1 |
| -import React, { useEffect, useState } from 'react'; |
| 1 | +import React, { ReactElement, useEffect, useState } from 'react'; |
2 | 2 | import cn from 'classnames';
|
3 | 3 |
|
| 4 | +import { IconAudioPlay, IconSearch } from '../icons'; |
| 5 | +import type { Word } from '../@types'; |
| 6 | + |
4 | 7 | import './styles.css';
|
5 | 8 |
|
| 9 | +type Type = Word & { |
| 10 | + view?: 'loading' | 'success' | 'error'; |
| 11 | + left?: string; |
| 12 | + top?: string; |
| 13 | + bottom?: string; |
| 14 | + onAdd?: () => void; |
| 15 | +}; |
| 16 | + |
6 | 17 | const onlyUnique = (value, index, self) => self.indexOf(value) === index;
|
7 | 18 |
|
8 |
| -export const Popup = ({ |
9 |
| - view, |
10 |
| - left, |
11 |
| - top, |
12 |
| - bottom, |
13 |
| - text, |
14 |
| - transcription, |
15 |
| - translations, |
16 |
| - onAdd, |
17 |
| -}) => { |
18 |
| - const [animated, setAnimated] = useState(false); |
| 19 | +export const Popup: React.FC<Type> = React.memo( |
| 20 | + ({ |
| 21 | + view = 'loading', |
| 22 | + left, |
| 23 | + top, |
| 24 | + bottom, |
| 25 | + text, |
| 26 | + transcription, |
| 27 | + translations, |
| 28 | + soundUrl, |
| 29 | + onAdd, |
| 30 | + }): ReactElement => { |
| 31 | + const [animated, setAnimated] = useState(false); |
19 | 32 |
|
20 |
| - useEffect(() => { |
21 |
| - setAnimated(false); |
22 |
| - }, [text]); |
| 33 | + useEffect(() => { |
| 34 | + setAnimated(false); |
| 35 | + }, [text]); |
23 | 36 |
|
24 |
| - const handleAdd = (e) => { |
25 |
| - e.stopPropagation(); |
| 37 | + const handleAdd = (e) => { |
| 38 | + e.stopPropagation(); |
26 | 39 |
|
27 |
| - setAnimated(true); |
28 |
| - onAdd(); |
29 |
| - }; |
| 40 | + setAnimated(true); |
| 41 | + onAdd(); |
| 42 | + }; |
30 | 43 |
|
31 |
| - return ( |
32 |
| - <div |
33 |
| - className={cn('hw_popup', { hw_popup_animated: animated })} |
34 |
| - style={{ left, top, bottom }} |
35 |
| - > |
36 |
| - <div className="hw_popup__container"> |
37 |
| - {view === 'loading' && <div className="hw_icon__loading" />} |
38 |
| - {view === 'success' && ( |
39 |
| - <> |
40 |
| - {transcription && ( |
41 |
| - <div className="hw_popup__transcription">{transcription}</div> |
42 |
| - )} |
43 |
| - <div className="hw_popup__main-word">{text}</div> |
44 |
| - <div className="hw_popup__translation"> |
45 |
| - {translations.filter(onlyUnique).slice(0, 2).join(', ')} |
46 |
| - </div> |
47 |
| - {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions */} |
48 |
| - <div |
49 |
| - className="hw_popup__button-add" |
50 |
| - onClick={onAdd && handleAdd} |
51 |
| - /> |
52 |
| - </> |
53 |
| - )} |
54 |
| - {view === 'error' && ( |
55 |
| - <div className="hw_popup__container" style={{ left }}> |
56 |
| - <div className="hw_popup__icon__search" /> |
57 |
| - <div className="hw_popup__empty-title">Translation not found</div> |
58 |
| - <div className="hw_popup__empty-description"> |
59 |
| - We can't find anything for the languages |
60 |
| - </div> |
61 |
| - </div> |
62 |
| - )} |
| 44 | + const playSound = React.useCallback(() => { |
| 45 | + if (!soundUrl) return null; |
| 46 | + const audio = new Audio(soundUrl); |
| 47 | + audio.play(); |
| 48 | + }, [soundUrl]); |
| 49 | + |
| 50 | + return ( |
| 51 | + <div |
| 52 | + className={cn('hw_popup', { hw_popup_animated: animated })} |
| 53 | + style={{ left, top, bottom }} |
| 54 | + > |
| 55 | + <div className="hw_popup__container"> |
| 56 | + {view === 'loading' && <div className="hw_icon__loading" />} |
| 57 | + {view === 'success' && ( |
| 58 | + <> |
| 59 | + <div className="hw_popup__header"> |
| 60 | + {playSound && ( |
| 61 | + <IconAudioPlay |
| 62 | + className="hw_popup__icon hw_popup__icon-sound" |
| 63 | + onClick={playSound} |
| 64 | + /> |
| 65 | + )} |
| 66 | + {transcription && ( |
| 67 | + <div className="hw_popup__transcription">{transcription}</div> |
| 68 | + )} |
| 69 | + </div> |
| 70 | + <div className="hw_popup__main-word">{text}</div> |
| 71 | + <div className="hw_popup__translation"> |
| 72 | + {translations.filter(onlyUnique).slice(0, 2).join(', ')} |
| 73 | + </div> |
| 74 | + {/* eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions */} |
| 75 | + <div |
| 76 | + className="hw_popup__button-add" |
| 77 | + onClick={onAdd && handleAdd} |
| 78 | + /> |
| 79 | + </> |
| 80 | + )} |
| 81 | + {view === 'error' && ( |
| 82 | + <> |
| 83 | + <IconSearch /> |
| 84 | + <div className="hw_popup__empty-title"> |
| 85 | + {chrome && chrome.i18n |
| 86 | + ? chrome.i18n.getMessage('translation_not_found_title') |
| 87 | + : 'Translation not found'} |
| 88 | + </div> |
| 89 | + <div className="hw_popup__empty-description"> |
| 90 | + {chrome && chrome.i18n |
| 91 | + ? chrome.i18n.getMessage('translation_not_found_description') |
| 92 | + : "We can't find anything for the word"} |
| 93 | + </div> |
| 94 | + </> |
| 95 | + )} |
| 96 | + </div> |
63 | 97 | </div>
|
64 |
| - </div> |
65 |
| - ); |
66 |
| -}; |
| 98 | + ); |
| 99 | + } |
| 100 | +); |
0 commit comments