import React, { useMemo } from 'react'; import { useUIStore } from '../../stores/uiStore'; import { useSpaceStore } from '../../stores/spaceStore'; import { useChatStore } from '../../stores/chatStore'; import { useAuthStore } from '../../stores/authStore'; import { useSocialStore } from '../../stores/socialStore'; import { useContextMenuStore } from '../../stores/contextMenuStore'; import { Avatar } from '../ui/Avatar'; import { Mascot } from '../ui/Mascot'; import { resolveAssetUrl } from '../../utils/assetUrls'; import { useNavigate } from 'react-router-dom'; import { parseFederatedUsername } from '../../utils/identity'; import { formatDmSidebarPreview } from '../../utils/dmFormatters'; export function MobileDmsScreen() { const pushMobileScreen = useUIStore((s) => s.pushMobileScreen); const openModal = useUIStore((s) => s.openModal); const dmChannels = useSpaceStore((s) => s.dmChannels); const readStates = useChatStore((s) => s.readStates); const authUser = useAuthStore((s) => s.user); const friends = useSocialStore((s) => s.friends); const navigate = useNavigate(); const openContextMenu = useContextMenuStore((s) => s.open); const setCurrentChannel = useChatStore((s) => s.setCurrentChannel); // Online friends for the activity row const onlineFriends = useMemo(() => friends.filter(f => f.status === 'online' || f.status === 'idle' || f.status === 'dnd'), [friends] ); // Sort DMs by last message time (newest first) const sortedDms = useMemo(() => [...dmChannels].sort((a, b) => { const aTime = a.lastMessage?.createdAt ?? a.createdAt; const bTime = b.lastMessage?.createdAt ?? b.createdAt; return bTime - aTime; }), [dmChannels] ); const handleDmTap = (dmId: string) => { navigate(`/channels/@me/${dmId}`); pushMobileScreen('channel-chat', { channelId: dmId, spaceId: '@me' }); }; const handleDmContextMenu = (e: React.MouseEvent, dmId: string, isGroup: boolean) => { if (!isGroup) return; e.preventDefault(); e.stopPropagation(); openContextMenu({ x: e.clientX, y: e.clientY }, [ { key: 'leave-group', type: 'action', label: 'Leave Group', danger: true, icon: , onClick: () => { const currentChId = useChatStore.getState().currentChannelId; if (currentChId === dmId) { navigate('/channels/@me'); setCurrentChannel(null); } useSpaceStore.getState().leaveDm(dmId); }, }, ]); }; const formatTimestamp = (ts: number): string => { const now = Date.now(); const diff = now - ts; if (diff < 60_000) return 'now'; if (diff < 3600_000) return `${Math.floor(diff / 60_000)}m`; if (diff < 86400_000) return `${Math.floor(diff / 3600_000)}h`; if (diff < 604800_000) return `${Math.floor(diff / 86400_000)}d`; return new Date(ts).toLocaleDateString(undefined, { month: 'short', day: 'numeric' }); }; return (
No conversations yet.