mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
feat(activity): surface active work across the session picker, tool rail and mobile
This commit is contained in:
1 parent
be874ee07c
commit
d8f2fcf29c
46 files changed
+3120
-277
No files matched your search
@@ -0,0 +1,144 @@
|
||||
import { act, cleanup, render } from '@testing-library/react'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { stubMatchMedia } from '@/test/test-utils'
|
||||
import { ActivitySpinner, SPINNER_FRAMES, brailleDots } from './activity-spinner'
|
||||
|
||||
const advanceFrames = (count: number) => {
|
||||
act(() => {
|
||||
vi.advanceTimersByTime(80 * count)
|
||||
})
|
||||
}
|
||||
|
||||
const circles = (root: ParentNode) => Array.from(root.querySelectorAll('circle'))
|
||||
|
||||
const litIndexes = (root: ParentNode) =>
|
||||
circles(root)
|
||||
.map((circle, index) => (circle.getAttribute('fill-opacity') === '1' ? index : -1))
|
||||
.filter((index) => index >= 0)
|
||||
|
||||
const litIndexesFor = (frame: string) =>
|
||||
brailleDots(frame)
|
||||
.map((lit, index) => (lit ? index : -1))
|
||||
.filter((index) => index >= 0)
|
||||
|
||||
const pattern = (root: ParentNode) => circles(root).map((circle) => circle.getAttribute('fill-opacity'))
|
||||
|
||||
describe('brailleDots', () => {
|
||||
it('lights dots 1, 2 and 4 for the first frame', () => {
|
||||
expect(brailleDots('⠋')).toEqual([true, true, false, true, false, false, false, false])
|
||||
})
|
||||
|
||||
it('lights dots 1 through 4 for the fourth frame', () => {
|
||||
expect(brailleDots('⠏')).toEqual([true, true, true, true, false, false, false, false])
|
||||
})
|
||||
})
|
||||
|
||||
describe('ActivitySpinner', () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
stubMatchMedia(false)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
cleanup()
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
it('renders eight dots with the first frame lit and hidden from assistive technology', () => {
|
||||
const { container } = render(<ActivitySpinner />)
|
||||
|
||||
expect(circles(container)).toHaveLength(8)
|
||||
expect(litIndexes(container)).toEqual(litIndexesFor(SPINNER_FRAMES[0]))
|
||||
expect(container.querySelector('svg')).toHaveAttribute('aria-hidden', 'true')
|
||||
})
|
||||
|
||||
it('advances one frame every 80ms and wraps after the last frame', () => {
|
||||
const { container } = render(<ActivitySpinner />)
|
||||
|
||||
expect(litIndexes(container)).toEqual(litIndexesFor(SPINNER_FRAMES[0]))
|
||||
|
||||
advanceFrames(1)
|
||||
expect(litIndexes(container)).toEqual(litIndexesFor(SPINNER_FRAMES[1]))
|
||||
|
||||
advanceFrames(1)
|
||||
expect(litIndexes(container)).toEqual(litIndexesFor(SPINNER_FRAMES[2]))
|
||||
|
||||
advanceFrames(SPINNER_FRAMES.length - 2)
|
||||
expect(litIndexes(container)).toEqual(litIndexesFor(SPINNER_FRAMES[0]))
|
||||
})
|
||||
|
||||
it('drives every mounted spinner from a single shared ticker', () => {
|
||||
const { container } = render(
|
||||
<>
|
||||
<ActivitySpinner />
|
||||
<ActivitySpinner />
|
||||
</>,
|
||||
)
|
||||
|
||||
expect(vi.getTimerCount()).toBe(1)
|
||||
|
||||
const [first, second] = Array.from(container.querySelectorAll('svg'))
|
||||
expect(pattern(first)).toEqual(pattern(second))
|
||||
|
||||
advanceFrames(1)
|
||||
|
||||
expect(litIndexes(first)).toEqual(litIndexesFor(SPINNER_FRAMES[1]))
|
||||
expect(pattern(first)).toEqual(pattern(second))
|
||||
})
|
||||
|
||||
it('clears the shared ticker when the last spinner unmounts', () => {
|
||||
const { unmount } = render(
|
||||
<>
|
||||
<ActivitySpinner />
|
||||
<ActivitySpinner />
|
||||
</>,
|
||||
)
|
||||
|
||||
expect(vi.getTimerCount()).toBe(1)
|
||||
|
||||
unmount()
|
||||
|
||||
expect(vi.getTimerCount()).toBe(0)
|
||||
})
|
||||
|
||||
it('keeps animating when the user prefers reduced motion', () => {
|
||||
stubMatchMedia(true)
|
||||
|
||||
const { container } = render(<ActivitySpinner />)
|
||||
|
||||
expect(vi.getTimerCount()).toBe(1)
|
||||
|
||||
advanceFrames(1)
|
||||
|
||||
expect(litIndexes(container)).toEqual(litIndexesFor(SPINNER_FRAMES[1]))
|
||||
})
|
||||
|
||||
it('keeps every dot inside the cell and clear of its neighbours', () => {
|
||||
const { container } = render(<ActivitySpinner />)
|
||||
|
||||
const svg = container.querySelector('svg')
|
||||
expect(svg).toHaveAttribute('viewBox', '0 0 9 16')
|
||||
|
||||
const dots = circles(container).map((circle) => ({
|
||||
cx: Number(circle.getAttribute('cx')),
|
||||
cy: Number(circle.getAttribute('cy')),
|
||||
r: Number(circle.getAttribute('r')),
|
||||
}))
|
||||
|
||||
expect(dots).toHaveLength(8)
|
||||
|
||||
for (const dot of dots) {
|
||||
expect(dot.cx - dot.r).toBeGreaterThan(0)
|
||||
expect(dot.cx + dot.r).toBeLessThan(9)
|
||||
expect(dot.cy - dot.r).toBeGreaterThan(0)
|
||||
expect(dot.cy + dot.r).toBeLessThan(16)
|
||||
}
|
||||
|
||||
for (let i = 0; i < dots.length; i += 1) {
|
||||
for (let j = i + 1; j < dots.length; j += 1) {
|
||||
const distance = Math.hypot(dots[i].cx - dots[j].cx, dots[i].cy - dots[j].cy)
|
||||
expect(distance).toBeGreaterThanOrEqual(dots[i].r + dots[j].r)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,96 @@
|
||||
import { useSyncExternalStore } from 'react'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
export const SPINNER_FRAMES = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] as const
|
||||
|
||||
const FRAME_INTERVAL_MS = 80
|
||||
|
||||
const BRAILLE_BASE = 0x2800
|
||||
const DOT_GRID: ReadonlyArray<readonly [number, number]> = [
|
||||
[0, 0],
|
||||
[0, 1],
|
||||
[0, 2],
|
||||
[1, 0],
|
||||
[1, 1],
|
||||
[1, 2],
|
||||
[0, 3],
|
||||
[1, 3],
|
||||
]
|
||||
const COLUMN_X = [2.25, 6.75] as const
|
||||
const ROW_Y = [2.25, 6, 9.75, 13.5] as const
|
||||
const DOT_RADIUS = 1.6
|
||||
const UNLIT_DOT_OPACITY = 0.25
|
||||
|
||||
let frameIndex = 0
|
||||
const listeners = new Set<() => void>()
|
||||
let ticker: ReturnType<typeof setInterval> | null = null
|
||||
|
||||
function emit() {
|
||||
for (const listener of listeners) {
|
||||
listener()
|
||||
}
|
||||
}
|
||||
|
||||
function subscribe(listener: () => void) {
|
||||
listeners.add(listener)
|
||||
if (ticker === null) {
|
||||
ticker = setInterval(() => {
|
||||
frameIndex = (frameIndex + 1) % SPINNER_FRAMES.length
|
||||
emit()
|
||||
}, FRAME_INTERVAL_MS)
|
||||
}
|
||||
|
||||
return () => {
|
||||
listeners.delete(listener)
|
||||
if (listeners.size === 0 && ticker !== null) {
|
||||
clearInterval(ticker)
|
||||
ticker = null
|
||||
frameIndex = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getSnapshot() {
|
||||
return frameIndex
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes one braille frame into its eight dot positions, ordered by braille dot numbering:
|
||||
* dot1, dot2 and dot3 are the left column top-to-bottom, dot4, dot5 and dot6 the right column
|
||||
* top-to-bottom, and dot7 and dot8 the bottom-left and bottom-right cells.
|
||||
*/
|
||||
export function brailleDots(frame: string): boolean[] {
|
||||
const bits = (frame.codePointAt(0) ?? BRAILLE_BASE) - BRAILLE_BASE
|
||||
return DOT_GRID.map((_, index) => (bits & (1 << index)) !== 0)
|
||||
}
|
||||
|
||||
/**
|
||||
* Braille spinner mirroring the OpenCode TUI activity spinner as an inline SVG cell so the dots
|
||||
* stay visible at small text sizes. Every mounted instance shares one module-level 80 ms ticker,
|
||||
* which starts with the first subscriber and stops with the last. It animates regardless of
|
||||
* `prefers-reduced-motion`: a still frame is indistinguishable from a stalled spinner, and the
|
||||
* motion is a small in-place status cue like the platform activity indicators.
|
||||
*/
|
||||
export function ActivitySpinner({ className }: { className?: string }) {
|
||||
const frame = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
|
||||
const dots = brailleDots(SPINNER_FRAMES[frame])
|
||||
|
||||
return (
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
viewBox="0 0 9 16"
|
||||
className={cn('inline-block h-4 w-[9px] shrink-0', className)}
|
||||
>
|
||||
{DOT_GRID.map(([column, row], index) => (
|
||||
<circle
|
||||
key={index}
|
||||
cx={COLUMN_X[column]}
|
||||
cy={ROW_Y[row]}
|
||||
r={DOT_RADIUS}
|
||||
fill="currentColor"
|
||||
fillOpacity={dots[index] ? 1 : UNLIT_DOT_OPACITY}
|
||||
/>
|
||||
))}
|
||||
</svg>
|
||||
)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import { beforeEach, describe, expect, it } from 'vitest'
|
||||
import { SessionStatusIndicator } from './session-status-indicator'
|
||||
import { SPINNER_FRAMES, brailleDots } from './activity-spinner'
|
||||
import { useSessionStatus } from '@/stores/sessionStatusStore'
|
||||
|
||||
const resetStore = () => {
|
||||
@@ -11,6 +12,7 @@ const resetStore = () => {
|
||||
knownSessions: new Set(),
|
||||
outcomes: new Map(),
|
||||
parents: new Map(),
|
||||
directories: new Map(),
|
||||
runningShells: new Map(),
|
||||
revision: 0,
|
||||
})
|
||||
@@ -34,4 +36,14 @@ describe('SessionStatusIndicator background activity', () => {
|
||||
|
||||
expect(container).toBeEmptyDOMElement()
|
||||
})
|
||||
|
||||
it('renders the braille spinner with a running label for a busy session', () => {
|
||||
useSessionStatus.getState().setStatus('session-a', { type: 'busy' })
|
||||
|
||||
const { container } = render(<SessionStatusIndicator sessionID="session-a" size="sm" />)
|
||||
|
||||
expect(screen.getByLabelText('Running')).toBeInTheDocument()
|
||||
const litDots = container.querySelectorAll('svg[aria-hidden="true"] circle[fill-opacity="1"]')
|
||||
expect(litDots).toHaveLength(brailleDots(SPINNER_FRAMES[0]).filter(Boolean).length)
|
||||
})
|
||||
})
|
||||
@@ -1,4 +1,5 @@
|
||||
import { memo, useEffect, useState } from 'react'
|
||||
import { ActivitySpinner } from '@/components/ui/activity-spinner'
|
||||
import { useHasBackgroundActivity, useSessionStatusForSession, type SessionStatusType } from '@/stores/sessionStatusStore'
|
||||
|
||||
interface SessionStatusIndicatorProps {
|
||||
@@ -11,6 +12,10 @@ interface SessionStatusIndicatorProps {
|
||||
const SCANNER_WIDTH = 6
|
||||
const SCANNER_SEGMENTS = 12
|
||||
|
||||
/**
|
||||
* Shows a session's activity. `sm` renders the braille spinner sized for list rows;
|
||||
* `md` renders the animated scanner used by the session composer.
|
||||
*/
|
||||
export const SessionStatusIndicator = memo(function SessionStatusIndicator({
|
||||
sessionID,
|
||||
className = '',
|
||||
@@ -25,6 +30,9 @@ export const SessionStatusIndicator = memo(function SessionStatusIndicator({
|
||||
const backgroundOnly = status.type === 'idle' && hasBackground
|
||||
|
||||
useEffect(() => {
|
||||
if (size !== 'md') {
|
||||
return
|
||||
}
|
||||
if (status.type === 'idle' && !hasBackground) {
|
||||
return
|
||||
}
|
||||
@@ -45,7 +53,7 @@ export const SessionStatusIndicator = memo(function SessionStatusIndicator({
|
||||
}, 60)
|
||||
|
||||
return () => clearInterval(interval)
|
||||
}, [status.type, hasBackground, direction])
|
||||
}, [size, status.type, hasBackground, direction])
|
||||
|
||||
useEffect(() => {
|
||||
if (!showLabel || status.type !== 'retry') {
|
||||
@@ -68,6 +76,34 @@ export const SessionStatusIndicator = memo(function SessionStatusIndicator({
|
||||
return null
|
||||
}
|
||||
|
||||
if (size === 'sm') {
|
||||
const colorClass = backgroundOnly
|
||||
? 'text-muted-foreground'
|
||||
: status.type === 'retry'
|
||||
? 'text-warning'
|
||||
: status.type === 'compact'
|
||||
? 'text-primary'
|
||||
: 'text-info'
|
||||
const label = backgroundOnly
|
||||
? 'Background tasks running'
|
||||
: status.type === 'retry'
|
||||
? 'Retrying'
|
||||
: status.type === 'compact'
|
||||
? 'Compacting'
|
||||
: 'Running'
|
||||
|
||||
return (
|
||||
<span
|
||||
role="img"
|
||||
aria-label={label}
|
||||
title={backgroundOnly ? label : undefined}
|
||||
className={`inline-flex items-center ${colorClass} ${className}`}
|
||||
>
|
||||
<ActivitySpinner />
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
const getSegmentColor = (index: number, statusType: SessionStatusType['type'], background: boolean) => {
|
||||
const distance = Math.abs(index - (position + SCANNER_WIDTH / 2))
|
||||
const maxDistance = SCANNER_WIDTH / 2
|
||||
@@ -105,7 +141,7 @@ export const SessionStatusIndicator = memo(function SessionStatusIndicator({
|
||||
return 'bg-info/20'
|
||||
}
|
||||
|
||||
const segmentClass = size === 'sm' ? 'w-0.5 h-2' : 'w-1 h-4'
|
||||
const segmentClass = 'w-1 h-4'
|
||||
const containerGap = showLabel ? 'gap-2' : ''
|
||||
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user