|
1 | 1 | // Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
3 | 3 | /** |
4 | | - * objectstack#36999 — the composer's context row while the agent honours a |
5 | | - * conversation focus (`capabilities.focus` on its catalog row). |
| 4 | + * objectstack#36999 / objectstack#37861 — the composer's context chip while |
| 5 | + * the agent honours a conversation focus (`capabilities.focus` on its catalog |
| 6 | + * row), drawn inside the input's frame at the start of the line. |
6 | 7 | * |
7 | | - * It takes the place of the page chip the composer draws today |
8 | | - * (`console.ai.discussing`), in the pane's in-flow composer row: the page chip |
9 | | - * with a pin on it when the page shows one record, or — once a record is |
10 | | - * pinned — the focus chip, naming the record by its display name, with a |
11 | | - * remove control. Opening another record while one is pinned asks once |
12 | | - * 「改为问《X》?」; a decline is remembered for the conversation. |
| 8 | + * Framework ADR-0160 D6 (seventh amendment): with a focus, the chip names the |
| 9 | + * record (the object's icon and the record's name) with × to remove it; × |
| 10 | + * shows while the composer has focus or the pointer is over the chip, and |
| 11 | + * always on a touch screen. Without a focus — a list or a dashboard, a |
| 12 | + * conversation sent without one, or after × — it is the page chip |
| 13 | + * 「正在看:X」 (`console.ai.discussing`), with no ×. There is no pin and no |
| 14 | + * offer to switch records: an unsent conversation follows the page on its |
| 15 | + * own, and a sent one keeps the record it was sent with. |
13 | 16 | * |
14 | 17 | * The record's name is read client-side, as the user: the object definition |
15 | | - * and one `findOne`, through the same seams the record page reads with. A |
| 18 | + * and one `findOne`, through the same seams the record page reads with. Until |
| 19 | + * that answers, the label the conversation's metadata stored stands in; a |
16 | 20 | * record the user cannot read keeps its `object · id` pair. |
17 | 21 | */ |
18 | 22 |
|
19 | 23 | import * as React from 'react'; |
20 | | -import { Pin, X } from 'lucide-react'; |
| 24 | +import { X } from 'lucide-react'; |
| 25 | +import { ObjectIconTile } from '@object-ui/components'; |
21 | 26 | import { useObjectTranslation } from '@object-ui/i18n'; |
22 | 27 | import { useMetadataItem } from '@object-ui/react'; |
23 | 28 | import { useAdapter } from '../../providers/AdapterProvider.js'; |
24 | 29 | import { getRecordDisplayName } from '../../utils/index.js'; |
25 | | -import type { ChatSurfaceContext } from '../../layout/chatSurfaceContext.js'; |
26 | | -import { sameChatFocus, type ChatFocus, type ConversationFocus } from '../../layout/chatFocus.js'; |
| 30 | +import type { ChatFocus, ConversationFocus } from '../../layout/chatFocus.js'; |
27 | 31 |
|
28 | | -/** The record a surface shows on its own (a record page, a record open in a list's drawer), or null. */ |
29 | | -export function surfaceRecord(surface: ChatSurfaceContext | undefined): ChatFocus | null { |
30 | | - const object = surface?.artifact?.type === 'object' ? surface.artifact.name : undefined; |
31 | | - const id = surface?.selection?.kind === 'record' ? surface.selection.id : undefined; |
32 | | - return object && id ? { object, id } : null; |
| 32 | +/** What the chip shows of a record: its display name and its object's icon and colour. */ |
| 33 | +export interface ChatRecordFace { |
| 34 | + /** The display name, undefined while it loads and when the record or its definition cannot be read. */ |
| 35 | + title?: string; |
| 36 | + icon?: string; |
| 37 | + color?: string; |
33 | 38 | } |
34 | 39 |
|
35 | | -/** |
36 | | - * The display name of `record`, read as the user: undefined while it loads, |
37 | | - * and when the record or its definition cannot be read. |
38 | | - */ |
39 | | -export function useChatRecordTitle(record: ChatFocus | null): string | undefined { |
| 40 | +/** The face of `record`, read as the user. */ |
| 41 | +export function useChatRecordFace(record: ChatFocus | null): ChatRecordFace { |
40 | 42 | const dataSource = useAdapter() as { findOne?: (object: string, id: string) => unknown } | null; |
41 | 43 | const { item: objectDef } = useMetadataItem('object', record?.object ?? null); |
42 | 44 | const object = record?.object; |
@@ -67,103 +69,75 @@ export function useChatRecordTitle(record: ChatFocus | null): string | undefined |
67 | 69 | }; |
68 | 70 | }, [object, id, canRead, dataSource]); |
69 | 71 |
|
70 | | - if (!record || !answer || answer.key !== key || !answer.row || !objectDef) return undefined; |
| 72 | + const def = record ? (objectDef as { icon?: unknown; color?: unknown } | undefined) : undefined; |
| 73 | + const icon = typeof def?.icon === 'string' ? def.icon : undefined; |
| 74 | + const color = typeof def?.color === 'string' ? def.color : undefined; |
| 75 | + if (!record || !answer || answer.key !== key || !answer.row || !objectDef) return { icon, color }; |
71 | 76 | const title = getRecordDisplayName(objectDef, answer.row, { fallback: '' }); |
72 | | - return title || undefined; |
| 77 | + return { title: title || undefined, icon, color }; |
73 | 78 | } |
74 | 79 |
|
75 | 80 | const CHIP = |
76 | | - 'inline-flex min-w-0 max-w-full items-center gap-1 rounded-full border bg-muted/40 px-2 py-0.5 text-[11px] text-muted-foreground'; |
77 | | -const CHIP_BUTTON = |
78 | | - 'inline-flex shrink-0 items-center justify-center rounded-full p-0.5 hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring'; |
| 81 | + 'group/focus-chip inline-flex min-w-0 max-w-full items-center gap-1 rounded-full border bg-muted/40 py-0.5 text-[11px] text-muted-foreground'; |
| 82 | +/** |
| 83 | + * × — a 24px hit area around a 12px glyph (WCAG 2.5.8). Revealed while the |
| 84 | + * pointer is over the chip, while anything in the composer has focus (the |
| 85 | + * frame is the `group/input-group`), on its own keyboard focus, and always on |
| 86 | + * a coarse pointer, where there is no hover. |
| 87 | + */ |
| 88 | +const REMOVE = |
| 89 | + '-my-1 -me-1 inline-flex size-6 shrink-0 items-center justify-center rounded-full opacity-0 transition-opacity hover:bg-muted hover:text-foreground focus-visible:opacity-100 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring group-hover/focus-chip:opacity-100 group-focus-within/input-group:opacity-100 pointer-coarse:opacity-100'; |
79 | 90 |
|
80 | 91 | export interface ChatFocusRowProps { |
81 | 92 | /** The host's focus state for the conversation on screen. */ |
82 | 93 | focus: ConversationFocus; |
83 | | - /** The page in view (`context.surface`). */ |
84 | | - surface?: ChatSurfaceContext; |
85 | 94 | /** The page chip's text, as the composer draws it today; absent → no page chip. */ |
86 | 95 | surfaceLabel?: string; |
87 | 96 | /** The page chip's tooltip (the internal `type · name` pair). */ |
88 | 97 | surfaceTitle?: string; |
89 | 98 | } |
90 | 99 |
|
91 | | -export function ChatFocusRow({ focus, surface, surfaceLabel, surfaceTitle }: ChatFocusRowProps) { |
| 100 | +export function ChatFocusRow({ focus, surfaceLabel, surfaceTitle }: ChatFocusRowProps) { |
92 | 101 | const { t } = useObjectTranslation(); |
93 | | - const pinned = focus.focus; |
94 | | - const pageRecord = surfaceRecord(surface); |
95 | | - const offerSwitch = Boolean(pinned && pageRecord && !sameChatFocus(pinned, pageRecord) && !focus.declined); |
96 | | - const pinnedTitle = useChatRecordTitle(pinned); |
97 | | - const pageTitle = useChatRecordTitle(offerSwitch ? pageRecord : null); |
| 102 | + const record = focus.focus; |
| 103 | + const face = useChatRecordFace(record); |
98 | 104 |
|
99 | | - if (pinned) { |
100 | | - const pair = `${pinned.object} · ${pinned.id}`; |
| 105 | + if (record) { |
| 106 | + const pair = `${record.object} · ${record.id}`; |
| 107 | + const name = face.title || focus.label || pair; |
| 108 | + const described = t('console.ai.focus.pinned', { defaultValue: 'Asking about: {{target}}', target: name }); |
101 | 109 | return ( |
102 | | - <div className="flex min-w-0 flex-1 flex-col items-start gap-1 px-3" data-testid="chat-focus-row"> |
103 | | - <span className={CHIP} title={pair} data-testid="chat-focus-chip"> |
104 | | - <Pin className="size-3 shrink-0" aria-hidden="true" /> |
105 | | - <span className="truncate"> |
106 | | - {t('console.ai.focus.pinned', { defaultValue: 'Asking about: {{target}}', target: pinnedTitle || pair })} |
107 | | - </span> |
108 | | - <button |
109 | | - type="button" |
110 | | - className={CHIP_BUTTON} |
111 | | - onClick={() => focus.setFocus(null)} |
112 | | - aria-label={t('console.ai.focus.remove', { defaultValue: 'Stop asking about this record' })} |
113 | | - title={t('console.ai.focus.remove', { defaultValue: 'Stop asking about this record' })} |
114 | | - data-testid="chat-focus-remove" |
115 | | - > |
116 | | - <X className="size-3" aria-hidden="true" /> |
117 | | - </button> |
| 110 | + <span |
| 111 | + className={`${CHIP} ps-1 pe-2`} |
| 112 | + role="group" |
| 113 | + aria-label={described} |
| 114 | + title={`${described} (${pair})`} |
| 115 | + data-testid="chat-focus-chip" |
| 116 | + data-focus-object={record.object} |
| 117 | + data-focus-id={record.id} |
| 118 | + > |
| 119 | + <ObjectIconTile icon={face.icon} color={face.color} size="xs" className="shrink-0" /> |
| 120 | + <span className="truncate text-foreground/90" data-testid="chat-focus-name"> |
| 121 | + {name} |
118 | 122 | </span> |
119 | | - {offerSwitch && pageRecord ? ( |
120 | | - <div className="flex min-w-0 max-w-full flex-wrap items-center gap-1.5 text-[11px]" role="group" data-testid="chat-focus-switch"> |
121 | | - <span className="min-w-0 truncate text-muted-foreground"> |
122 | | - {t('console.ai.focus.switchPrompt', { |
123 | | - defaultValue: 'Ask about "{{target}}" instead?', |
124 | | - target: pageTitle || `${pageRecord.object} · ${pageRecord.id}`, |
125 | | - })} |
126 | | - </span> |
127 | | - <button |
128 | | - type="button" |
129 | | - className="rounded px-1.5 py-0.5 font-medium text-foreground hover:bg-muted" |
130 | | - onClick={() => focus.setFocus(pageRecord)} |
131 | | - data-testid="chat-focus-switch-accept" |
132 | | - > |
133 | | - {t('console.ai.focus.switchAccept', { defaultValue: 'Switch' })} |
134 | | - </button> |
135 | | - <button |
136 | | - type="button" |
137 | | - className="rounded px-1.5 py-0.5 text-muted-foreground hover:bg-muted hover:text-foreground" |
138 | | - onClick={focus.decline} |
139 | | - data-testid="chat-focus-switch-decline" |
140 | | - > |
141 | | - {t('console.ai.focus.switchDecline', { defaultValue: 'Keep' })} |
142 | | - </button> |
143 | | - </div> |
144 | | - ) : null} |
145 | | - </div> |
| 123 | + <button |
| 124 | + type="button" |
| 125 | + className={REMOVE} |
| 126 | + onClick={focus.dismiss} |
| 127 | + aria-label={t('console.ai.focus.remove', { defaultValue: 'Stop asking about this record' })} |
| 128 | + title={t('console.ai.focus.remove', { defaultValue: 'Stop asking about this record' })} |
| 129 | + data-testid="chat-focus-remove" |
| 130 | + > |
| 131 | + <X className="size-3" aria-hidden="true" /> |
| 132 | + </button> |
| 133 | + </span> |
146 | 134 | ); |
147 | 135 | } |
148 | 136 |
|
149 | 137 | if (!surfaceLabel) return null; |
150 | 138 | return ( |
151 | | - <div className="flex min-w-0 flex-1 px-3" data-testid="chat-focus-row"> |
152 | | - <span className={CHIP} title={surfaceTitle} data-testid="surface-context-chip"> |
153 | | - <span className="truncate">{surfaceLabel}</span> |
154 | | - {pageRecord ? ( |
155 | | - <button |
156 | | - type="button" |
157 | | - className={CHIP_BUTTON} |
158 | | - onClick={() => focus.setFocus(pageRecord)} |
159 | | - aria-label={t('console.ai.focus.pin', { defaultValue: 'Keep asking about this record' })} |
160 | | - title={t('console.ai.focus.pin', { defaultValue: 'Keep asking about this record' })} |
161 | | - data-testid="chat-focus-pin" |
162 | | - > |
163 | | - <Pin className="size-3" aria-hidden="true" /> |
164 | | - </button> |
165 | | - ) : null} |
166 | | - </span> |
167 | | - </div> |
| 139 | + <span className={`${CHIP} px-2`} title={surfaceTitle} data-testid="surface-context-chip"> |
| 140 | + <span className="truncate">{surfaceLabel}</span> |
| 141 | + </span> |
168 | 142 | ); |
169 | 143 | } |
0 commit comments