Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
2.4 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import styles from '@app/features/channel/components/AutocompleteEmoji.module.css';
import {AutocompleteItem} from '@app/features/channel/components/AutocompleteItem';
import type {AutocompleteListProps} from '@app/features/channel/components/AutocompleteTypes';
import {isSticker} from '@app/features/channel/components/AutocompleteTypes';
import {useStickerAnimation} from '@app/features/emoji/hooks/useStickerAnimation';
import * as AvatarUtils from '@app/features/user/utils/AvatarUtils';
import {observer} from 'mobx-react-lite';

const AutocompleteStickerIcon = observer(
	({id, name, animated, isInteracting}: {id: string; name: string; animated: boolean; isInteracting: boolean}) => {
		const {shouldAnimate} = useStickerAnimation({isInteracting, isAnimated: animated});
		return (
			<div className={styles.stickerIconWrapper} data-flx="channel.autocomplete-sticker.sticker-icon-wrapper">
				<img
					draggable={false}
					className={styles.stickerIcon}
					src={AvatarUtils.getStickerURL({id, animated: shouldAnimate, isAnimatable: animated, size: 320})}
					alt={name}
					aria-hidden={true}
					data-flx="channel.autocomplete-sticker.sticker-icon"
				/>
			</div>
		);
	},
);
export const AutocompleteSticker = observer(
	({
		onSelect,
		keyboardFocusIndex,
		hoverIndex,
		options,
		onMouseEnter,
		onMouseLeave,
		rowRefs,
		getOptionId,
	}: AutocompleteListProps) => {
		const stickers = options.filter(isSticker);
		return stickers.map((option, index) => (
			<AutocompleteItem
				key={option.sticker.id}
				id={getOptionId?.(index)}
				name={option.sticker.name}
				description={
					option.sticker.tags.length > 0 ? option.sticker.tags.join(', ') : option.sticker.description || undefined
				}
				icon={
					<AutocompleteStickerIcon
						id={option.sticker.id}
						name={option.sticker.name}
						animated={option.sticker.animated}
						isInteracting={index === keyboardFocusIndex || index === hoverIndex}
						data-flx="channel.autocomplete-sticker.autocomplete-sticker-icon"
					/>
				}
				isKeyboardSelected={index === keyboardFocusIndex}
				isHovered={index === hoverIndex}
				onSelect={() => onSelect(option)}
				onMouseEnter={() => onMouseEnter(index)}
				onMouseLeave={onMouseLeave}
				innerRef={
					rowRefs
						? (node) => {
								rowRefs.current[index] = node;
							}
						: undefined
				}
				data-flx="channel.autocomplete-sticker.autocomplete-item.select"
			/>
		));
	},
);