You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import { GiphyFetch, TrendingOptions } from '@giphy/js-fetch-api';
  2. import { Grid } from '@giphy/react-components';
  3. import React, { useCallback, useEffect, useState } from 'react';
  4. import { useTranslation } from 'react-i18next';
  5. import { batch, useDispatch, useSelector } from 'react-redux';
  6. import { makeStyles } from 'tss-react/mui';
  7. import { createGifSentEvent } from '../../../analytics/AnalyticsEvents';
  8. import { sendAnalytics } from '../../../analytics/functions';
  9. import { IReduxState } from '../../../app/types';
  10. import Input from '../../../base/ui/components/web/Input';
  11. import { sendMessage } from '../../../chat/actions.any';
  12. import { SCROLL_SIZE } from '../../../filmstrip/constants';
  13. import { toggleReactionsMenuVisibility } from '../../../reactions/actions.web';
  14. import { IReactionsMenuParent } from '../../../reactions/types';
  15. import Drawer from '../../../toolbox/components/web/Drawer';
  16. import JitsiPortal from '../../../toolbox/components/web/JitsiPortal';
  17. import { setGifMenuVisibility } from '../../actions';
  18. import {
  19. formatGifUrlMessage,
  20. getGifAPIKey,
  21. getGifRating,
  22. getGifUrl
  23. } from '../../function.any';
  24. const OVERFLOW_DRAWER_PADDING = 16;
  25. const useStyles = makeStyles()(theme => {
  26. return {
  27. gifsMenu: {
  28. width: '100%',
  29. marginBottom: theme.spacing(2),
  30. display: 'flex',
  31. flexDirection: 'column',
  32. '& div:focus': {
  33. border: '1px solid red !important',
  34. boxSizing: 'border-box'
  35. }
  36. },
  37. searchField: {
  38. marginBottom: theme.spacing(3)
  39. },
  40. gifContainer: {
  41. height: '245px',
  42. overflowY: 'auto'
  43. },
  44. logoContainer: {
  45. width: `calc(100% - ${SCROLL_SIZE}px)`,
  46. backgroundColor: '#121119',
  47. display: 'flex',
  48. alignItems: 'center',
  49. justifyContent: 'center',
  50. color: '#fff',
  51. marginTop: theme.spacing(1)
  52. },
  53. overflowDrawerMenu: {
  54. padding: theme.spacing(3),
  55. width: '100%',
  56. boxSizing: 'border-box',
  57. height: '100%'
  58. },
  59. overflowMenu: {
  60. height: '200px',
  61. width: '201px',
  62. marginBottom: '0px'
  63. },
  64. gifContainerOverflow: {
  65. flexGrow: 1
  66. },
  67. drawer: {
  68. display: 'flex',
  69. height: '100%'
  70. }
  71. };
  72. });
  73. interface IProps {
  74. columns?: number;
  75. parent: IReactionsMenuParent;
  76. }
  77. /**
  78. * Gifs menu.
  79. *
  80. * @returns {ReactElement}
  81. */
  82. function GifsMenu({ columns = 2, parent }: IProps) {
  83. const API_KEY = useSelector(getGifAPIKey);
  84. const giphyFetch = new GiphyFetch(API_KEY);
  85. const [ searchKey, setSearchKey ] = useState<string>();
  86. const { classes: styles, cx } = useStyles();
  87. const dispatch = useDispatch();
  88. const { t } = useTranslation();
  89. const isInOverflowMenu
  90. = parent === IReactionsMenuParent.OverflowDrawer || parent === IReactionsMenuParent.OverflowMenu;
  91. const { clientWidth } = useSelector((state: IReduxState) => state['features/base/responsive-ui']);
  92. const rating = useSelector(getGifRating);
  93. const fetchGifs = useCallback(async (offset = 0) => {
  94. const options: TrendingOptions = {
  95. limit: 20,
  96. offset,
  97. rating
  98. };
  99. if (!searchKey) {
  100. return await giphyFetch.trending(options);
  101. }
  102. return await giphyFetch.search(searchKey, options);
  103. }, [ searchKey ]);
  104. const onDrawerClose = useCallback(() => {
  105. dispatch(setGifMenuVisibility(false));
  106. }, []);
  107. const handleGifClick = useCallback((gif, e) => {
  108. e?.stopPropagation();
  109. const url = getGifUrl(gif);
  110. sendAnalytics(createGifSentEvent());
  111. batch(() => {
  112. dispatch(sendMessage(formatGifUrlMessage(url), true));
  113. dispatch(toggleReactionsMenuVisibility());
  114. isInOverflowMenu && onDrawerClose();
  115. });
  116. }, [ dispatch, isInOverflowMenu ]);
  117. const handleGifKeyPress = useCallback((gif, e) => {
  118. if (e.nativeEvent.keyCode === 13) {
  119. handleGifClick(gif, null);
  120. }
  121. }, [ handleGifClick ]);
  122. const handleSearchKeyChange = useCallback(value => {
  123. setSearchKey(value);
  124. }, []);
  125. const handleKeyDown = useCallback(e => {
  126. if (!document.activeElement) {
  127. return;
  128. }
  129. if (e.keyCode === 38) { // up arrow
  130. e.preventDefault();
  131. // if the first gif is focused move focus to the input
  132. if (document.activeElement.previousElementSibling === null) {
  133. const element = document.querySelector('.gif-input') as HTMLElement;
  134. element?.focus();
  135. } else {
  136. const element = document.activeElement.previousElementSibling as HTMLElement;
  137. element?.focus();
  138. }
  139. } else if (e.keyCode === 40) { // down arrow
  140. e.preventDefault();
  141. // if the input is focused move focus to the first gif
  142. if (document.activeElement.classList.contains('gif-input')) {
  143. const element = document.querySelector('.giphy-gif') as HTMLElement;
  144. element?.focus();
  145. } else {
  146. const element = document.activeElement.nextElementSibling as HTMLElement;
  147. element?.focus();
  148. }
  149. }
  150. }, []);
  151. useEffect(() => {
  152. document.addEventListener('keydown', handleKeyDown);
  153. return () => document.removeEventListener('keydown', handleKeyDown);
  154. }, []);
  155. // For some reason, the Grid component does not do an initial call on mobile.
  156. // This fixes that.
  157. useEffect(() => setSearchKey(''), []);
  158. const onInputKeyPress = useCallback((e: React.KeyboardEvent) => {
  159. e.stopPropagation();
  160. }, []);
  161. const gifMenu = (
  162. <div
  163. className = { cx(styles.gifsMenu,
  164. parent === IReactionsMenuParent.OverflowDrawer && styles.overflowDrawerMenu,
  165. parent === IReactionsMenuParent.OverflowMenu && styles.overflowMenu
  166. ) }>
  167. <Input
  168. autoFocus = { true }
  169. className = { cx(styles.searchField, 'gif-input') }
  170. id = 'gif-search-input'
  171. onChange = { handleSearchKeyChange }
  172. onKeyPress = { onInputKeyPress }
  173. placeholder = { t('giphy.search') }
  174. // eslint-disable-next-line react/jsx-no-bind
  175. ref = { inputElement => {
  176. inputElement?.focus();
  177. setTimeout(() => inputElement?.focus(), 200);
  178. } }
  179. type = 'text'
  180. value = { searchKey ?? '' } />
  181. <div
  182. className = { cx(styles.gifContainer,
  183. parent === IReactionsMenuParent.OverflowDrawer && styles.gifContainerOverflow) }>
  184. <Grid
  185. columns = { columns }
  186. fetchGifs = { fetchGifs }
  187. gutter = { 6 }
  188. hideAttribution = { true }
  189. key = { searchKey }
  190. noLink = { true }
  191. noResultsMessage = { t('giphy.noResults') }
  192. onGifClick = { handleGifClick }
  193. onGifKeyPress = { handleGifKeyPress }
  194. width = { parent === IReactionsMenuParent.OverflowDrawer
  195. ? clientWidth - (2 * OVERFLOW_DRAWER_PADDING) - SCROLL_SIZE
  196. : parent === IReactionsMenuParent.OverflowMenu ? 201 : 320
  197. } />
  198. </div>
  199. <div className = { styles.logoContainer }>
  200. <span>Powered by</span>
  201. <img
  202. alt = 'GIPHY Logo'
  203. src = 'images/GIPHY_logo.png' />
  204. </div>
  205. </div>
  206. );
  207. return parent === IReactionsMenuParent.OverflowDrawer ? (
  208. <JitsiPortal>
  209. <Drawer
  210. className = { styles.drawer }
  211. isOpen = { true }
  212. onClose = { onDrawerClose }>
  213. {gifMenu}
  214. </Drawer>
  215. </JitsiPortal>
  216. ) : gifMenu;
  217. }
  218. export default GifsMenu;