A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
import {EmojiPicker} from '@app/features/channel/components/EmojiPicker';
import {
EMOJI_GRID_SCROLLER_PADDING_WIDTH,
EMOJI_GRID_TRACK_WIDTH,
} from '@app/features/channel/components/emoji_picker/EmojiPickerConstants';
import {GifPicker} from '@app/features/channel/components/pickers/gif/GifPicker';
import {MemesPicker} from '@app/features/channel/components/pickers/memes/MemesPicker';
import {StickersPicker} from '@app/features/channel/components/StickersPicker';
import ExpressionPicker from '@app/features/emoji/state/ExpressionPicker';
import type {FlatEmoji} from '@app/features/emoji/types/EmojiTypes';
import styles from '@app/features/expressions/components/popouts/ExpressionPickerPopout.module.css';
import type {GuildSticker} from '@app/features/expressions/models/GuildSticker';
import * as StickerSendUtils from '@app/features/expressions/utils/StickerSendUtils';
import {
EMOJIS_DESCRIPTOR,
EXPRESSION_PICKER_CATEGORIES_DESCRIPTOR,
GIFS_DESCRIPTOR,
MEDIA_DESCRIPTOR,
STICKERS_DESCRIPTOR,
} from '@app/features/i18n/utils/CommonMessageDescriptors';
import {AppStorageKey} from '@app/features/platform/state/AppStorageKeys';
import {ComponentBus} from '@app/features/platform/utils/ComponentBus';
import {remFromPx} from '@app/features/theme/layout/RemFromPx';
import {useResizablePane} from '@app/features/ui/hooks/useResizablePane';
import {
type ResizablePaneHandleLabels,
ResizablePaneHandles,
} from '@app/features/ui/resizable_pane/ResizablePaneHandles';
import {getNextTabIndex, getTabNavigationDirection} from '@app/features/ui/tabs/TabKeyboardNavigation';
import type {I18n} from '@lingui/core';
import {msg} from '@lingui/core/macro';
import {useLingui} from '@lingui/react/macro';
import {clsx} from 'clsx';
import {observer} from 'mobx-react-lite';
import React, {useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import ReactDOM from 'react-dom';
const RESIZE_EXPRESSION_PICKER_TOP_DESCRIPTOR = msg({
message: 'Resize expression picker from the top edge',
comment: 'Accessible label for the top resize handle on the expression picker popout.',
});
const RESIZE_EXPRESSION_PICKER_BOTTOM_DESCRIPTOR = msg({
message: 'Resize expression picker from the bottom edge',
comment: 'Accessible label for the bottom resize handle on the expression picker popout.',
});
const RESIZE_EXPRESSION_PICKER_LEFT_DESCRIPTOR = msg({
message: 'Resize expression picker from the left edge',
comment: 'Accessible label for the left resize handle on the expression picker popout.',
});
const RESIZE_EXPRESSION_PICKER_RIGHT_DESCRIPTOR = msg({
message: 'Resize expression picker from the right edge',
comment: 'Accessible label for the right resize handle on the expression picker popout.',
});
const RESIZE_EXPRESSION_PICKER_TOP_LEFT_DESCRIPTOR = msg({
message: 'Resize expression picker from top left',
comment: 'Accessible label for the top-left resize handle on the expression picker popout.',
});
const RESIZE_EXPRESSION_PICKER_TOP_RIGHT_DESCRIPTOR = msg({
message: 'Resize expression picker from top right',
comment: 'Accessible label for the top-right resize handle on the expression picker popout.',
});
const RESIZE_EXPRESSION_PICKER_BOTTOM_LEFT_DESCRIPTOR = msg({
message: 'Resize expression picker from bottom left',
comment: 'Accessible label for the bottom-left resize handle on the expression picker popout.',
});
const RESIZE_EXPRESSION_PICKER_BOTTOM_RIGHT_DESCRIPTOR = msg({
message: 'Resize expression picker from bottom right',
comment: 'Accessible label for the bottom-right resize handle on the expression picker popout.',
});
const EXPRESSION_PICKER_CATEGORY_RAIL_WIDTH = 46;
const EXPRESSION_PICKER_BORDER_WIDTH = 1;
const EXPRESSION_PICKER_GRID_CHROME_WIDTH =
EXPRESSION_PICKER_CATEGORY_RAIL_WIDTH + EXPRESSION_PICKER_BORDER_WIDTH * 2 + EMOJI_GRID_SCROLLER_PADDING_WIDTH * 2;
const EXPRESSION_PICKER_WIDTH_SNAP = {
step: EMOJI_GRID_TRACK_WIDTH,
offset: EXPRESSION_PICKER_GRID_CHROME_WIDTH,
};
const EXPRESSION_PICKER_DEFAULT_SIZE = {width: 592, height: 690};
const EXPRESSION_PICKER_MIN_SIZE = {width: 360, height: 360};
const ROLE_ICON_PICKER_COLUMNS = 8;
const ROLE_ICON_PICKER_SIZE = {
width: EXPRESSION_PICKER_GRID_CHROME_WIDTH + EMOJI_GRID_TRACK_WIDTH * ROLE_ICON_PICKER_COLUMNS,
height: EXPRESSION_PICKER_MIN_SIZE.height,
};
const EXPRESSION_PICKER_VIEWPORT_PADDING = 16;
const EXPRESSION_PICKER_RESIZING_CLASS = 'expression-picker-resizing';
const EXPRESSION_PICKER_RESIZE_CURSOR_PROPERTY = '--expression-picker-resize-cursor';
interface ExpressionPickerHeaderContextType {
headerPortalElement: HTMLDivElement | null;
}
export const ExpressionPickerHeaderContext = React.createContext<ExpressionPickerHeaderContextType | null>(null);
export const useExpressionPickerHeaderPortal = () => {
return useContext(ExpressionPickerHeaderContext);
};
export const ExpressionPickerHeaderPortal = ({children}: {children: React.ReactNode}) => {
const context = useExpressionPickerHeaderPortal();
if (context == null || context.headerPortalElement == null) return null;
return ReactDOM.createPortal(children, context.headerPortalElement);
};
export type ExpressionPickerTabType = 'gifs' | 'memes' | 'stickers' | 'emojis';
interface ExpressionPickerCategory {
type: ExpressionPickerTabType;
label: string;
renderComponent: (props: {
channelId?: string;
roleIconGuildId?: string;
onSelect: (emoji: FlatEmoji, shiftKey?: boolean) => void;
onClose?: () => void;
}) => React.ReactNode;
}
const createAllCategories = (i18n: I18n): Array<ExpressionPickerCategory> => [
{
type: 'gifs' as const,
label: i18n._(GIFS_DESCRIPTOR),
renderComponent: ({onClose}) => (
<GifPicker onClose={onClose} data-flx="expressions.expression-picker-popout.render-component.gif-picker" />
),
},
{
type: 'memes' as const,
label: i18n._(MEDIA_DESCRIPTOR),
renderComponent: ({onClose}) => (
<MemesPicker onClose={onClose} data-flx="expressions.expression-picker-popout.render-component.memes-picker" />
),
},
{
type: 'stickers' as const,
label: i18n._(STICKERS_DESCRIPTOR),
renderComponent: ({channelId, onClose}) => {
const handleStickerSelect = (sticker: GuildSticker, shiftKey?: boolean) => {
if (channelId) {
if (StickerSendUtils.shouldSetPendingSticker(channelId)) {
StickerSendUtils.setPendingSticker(channelId, sticker);
} else {
ComponentBus.dispatch('STICKER_SELECT', {sticker});
}
if (onClose && !shiftKey) {
onClose();
}
}
};
return (
<StickersPicker
channelId={channelId}
handleSelect={handleStickerSelect}
data-flx="expressions.expression-picker-popout.render-component.stickers-picker"
/>
);
},
},
{
type: 'emojis' as const,
label: i18n._(EMOJIS_DESCRIPTOR),
renderComponent: ({channelId, roleIconGuildId, onSelect}) => (
<EmojiPicker
channelId={channelId}
roleIconGuildId={roleIconGuildId}
handleSelect={onSelect}
data-flx="expressions.expression-picker-popout.render-component.emoji-picker"
/>
),
},
];
interface ExpressionPickerPopoutProps {
channelId?: string;
roleIconGuildId?: string;
onEmojiSelect: (emoji: FlatEmoji, shiftKey?: boolean) => void;
onClose?: () => void;
visibleTabs?: Array<ExpressionPickerTabType>;
}
const RESIZE_HANDLE_LABELS: ResizablePaneHandleLabels = {
top: RESIZE_EXPRESSION_PICKER_TOP_DESCRIPTOR,
bottom: RESIZE_EXPRESSION_PICKER_BOTTOM_DESCRIPTOR,
left: RESIZE_EXPRESSION_PICKER_LEFT_DESCRIPTOR,
right: RESIZE_EXPRESSION_PICKER_RIGHT_DESCRIPTOR,
'top-left': RESIZE_EXPRESSION_PICKER_TOP_LEFT_DESCRIPTOR,
'top-right': RESIZE_EXPRESSION_PICKER_TOP_RIGHT_DESCRIPTOR,
'bottom-left': RESIZE_EXPRESSION_PICKER_BOTTOM_LEFT_DESCRIPTOR,
'bottom-right': RESIZE_EXPRESSION_PICKER_BOTTOM_RIGHT_DESCRIPTOR,
};
export const ExpressionPickerPopout = observer(
({
channelId,
roleIconGuildId,
onEmojiSelect,
onClose,
visibleTabs = ['gifs', 'memes', 'stickers', 'emojis'],
}: ExpressionPickerPopoutProps) => {
const {i18n} = useLingui();
const categories = useMemo(() => {
const all = createAllCategories(i18n);
return all.filter(
(category) => visibleTabs.includes(category.type) && (category.type !== 'gifs' || RuntimeConfig.gifEnabled),
);
}, [i18n.locale, visibleTabs, RuntimeConfig.gifEnabled]);
const selectedTab = ExpressionPicker.selectedTab;
const setSelectedTab = useCallback(
(tab: ExpressionPickerTabType) => {
if (tab === selectedTab) {
return;
}
const pickerChannelId = ExpressionPicker.channelId;
if (pickerChannelId) {
ExpressionPicker.toggle(pickerChannelId, tab);
}
},
[selectedTab],
);
const selectedCategory = categories.find((category) => category.type === selectedTab) || categories[0];
const containerRef = useRef<HTMLDivElement>(null);
const tabRefs = useRef<Map<ExpressionPickerTabType, HTMLButtonElement>>(new Map());
const {size, getHandleProps} = useResizablePane(containerRef, {
storageKey: AppStorageKey.UI_EXPRESSION_PICKER_SIZE,
defaultSize: EXPRESSION_PICKER_DEFAULT_SIZE,
minSize: EXPRESSION_PICKER_MIN_SIZE,
widthSnap: EXPRESSION_PICKER_WIDTH_SNAP,
viewportPadding: EXPRESSION_PICKER_VIEWPORT_PADDING,
resizingClassName: EXPRESSION_PICKER_RESIZING_CLASS,
cursorProperty: EXPRESSION_PICKER_RESIZE_CURSOR_PROPERTY,
});
useEffect(() => {
if (containerRef.current) {
const firstInput = containerRef.current.querySelector('input[type="text"]') as HTMLInputElement | null;
if (firstInput) {
firstInput.focus();
}
}
}, []);
const handleEmojiSelect = useCallback(
(emoji: FlatEmoji, shiftKey?: boolean) => {
onEmojiSelect(emoji, shiftKey);
if (onClose && !shiftKey) {
onClose();
}
},
[onEmojiSelect, onClose],
);
const handleTabKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLButtonElement>, tab: ExpressionPickerTabType) => {
const currentIndex = categories.findIndex((category) => category.type === tab);
if (currentIndex === -1) return;
const direction = getTabNavigationDirection(event.key, 'horizontal');
if (!direction) return;
const nextIndex = getNextTabIndex(currentIndex, categories.length, direction);
if (nextIndex == null) return;
event.preventDefault();
event.stopPropagation();
const nextCategory = categories[nextIndex];
if (nextCategory == null) return;
const nextTab = nextCategory.type;
setSelectedTab(nextTab);
const ownerWindow = event.currentTarget.ownerDocument.defaultView;
if (ownerWindow == null) return;
ownerWindow.requestAnimationFrame(() => {
const nextTabElement = tabRefs.current.get(nextTab);
if (nextTabElement != null) nextTabElement.focus();
});
},
[categories, setSelectedTab],
);
const showTabs = categories.length > 1;
const isRoleIconPicker = roleIconGuildId !== undefined;
const pickerSize = isRoleIconPicker ? ROLE_ICON_PICKER_SIZE : size;
const [headerPortalElement, setHeaderPortalElement] = useState<HTMLDivElement | null>(null);
const headerPortalCallback = useCallback((node: HTMLDivElement | null) => {
setHeaderPortalElement(node);
}, []);
const headerContextValue = useMemo(() => ({headerPortalElement}), [headerPortalElement]);
return (
<ExpressionPickerHeaderContext.Provider value={headerContextValue}>
<div
ref={containerRef}
className={clsx(styles.container, showTabs ? styles.containerWithTabs : styles.containerNoTabs)}
style={{width: remFromPx(pickerSize.width), height: remFromPx(pickerSize.height)}}
data-flx="expressions.expression-picker-popout.container"
>
<div className={styles.header} data-flx="expressions.expression-picker-popout.header">
{showTabs && (
<nav className={styles.nav} data-flx="expressions.expression-picker-popout.nav">
<div
className={styles.tabList}
role="tablist"
aria-label={i18n._(EXPRESSION_PICKER_CATEGORIES_DESCRIPTOR)}
data-flx="expressions.expression-picker-popout.tab-list"
>
{categories.map((category) => {
const isSelected = selectedCategory.type === category.type;
return (
<button
key={category.type}
ref={(element) => {
if (element) {
tabRefs.current.set(category.type, element);
} else {
tabRefs.current.delete(category.type);
}
}}
id={category.type}
role="tab"
type="button"
aria-selected={isSelected}
tabIndex={isSelected ? 0 : -1}
className={clsx(styles.tab, isSelected ? styles.tabActive : styles.tabInactive)}
onClick={() => setSelectedTab(category.type)}
onKeyDown={(event) => handleTabKeyDown(event, category.type)}
data-flx="expressions.expression-picker-popout.tab.button"
>
{category.label}
</button>
);
})}
</div>
</nav>
)}
<div
ref={headerPortalCallback}
className={styles.headerPortal}
data-flx="expressions.expression-picker-popout.header-portal"
/>
</div>
<div className={styles.content} data-flx="expressions.expression-picker-popout.content">
{selectedCategory.renderComponent({channelId, roleIconGuildId, onSelect: handleEmojiSelect, onClose})}
</div>
{!isRoleIconPicker && (
<ResizablePaneHandles
getHandleProps={getHandleProps}
labels={RESIZE_HANDLE_LABELS}
data-flx="expressions.expression-picker-popout.resizable-pane-handles"
/>
)}
</div>
</ExpressionPickerHeaderContext.Provider>
);
},
);