Skip to content

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

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

import {
	getTimeWindowKeyForExpiresAt,
	minutesToMs,
	type TimeWindowKey,
} from '@app/features/app/config/TimeWindowPresets';
import DeveloperMode from '@app/features/devtools/state/DeveloperMode';
import {ExpressionPickerSheet} from '@app/features/expressions/components/modals/ExpressionPickerSheet';
import Presence from '@app/features/presence/state/Presence';
import {BottomSheet} from '@app/features/ui/bottom_sheet/BottomSheet';
import {Button} from '@app/features/ui/button/Button';
import {Input} from '@app/features/ui/components/form/FormInput';
import FocusRing from '@app/features/ui/focus_ring/FocusRing';
import styles from '@app/features/user/components/modals/CustomStatusBottomSheet.module.css';
import {
	CustomStatusEmojiPreview,
	hasCustomStatusEmojiPreview,
	useCustomStatusDraft,
} from '@app/features/user/hooks/useCustomStatusDraft';
import {normalizeCustomStatus} from '@app/features/user/state/CustomStatus';
import Users from '@app/features/user/state/Users';
import {
	CHANGE_EMOJI_DESCRIPTOR,
	CHOOSE_AN_EMOJI_DESCRIPTOR,
	CLEAR_CUSTOM_STATUS_DESCRIPTOR,
	WHAT_S_HAPPENING_DESCRIPTOR,
} from '@app/features/user/utils/UserMessageDescriptors';
import {msg} from '@lingui/core/macro';
import {Trans, useLingui} from '@lingui/react/macro';
import {SmileyIcon, XIcon} from '@phosphor-icons/react';
import clsx from 'clsx';
import {observer} from 'mobx-react-lite';
import {useCallback, useEffect, useState} from 'react';

const SET_CUSTOM_STATUS_DESCRIPTOR = msg({
	message: 'Set custom status',
	comment: 'Short label in the custom status bottom sheet. Keep it concise.',
});

const CUSTOM_STATUS_SNAP_POINTS: Array<number> = [0, 1];

interface CustomStatusBottomSheetProps {
	isOpen: boolean;
	onClose: () => void;
}

