Show favicons on browser tabs (#2977)

* Show favicons on browser tabs

Reference design: docs/browser-tab-favicons.md

* rm design doc

* Keep browser tab favicons visible
This commit is contained in:
Jinjing
2026-05-28 00:07:51 -07:00
committed by GitHub
parent e53300a4be
commit d963a03051
2 changed files with 289 additions and 1 deletions
@@ -0,0 +1,248 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { BrowserTab as BrowserTabState } from '../../../../shared/types'
const reactHookRuntime = vi.hoisted(() => ({
states: [] as unknown[],
index: 0
}))
vi.mock('react', async () => {
const actual = await vi.importActual<typeof import('react')>('react') // eslint-disable-line @typescript-eslint/consistent-type-imports -- vi.importActual requires inline import()
return {
...actual,
useEffect: () => {},
useState<T>(initial: T | (() => T)) {
const stateIndex = reactHookRuntime.index++
if (!(stateIndex in reactHookRuntime.states)) {
reactHookRuntime.states[stateIndex] =
typeof initial === 'function' ? (initial as () => T)() : initial
}
const setState = (next: T | ((previous: T) => T)): void => {
reactHookRuntime.states[stateIndex] =
typeof next === 'function'
? (next as (previous: T) => T)(reactHookRuntime.states[stateIndex] as T)
: next
}
return [reactHookRuntime.states[stateIndex] as T, setState] as const
}
}
})
vi.mock('@dnd-kit/sortable', () => ({
useSortable: () => ({
attributes: {},
listeners: { onPointerDown: vi.fn() },
setNodeRef: vi.fn()
})
}))
vi.mock('lucide-react', () => ({
Columns2: function Columns2(props: Record<string, unknown>) {
return { type: 'Columns2', props }
},
Copy: function Copy(props: Record<string, unknown>) {
return { type: 'Copy', props }
},
ExternalLink: function ExternalLink(props: Record<string, unknown>) {
return { type: 'ExternalLink', props }
},
Globe: function Globe(props: Record<string, unknown>) {
return { type: 'Globe', props }
},
Rows2: function Rows2(props: Record<string, unknown>) {
return { type: 'Rows2', props }
},
X: function X(props: Record<string, unknown>) {
return { type: 'X', props }
}
}))
vi.mock('@/components/ui/dropdown-menu', () => ({
DropdownMenu: function DropdownMenu(props: { children?: unknown }) {
return { type: 'DropdownMenu', props }
},
DropdownMenuContent: function DropdownMenuContent(props: { children?: unknown }) {
return { type: 'DropdownMenuContent', props }
},
DropdownMenuItem: function DropdownMenuItem(props: { children?: unknown }) {
return { type: 'DropdownMenuItem', props }
},
DropdownMenuSeparator: function DropdownMenuSeparator() {
return { type: 'DropdownMenuSeparator', props: {} }
},
DropdownMenuTrigger: function DropdownMenuTrigger(props: { children?: unknown }) {
return { type: 'DropdownMenuTrigger', props }
}
}))
vi.mock('../browser-pane/browser-runtime', () => ({
getLiveBrowserUrl: () => null
}))
type ReactElementLike = {
type: unknown
props: Record<string, unknown>
}
function baseBrowserTab(overrides: Partial<BrowserTabState> = {}): BrowserTabState {
return {
id: 'browser-1',
worktreeId: 'wt-1',
label: 'Browser 1',
sessionProfileId: null,
activePageId: 'page-1',
pageIds: ['page-1'],
url: 'https://example.com/',
title: 'Example',
loading: false,
faviconUrl: null,
canGoBack: false,
canGoForward: false,
loadError: null,
createdAt: 0,
...overrides
}
}
async function renderBrowserTab(tab: BrowserTabState): Promise<unknown> {
reactHookRuntime.index = 0
const module = await import('./BrowserTab')
return module.default({
tab,
isActive: true,
hasTabsToRight: false,
onActivate: () => {},
onClose: () => {},
onCloseToRight: () => {},
onSplitGroup: () => {},
onDuplicate: () => {},
dragData: {
kind: 'tab',
worktreeId: tab.worktreeId,
groupId: 'group-1',
unifiedTabId: tab.id,
visibleTabId: tab.id,
tabType: 'browser',
label: tab.title
},
dropIndicator: undefined
})
}
function expandNode(node: unknown): unknown {
if (node == null || typeof node === 'string' || typeof node === 'number') {
return node
}
if (Array.isArray(node)) {
return node.map(expandNode)
}
const el = node as ReactElementLike
if (typeof el.type === 'function') {
return expandNode(el.type(el.props))
}
return {
...el,
props: {
...el.props,
children: expandNode(el.props?.children)
}
}
}
function findElementsByType(node: unknown, typeName: string): ReactElementLike[] {
const results: ReactElementLike[] = []
const visit = (current: unknown): void => {
if (current == null || typeof current === 'string' || typeof current === 'number') {
return
}
if (Array.isArray(current)) {
for (const child of current) {
visit(child)
}
return
}
const el = current as ReactElementLike
if (el.type === typeName) {
results.push(el)
}
visit(el.props?.children)
}
visit(node)
return results
}
async function renderExpandedBrowserTab(tab: BrowserTabState): Promise<unknown> {
return expandNode(await renderBrowserTab(tab))
}
describe('BrowserTab favicon', () => {
beforeEach(() => {
reactHookRuntime.states = []
reactHookRuntime.index = 0
vi.clearAllMocks()
})
it('renders the favicon image when faviconUrl is present', async () => {
const iconUrl = 'https://example.com/favicon.ico'
const element = await renderExpandedBrowserTab(baseBrowserTab({ faviconUrl: iconUrl }))
const images = findElementsByType(element, 'img')
expect(images).toHaveLength(1)
expect(images[0].props.src).toBe(iconUrl)
expect(images[0].props.alt).toBe('')
expect(images[0].props['aria-hidden']).toBe(true)
expect(images[0].props.draggable).toBe(false)
expect(images[0].props.className).toContain('size-3 mr-1 shrink-0')
expect(images[0].props.className).toContain('object-contain')
expect(images[0].props.className).toContain('drop-shadow-[0_0_1px_var(--foreground)]')
expect(findElementsByType(element, 'Globe')).toHaveLength(0)
})
it('keeps the globe fallback for blank tabs without faviconUrl', async () => {
const element = await renderExpandedBrowserTab(
baseBrowserTab({
url: 'about:blank',
title: 'about:blank',
faviconUrl: null
})
)
expect(findElementsByType(element, 'img')).toHaveLength(0)
const globes = findElementsByType(element, 'Globe')
expect(globes).toHaveLength(1)
expect(globes[0].props.className).toContain('size-3 mr-1 shrink-0')
expect(globes[0].props.className).toContain('text-blue-500')
})
it('falls back to the globe after the favicon image errors', async () => {
const tab = baseBrowserTab({ faviconUrl: 'https://example.com/favicon.ico' })
const firstRender = await renderExpandedBrowserTab(tab)
const image = findElementsByType(firstRender, 'img')[0]
;(image.props.onError as () => void)()
const secondRender = await renderExpandedBrowserTab(tab)
expect(findElementsByType(secondRender, 'img')).toHaveLength(0)
expect(findElementsByType(secondRender, 'Globe')).toHaveLength(1)
})
it('resets the image-error fallback when faviconUrl changes', async () => {
const tab = baseBrowserTab({ faviconUrl: 'https://example.com/favicon.ico' })
const firstRender = await renderExpandedBrowserTab(tab)
const image = findElementsByType(firstRender, 'img')[0]
;(image.props.onError as () => void)()
const failedRender = await renderExpandedBrowserTab(tab)
expect(findElementsByType(failedRender, 'Globe')).toHaveLength(1)
const nextIconUrl = 'data:image/png;base64,abc123'
const resetRender = await renderExpandedBrowserTab(
baseBrowserTab({ id: tab.id, faviconUrl: nextIconUrl })
)
const images = findElementsByType(resetRender, 'img')
expect(images).toHaveLength(1)
expect(images[0].props.src).toBe(nextIconUrl)
expect(findElementsByType(resetRender, 'Globe')).toHaveLength(0)
})
})
@@ -49,6 +49,46 @@ function isBlankBrowserTab(tab: BrowserTabState): boolean {
return tab.url === ORCA_BROWSER_BLANK_URL || tab.url === 'about:blank'
}
type FailedFavicon = {
tabId: string
faviconUrl: string
}
function BrowserTabFavicon({
tabId,
faviconUrl
}: {
tabId: string
faviconUrl: string | null
}): React.JSX.Element {
const displayFaviconUrl = faviconUrl?.trim() ? faviconUrl : null
const [failedFavicon, setFailedFavicon] = useState<FailedFavicon | null>(null)
useEffect(() => {
setFailedFavicon(null)
}, [tabId, displayFaviconUrl])
const currentFaviconFailed =
failedFavicon?.tabId === tabId && failedFavicon.faviconUrl === displayFaviconUrl
if (displayFaviconUrl && !currentFaviconFailed) {
return (
<img
src={displayFaviconUrl}
alt=""
aria-hidden
draggable={false}
// Why: transparent dark/light-mode favicons can disappear against tab
// chrome; a token-colored 1px shadow keeps the 12px mark legible.
className="size-3 mr-1 shrink-0 rounded-sm object-contain drop-shadow-[0_0_1px_var(--foreground)]"
onError={() => setFailedFavicon({ tabId, faviconUrl: displayFaviconUrl })}
/>
)
}
return <Globe className="size-3 mr-1 shrink-0 text-blue-500" />
}
export default function BrowserTab({
tab,
isActive,
@@ -159,7 +199,7 @@ export default function BrowserTab({
browser tabs at a glance even when the strip is saturated. We
keep full color on both active and inactive tabs — dimming to
muted-foreground made the icon read as "disabled" in practice. */}
<Globe className="w-3 h-3 mr-1 shrink-0 text-blue-500" />
<BrowserTabFavicon tabId={tab.id} faviconUrl={tab.faviconUrl} />
<span className="truncate max-w-[100px] mr-1">{getBrowserTabLabel(tab)}</span>
{tab.loading && !tab.loadError && !isBlankBrowserTab(tab) && (
<span className="mr-1 size-1.5 rounded-full bg-sky-500/80 shrink-0" />