Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

GifsMenu.tsx 8.3KB

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