export const CustomStatusBottomSheet = observer(({isOpen, onClose}: CustomStatusBottomSheetProps) => {
	const {i18n} = useLingui();
	const currentUser = Users.getCurrentUser();
	const currentUserId = currentUser?.id ?? null;
	const existingCustomStatus = currentUserId ? Presence.getCustomStatus(currentUserId) : null;
	const normalizedExisting = normalizeCustomStatus(existingCustomStatus);
	const isDeveloper = DeveloperMode.isDeveloper;
	const {
		statusText,
		setStatusText,
		isSaving,
		draftStatus,
		handleEmojiSelect,
		handleClearDraft,
		handleSave,
		resetDraft,
		expirationPresets,
	} = useCustomStatusDraft({initialStatus: null, onSaved: onClose, getExpiresAt: () => getExpiresAtForSave()});

	const [selectedExpiry, setSelectedExpiry] = useState<TimeWindowKey>('never');

	const [emojiPickerOpen, setEmojiPickerOpen] = useState(false);
	useEffect(() => {
		if (isOpen) {
			resetDraft(normalizedExisting);
			setSelectedExpiry(
				normalizedExisting
					? getTimeWindowKeyForExpiresAt(normalizedExisting.expiresAt, {
							includeDeveloperOptions: isDeveloper,
							fallbackKey: 'never',
						})
					: 'never',
			);
		}
	}, [
		isOpen,
		resetDraft,
		normalizedExisting?.text,
		normalizedExisting?.emojiId,
		normalizedExisting?.emojiName,
		normalizedExisting?.expiresAt,
		isDeveloper,
	]);

	const getExpiresAtForSave = useCallback((): string | null => {
		const option = expirationPresets.find((o) => o.key === selectedExpiry);
		if (!option?.minutes) return null;
		return new Date(Date.now() + minutesToMs(option.minutes)!).toISOString();
	}, [expirationPresets, selectedExpiry]);
	const emojiPreview = hasCustomStatusEmojiPreview(draftStatus) ? (
		<CustomStatusEmojiPreview
			status={draftStatus}
			className={styles.emojiPreviewImage}
			data-flx="user.custom-status-bottom-sheet.emoji-preview-image"
		/>
	) : null;
	return (
		<BottomSheet
			isOpen={isOpen}
			onClose={onClose}
			snapPoints={CUSTOM_STATUS_SNAP_POINTS}
			initialSnap={CUSTOM_STATUS_SNAP_POINTS.length - 1}
			title={i18n._(SET_CUSTOM_STATUS_DESCRIPTOR)}
			zIndex={10001}
			data-flx="user.custom-status-bottom-sheet.bottom-sheet"
		>
			<div className={styles.content} data-flx="user.custom-status-bottom-sheet.content">
				<Input
					id="custom-status-text"
					value={statusText}
					onChange={(event) => setStatusText(event.target.value.slice(0, 128))}
					maxLength={128}
					placeholder={i18n._(WHAT_S_HAPPENING_DESCRIPTOR)}
					leftElement={
						<FocusRing offset={-2} enabled={!isSaving} data-flx="user.custom-status-bottom-sheet.focus-ring">
							<button
								type="button"
								className={clsx(styles.emojiTriggerButton, emojiPickerOpen && styles.emojiTriggerButtonActive)}
								aria-label={emojiPreview ? i18n._(CHANGE_EMOJI_DESCRIPTOR) : i18n._(CHOOSE_AN_EMOJI_DESCRIPTOR)}
								aria-haspopup="dialog"
								aria-expanded={emojiPickerOpen}
								disabled={isSaving}
								onClick={() => setEmojiPickerOpen(true)}
								data-flx="user.custom-status-bottom-sheet.emoji-trigger-button.set-emoji-picker-open"
							>
								{emojiPreview ?? (
									<SmileyIcon
										size={22}
										weight="fill"
										aria-hidden="true"
										data-flx="user.custom-status-bottom-sheet.smiley-icon"
									/>
								)}
							</button>
						</FocusRing>
					}
					rightElement={
						draftStatus ? (
							<FocusRing offset={-2} enabled={!isSaving} data-flx="user.custom-status-bottom-sheet.focus-ring--2">
								<button
									type="button"
									className={styles.clearButtonIcon}
									onClick={handleClearDraft}
									disabled={isSaving}
									aria-label={i18n._(CLEAR_CUSTOM_STATUS_DESCRIPTOR)}
									data-flx="user.custom-status-bottom-sheet.clear-button-icon.clear-draft"
								>
									<XIcon size={16} weight="bold" data-flx="user.custom-status-bottom-sheet.x-icon" />
								</button>
							</FocusRing>
						) : null
					}
					data-flx="user.custom-status-bottom-sheet.custom-status-text.set-status-text"
				/>
				<ExpressionPickerSheet
					isOpen={emojiPickerOpen}
					onClose={() => setEmojiPickerOpen(false)}
					onEmojiSelect={(emoji) => {
						handleEmojiSelect(emoji);
						setEmojiPickerOpen(false);
					}}
					visibleTabs={['emojis']}
					zIndex={10002}
					data-flx="user.custom-status-bottom-sheet.expression-picker-sheet"
				/>
				<div className={styles.footer} data-flx="user.custom-status-bottom-sheet.footer">
					<div className={styles.expirySelector} data-flx="user.custom-status-bottom-sheet.expiry-selector">
						<span
							className={styles.expirySelectorLabel}
							data-flx="user.custom-status-bottom-sheet.expiry-selector-label"
						>
							<Trans>Clear after</Trans>
						</span>
						<select
							className={styles.expirySelect}
							value={selectedExpiry}
							onChange={(e) => setSelectedExpiry(e.target.value as TimeWindowKey)}
							disabled={isSaving}
							data-flx="user.custom-status-bottom-sheet.expiry-select.set-selected-expiry"
						>
							{expirationPresets.map((option) => (
								<option key={option.key} value={option.key} data-flx="user.custom-status-bottom-sheet.option">
									{option.label}
								</option>
							))}
						</select>
					</div>
					<Button
						variant="primary"
						onClick={handleSave}
						submitting={isSaving}
						className={styles.saveButton}
						data-flx="user.custom-status-bottom-sheet.save-button"
					>
						<Trans>Save</Trans>
					</Button>
				</div>
			</div>
		</BottomSheet>
	);
});

CustomStatusBottomSheet.displayName = 'CustomStatusBottomSheet';