Files
cinny/src/app/organisms/emoji-board/EmojiBoard.jsx
T

357 lines
12 KiB
React
Raw Normal View History

2021-07-28 18:45:52 +05:30
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
import React, { useState, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import './EmojiBoard.scss';
import parse from 'html-react-parser';
import twemoji from 'twemoji';
2021-08-25 15:00:40 +05:30
import { emojiGroups, emojis } from './emoji';
2021-12-29 23:02:49 -05:00
import { getRelevantPacks } from './custom-emoji';
import initMatrix from '../../../client/initMatrix';
import cons from '../../../client/state/cons';
import navigation from '../../../client/state/navigation';
2021-08-25 15:00:40 +05:30
import AsyncSearch from '../../../util/AsyncSearch';
import { addRecentEmoji, getRecentEmojis } from './recent';
2023-01-14 18:51:42 +05:30
import { TWEMOJI_BASE_URL } from '../../../util/twemojify';
2021-07-28 18:45:52 +05:30
import Text from '../../atoms/text/Text';
import RawIcon from '../../atoms/system-icons/RawIcon';
import IconButton from '../../atoms/button/IconButton';
import Input from '../../atoms/input/Input';
import ScrollView from '../../atoms/scroll/ScrollView';
import SearchIC from '../../../../public/res/ic/outlined/search.svg';
import RecentClockIC from '../../../../public/res/ic/outlined/recent-clock.svg';
2021-07-28 18:45:52 +05:30
import EmojiIC from '../../../../public/res/ic/outlined/emoji.svg';
import DogIC from '../../../../public/res/ic/outlined/dog.svg';
import CupIC from '../../../../public/res/ic/outlined/cup.svg';
import BallIC from '../../../../public/res/ic/outlined/ball.svg';
import PhotoIC from '../../../../public/res/ic/outlined/photo.svg';
import BulbIC from '../../../../public/res/ic/outlined/bulb.svg';
import PeaceIC from '../../../../public/res/ic/outlined/peace.svg';
import FlagIC from '../../../../public/res/ic/outlined/flag.svg';
const ROW_EMOJIS_COUNT = 7;
2021-12-30 16:37:11 +05:30
const EmojiGroup = React.memo(({ name, groupEmojis }) => {
2021-07-28 18:45:52 +05:30
function getEmojiBoard() {
2021-08-13 16:31:22 +05:30
const emojiBoard = [];
2021-08-25 15:00:40 +05:30
const totalEmojis = groupEmojis.length;
2021-07-28 18:45:52 +05:30
for (let r = 0; r < totalEmojis; r += ROW_EMOJIS_COUNT) {
const emojiRow = [];
for (let c = r; c < r + ROW_EMOJIS_COUNT; c += 1) {
2021-08-13 16:31:22 +05:30
const emojiIndex = c;
2021-07-28 18:45:52 +05:30
if (emojiIndex >= totalEmojis) break;
2021-08-25 15:00:40 +05:30
const emoji = groupEmojis[emojiIndex];
2021-07-28 18:45:52 +05:30
emojiRow.push(
<span key={emojiIndex}>
2023-01-14 18:51:42 +05:30
{emoji.hexcode ? (
// This is a unicode emoji, and should be rendered with twemoji
parse(
twemoji.parse(emoji.unicode, {
attributes: () => ({
unicode: emoji.unicode,
shortcodes: emoji.shortcodes?.toString(),
hexcode: emoji.hexcode,
loading: 'lazy',
}),
base: TWEMOJI_BASE_URL,
})
)
) : (
// This is a custom emoji, and should be render as an mxc
<img
className="emoji"
draggable="false"
loading="lazy"
alt={emoji.shortcode}
unicode={`:${emoji.shortcode}:`}
shortcodes={emoji.shortcode}
src={initMatrix.matrixClient.mxcUrlToHttp(emoji.mxc)}
data-mx-emoticon={emoji.mxc}
/>
)}
</span>
2021-07-28 18:45:52 +05:30
);
}
2023-01-14 18:51:42 +05:30
emojiBoard.push(
<div key={r} className="emoji-row">
{emojiRow}
</div>
);
2021-07-28 18:45:52 +05:30
}
2021-08-13 16:31:22 +05:30
return emojiBoard;
2021-07-28 18:45:52 +05:30
}
return (
<div className="emoji-group">
2023-01-14 18:51:42 +05:30
<Text className="emoji-group__header" variant="b2" weight="bold">
{name}
</Text>
2022-02-05 18:07:41 +05:30
{groupEmojis.length !== 0 && <div className="emoji-set noselect">{getEmojiBoard()}</div>}
2021-07-28 18:45:52 +05:30
</div>
);
2021-12-30 16:37:11 +05:30
});
2021-07-28 18:45:52 +05:30
EmojiGroup.propTypes = {
name: PropTypes.string.isRequired,
2023-01-14 18:51:42 +05:30
groupEmojis: PropTypes.arrayOf(
PropTypes.shape({
length: PropTypes.number,
unicode: PropTypes.string,
hexcode: PropTypes.string,
mxc: PropTypes.string,
shortcode: PropTypes.string,
shortcodes: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
})
).isRequired,
2021-07-28 18:45:52 +05:30
};
2021-08-25 15:00:40 +05:30
const asyncSearch = new AsyncSearch();
2021-10-25 16:46:23 +05:30
asyncSearch.setup(emojis, { keys: ['shortcode'], isContain: true, limit: 40 });
2021-07-28 18:45:52 +05:30
function SearchedEmoji() {
2021-08-25 15:00:40 +05:30
const [searchedEmojis, setSearchedEmojis] = useState(null);
2021-07-28 18:45:52 +05:30
2021-08-25 15:00:40 +05:30
function handleSearchEmoji(resultEmojis, term) {
if (term === '' || resultEmojis.length === 0) {
if (term === '') setSearchedEmojis(null);
2021-10-22 17:02:42 +05:30
else setSearchedEmojis({ emojis: [] });
2021-07-28 18:45:52 +05:30
return;
}
2021-10-22 17:02:42 +05:30
setSearchedEmojis({ emojis: resultEmojis });
2021-07-28 18:45:52 +05:30
}
useEffect(() => {
2021-08-25 15:00:40 +05:30
asyncSearch.on(asyncSearch.RESULT_SENT, handleSearchEmoji);
2021-07-28 18:45:52 +05:30
return () => {
2021-08-25 15:00:40 +05:30
asyncSearch.removeListener(asyncSearch.RESULT_SENT, handleSearchEmoji);
2021-07-28 18:45:52 +05:30
};
}, []);
2021-08-25 15:00:40 +05:30
if (searchedEmojis === null) return false;
2023-01-14 18:51:42 +05:30
return (
<EmojiGroup
key="-1"
name={searchedEmojis.emojis.length === 0 ? 'No search result found' : 'Search results'}
groupEmojis={searchedEmojis.emojis}
/>
);
2021-07-28 18:45:52 +05:30
}
2022-02-08 12:43:59 +01:00
function EmojiBoard({ onSelect, searchRef }) {
2021-07-28 18:45:52 +05:30
const scrollEmojisRef = useRef(null);
2021-08-13 16:31:22 +05:30
const emojiInfo = useRef(null);
2021-07-28 18:45:52 +05:30
function isTargetNotEmoji(target) {
return target.classList.contains('emoji') === false;
}
function getEmojiDataFromTarget(target) {
const unicode = target.getAttribute('unicode');
2021-08-13 16:31:22 +05:30
const hexcode = target.getAttribute('hexcode');
2022-08-06 09:04:23 +05:30
const mxc = target.getAttribute('data-mx-emoticon');
2021-07-28 18:45:52 +05:30
let shortcodes = target.getAttribute('shortcodes');
if (typeof shortcodes === 'undefined') shortcodes = undefined;
else shortcodes = shortcodes.split(',');
2022-08-06 09:04:23 +05:30
return {
2023-01-14 18:51:42 +05:30
unicode,
hexcode,
shortcodes,
mxc,
2022-08-06 09:04:23 +05:30
};
2021-07-28 18:45:52 +05:30
}
function selectEmoji(e) {
if (isTargetNotEmoji(e.target)) return;
const emoji = getEmojiDataFromTarget(e.target);
onSelect(emoji);
if (emoji.hexcode) addRecentEmoji(emoji.unicode);
2021-07-28 18:45:52 +05:30
}
2021-08-13 16:31:22 +05:30
function setEmojiInfo(emoji) {
const infoEmoji = emojiInfo.current.firstElementChild.firstElementChild;
const infoShortcode = emojiInfo.current.lastElementChild;
2021-12-29 23:02:49 -05:00
infoEmoji.src = emoji.src;
infoEmoji.alt = emoji.unicode;
2021-08-13 16:31:22 +05:30
infoShortcode.textContent = `:${emoji.shortcode}:`;
}
2021-07-28 18:45:52 +05:30
function hoverEmoji(e) {
if (isTargetNotEmoji(e.target)) return;
const emoji = e.target;
2021-12-29 23:02:49 -05:00
const { shortcodes, unicode } = getEmojiDataFromTarget(emoji);
const { src } = e.target;
2021-07-28 18:45:52 +05:30
if (typeof shortcodes === 'undefined') {
searchRef.current.placeholder = 'Search';
2021-12-29 23:02:49 -05:00
setEmojiInfo({
unicode: '🙂',
shortcode: 'slight_smile',
src: 'https://twemoji.maxcdn.com/v/13.1.0/72x72/1f642.png',
});
2021-07-28 18:45:52 +05:30
return;
}
if (searchRef.current.placeholder === shortcodes[0]) return;
2021-08-25 15:00:40 +05:30
searchRef.current.setAttribute('placeholder', shortcodes[0]);
2021-12-29 23:02:49 -05:00
setEmojiInfo({ shortcode: shortcodes[0], src, unicode });
2021-07-28 18:45:52 +05:30
}
2022-02-08 12:43:59 +01:00
function handleSearchChange() {
const term = searchRef.current.value;
2021-08-25 15:00:40 +05:30
asyncSearch.search(term);
scrollEmojisRef.current.scrollTop = 0;
2021-07-28 18:45:52 +05:30
}
2021-12-29 23:02:49 -05:00
const [availableEmojis, setAvailableEmojis] = useState([]);
const [recentEmojis, setRecentEmojis] = useState([]);
const recentOffset = recentEmojis.length > 0 ? 1 : 0;
2021-12-29 23:02:49 -05:00
2022-01-09 10:29:06 +05:30
useEffect(() => {
const updateAvailableEmoji = (selectedRoomId) => {
if (!selectedRoomId) {
setAvailableEmojis([]);
return;
}
2021-12-29 23:02:49 -05:00
2022-08-06 09:04:23 +05:30
const mx = initMatrix.matrixClient;
const room = mx.getRoom(selectedRoomId);
const parentIds = initMatrix.roomList.getAllParentSpaces(room.roomId);
const parentRooms = [...parentIds].map((id) => mx.getRoom(id));
if (room) {
2023-01-14 18:51:42 +05:30
const packs = getRelevantPacks(room.client, [room, ...parentRooms]).filter(
(pack) => pack.getEmojis().length !== 0
);
2022-08-06 09:04:23 +05:30
// Set an index for each pack so that we know where to jump when the user uses the nav
for (let i = 0; i < packs.length; i += 1) {
packs[i].packIndex = i;
}
setAvailableEmojis(packs);
}
2022-01-09 10:29:06 +05:30
};
2021-12-29 23:02:49 -05:00
2022-02-08 12:43:59 +01:00
const onOpen = () => {
searchRef.current.value = '';
handleSearchChange();
// only update when board is getting opened to prevent shifting UI
setRecentEmojis(getRecentEmojis(3 * ROW_EMOJIS_COUNT));
2022-02-08 12:43:59 +01:00
};
2021-12-29 23:02:49 -05:00
navigation.on(cons.events.navigation.ROOM_SELECTED, updateAvailableEmoji);
2022-02-08 12:43:59 +01:00
navigation.on(cons.events.navigation.EMOJIBOARD_OPENED, onOpen);
2021-12-29 23:02:49 -05:00
return () => {
navigation.removeListener(cons.events.navigation.ROOM_SELECTED, updateAvailableEmoji);
2022-02-08 12:43:59 +01:00
navigation.removeListener(cons.events.navigation.EMOJIBOARD_OPENED, onOpen);
2021-12-29 23:02:49 -05:00
};
}, []);
2021-07-28 18:45:52 +05:30
function openGroup(groupOrder) {
let tabIndex = groupOrder;
const $emojiContent = scrollEmojisRef.current.firstElementChild;
const groupCount = $emojiContent.childElementCount;
2021-12-29 23:02:49 -05:00
if (groupCount > emojiGroups.length) {
tabIndex += groupCount - emojiGroups.length - availableEmojis.length - recentOffset;
2021-12-29 23:02:49 -05:00
}
2021-07-28 18:45:52 +05:30
$emojiContent.children[tabIndex].scrollIntoView();
}
return (
<div id="emoji-board" className="emoji-board">
2021-12-29 23:02:49 -05:00
<ScrollView invisible>
<div className="emoji-board__nav">
{recentEmojis.length > 0 && (
<IconButton
onClick={() => openGroup(0)}
src={RecentClockIC}
tooltip="Recent"
2022-08-11 16:13:53 +05:30
tooltipPlacement="left"
/>
)}
<div className="emoji-board__nav-custom">
2023-01-14 18:51:42 +05:30
{availableEmojis.map((pack) => {
const src = initMatrix.matrixClient.mxcUrlToHttp(
pack.avatarUrl ?? pack.getEmojis()[0].mxc
);
return (
2021-12-30 14:17:55 +05:30
<IconButton
2023-01-14 18:51:42 +05:30
onClick={() => openGroup(recentOffset + pack.packIndex)}
src={src}
key={pack.packIndex}
tooltip={pack.displayName ?? 'Unknown'}
2022-08-11 16:13:53 +05:30
tooltipPlacement="left"
2023-01-14 18:51:42 +05:30
isImage
2021-12-30 14:17:55 +05:30
/>
2023-01-14 18:51:42 +05:30
);
})}
</div>
<div className="emoji-board__nav-twemoji">
{[
[0, EmojiIC, 'Smilies'],
[1, DogIC, 'Animals'],
[2, CupIC, 'Food'],
[3, BallIC, 'Activities'],
[4, PhotoIC, 'Travel'],
[5, BulbIC, 'Objects'],
[6, PeaceIC, 'Symbols'],
[7, FlagIC, 'Flags'],
].map(([indx, ico, name]) => (
<IconButton
onClick={() => openGroup(recentOffset + availableEmojis.length + indx)}
key={indx}
src={ico}
tooltip={name}
tooltipPlacement="left"
/>
))}
2021-12-30 14:17:55 +05:30
</div>
2021-12-29 23:02:49 -05:00
</div>
</ScrollView>
2022-08-11 16:13:53 +05:30
<div className="emoji-board__content">
<div className="emoji-board__content__search">
<RawIcon size="small" src={SearchIC} />
<Input onChange={handleSearchChange} forwardRef={searchRef} placeholder="Search" />
</div>
<div className="emoji-board__content__emojis">
<ScrollView ref={scrollEmojisRef} autoHide>
<div onMouseMove={hoverEmoji} onClick={selectEmoji}>
<SearchedEmoji />
2023-01-14 18:51:42 +05:30
{recentEmojis.length > 0 && (
<EmojiGroup name="Recently used" groupEmojis={recentEmojis} />
)}
{availableEmojis.map((pack) => (
<EmojiGroup
name={pack.displayName ?? 'Unknown'}
key={pack.packIndex}
groupEmojis={pack.getEmojis()}
className="custom-emoji-group"
/>
))}
{emojiGroups.map((group) => (
<EmojiGroup key={group.name} name={group.name} groupEmojis={group.emojis} />
))}
2022-08-11 16:13:53 +05:30
</div>
</ScrollView>
</div>
<div ref={emojiInfo} className="emoji-board__content__info">
2023-01-14 18:51:42 +05:30
<div>{parse(twemoji.parse('🙂', { base: TWEMOJI_BASE_URL }))}</div>
2022-08-11 16:13:53 +05:30
<Text>:slight_smile:</Text>
</div>
</div>
2021-07-28 18:45:52 +05:30
</div>
);
}
EmojiBoard.propTypes = {
onSelect: PropTypes.func.isRequired,
2022-02-08 12:43:59 +01:00
searchRef: PropTypes.shape({}).isRequired,
2021-07-28 18:45:52 +05:30
};
export default EmojiBoard;