Fixes a render bug where a federated user (e.g. axel@nova) appeared with the federation globe icon and a broken avatar when viewed on his own home instance. Root cause: `populateFromReady` is first-wins by federatedId and discards the entire skipped DM payload — including its `members` array — so when a sibling instance's ready arrived first, the home instance's view of every shared user was dropped on the floor. Adds a render-only `userViews` cache that mirrors the `dmAlternatives` philosophy: information from skipped ready payloads is preserved for rendering. Every wire surface that delivers a User upserts into the cache regardless of dedup outcome; render sites read through a Zustand selector hook to surface the home view when one is loaded. The DM channel ingestion race is left untouched — the existing no-flapping invariant on origin reconnect is intentional and load-bearing for failover. Layered changes: - `identity.ts`: `normalizeOriginToHost`, `canonicalUserKey`, `isDeliveryFromHome`, `isFederationGlobeApplicable` — single helpers for origin/host normalization and the home/stub tier decision. - `spaceStore.ts`: `userViews` Map, `UserViewEntry` type, `upsertUserView` action with the home-wins preference rule, prune by `deliveredBy` in `removeInstanceSpaces` (mirrors `dmAlternatives` cleanup), `reset` clears. - `userViewLookup.ts`: `useCanonicalUserView` (Zustand selector hook for React) + `getCanonicalUserView` (sync getter for non-React paths). Render reactivity is structural via the selector, not coincidence on legacy update paths. - `populateFromReady` upsert pass runs BEFORE the federatedId dedup so members of skipped DMs still reach the cache. - WS handlers (dm_message_*, message_*, user_updated, member_joined, friend_request_*, dm_channel_created, dm_member_added) and REST hydrators (socialStore, discoverStore, mutuals) feed the cache with their delivering origin. - Render-site routing through `useCanonicalUserView` at every audited user-rendering site (sidebar, header, search, message bubble, reply chips, profile popout/modal, group settings, voice tiles, mention chips, member lists, friends, invites). Self-rendering sites compose alongside via existing `isSelf`/`resolveDisplayIdentity`. - Globe predicate hoisted to `isFederationGlobeApplicable` and applied at three sites, gating on `domain !== window.location.host` so we never show the globe for users whose home IS our own. Tests: 31 new unit tests across `identity`, `userViews` store, and `userViewLookup`. Full suite 276/276. Docs: `client-federation.md` §3 gains a "User View Cache" section parallel to "DM Origin Failover"; `dm-system.md` notes the new store action and WS handler upserts. Bug 3 (federation profile-sync gap — orbit's stale profile data on nova-Axel after a clear/color-change on nova never propagated) remains open. The user-view cache routes around it for the common case (home instance is connected), but the underlying S2S relay gap is its own diagnosis and follows in a separate branch.
148 lines
4.5 KiB
TypeScript
148 lines
4.5 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 { useSpaceStore } from '../../stores/spaceStore';
|
|
import { api } from '../../api/client';
|
|
import type { User } from '@backspace/shared';
|
|
import { parseFederatedUsername } from '../../utils/identity';
|
|
import { useCanonicalUserView } from '../../utils/userViewLookup';
|
|
|
|
function NewDmUserRow({
|
|
user,
|
|
onSelect,
|
|
}: {
|
|
user: User;
|
|
onSelect: (user: User) => void;
|
|
}) {
|
|
const canonical = useCanonicalUserView(user);
|
|
const { baseName } = parseFederatedUsername(canonical.username);
|
|
const displayName = canonical.displayName ?? baseName;
|
|
return (
|
|
<button
|
|
onClick={() => onSelect(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={canonical.avatar} name={displayName} size={36} status={canonical.status as any} userId={canonical.homeUserId ?? canonical.id} avatarColor={canonical.avatarColor} />
|
|
<div className="flex-1 min-w-0">
|
|
<div className="text-[14px] font-medium text-txt-primary truncate">
|
|
{displayName}
|
|
</div>
|
|
<div className="text-[12px] text-txt-tertiary truncate">@{canonical.username}</div>
|
|
</div>
|
|
</button>
|
|
);
|
|
}
|
|
|
|
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 = useSpaceStore((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 = useSpaceStore.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.homeInstance ? undefined : user.id,
|
|
homeUserId: user.homeUserId ?? undefined,
|
|
homeInstance: user.homeInstance ?? undefined,
|
|
});
|
|
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" mobileStyle="sheet">
|
|
<div className="space-y-3">
|
|
<input
|
|
ref={inputRef}
|
|
type="text"
|
|
value={query}
|
|
onChange={(e) => handleSearch(e.target.value)}
|
|
placeholder="Search for a user..."
|
|
className="input-search w-full py-2 text-[14px]"
|
|
/>
|
|
|
|
{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) => (
|
|
<NewDmUserRow
|
|
key={user.id}
|
|
user={user}
|
|
onSelect={handleSelectUser}
|
|
/>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</Modal>
|
|
);
|
|
}
|