Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(presence): pure facepile logic (label, dimming, click target)

enes3448c9fd0120
diff --git a/src/renderer/lib/facepile.test.ts b/src/renderer/lib/facepile.test.ts
new file mode 100644
index 000000000..8e3d95000
--- /dev/null
+++ b/src/renderer/lib/facepile.test.ts
@@ -0,0 +1,113 @@
+import { describe, it, expect } from 'vitest'
+import { PRESENCE_COLORS } from '@shared/presence'
+import { faceClickTarget, facepileEntries, initials, type FacepileEntry } from './facepile'
+import type { PeerFace } from '../state/presence'
+
+function face(clientId: number, over: Partial<PeerFace> = {}): PeerFace {
+ return {
+ clientId,
+ name: `P${clientId}`,
+ color: PRESENCE_COLORS[0],
+ projectId: 'web',
+ kind: 'browser',
+ ...over
+ }
+}
+
+const PROJECTS = [
+ { id: 'web', name: 'web' },
+ { id: 'api', name: 'api' }
+]
+
+const only = (faces: PeerFace[], active: string | null): FacepileEntry =>
+ facepileEntries(faces, PROJECTS, active)[0]
+
+describe('initials', () => {
+ it('takes the first letter of the first two words', () => {
+ expect(initials('Enes Kirca')).toBe('EK')
+ expect(initials(' ada lovelace byron ')).toBe('AL')
+ })
+
+ it('takes two letters from a single word, and falls back for an empty name', () => {
+ expect(initials('Phone')).toBe('PH')
+ expect(initials('x')).toBe('X')
+ expect(initials(' ')).toBe('?')
+ })
+})
+
+describe('facepileEntries', () => {
+ it('shows a peer on this canvas undimmed, labelled by name alone', () => {
+ const e = only([face(8, { name: 'Ada' })], 'web')
+ expect(e.away).toBe(false)
+ expect(e.label).toBe('Ada')
+ expect(e.initials).toBe('AD')
+ expect(e.projectName).toBe('web')
+ expect(e.actionable).toBe(true)
+ })
+
+ it('shows an off-project peer dimmed and labelled with their project ("Ada · api")', () => {
+ const e = only([face(8, { name: 'Ada', projectId: 'api' })], 'web')
+ expect(e.away).toBe(true)
+ expect(e.projectName).toBe('api')
+ expect(e.label).toBe('Ada · api')
+ expect(e.actionable).toBe(true) // clicking takes you there
+ })
+
+ it('degrades gracefully for a project we do not have: no broken label, not clickable', () => {
+ const e = only([face(8, { name: 'Ada', projectId: 'ghost' })], 'web')
+ expect(e.away).toBe(true)
+ expect(e.projectName).toBeNull()
+ expect(e.label).toBe('Ada') // never "Ada · undefined"
+ expect(e.actionable).toBe(false)
+ expect(e.title).not.toContain('·')
+ })
+
+ it('degrades gracefully for a peer with no project open (welcome screen)', () => {
+ const e = only([face(8, { name: 'Ada', projectId: null })], 'web')
+ expect(e.away).toBe(true)
+ expect(e.projectName).toBeNull()
+ expect(e.label).toBe('Ada')
+ expect(e.actionable).toBe(false)
+ })
+
+ it('treats every peer as away when no project is open here', () => {
+ const entries = facepileEntries([face(8), face(9, { projectId: 'api' })], PROJECTS, null)
+ expect(entries.map((e) => e.away)).toEqual([true, true])
+ // ...but they are still reachable: that is how you get off the welcome screen to a teammate.
+ expect(entries.map((e) => e.actionable)).toEqual([true, true])
+ })
+
+ it('marks a cursorless phone so the facepile can give it a phone affordance', () => {
+ const phone = only([face(8, { name: 'Phone', kind: 'phone' })], 'web')
+ expect(phone.isPhone).toBe(true)
+ expect(only([face(8)], 'web').isPhone).toBe(false)
+ })
+
+ it('carries the peer color through untouched (palette-sanitized on the wire)', () => {
+ expect(only([face(8, { color: PRESENCE_COLORS[3] })], 'web').color).toBe(PRESENCE_COLORS[3])
+ })
+
+ it('keeps the peer order it is given and preserves clientIds', () => {
+ const entries = facepileEntries([face(9), face(8)], PROJECTS, 'web')
+ expect(entries.map((e) => e.clientId)).toEqual([9, 8])
+ })
+})
+
+describe('faceClickTarget', () => {
+ const entry = (over: Partial<FacepileEntry> = {}): FacepileEntry =>
+ ({ projectId: 'api', actionable: true, ...over }) as FacepileEntry
+
+ it('jumps to the peer’s focused node when they have one (focusNodeById switches projects)', () => {
+ expect(faceClickTarget(entry(), 'term-1')).toEqual({ kind: 'node', nodeId: 'term-1' })
+ })
+
+ it('falls back to their project when they are focused on nothing', () => {
+ expect(faceClickTarget(entry(), null)).toEqual({ kind: 'project', projectId: 'api' })
+ })
+
+ it('goes nowhere for a peer we cannot follow (unknown / no project)', () => {
+ // Even a focused node: its project is not in our workspace, so we could not open it.
+ expect(faceClickTarget(entry({ actionable: false }), 'term-1')).toBeNull()
+ expect(faceClickTarget(entry({ actionable: false, projectId: null }), null)).toBeNull()
+ })
+})
diff --git a/src/renderer/lib/facepile.ts b/src/renderer/lib/facepile.ts
new file mode 100644
index 000000000..02d1e231b
--- /dev/null
+++ b/src/renderer/lib/facepile.ts
@@ -0,0 +1,111 @@
+// Pure logic behind the presence facepile (components/Facepile.tsx): what each avatar is labelled
+// with, whether it is dimmed, and where clicking it takes you. Kept out of the component because
+// vitest runs in the node environment (no jsdom), so a React component cannot be unit-tested —
+// this can.
+//
+// THE FACEPILE IS THE ONE PRESENCE SURFACE THAT DOES NOT PROJECT-FILTER. Cursors (PresenceLayer)
+// and node chips are hard-filtered to the active project — a peer's flow coordinates and node ids
+// mean nothing on another canvas. The facepile instead shows EVERYONE, dims the peers who are on
+// another canvas, labels them with the project they are in ("Ada · api"), and makes them clickable:
+// "why can't I see them?" becomes "who is working where".
+
+import type { PeerFace } from '../state/presence'
+
+/** The minimum a project has to be for the facepile: an id and a display name (structural, so the
+ * helper stays testable without the whole Project type). */
+export interface FacepileProject {
+ id: string
+ name: string
+}
+
+export interface FacepileEntry {
+ clientId: number
+ name: string
+ color: string
+ /** Two letters for the avatar circle. */
+ initials: string
+ /** A cursorless peer (a phone): the facepile is its only surface, so it gets a phone glyph. */
+ isPhone: boolean
+ /** On another canvas (or on none) — rendered dimmed. */
+ away: boolean
+ projectId: string | null
+ /** The name of the project they are in, or null when they are in none / one we do not have. */
+ projectName: string | null
+ /** "Ada · api" for a known off-project peer, plain "Ada" otherwise (NEVER "Ada · undefined"). */
+ label: string
+ title: string
+ /** True when we can actually follow them (we have their project). Clicking a peer whose project
+ * is not in our workspace could only fail, so their avatar is inert. */
+ actionable: boolean
+}
+
+/** Where a click on a face goes: to the node the peer is working in, else to their canvas. */
+export type FaceTarget =
+ | { kind: 'node'; nodeId: string }
+ | { kind: 'project'; projectId: string }
+ | null
+
+/** Two-letter initials for the avatar circle ("Enes Kirca" → "EK", "Phone" → "PH"). */
+export function initials(name: string): string {
+ const parts = name.trim().split(/\s+/).filter(Boolean)
+ if (parts.length === 0) return '?'
+ if (parts.length === 1) return parts[0].slice(0, 2).toUpperCase()
+ return (parts[0][0] + parts[1][0]).toUpperCase()
+}
+
+/**
+ * Project the cursor-immune faces (selectFaces) into what the facepile draws. `activeProjectId`
+ * is the canvas WE are on — null/'' on the welcome screen, where everyone is "away" but still
+ * reachable (clicking a teammate is a legitimate way off the welcome screen).
+ */
+export function facepileEntries(
+ faces: readonly PeerFace[],
+ projects: readonly FacepileProject[],
+ activeProjectId: string | null
+): FacepileEntry[] {
+ const active = activeProjectId || null
+ const names = new Map(projects.map((p) => [p.id, p.name]))
+ return faces.map((f) => {
+ const projectName = f.projectId ? (names.get(f.projectId) ?? null) : null
+ const away = f.projectId !== active
+ // Following a peer means opening their canvas — only possible for a project we have.
+ const actionable = projectName !== null
+ const label = away && projectName ? `${f.name} · ${projectName}` : f.name
+ const isPhone = f.kind === 'phone'
+ const where = away
+ ? actionable
+ ? `${label} (click to go there)`
+ : f.projectId
+ ? `${f.name} — on a canvas you do not have`
+ : `${f.name} — no project open`
+ : f.name
+ return {
+ clientId: f.clientId,
+ name: f.name,
+ color: f.color,
+ initials: initials(f.name),
+ isPhone,
+ away,
+ projectId: f.projectId,
+ projectName,
+ label,
+ title: isPhone ? `${where} · phone` : where,
+ actionable
+ }
+ })
+}
+
+/**
+ * Where clicking this face takes you. `focus` is read from the live store AT CLICK TIME (it is
+ * deliberately not part of PeerFace — the facepile must not re-render when a peer changes node),
+ * so this stays pure and the component stays cursor-immune.
+ *
+ * A focused node wins: Canvas.focusNodeById already switches projects for a node that lives in
+ * another one, so one call both travels and centers. Otherwise we can only land on their canvas.
+ */
+export function faceClickTarget(entry: FacepileEntry, focus: string | null): FaceTarget {
+ if (!entry.actionable) return null
+ if (focus) return { kind: 'node', nodeId: focus }
+ if (entry.projectId) return { kind: 'project', projectId: entry.projectId }
+ return null
+}