diff --git a/apps/desktop/src/app/chat/composer/chip-typography.test.ts b/apps/desktop/src/app/chat/composer/chip-typography.test.ts new file mode 100644 index 000000000000..bdd6159f8e56 --- /dev/null +++ b/apps/desktop/src/app/chat/composer/chip-typography.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, it } from 'vitest' + +import { + DIRECTIVE_CHIP_CLASS, + directiveChipClass, + SLASH_CHIP_BASE_CLASS +} from '@/components/assistant-ui/directive-text' +import { REFERENCE_STYLES, referenceStyle } from '@/components/assistant-ui/reference-kinds' + +/** + * A chip is inline text, not a badge: same size as the words around it, no + * background or padding to step over, and a per-kind color + icon carrying the + * "what kind of thing is this" signal. + */ +describe('chip typography', () => { + for (const [name, cls] of [ + ['directive chip', DIRECTIVE_CHIP_CLASS], + ['slash chip', SLASH_CHIP_BASE_CLASS] + ] as const) { + it(`${name} inherits the surrounding font size`, () => { + expect(cls).not.toMatch(/\btext-\[0\.\d+em\]/) + }) + + it(`${name} renders as text, with no badge chrome`, () => { + for (const chrome of ['bg-', 'rounded', 'px-', 'py-', 'border']) { + expect(cls).not.toContain(chrome) + } + }) + + it(`${name} sits on the text baseline without a nudge`, () => { + // With no vertical padding there's nothing to cancel, so the pill needs + // no magic em offset to stop riding low. + expect(cls).toContain('align-baseline') + expect(cls).not.toMatch(/align-\[-/) + }) + } + + it('directive and slash chips are literally the same shape', () => { + expect(SLASH_CHIP_BASE_CLASS).toBe(DIRECTIVE_CHIP_CLASS) + }) + + it('resolves to the same font size as its container', () => { + const host = document.createElement('div') + + host.style.fontSize = '16px' + host.innerHTML = `apps/desktop/` + document.body.append(host) + + const chip = host.querySelector('#chip') as HTMLElement + + expect(getComputedStyle(chip).fontSize).toBe(getComputedStyle(host).fontSize) + + host.remove() + }) +}) + +describe('the shared reference vocabulary', () => { + it('gives every kind an icon, a color, and a label', () => { + for (const [kind, style] of Object.entries(REFERENCE_STYLES)) { + expect(style.codicon, `${kind} codicon`).toBeTruthy() + expect(style.color, `${kind} color`).toBeTruthy() + expect(style.label, `${kind} label`).toBeTruthy() + + // Emoji rows render the emoji itself instead of a glyph. + if (kind !== 'emoji') { + expect(style.paths.length, `${kind} paths`).toBeGreaterThan(0) + } + } + }) + + it('carries the kind colour into the chip class', () => { + for (const kind of ['file', 'url', 'skill', 'command'] as const) { + expect(directiveChipClass(kind)).toContain(referenceStyle(kind).color) + } + }) + + it('falls back to a real style for an unknown kind', () => { + const style = referenceStyle('something-new') + + expect(style).toBe(REFERENCE_STYLES.other) + expect(style.codicon).toBeTruthy() + }) + + it('gives commands and skills distinct accents so a list reads at a glance', () => { + expect(referenceStyle('skill').color).not.toBe(referenceStyle('command').color) + }) +}) diff --git a/apps/desktop/src/app/chat/composer/rich-editor.ts b/apps/desktop/src/app/chat/composer/rich-editor.ts index 60da3fb97211..ba691b12db3c 100644 --- a/apps/desktop/src/app/chat/composer/rich-editor.ts +++ b/apps/desktop/src/app/chat/composer/rich-editor.ts @@ -7,7 +7,7 @@ * plain-text round-trip. */ import { - DIRECTIVE_CHIP_CLASS, + directiveChipClass, directiveIconElement, directiveIconSvg, formatRefValue, @@ -60,7 +60,7 @@ export function refChipHtml(kind: string, rawValue: string, displayLabel?: strin const label = displayLabel || refChipLabel(kind, id) - return `${directiveIconSvg(kind)}${escapeHtml(label)}` + return `${directiveIconSvg(kind)}${escapeHtml(label)}` } export function refChipElement(kind: string, rawValue: string, displayLabel?: string) { @@ -74,7 +74,7 @@ export function refChipElement(kind: string, rawValue: string, displayLabel?: st chip.dataset.refText = text chip.dataset.refId = id chip.dataset.refKind = kind - chip.className = DIRECTIVE_CHIP_CLASS + chip.className = directiveChipClass(kind) label.className = 'truncate' label.textContent = displayLabel || refChipLabel(kind, id) chip.append(directiveIconElement(kind), label) diff --git a/apps/desktop/src/app/chat/composer/trigger-popover-parity.test.tsx b/apps/desktop/src/app/chat/composer/trigger-popover-parity.test.tsx new file mode 100644 index 000000000000..e3f2ab08eba4 --- /dev/null +++ b/apps/desktop/src/app/chat/composer/trigger-popover-parity.test.tsx @@ -0,0 +1,151 @@ +import { render, screen } from '@testing-library/react' +import { describe, expect, it, vi } from 'vitest' + +import { ComposerTriggerPopover } from './trigger-popover' + +vi.mock('@/i18n', () => ({ + useI18n: () => ({ + t: { + composer: { + lookupLoading: 'Loading…', + lookupNoMatches: 'No matches', + lookupTry: 'Try', + lookupOr: 'or' + } + } + }) +})) + +function atItem(type: string, display: string, rawText: string, meta = '') { + return { + id: `${rawText}|0`, + type, + label: display, + metadata: { icon: type, display, meta, rawText, insertId: display } + } +} + +function slashItem(command: string, group: string, meta = '') { + return { + id: `${command}|0`, + type: 'slash', + label: command.slice(1), + metadata: { command, display: command, meta, group, action: '', rawText: command } + } +} + +const noop = () => {} + +/** The rendered shape of one row: does it have an icon, and how is it laid out? + * Icons are codicons β€” an ``, not an SVG. */ +function rowShape(root: HTMLElement) { + const row = root.querySelector('button') as HTMLElement + const icon = row.querySelector('i.codicon') + + return { + hasIcon: Boolean(icon), + iconName: icon?.className.match(/codicon-([\w-]+)/)?.[1], + classes: row.className + } +} + +describe('@ and / are one menu', () => { + it('a slash row has an icon, just like an @ row', () => { + const at = render( + + ) + + const atShape = rowShape(at.container) + at.unmount() + + const slash = render( + + ) + + const slashShape = rowShape(slash.container) + + // The whole point: `/` used to render a stacked, icon-less row. + expect(slashShape.hasIcon).toBe(true) + expect(atShape.hasIcon).toBe(true) + expect(slashShape.classes).toBe(atShape.classes) + + // And the glyph reflects the kind, not one generic bullet. + expect(slashShape.iconName).toBe('zap') + expect(atShape.iconName).toBe('folder') + }) + + it('renders the name and description for both kinds', () => { + const { rerender } = render( + + ) + + expect(screen.getByText('/work')).toBeTruthy() + expect(screen.getByText('Start in a worktree')).toBeTruthy() + + rerender( + + ) + + expect(screen.getByText('src/main.tsx')).toBeTruthy() + expect(screen.getByText('src')).toBeTruthy() + }) + + it('an emoji row stays icon-less β€” the emoji IS the icon', () => { + const { container } = render( + + ) + + expect(rowShape(container).hasIcon).toBe(false) + }) + + it('labels the active browse scope from the shared vocabulary', () => { + render( + + ) + + expect(screen.getByText('Folders')).toBeTruthy() + }) +}) diff --git a/apps/desktop/src/app/chat/composer/trigger-popover.tsx b/apps/desktop/src/app/chat/composer/trigger-popover.tsx index 38a9ac998a99..6ba9a81a02fc 100644 --- a/apps/desktop/src/app/chat/composer/trigger-popover.tsx +++ b/apps/desktop/src/app/chat/composer/trigger-popover.tsx @@ -1,6 +1,7 @@ import type { Unstable_TriggerItem } from '@assistant-ui/core' import { Fragment } from 'react' +import { referenceStyle } from '@/components/assistant-ui/reference-kinds' import { Codicon } from '@/components/ui/codicon' import { GlyphSpinner } from '@/components/ui/glyph-spinner' import { useI18n } from '@/i18n' @@ -9,45 +10,47 @@ import { cn } from '@/lib/utils' import { COMPLETION_DRAWER_BELOW_CLASS, COMPLETION_DRAWER_CLASS, CompletionDrawerEmpty } from './completion-drawer' import type { DirectiveScope } from './text-utils' -const AT_ICON_BY_TYPE: Record = { - diff: 'diff', - file: 'book', - folder: 'folder', - git: 'git-branch', - image: 'file-media', - simple: 'symbol-misc', - staged: 'diff-added', - tool: 'tools', - url: 'globe' -} - -function atIcon(item: Unstable_TriggerItem) { - const meta = item.metadata as { rawText?: string } | undefined - const raw = meta?.rawText || item.label - - if (raw.startsWith('@diff')) { - return AT_ICON_BY_TYPE.diff - } - - if (raw.startsWith('@staged')) { - return AT_ICON_BY_TYPE.staged - } - - return AT_ICON_BY_TYPE[item.type] || AT_ICON_BY_TYPE.simple -} - interface RowMeta { display?: string group?: string meta?: string } -const ROW_BASE_CLASS = [ - 'relative flex w-full cursor-default select-none rounded-md px-2 py-1 text-left', +/** The kind a row represents, for its icon. `@` rows carry it as the item type; + * `/` rows carry it as the completion group (Skills / Themes / Commands). */ +function rowKind(item: Unstable_TriggerItem, isSlash: boolean): string { + const meta = item.metadata as (RowMeta & { rawText?: string }) | undefined + + if (isSlash) { + const group = meta?.group?.trim() + + return group === 'Skills' ? 'skill' : group === 'Themes' ? 'theme' : 'command' + } + + // The gateway's simple refs (`@diff`, `@staged`) share one item type, so the + // glyph comes from the directive itself. + const raw = meta?.rawText || item.label + + if (raw.startsWith('@diff')) { + return 'diff' + } + + if (raw.startsWith('@staged')) { + return 'staged' + } + + return item.type +} + +const ROW_CLASS = [ + 'relative flex w-full cursor-default select-none items-center gap-2 rounded-md px-2 py-1 text-left', 'outline-hidden transition-colors hover:bg-(--ui-bg-tertiary)', 'data-[highlighted]:bg-(--ui-bg-tertiary) data-[highlighted]:text-foreground' ].join(' ') +const GROUP_HEADER_CLASS = + 'select-none px-2 pb-0.5 text-[0.625rem] font-semibold uppercase tracking-wider text-(--ui-text-tertiary)' + interface ComposerTriggerPopoverProps { activeIndex: number items: readonly Unstable_TriggerItem[] @@ -62,16 +65,18 @@ interface ComposerTriggerPopoverProps { scope?: DirectiveScope } -/** What each scope is actually browsing, for the popover header. */ -const SCOPE_LABEL: Record = { - file: 'Files', - folder: 'Folders', - git: 'Git', - image: 'Images', - tool: 'Tools', - url: 'URL' -} - +/** + * The composer's completion list, for every trigger. + * + * `@` and `/` render through the SAME row: icon, name, description. They used + * to be two layouts in one file β€” `@` horizontal with an icon, `/` stacked with + * none β€” which is why picking a file and picking a skill felt like features + * from different apps. Icons and accents come from the shared reference + * vocabulary, so a row looks like the chip it will become. + * + * `:` emoji is the one exception: the emoji IS the icon, so it renders as a + * single display string (Slack's exact shape). + */ export function ComposerTriggerPopover({ activeIndex, items, @@ -85,6 +90,7 @@ export function ComposerTriggerPopover({ const { t } = useI18n() const copy = t.composer const isSlash = kind === '/' + const isEmoji = kind === ':' let lastGroup: string | undefined @@ -96,11 +102,7 @@ export function ComposerTriggerPopover({ onMouseDown={event => event.preventDefault()} role="listbox" > - {scope && ( -
- {SCOPE_LABEL[scope]} -
- )} + {scope &&
{referenceStyle(scope).label}
} {items.length === 0 ? ( loading ? (
@@ -114,7 +116,7 @@ export function ComposerTriggerPopover({ {copy.lookupTry} @file: {copy.lookupOr}{' '} @folder:. - ) : kind === ':' ? ( + ) : isEmoji ? ( <> {copy.lookupTry} :joy:. @@ -138,58 +140,26 @@ export function ComposerTriggerPopover({ return ( - {showHeader && ( -
- {group} -
- )} + {showHeader &&
{group}
}