mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(frontend): move session activity scanner under the composer
- Keep the model picker visible while a session is active - Render a full-width segmented scanner below the composer via shared ScannerBars - Reduce SessionStatusIndicator to the list-row spinner
This commit is contained in:
1 parent
e8adbb3e65
commit
332fb07f45
15 files changed
+554
-185
No files matched your search
@@ -0,0 +1,205 @@
|
||||
import { useLayoutEffect, useRef, useState, type RefObject } from 'react'
|
||||
import { useMediaQuery } from '@/hooks/useMediaQuery'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
export type ScannerTone = 'info' | 'primary' | 'warning' | 'muted-foreground'
|
||||
|
||||
export const SCANNER_REDUCED_MOTION_QUERY = '(prefers-reduced-motion: reduce)'
|
||||
|
||||
const SCANNER_SEGMENT_WIDTH = 4
|
||||
const SCANNER_SEGMENT_GAP = 2
|
||||
const SCANNER_SEGMENT_PITCH = SCANNER_SEGMENT_WIDTH + SCANNER_SEGMENT_GAP
|
||||
const SCANNER_MIN_SEGMENTS = 12
|
||||
|
||||
const SCANNER_SEGMENT_CLASS = 'w-1 h-2 rounded-[1px] transition-colors duration-75'
|
||||
|
||||
const SCANNER_TONE_TIERS: Record<ScannerTone, readonly [string, string, string, string]> = {
|
||||
info: ['bg-info', 'bg-info/70', 'bg-info/40', 'bg-info/20'],
|
||||
primary: ['bg-primary', 'bg-primary/70', 'bg-primary/40', 'bg-primary/20'],
|
||||
warning: ['bg-warning', 'bg-warning/70', 'bg-warning/40', 'bg-warning/20'],
|
||||
'muted-foreground': ['bg-muted-foreground', 'bg-muted-foreground/70', 'bg-muted-foreground/40', 'bg-muted-foreground/20'],
|
||||
}
|
||||
|
||||
const SCANNER_STATIC_TONE_CLASS: Record<ScannerTone, string> = {
|
||||
info: 'bg-info/60',
|
||||
primary: 'bg-primary/60',
|
||||
warning: 'bg-warning/60',
|
||||
'muted-foreground': 'bg-muted-foreground/60',
|
||||
}
|
||||
|
||||
const SCANNER_OFF_CLASS = 'bg-muted/20'
|
||||
const SCANNER_INTENSITY_THRESHOLDS = [0.8, 0.5, 0.2] as const
|
||||
const SCANNER_FAINTEST_TIER = SCANNER_INTENSITY_THRESHOLDS.length
|
||||
|
||||
/**
|
||||
* Returns how many fixed-width segments fit into `width`: each segment is 4px wide and the
|
||||
* container adds a 2px gap between neighbors, so `n` segments occupy `6n - 2` pixels. Never
|
||||
* drops below `SCANNER_MIN_SEGMENTS` so an unmeasured container still renders a usable scanner.
|
||||
*/
|
||||
export function scannerSegmentCountForWidth(width: number): number {
|
||||
return Math.max(SCANNER_MIN_SEGMENTS, Math.floor((width + SCANNER_SEGMENT_GAP) / SCANNER_SEGMENT_PITCH))
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks the content width of a container across resizes. Returns 0 until the first measurement.
|
||||
*/
|
||||
function useContainerWidth(ref: RefObject<HTMLElement | null>): number {
|
||||
const [width, setWidth] = useState(0)
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const element = ref.current
|
||||
if (!element) {
|
||||
return
|
||||
}
|
||||
|
||||
const update = (next: number) => setWidth((previous) => (previous === next ? previous : next))
|
||||
update(element.getBoundingClientRect().width)
|
||||
|
||||
if (typeof ResizeObserver === 'undefined') {
|
||||
return
|
||||
}
|
||||
|
||||
const observer = new ResizeObserver((entries) => {
|
||||
const entry = entries[0]
|
||||
if (entry) {
|
||||
update(entry.contentRect.width)
|
||||
}
|
||||
})
|
||||
observer.observe(element)
|
||||
return () => observer.disconnect()
|
||||
}, [ref])
|
||||
|
||||
return width
|
||||
}
|
||||
|
||||
interface ScannerSegmentClassOptions {
|
||||
index: number
|
||||
position: number
|
||||
litWidth: number
|
||||
tone: ScannerTone
|
||||
static?: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the background class for one scanner segment. Segments within the lit window fade out
|
||||
* with distance from the window center across four intensity tiers; everything outside the window
|
||||
* stays muted. `static` skips the window math and renders the uniform reduced-motion tint.
|
||||
*/
|
||||
export function scannerSegmentClass({
|
||||
index,
|
||||
position,
|
||||
litWidth,
|
||||
tone,
|
||||
static: isStatic = false,
|
||||
}: ScannerSegmentClassOptions): string {
|
||||
if (isStatic) {
|
||||
return SCANNER_STATIC_TONE_CLASS[tone]
|
||||
}
|
||||
|
||||
const halfWidth = litWidth / 2
|
||||
const distance = Math.abs(index - (position + halfWidth))
|
||||
|
||||
if (distance > halfWidth + 1) {
|
||||
return SCANNER_OFF_CLASS
|
||||
}
|
||||
|
||||
const intensity = Math.max(0, 1 - distance / (halfWidth + 2))
|
||||
const tier = SCANNER_INTENSITY_THRESHOLDS.findIndex((threshold) => intensity > threshold)
|
||||
|
||||
return SCANNER_TONE_TIERS[tone][tier === -1 ? SCANNER_FAINTEST_TIER : tier]
|
||||
}
|
||||
|
||||
interface ScannerPositionOptions {
|
||||
segmentCount: number
|
||||
litWidth: number
|
||||
intervalMs: number
|
||||
paused: boolean
|
||||
}
|
||||
|
||||
function useScannerPosition({
|
||||
segmentCount,
|
||||
litWidth,
|
||||
intervalMs,
|
||||
paused,
|
||||
}: ScannerPositionOptions): number {
|
||||
const [position, setPosition] = useState(0)
|
||||
const direction = useRef(1)
|
||||
const maxPosition = Math.max(0, segmentCount - litWidth)
|
||||
|
||||
useLayoutEffect(() => {
|
||||
setPosition((previous) => Math.min(previous, maxPosition))
|
||||
}, [maxPosition])
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (paused || maxPosition === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const interval = setInterval(() => {
|
||||
setPosition((previous) => {
|
||||
const next = previous + direction.current
|
||||
if (next >= maxPosition) {
|
||||
direction.current = -1
|
||||
return maxPosition
|
||||
}
|
||||
if (next <= 0) {
|
||||
direction.current = 1
|
||||
return 0
|
||||
}
|
||||
return next
|
||||
})
|
||||
}, intervalMs)
|
||||
|
||||
return () => clearInterval(interval)
|
||||
}, [paused, maxPosition, intervalMs])
|
||||
|
||||
return position
|
||||
}
|
||||
|
||||
interface ScannerBarsProps {
|
||||
tone: ScannerTone
|
||||
segmentCount?: number
|
||||
litWidth?: number
|
||||
intervalMs?: number
|
||||
className?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Segmented scanner matching the session status scanner: 4x8px segments with a 2px gap and a lit
|
||||
* window of `litWidth` segments that sweeps back and forth. Without an explicit `segmentCount` the
|
||||
* count is measured from the container width so the strip spans the space it is given. Under
|
||||
* `prefers-reduced-motion: reduce` the sweep is not started and every segment renders statically at
|
||||
* 60% opacity.
|
||||
*/
|
||||
export function ScannerBars({
|
||||
tone,
|
||||
segmentCount,
|
||||
litWidth = 6,
|
||||
intervalMs = 60,
|
||||
className,
|
||||
}: ScannerBarsProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const reducedMotion = useMediaQuery(SCANNER_REDUCED_MOTION_QUERY)
|
||||
const measuredWidth = useContainerWidth(containerRef)
|
||||
const resolvedCount = segmentCount ?? scannerSegmentCountForWidth(measuredWidth)
|
||||
const position = useScannerPosition({
|
||||
segmentCount: resolvedCount,
|
||||
litWidth,
|
||||
intervalMs,
|
||||
paused: reducedMotion,
|
||||
})
|
||||
|
||||
return (
|
||||
<div ref={containerRef} className={cn('flex w-full items-center gap-0.5', className)}>
|
||||
{Array.from({ length: resolvedCount }, (_, index) => (
|
||||
<div
|
||||
key={index}
|
||||
className={cn(
|
||||
SCANNER_SEGMENT_CLASS,
|
||||
scannerSegmentClass({ index, position, litWidth, tone, static: reducedMotion }),
|
||||
)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -26,13 +26,13 @@ describe('SessionStatusIndicator background activity', () => {
|
||||
it('shows the background tasks label when idle with a running shell', () => {
|
||||
useSessionStatus.getState().setShellRunning('shell-1', 'session-a', '/repo')
|
||||
|
||||
render(<SessionStatusIndicator sessionID="session-a" showLabel />)
|
||||
render(<SessionStatusIndicator sessionID="session-a" />)
|
||||
|
||||
expect(screen.getByLabelText('Background tasks running')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('renders nothing when idle without background activity', () => {
|
||||
const { container } = render(<SessionStatusIndicator sessionID="session-a" showLabel />)
|
||||
const { container } = render(<SessionStatusIndicator sessionID="session-a" />)
|
||||
|
||||
expect(container).toBeEmptyDOMElement()
|
||||
})
|
||||
@@ -40,7 +40,7 @@ describe('SessionStatusIndicator background activity', () => {
|
||||
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" />)
|
||||
const { container } = render(<SessionStatusIndicator sessionID="session-a" />)
|
||||
|
||||
expect(screen.getByLabelText('Running')).toBeInTheDocument()
|
||||
const litDots = container.querySelectorAll('svg[aria-hidden="true"] circle[fill-opacity="1"]')
|
||||
|
||||
@@ -1,169 +1,51 @@
|
||||
import { memo, useEffect, useState } from 'react'
|
||||
import { memo } from 'react'
|
||||
import { ActivitySpinner } from '@/components/ui/activity-spinner'
|
||||
import { useHasBackgroundActivity, useSessionStatusForSession, type SessionStatusType } from '@/stores/sessionStatusStore'
|
||||
import { useHasBackgroundActivity, useSessionStatusForSession } from '@/stores/sessionStatusStore'
|
||||
|
||||
interface SessionStatusIndicatorProps {
|
||||
sessionID: string
|
||||
className?: string
|
||||
size?: 'sm' | 'md'
|
||||
showLabel?: boolean
|
||||
}
|
||||
|
||||
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.
|
||||
* Shows a session's activity as the braille spinner sized for list rows.
|
||||
* Renders nothing while the session is idle and has no background activity.
|
||||
*/
|
||||
export const SessionStatusIndicator = memo(function SessionStatusIndicator({
|
||||
sessionID,
|
||||
export const SessionStatusIndicator = memo(function SessionStatusIndicator({
|
||||
sessionID,
|
||||
className = '',
|
||||
size = 'md',
|
||||
showLabel = false
|
||||
}: SessionStatusIndicatorProps) {
|
||||
const status = useSessionStatusForSession(sessionID)
|
||||
const hasBackground = useHasBackgroundActivity(sessionID)
|
||||
const [position, setPosition] = useState(0)
|
||||
const [direction, setDirection] = useState(1)
|
||||
const [retryCountdown, setRetryCountdown] = useState(0)
|
||||
const backgroundOnly = status.type === 'idle' && hasBackground
|
||||
|
||||
useEffect(() => {
|
||||
if (size !== 'md') {
|
||||
return
|
||||
}
|
||||
if (status.type === 'idle' && !hasBackground) {
|
||||
return
|
||||
}
|
||||
|
||||
const interval = setInterval(() => {
|
||||
setPosition(prev => {
|
||||
const next = prev + direction
|
||||
if (next >= SCANNER_SEGMENTS - SCANNER_WIDTH) {
|
||||
setDirection(-1)
|
||||
return SCANNER_SEGMENTS - SCANNER_WIDTH
|
||||
}
|
||||
if (next <= 0) {
|
||||
setDirection(1)
|
||||
return 0
|
||||
}
|
||||
return next
|
||||
})
|
||||
}, 60)
|
||||
|
||||
return () => clearInterval(interval)
|
||||
}, [size, status.type, hasBackground, direction])
|
||||
|
||||
useEffect(() => {
|
||||
if (!showLabel || status.type !== 'retry') {
|
||||
setRetryCountdown(0)
|
||||
return
|
||||
}
|
||||
|
||||
const remaining = Math.max(0, Math.ceil((status.next - Date.now()) / 1000))
|
||||
setRetryCountdown(remaining)
|
||||
|
||||
const interval = setInterval(() => {
|
||||
const newRemaining = Math.max(0, Math.ceil((status.next - Date.now()) / 1000))
|
||||
setRetryCountdown(newRemaining)
|
||||
}, 1000)
|
||||
|
||||
return () => clearInterval(interval)
|
||||
}, [status, showLabel])
|
||||
|
||||
|
||||
if (status.type === 'idle' && !hasBackground) {
|
||||
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
|
||||
|
||||
if (distance > maxDistance + 1) {
|
||||
return 'bg-muted/20'
|
||||
}
|
||||
|
||||
const intensity = Math.max(0, 1 - distance / (maxDistance + 2))
|
||||
|
||||
if (background) {
|
||||
if (intensity > 0.8) return 'bg-muted-foreground'
|
||||
if (intensity > 0.5) return 'bg-muted-foreground/70'
|
||||
if (intensity > 0.2) return 'bg-muted-foreground/40'
|
||||
return 'bg-muted-foreground/20'
|
||||
}
|
||||
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'
|
||||
|
||||
if (statusType === 'retry') {
|
||||
if (intensity > 0.8) return 'bg-warning'
|
||||
if (intensity > 0.5) return 'bg-warning/70'
|
||||
if (intensity > 0.2) return 'bg-warning/40'
|
||||
return 'bg-warning/20'
|
||||
}
|
||||
|
||||
if (statusType === 'compact') {
|
||||
if (intensity > 0.8) return 'bg-primary'
|
||||
if (intensity > 0.5) return 'bg-primary/70'
|
||||
if (intensity > 0.2) return 'bg-primary/40'
|
||||
return 'bg-primary/20'
|
||||
}
|
||||
|
||||
if (intensity > 0.8) return 'bg-info'
|
||||
if (intensity > 0.5) return 'bg-info/70'
|
||||
if (intensity > 0.2) return 'bg-info/40'
|
||||
return 'bg-info/20'
|
||||
}
|
||||
|
||||
const segmentClass = 'w-1 h-4'
|
||||
const containerGap = showLabel ? 'gap-2' : ''
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`flex items-center ${containerGap} ${className}`}
|
||||
title={backgroundOnly ? 'Background tasks running' : undefined}
|
||||
aria-label={backgroundOnly ? 'Background tasks running' : undefined}
|
||||
<span
|
||||
role="img"
|
||||
aria-label={label}
|
||||
title={backgroundOnly ? label : undefined}
|
||||
className={`inline-flex items-center ${colorClass} ${className}`}
|
||||
>
|
||||
<div className="flex gap-0.5">
|
||||
{Array.from({ length: SCANNER_SEGMENTS }).map((_, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className={`${segmentClass} rounded-sm transition-colors duration-75 ${getSegmentColor(i, status.type, backgroundOnly)}`}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
{showLabel && status.type === 'retry' && (
|
||||
<span className="text-xs text-muted-foreground">
|
||||
Retry #{status.attempt}
|
||||
{retryCountdown > 0 && <span className="text-warning ml-1">({retryCountdown}s)</span>}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<ActivitySpinner />
|
||||
</span>
|
||||
)
|
||||
})
|
||||
Reference in new issue
Block a user