import React, { useState, useCallback, ChangeEventHandler } from 'react'; import { Box, Button, config, Dialog, Input, Text, Overlay, OverlayBackdrop, OverlayCenter, } from 'folds'; import FocusTrap from 'focus-trap-react'; import { useMatrixClient } from '../../hooks/useMatrixClient'; import { useUserPresence } from '../../hooks/useUserPresence'; import { AsyncStatus, useAsyncCallback } from '../../hooks/useAsyncCallback'; /** * Props for the CustomStatusDialog component */ export interface CustomStatusDialogProps { /** Whether the dialog is currently open */ open: boolean; /** Callback function invoked when the dialog should close */ onClose: () => void; } /** * CustomStatusDialog Component * * Provides a dialog interface for users to set or clear their custom status message. * The status is stored in Matrix presence and will be visible to other users. * * @param props - Component props * @returns The custom status dialog component */ export function CustomStatusDialog({ open, onClose }: CustomStatusDialogProps) { const mx = useMatrixClient(); const userId = mx.getUserId(); const presence = useUserPresence(userId ?? ''); const [statusText, setStatusText] = useState(presence?.status || ''); /** * Handles input changes for the status text field * @param evt - The change event from the input */ const handleChange: ChangeEventHandler = (evt) => { setStatusText(evt.currentTarget.value); }; /** * Async callback to save the status to Matrix presence */ const [saveState, saveStatus] = useAsyncCallback( useCallback(async () => { await mx.setPresence({ presence: presence?.presence || 'online', status_msg: statusText, }); onClose(); }, [mx, presence?.presence, statusText, onClose]) ); /** * Async callback to clear the status from Matrix presence */ const [clearState, clearStatus] = useAsyncCallback( useCallback(async () => { await mx.setPresence({ presence: presence?.presence || 'online', status_msg: '', }); setStatusText(''); onClose(); }, [mx, presence?.presence, onClose]) ); /** * Handles form submission to save the custom status * @param evt - The form submit event */ const handleSubmit = (evt: React.FormEvent) => { evt.preventDefault(); saveStatus(); }; /** * Handles the clear status button click */ const handleClear = () => { clearStatus(); }; if (!open) return null; return ( }> Set Custom Status Your custom status will be visible to other users {statusText.length > 0 && ( {statusText.length}/100 characters )} {(saveState.status === AsyncStatus.Error || clearState.status === AsyncStatus.Error) && ( Failed to update status. Please try again. )} {presence?.status && ( )} ); }