feat(client-federation): user-view cache for cross-instance DM render

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.
This commit is contained in:
Jannis Braun
2026-05-05 01:59:05 +02:00
parent fb96b1457a
commit 49e9047005
33 changed files with 2150 additions and 791 deletions
@@ -5,9 +5,76 @@ import { Avatar } from '../ui/Avatar';
import { useUIStore } from '../../stores/uiStore';
import { useSpaceStore } from '../../stores/spaceStore';
import { useAuthStore } from '../../stores/authStore';
import { useSocialStore } from '../../stores/socialStore';
import { useSocialStore, type TaggedFriend } from '../../stores/socialStore';
import { api } from '../../api/client';
import { isSelf, parseFederatedUsername } from '../../utils/identity';
import { useCanonicalUserView } from '../../utils/userViewLookup';
import type { User } from '@backspace/shared';
function AddDmFriendRow({
friend,
isInDm,
isSelected,
atCapacity,
isAdding,
onToggle,
}: {
friend: TaggedFriend;
isInDm: boolean;
isSelected: boolean;
atCapacity: boolean;
isAdding: boolean;
onToggle: (id: string) => void;
}) {
const canonical = useCanonicalUserView(friend as unknown as User);
const { baseName } = parseFederatedUsername(canonical.username);
const friendDisplayName = canonical.displayName ?? baseName;
return (
<button
onClick={() => onToggle(friend.id)}
disabled={isInDm || isAdding || atCapacity}
className={`w-full flex items-center gap-3 px-3 py-2 rounded-[4px] transition-colors text-left ${
isInDm
? 'opacity-40 cursor-not-allowed'
: isSelected
? 'bg-accent-mint/[0.08]'
: 'hover:bg-interactive-hover'
} ${atCapacity && !isInDm ? 'opacity-50 cursor-not-allowed' : ''}`}
>
<Avatar
src={canonical.avatar}
name={friendDisplayName}
size={30}
status={canonical.status as any}
userId={canonical.homeUserId ?? canonical.id}
avatarColor={canonical.avatarColor}
/>
<div className="flex-1 min-w-0">
<div className="text-[13px] font-medium text-txt-primary truncate">
{friendDisplayName}
</div>
<div className="text-[11px] text-txt-tertiary truncate">
{isInDm ? 'Already in this DM' : `@${canonical.username}`}
</div>
</div>
{!isInDm && (
<div
className={`w-[18px] h-[18px] rounded flex-shrink-0 flex items-center justify-center ${
isSelected
? 'bg-accent-mint'
: 'border-2 border-border-hard'
}`}
>
{isSelected && (
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" className="text-surface-base">
<path d="M2.5 6L5 8.5L9.5 3.5" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
</svg>
)}
</div>
)}
</button>
);
}
export function AddDmMemberModal() {
const [query, setQuery] = useState('');
@@ -197,54 +264,16 @@ export function AddDmMemberModal() {
const isInDm = currentMemberIds.has(friend.id);
const isSelected = selected.has(friend.id);
const atCapacity = !isSelected && selected.size >= remainingSlots;
const { baseName, domain } = parseFederatedUsername(friend.username);
const friendDisplayName = friend.displayName ?? baseName;
return (
<button
<AddDmFriendRow
key={friend.id}
onClick={() => toggleFriend(friend.id)}
disabled={isInDm || isAdding || atCapacity}
className={`w-full flex items-center gap-3 px-3 py-2 rounded-[4px] transition-colors text-left ${
isInDm
? 'opacity-40 cursor-not-allowed'
: isSelected
? 'bg-accent-mint/[0.08]'
: 'hover:bg-interactive-hover'
} ${atCapacity && !isInDm ? 'opacity-50 cursor-not-allowed' : ''}`}
>
<Avatar
src={friend.avatar}
name={friendDisplayName}
size={30}
status={friend.status as any}
userId={friend.homeUserId ?? friend.id}
avatarColor={friend.avatarColor}
/>
<div className="flex-1 min-w-0">
<div className="text-[13px] font-medium text-txt-primary truncate">
{friendDisplayName}
</div>
<div className="text-[11px] text-txt-tertiary truncate">
{isInDm ? 'Already in this DM' : `@${friend.username}`}
</div>
</div>
{!isInDm && (
<div
className={`w-[18px] h-[18px] rounded flex-shrink-0 flex items-center justify-center ${
isSelected
? 'bg-accent-mint'
: 'border-2 border-border-hard'
}`}
>
{isSelected && (
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" className="text-surface-base">
<path d="M2.5 6L5 8.5L9.5 3.5" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
</svg>
)}
</div>
)}
</button>
friend={friend}
isInDm={isInDm}
isSelected={isSelected}
atCapacity={atCapacity}
isAdding={isAdding}
onToggle={toggleFriend}
/>
);
})}
</div>
+114 -109
View File
@@ -7,7 +7,8 @@ import { useAuthStore } from '../../stores/authStore';
import { useSocialStore } from '../../stores/socialStore';
import { api } from '../../api/client';
import { isSelf, parseFederatedUsername } from '../../utils/identity';
import type { Friend, MemberWithUser, SpaceInviteRequest } from '@backspace/shared';
import { useCanonicalUserView } from '../../utils/userViewLookup';
import type { Friend, MemberWithUser, SpaceInviteRequest, User } from '@backspace/shared';
type SendStatus =
| { kind: 'pending' }
@@ -40,6 +41,101 @@ function reasonForError(error: unknown): string {
return "Couldn't send (server error)";
}
function InviteResultFriendRow({
friend,
status,
}: {
friend: Friend;
status: SendStatus | undefined;
}) {
const canonical = useCanonicalUserView(friend as unknown as User);
const { baseName } = parseFederatedUsername(canonical.username);
const dn = canonical.displayName ?? baseName;
return (
<div className="flex items-center gap-3 px-3 py-2 rounded-[4px]">
<Avatar
src={canonical.avatar}
name={dn}
size={30}
userId={canonical.homeUserId ?? canonical.id}
avatarColor={canonical.avatarColor}
/>
<div className="flex-1 min-w-0">
<div className="text-[13px] font-medium text-txt-primary truncate">{dn}</div>
<div className="text-[11px] text-txt-tertiary truncate">@{canonical.username}</div>
</div>
{status?.kind === 'success' && (
<span className="text-[12px] text-accent-mint flex-shrink-0"> Sent</span>
)}
{status?.kind === 'failure' && (
<span className="text-[12px] text-txt-danger flex-shrink-0"> {status.reason}</span>
)}
{status?.kind === 'pending' && (
<span className="text-[12px] text-txt-tertiary flex-shrink-0">...</span>
)}
</div>
);
}
function InviteSelectFriendRow({
friend,
isSelected,
alreadyMember,
sending,
onToggle,
}: {
friend: Friend;
isSelected: boolean;
alreadyMember: boolean;
sending: boolean;
onToggle: (id: string, friend: Friend) => void;
}) {
const canonical = useCanonicalUserView(friend as unknown as User);
const { baseName } = parseFederatedUsername(canonical.username);
const dn = canonical.displayName ?? baseName;
return (
<button
onClick={() => onToggle(friend.id, friend)}
disabled={alreadyMember || sending}
className={`w-full flex items-center gap-3 px-3 py-2 rounded-[4px] transition-colors text-left ${
alreadyMember
? 'opacity-40 cursor-not-allowed'
: isSelected
? 'bg-accent-mint/[0.08]'
: 'hover:bg-interactive-hover'
}`}
>
<Avatar
src={canonical.avatar}
name={dn}
size={30}
status={canonical.status as any}
userId={canonical.homeUserId ?? canonical.id}
avatarColor={canonical.avatarColor}
/>
<div className="flex-1 min-w-0">
<div className="text-[13px] font-medium text-txt-primary truncate">{dn}</div>
<div className="text-[11px] text-txt-tertiary truncate">
{alreadyMember ? 'Already in space' : `@${canonical.username}`}
</div>
</div>
{!alreadyMember && (
<div
className={`w-[18px] h-[18px] rounded flex-shrink-0 flex items-center justify-center ${
isSelected ? 'bg-accent-mint' : 'border-2 border-border-hard'
}`}
>
{isSelected && (
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" className="text-surface-base">
<path d="M2.5 6L5 8.5L9.5 3.5" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" />
</svg>
)}
</div>
)}
</button>
);
}
export function InviteModal() {
const activeModal = useUIStore((s) => s.activeModal);
const closeModal = useUIStore((s) => s.closeModal);
@@ -278,48 +374,13 @@ export function InviteModal() {
{/* Friend list / Results view */}
<div className="max-h-[280px] overflow-y-auto space-y-[2px]">
{inResultsView ? (
selectedFriends.map((f) => {
const status = results.get(f.id);
const { baseName } = parseFederatedUsername(f.username);
const dn = f.displayName ?? baseName;
return (
<div
key={f.id}
className="flex items-center gap-3 px-3 py-2 rounded-[4px]"
>
<Avatar
src={f.avatar}
name={dn}
size={30}
userId={f.homeUserId ?? f.id}
avatarColor={f.avatarColor}
/>
<div className="flex-1 min-w-0">
<div className="text-[13px] font-medium text-txt-primary truncate">
{dn}
</div>
<div className="text-[11px] text-txt-tertiary truncate">
@{f.username}
</div>
</div>
{status?.kind === 'success' && (
<span className="text-[12px] text-accent-mint flex-shrink-0">
Sent
</span>
)}
{status?.kind === 'failure' && (
<span className="text-[12px] text-txt-danger flex-shrink-0">
{status.reason}
</span>
)}
{status?.kind === 'pending' && (
<span className="text-[12px] text-txt-tertiary flex-shrink-0">
...
</span>
)}
</div>
);
})
selectedFriends.map((f) => (
<InviteResultFriendRow
key={f.id}
friend={f}
status={results.get(f.id)}
/>
))
) : (
<>
{filteredFriends.length === 0 && (
@@ -329,72 +390,16 @@ export function InviteModal() {
: 'No friends yet'}
</div>
)}
{filteredFriends.map((friend) => {
const alreadyMember = isFriendAlreadyMember(friend);
const isSelected = selected.has(friend.id);
const { baseName } = parseFederatedUsername(friend.username);
const dn = friend.displayName ?? baseName;
return (
<button
key={friend.id}
onClick={() => toggleFriend(friend.id, friend)}
disabled={alreadyMember || sending}
className={`w-full flex items-center gap-3 px-3 py-2 rounded-[4px] transition-colors text-left ${
alreadyMember
? 'opacity-40 cursor-not-allowed'
: isSelected
? 'bg-accent-mint/[0.08]'
: 'hover:bg-interactive-hover'
}`}
>
<Avatar
src={friend.avatar}
name={dn}
size={30}
status={friend.status as any}
userId={friend.homeUserId ?? friend.id}
avatarColor={friend.avatarColor}
/>
<div className="flex-1 min-w-0">
<div className="text-[13px] font-medium text-txt-primary truncate">
{dn}
</div>
<div className="text-[11px] text-txt-tertiary truncate">
{alreadyMember
? 'Already in space'
: `@${friend.username}`}
</div>
</div>
{!alreadyMember && (
<div
className={`w-[18px] h-[18px] rounded flex-shrink-0 flex items-center justify-center ${
isSelected
? 'bg-accent-mint'
: 'border-2 border-border-hard'
}`}
>
{isSelected && (
<svg
width="12"
height="12"
viewBox="0 0 12 12"
fill="none"
className="text-surface-base"
>
<path
d="M2.5 6L5 8.5L9.5 3.5"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
)}
</div>
)}
</button>
);
})}
{filteredFriends.map((friend) => (
<InviteSelectFriendRow
key={friend.id}
friend={friend}
isSelected={selected.has(friend.id)}
alreadyMember={isFriendAlreadyMember(friend)}
sending={sending}
onToggle={toggleFriend}
/>
))}
</>
)}
</div>
@@ -7,6 +7,33 @@ 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('');
@@ -106,25 +133,13 @@ export function NewDmModal() {
<div className="py-4 text-center text-txt-tertiary text-[14px]">No users found</div>
)}
{results.map((user) => {
const { baseName } = parseFederatedUsername(user.username);
const displayName = user.displayName ?? baseName;
return (
<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={displayName} size={36} status={user.status as any} userId={user.homeUserId ?? user.id} avatarColor={user.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">@{user.username}</div>
</div>
</button>
);
})}
{results.map((user) => (
<NewDmUserRow
key={user.id}
user={user}
onSelect={handleSelectUser}
/>
))}
</div>
</div>
</Modal>
@@ -5,8 +5,44 @@ import { useSpaceStore, getApiForOrigin, type TaggedSpace } from '../../stores/s
import { useAuthStore } from '../../stores/authStore';
import { useUIStore } from '../../stores/uiStore';
import { normalizeUserAssets } from '../../utils/assetUrls';
import { useCanonicalUserView } from '../../utils/userViewLookup';
import { Avatar } from '../ui/Avatar';
function TransferMemberRow({
member,
onSelect,
}: {
member: MemberWithUser;
onSelect: (userId: string) => void;
}) {
const canonical = useCanonicalUserView(member.user);
const displayName = canonical.displayName || canonical.username;
return (
<button
onClick={() => onSelect(member.userId)}
className="w-full flex items-center gap-2.5 px-2.5 py-1.5 rounded-md hover:bg-white/[0.06] transition-colors"
>
<Avatar
src={canonical.avatar}
name={displayName}
size={32}
user={canonical}
userId={member.userId}
/>
<div className="flex flex-col items-start min-w-0">
<span className="text-sm text-txt-primary truncate max-w-full">
{displayName}
</span>
{canonical.displayName && (
<span className="text-[11px] text-txt-tertiary truncate max-w-full">
{canonical.username}
</span>
)}
</div>
</button>
);
}
export function TransferOwnershipModal({ spaceId, onClose }: { spaceId: string; onClose: () => void }) {
const modalRef = useRef<HTMLDivElement>(null);
const space = useSpaceStore((s) => s.spaces.find(sp => sp.id === spaceId));
@@ -29,8 +65,9 @@ export function TransferOwnershipModal({ spaceId, onClose }: { spaceId: string;
const client = getApiForOrigin(origin);
const fetched = await client.spaces.members(spaceId);
if (cancelled) return;
if (origin) {
for (const m of fetched) normalizeUserAssets(m.user, origin);
for (const m of fetched) {
if (origin) normalizeUserAssets(m.user, origin);
useSpaceStore.getState().upsertUserView(m.user, origin);
}
setMembers(fetched);
} catch {
@@ -156,33 +193,13 @@ export function TransferOwnershipModal({ spaceId, onClose }: { spaceId: string;
) : filteredMembers.length === 0 ? (
<p className="text-xs text-txt-tertiary text-center py-4">No members found</p>
) : (
filteredMembers.map((member) => {
return (
<button
key={member.userId}
onClick={() => setSelectedUserId(member.userId)}
className="w-full flex items-center gap-2.5 px-2.5 py-1.5 rounded-md hover:bg-white/[0.06] transition-colors"
>
<Avatar
src={member.user.avatar}
name={member.user.displayName || member.user.username}
size={32}
user={member.user}
userId={member.userId}
/>
<div className="flex flex-col items-start min-w-0">
<span className="text-sm text-txt-primary truncate max-w-full">
{member.user.displayName || member.user.username}
</span>
{member.user.displayName && (
<span className="text-[11px] text-txt-tertiary truncate max-w-full">
{member.user.username}
</span>
)}
</div>
</button>
);
})
filteredMembers.map((member) => (
<TransferMemberRow
key={member.userId}
member={member}
onSelect={setSelectedUserId}
/>
))
)}
</div>
</>
@@ -87,6 +87,7 @@ export function UserProfileModal() {
const targetApi = getApiForOrigin(origin);
const u = await targetApi.users.get(id);
setUser(u);
useSpaceStore.getState().upsertUserView(u, origin);
} catch {
// User not found
}
@@ -3,9 +3,168 @@ import { Avatar } from '../../ui/Avatar';
import { ConfirmDialog } from '../../ui/ConfirmDialog';
import { useSpaceStore, getApiForOrigin } from '../../../stores/spaceStore';
import { useAuthStore } from '../../../stores/authStore';
import { parseFederatedUsername } from '../../../utils/identity';
import { parseFederatedUsername, isFederationGlobeApplicable } from '../../../utils/identity';
import { useCanonicalUserView } from '../../../utils/userViewLookup';
import { hasPermissionBit, PermissionBits } from '../../../utils/permissions';
import type { MemberWithUser } from '@backspace/shared';
import type { MemberWithUser, Role } from '@backspace/shared';
function MembersPanelRow({
member,
spaceId,
ownerId,
isExpanded,
expandable,
canKick,
canBan,
currentUserId,
assignableRoles,
memberRoleIds,
hasPendingChanges,
onToggleExpand,
onRoleToggle,
onSaveRoles,
onCancelRoleChange,
onPendingAction,
}: {
member: MemberWithUser;
spaceId: string;
ownerId: string | undefined;
isExpanded: boolean;
expandable: boolean;
canKick: boolean;
canBan: boolean;
currentUserId: string | undefined;
assignableRoles: Role[];
memberRoleIds: Set<string>;
hasPendingChanges: boolean;
onToggleExpand: (userId: string) => void;
onRoleToggle: (userId: string, roleId: string, currentRoleIds: Set<string>) => void;
onSaveRoles: (userId: string) => void;
onCancelRoleChange: (userId: string) => void;
onPendingAction: (action: { type: 'kick' | 'ban'; userId: string; displayName: string }) => void;
}) {
const canonical = useCanonicalUserView(member.user);
const isOwner = member.userId === ownerId;
const displayName = canonical.displayName ?? canonical.username;
return (
<div>
<div
className={`flex items-center justify-between p-2 rounded transition-colors ${
expandable ? 'cursor-pointer hover:bg-interactive-hover' : ''
} ${isExpanded ? 'bg-interactive-hover' : ''}`}
onClick={() => {
if (expandable) onToggleExpand(member.userId);
}}
>
<div className="flex items-center gap-2 min-w-0">
<Avatar
src={canonical.avatar}
name={displayName}
size={32}
status={canonical.status}
user={canonical}
/>
<div className="min-w-0">
<div className="text-sm font-medium truncate">
{displayName}
{isFederationGlobeApplicable(canonical) && (
<span className="ml-1 text-[10px] text-txt-tertiary opacity-60">@{parseFederatedUsername(canonical.username).domain}</span>
)}
</div>
<div className="flex items-center gap-1 flex-wrap">
{isOwner && (
<span className="text-[10px] px-1.5 py-0.5 rounded bg-accent-rose/20 text-txt-danger font-medium">
Owner
</span>
)}
{member.roles?.filter((r) => r.id !== spaceId).map((r) => (
<span
key={r.id}
className="text-[10px] px-1.5 py-0.5 rounded font-medium"
style={{ backgroundColor: `${r.color}20`, color: r.color }}
>
{r.name}
</span>
))}
{!isOwner && (!member.roles || member.roles.filter((r) => r.id !== spaceId).length === 0) && (
<span className="text-[10px] text-txt-tertiary">No roles</span>
)}
</div>
</div>
</div>
<div className="flex items-center gap-1 flex-shrink-0">
{canBan && member.userId !== currentUserId && !isOwner && (
<button
onClick={(e) => { e.stopPropagation(); onPendingAction({ type: 'ban', userId: member.userId, displayName }); }}
className="px-2 py-1 text-xs text-txt-danger hover:bg-accent-rose/10 rounded transition-colors"
>
Ban
</button>
)}
{canKick && member.userId !== currentUserId && !isOwner && (
<button
onClick={(e) => { e.stopPropagation(); onPendingAction({ type: 'kick', userId: member.userId, displayName }); }}
className="px-2 py-1 text-xs text-txt-danger hover:bg-accent-rose/10 rounded transition-colors"
>
Kick
</button>
)}
{expandable && (
<svg
className={`w-4 h-4 text-txt-tertiary transition-transform ${isExpanded ? 'rotate-90' : ''}`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path strokeLinecap="round" strokeLinejoin="round" d="M9 5l7 7-7 7" />
</svg>
)}
</div>
</div>
{/* Role checkboxes — only shown when expanded */}
{isExpanded && expandable && (
<div className="mt-1 mb-1 ml-10 space-y-1">
{assignableRoles.map((role) => (
<label key={role.id} className="flex items-center gap-2 cursor-pointer group/role">
<input
type="checkbox"
checked={memberRoleIds.has(role.id)}
onChange={() => onRoleToggle(member.userId, role.id, memberRoleIds)}
className="w-3.5 h-3.5 rounded border-txt-tertiary accent-accent-primary"
/>
<span
className="text-xs font-medium"
style={{ color: role.color !== '#9ca3af' ? role.color : undefined }}
>
{role.name}
</span>
</label>
))}
{hasPendingChanges && (
<div className="flex items-center gap-2 mt-1.5">
<button
onClick={() => onSaveRoles(member.userId)}
className="px-2 py-0.5 text-xs bg-accent-primary hover:bg-accent-primary/80 text-white rounded transition-colors"
>
Save
</button>
<button
onClick={() => onCancelRoleChange(member.userId)}
className="px-2 py-0.5 text-xs text-txt-tertiary hover:text-txt-secondary transition-colors"
>
Cancel
</button>
</div>
)}
</div>
)}
</div>
);
}
interface MembersPanelProps {
spaceId: string;
@@ -112,135 +271,27 @@ export function MembersPanel({ spaceId }: MembersPanelProps) {
</div>
<div className="rounded-lg bg-white/[0.02] p-2">
<div className="space-y-0.5">
{members.map((member) => {
const { domain } = parseFederatedUsername(member.user.username);
const displayName = member.user.displayName ?? member.user.username;
const isOwner = member.userId === space.ownerId;
const memberRoleIds = getMemberRoleIds(member);
const hasPendingChanges = pendingRoleChanges.has(member.userId);
const isExpanded = expandedMemberId === member.userId;
const expandable = canExpandMember(member);
return (
<div key={member.userId}>
<div
className={`flex items-center justify-between p-2 rounded transition-colors ${
expandable ? 'cursor-pointer hover:bg-interactive-hover' : ''
} ${isExpanded ? 'bg-interactive-hover' : ''}`}
onClick={() => {
if (expandable) {
setExpandedMemberId(isExpanded ? null : member.userId);
}
}}
>
<div className="flex items-center gap-2 min-w-0">
<Avatar
src={member.user.avatar}
name={displayName}
size={32}
status={member.user.status}
user={member.user}
/>
<div className="min-w-0">
<div className="text-sm font-medium truncate">
{displayName}
{domain && (
<span className="ml-1 text-[10px] text-txt-tertiary opacity-60">@{domain}</span>
)}
</div>
<div className="flex items-center gap-1 flex-wrap">
{isOwner && (
<span className="text-[10px] px-1.5 py-0.5 rounded bg-accent-rose/20 text-txt-danger font-medium">
Owner
</span>
)}
{member.roles?.filter((r) => r.id !== spaceId).map((r) => (
<span
key={r.id}
className="text-[10px] px-1.5 py-0.5 rounded font-medium"
style={{ backgroundColor: `${r.color}20`, color: r.color }}
>
{r.name}
</span>
))}
{!isOwner && (!member.roles || member.roles.filter((r) => r.id !== spaceId).length === 0) && (
<span className="text-[10px] text-txt-tertiary">No roles</span>
)}
</div>
</div>
</div>
<div className="flex items-center gap-1 flex-shrink-0">
{canBan && member.userId !== currentUser?.id && !isOwner && (
<button
onClick={(e) => { e.stopPropagation(); setPendingAction({ type: 'ban', userId: member.userId, displayName }); }}
className="px-2 py-1 text-xs text-txt-danger hover:bg-accent-rose/10 rounded transition-colors"
>
Ban
</button>
)}
{canKick && member.userId !== currentUser?.id && !isOwner && (
<button
onClick={(e) => { e.stopPropagation(); setPendingAction({ type: 'kick', userId: member.userId, displayName }); }}
className="px-2 py-1 text-xs text-txt-danger hover:bg-accent-rose/10 rounded transition-colors"
>
Kick
</button>
)}
{expandable && (
<svg
className={`w-4 h-4 text-txt-tertiary transition-transform ${isExpanded ? 'rotate-90' : ''}`}
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path strokeLinecap="round" strokeLinejoin="round" d="M9 5l7 7-7 7" />
</svg>
)}
</div>
</div>
{/* Role checkboxes — only shown when expanded */}
{isExpanded && expandable && (
<div className="mt-1 mb-1 ml-10 space-y-1">
{assignableRoles.map((role) => (
<label key={role.id} className="flex items-center gap-2 cursor-pointer group/role">
<input
type="checkbox"
checked={memberRoleIds.has(role.id)}
onChange={() => handleRoleToggle(member.userId, role.id, memberRoleIds)}
className="w-3.5 h-3.5 rounded border-txt-tertiary accent-accent-primary"
/>
<span
className="text-xs font-medium"
style={{ color: role.color !== '#9ca3af' ? role.color : undefined }}
>
{role.name}
</span>
</label>
))}
{hasPendingChanges && (
<div className="flex items-center gap-2 mt-1.5">
<button
onClick={() => handleSaveRoles(member.userId)}
className="px-2 py-0.5 text-xs bg-accent-primary hover:bg-accent-primary/80 text-white rounded transition-colors"
>
Save
</button>
<button
onClick={() => handleCancelRoleChange(member.userId)}
className="px-2 py-0.5 text-xs text-txt-tertiary hover:text-txt-secondary transition-colors"
>
Cancel
</button>
</div>
)}
</div>
)}
</div>
);
})}
{members.map((member) => (
<MembersPanelRow
key={member.userId}
member={member}
spaceId={spaceId}
ownerId={space.ownerId}
isExpanded={expandedMemberId === member.userId}
expandable={canExpandMember(member)}
canKick={canKick}
canBan={canBan}
currentUserId={currentUser?.id}
assignableRoles={assignableRoles}
memberRoleIds={getMemberRoleIds(member)}
hasPendingChanges={pendingRoleChanges.has(member.userId)}
onToggleExpand={(uid) => setExpandedMemberId(expandedMemberId === uid ? null : uid)}
onRoleToggle={handleRoleToggle}
onSaveRoles={handleSaveRoles}
onCancelRoleChange={handleCancelRoleChange}
onPendingAction={setPendingAction}
/>
))}
</div>
</div>
</div>