Replace the unreliable client-side DM dedup loop in populateFromReady with a creation-time guard (findExistingDmForUser) that checks all instances before opening a new DM. Guards added to FriendsPage, NewDmModal, and UserProfilePopout. Also fixes: corrupted read_states from temp_ optimistic message IDs (server migration + client-side validation), federation-aware closeDm/addDmMember API routing, isSelf-based DM member filtering in sidebar/header, and WS event error isolation.
124 lines
4.1 KiB
TypeScript
124 lines
4.1 KiB
TypeScript
import React, { useState, useRef, useEffect } from 'react';
|
|
import { useNavigate } from 'react-router-dom';
|
|
import { Modal } from '../ui/Modal';
|
|
import { Avatar } from '../ui/Avatar';
|
|
import { useUIStore } from '../../stores/uiStore';
|
|
import { useServerStore } from '../../stores/serverStore';
|
|
import { api } from '../../api/client';
|
|
import type { User } from '@backspace/shared';
|
|
|
|
export function NewDmModal() {
|
|
const [query, setQuery] = useState('');
|
|
const [results, setResults] = useState<User[]>([]);
|
|
const [isSearching, setIsSearching] = useState(false);
|
|
const [error, setError] = useState('');
|
|
const activeModal = useUIStore((s) => s.activeModal);
|
|
const closeModal = useUIStore((s) => s.closeModal);
|
|
const addDmChannel = useServerStore((s) => s.addDmChannel);
|
|
const navigate = useNavigate();
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
const searchTimer = useRef<ReturnType<typeof setTimeout>>();
|
|
|
|
const isOpen = activeModal === 'newDm';
|
|
|
|
useEffect(() => {
|
|
if (isOpen) {
|
|
setQuery('');
|
|
setResults([]);
|
|
setError('');
|
|
setTimeout(() => inputRef.current?.focus(), 100);
|
|
}
|
|
}, [isOpen]);
|
|
|
|
const handleSearch = (value: string) => {
|
|
setQuery(value);
|
|
setError('');
|
|
|
|
if (searchTimer.current) {
|
|
clearTimeout(searchTimer.current);
|
|
}
|
|
|
|
if (value.trim().length < 2) {
|
|
setResults([]);
|
|
return;
|
|
}
|
|
|
|
searchTimer.current = setTimeout(async () => {
|
|
setIsSearching(true);
|
|
try {
|
|
const users = await api.social.search(value.trim());
|
|
setResults(users);
|
|
} catch {
|
|
setResults([]);
|
|
} finally {
|
|
setIsSearching(false);
|
|
}
|
|
}, 300);
|
|
};
|
|
|
|
const handleSelectUser = async (user: User) => {
|
|
setError('');
|
|
try {
|
|
const existing = useServerStore.getState().findExistingDmForUser(user);
|
|
if (existing) {
|
|
closeModal();
|
|
useUIStore.getState().setShowDms(true);
|
|
navigate(`/channels/@me/${existing.dm.id}`);
|
|
return;
|
|
}
|
|
const channel = await api.dm.create({ userId: user.id });
|
|
addDmChannel(channel);
|
|
closeModal();
|
|
useUIStore.getState().setShowDms(true);
|
|
navigate(`/channels/@me/${channel.id}`);
|
|
} catch (err) {
|
|
setError((err as Error).message || 'Failed to create DM');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Modal isOpen={isOpen} onClose={closeModal} title="New Direct Message">
|
|
<div className="space-y-3">
|
|
<input
|
|
ref={inputRef}
|
|
type="text"
|
|
value={query}
|
|
onChange={(e) => handleSearch(e.target.value)}
|
|
placeholder="Search for a user..."
|
|
className="w-full px-3 py-2 bg-surface-input text-txt-primary placeholder-txt-tertiary/60 rounded-[4px] text-[14px] outline-none focus:ring-1 focus:ring-accent-primary"
|
|
/>
|
|
|
|
{error && (
|
|
<p className="text-txt-danger text-[13px]">{error}</p>
|
|
)}
|
|
|
|
<div className="max-h-[300px] overflow-y-auto space-y-[2px]">
|
|
{isSearching && (
|
|
<div className="py-4 text-center text-txt-tertiary text-[14px]">Searching...</div>
|
|
)}
|
|
|
|
{!isSearching && query.trim().length >= 2 && results.length === 0 && (
|
|
<div className="py-4 text-center text-txt-tertiary text-[14px]">No users found</div>
|
|
)}
|
|
|
|
{results.map((user) => (
|
|
<button
|
|
key={user.id}
|
|
onClick={() => handleSelectUser(user)}
|
|
className="w-full flex items-center gap-3 px-3 py-2 rounded-[4px] hover:bg-interactive-hover transition-colors text-left"
|
|
>
|
|
<Avatar src={user.avatar} name={user.displayName ?? user.username} size={36} status={user.status as any} userId={user.homeUserId ?? user.id} />
|
|
<div className="flex-1 min-w-0">
|
|
<div className="text-[14px] font-medium text-txt-primary truncate">
|
|
{user.displayName ?? user.username}
|
|
</div>
|
|
<div className="text-[12px] text-txt-tertiary truncate">@{user.username}</div>
|
|
</div>
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
);
|
|
}
|