import React, { ChangeEventHandler, FormEventHandler, Suspense, useCallback, useEffect, useMemo, useRef, useState, } from 'react'; import { Box, Text, IconButton, Icon, Icons, Input, Avatar, Button, Overlay, OverlayBackdrop, OverlayCenter, Modal, Dialog, Header, config, Spinner, PopOut, RectCords, } from 'folds'; import FocusTrap from 'focus-trap-react'; import { SequenceCard } from '../../../components/sequence-card'; import { SequenceCardStyle } from '../styles.css'; import { SettingTile } from '../../../components/setting-tile'; import { useMatrixClient } from '../../../hooks/useMatrixClient'; import { UserProfile, useUserProfile } from '../../../hooks/useUserProfile'; import { getMxIdLocalPart, mxcUrlToHttp } from '../../../utils/matrix'; import { UserAvatar } from '../../../components/user-avatar'; import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication'; import { nameInitials } from '../../../utils/common'; import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback'; import { useFilePicker } from '../../../hooks/useFilePicker'; import { useObjectURL } from '../../../hooks/useObjectURL'; import { stopPropagation } from '../../../utils/keyboard'; import { ImageEditor } from '../../../components/image-editor'; import { ModalWide } from '../../../styles/Modal.css'; import { createUploadAtom, UploadSuccess } from '../../../state/upload'; import { CompactUploadCardRenderer } from '../../../components/upload-card'; import { useCapabilities } from '../../../hooks/useCapabilities'; import { useUserPresence } from '../../../hooks/useUserPresence'; const EmojiBoard = React.lazy(() => import('../../../components/emoji-board').then((m) => ({ default: m.EmojiBoard })), ); type ProfileProps = { profile: UserProfile; userId: string; }; function ProfileAvatar({ profile, userId }: ProfileProps) { const mx = useMatrixClient(); const useAuthentication = useMediaAuthentication(); const capabilities = useCapabilities(); const [alertRemove, setAlertRemove] = useState(false); const disableSetAvatar = capabilities['m.set_avatar_url']?.enabled === false; const defaultDisplayName = profile.displayName ?? getMxIdLocalPart(userId) ?? userId; const avatarUrl = profile.avatarUrl ? (mxcUrlToHttp(mx, profile.avatarUrl, useAuthentication, 96, 96, 'crop') ?? undefined) : undefined; const [imageFile, setImageFile] = useState(); const imageFileURL = useObjectURL(imageFile); const uploadAtom = useMemo(() => { if (imageFile) return createUploadAtom(imageFile); return undefined; }, [imageFile]); const pickFile = useFilePicker(setImageFile, false); const handleRemoveUpload = useCallback(() => { setImageFile(undefined); }, []); const handleUploaded = useCallback( (upload: UploadSuccess) => { const { mxc } = upload; mx.setAvatarUrl(mxc); handleRemoveUpload(); }, [mx, handleRemoveUpload], ); const handleRemoveAvatar = () => { mx.setAvatarUrl(''); setAlertRemove(false); }; return ( Avatar } after={ {nameInitials(defaultDisplayName)}} /> } > {uploadAtom ? ( ) : ( {avatarUrl && ( )} )} {imageFileURL && ( }> )} }> setAlertRemove(false), clickOutsideDeactivates: true, escapeDeactivates: stopPropagation, }} >
Remove Avatar setAlertRemove(false)} radii="300" aria-label="Cancel" >
Are you sure you want to remove profile avatar?
); } function ProfileDisplayName({ profile, userId }: ProfileProps) { const mx = useMatrixClient(); const capabilities = useCapabilities(); const disableSetDisplayname = capabilities['m.set_displayname']?.enabled === false; const defaultDisplayName = profile.displayName ?? getMxIdLocalPart(userId) ?? userId; const [displayName, setDisplayName] = useState(defaultDisplayName); const [changeState, changeDisplayName] = useAsyncCallback( useCallback((name: string) => mx.setDisplayName(name), [mx]), ); const changingDisplayName = changeState.status === AsyncStatus.Loading; useEffect(() => { setDisplayName(defaultDisplayName); }, [defaultDisplayName]); const handleChange: ChangeEventHandler = (evt) => { const name = evt.currentTarget.value; setDisplayName(name); }; const handleReset = () => { setDisplayName(defaultDisplayName); }; const handleSubmit: FormEventHandler = (evt) => { evt.preventDefault(); if (changingDisplayName) return; const target = evt.target as HTMLFormElement | undefined; const displayNameInput = target?.displayNameInput as HTMLInputElement | undefined; const name = displayNameInput?.value; if (!name) return; changeDisplayName(name); }; const hasChanges = displayName !== defaultDisplayName; return ( Display Name } > ) } /> ); } const STATUS_EXPIRY_KEY = (id: string) => `lotus-status-expiry-${id}`; const CLEAR_AFTER_OPTIONS = [ { label: 'Never', value: '0' }, { label: '30 minutes', value: String(30 * 60 * 1000) }, { label: '1 hour', value: String(60 * 60 * 1000) }, { label: '4 hours', value: String(4 * 60 * 60 * 1000) }, { label: '8 hours', value: String(8 * 60 * 60 * 1000) }, { label: 'Until midnight', value: 'today' }, { label: '1 day', value: String(24 * 60 * 60 * 1000) }, { label: '7 days', value: String(7 * 24 * 60 * 60 * 1000) }, ]; function getMsFromOption(value: string): number { if (value === '0') return 0; if (value === 'today') { const eod = new Date(); eod.setHours(23, 59, 59, 999); return eod.getTime() - Date.now(); } return parseInt(value, 10); } function ProfileStatus() { const mx = useMatrixClient(); const userId = mx.getUserId()!; const presence = useUserPresence(userId); const [statusMsg, setStatusMsg] = useState(presence?.status ?? ''); const [clearAfter, setClearAfter] = useState('0'); const [emojiAnchor, setEmojiAnchor] = useState(); const inputRef = useRef(null); // Initialise expiry from localStorage so timer survives page reload const [expiryTs, setExpiryTs] = useState(() => { const stored = localStorage.getItem(STATUS_EXPIRY_KEY(userId)); return stored ? parseInt(stored, 10) : 0; }); // Sync input when another device changes the status useEffect(() => { setStatusMsg(presence?.status ?? ''); }, [presence?.status]); // Drive the auto-clear timer off expiryTs so re-saving cancels the old timer useEffect(() => { if (!expiryTs) return undefined; const remaining = expiryTs - Date.now(); if (remaining <= 0) { localStorage.removeItem(STATUS_EXPIRY_KEY(userId)); setExpiryTs(0); mx.setPresence({ presence: 'online', status_msg: '' }); return undefined; } const timer = window.setTimeout(() => { localStorage.removeItem(STATUS_EXPIRY_KEY(userId)); setExpiryTs(0); mx.setPresence({ presence: 'online', status_msg: '' }); }, remaining); return () => clearTimeout(timer); }, [expiryTs, userId, mx]); const [saveState, saveStatus] = useAsyncCallback( useCallback( (msg: string) => mx.setPresence({ presence: 'online', status_msg: msg, }), [mx], ), ); const saving = saveState.status === AsyncStatus.Loading; const handleEmojiSelect = useCallback( (unicode: string) => { const input = inputRef.current; if (input) { const start = input.selectionStart ?? statusMsg.length; const end = input.selectionEnd ?? statusMsg.length; const next = statusMsg.slice(0, start) + unicode + statusMsg.slice(end); setStatusMsg(next); requestAnimationFrame(() => { input.focus(); input.setSelectionRange(start + unicode.length, start + unicode.length); }); } else { setStatusMsg((prev) => prev + unicode); } setEmojiAnchor(undefined); }, [statusMsg], ); const handleChange: ChangeEventHandler = (evt) => { setStatusMsg(evt.currentTarget.value); }; const handleSubmit: FormEventHandler = (evt) => { evt.preventDefault(); if (saving) return; const msg = statusMsg.trim(); saveStatus(msg); const delayMs = getMsFromOption(clearAfter); if (msg && delayMs > 0) { const ts = Date.now() + delayMs; localStorage.setItem(STATUS_EXPIRY_KEY(userId), String(ts)); setExpiryTs(ts); } else { localStorage.removeItem(STATUS_EXPIRY_KEY(userId)); setExpiryTs(0); } }; const handleClear = () => { setStatusMsg(''); localStorage.removeItem(STATUS_EXPIRY_KEY(userId)); setExpiryTs(0); mx.setPresence({ presence: 'online', status_msg: '' }); }; const hasChanges = statusMsg !== (presence?.status ?? ''); return ( Status Message } description={ Shown below your name in member lists. Supports emoji. } > }> setEmojiAnchor(undefined)} /> } > ) => { const rect = evt.currentTarget.getBoundingClientRect(); setEmojiAnchor((prev) => (prev ? undefined : rect)); }} > } /> Auto-clear after: {(presence?.status || statusMsg) && ( )} ); } export function Profile() { const mx = useMatrixClient(); const userId = mx.getUserId()!; const profile = useUserProfile(userId); return ( Profile ); }