Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

5.7 KB
Raw Blame
import { useLayoutEffect, useRef, useState, type ReactNode } from 'react'
import { Platform, StyleSheet, View, type LayoutRectangle } from 'react-native'
import Svg, { Defs, LinearGradient, Rect, Stop } from 'react-native-svg'
import Animated, {
  useAnimatedRef,
  useSharedValue,
  type AnimatedRef,
  type SharedValue
} from 'react-native-reanimated'
import { colors, spacing } from '../theme/mobile-theme'
import { useMobileColorScheme } from '../theme/use-mobile-color-scheme'

/** Desktop's `max-h-40`: three one-line cards and part of a fourth, whose cut-off edge is the
 *  sign that the list scrolls. */
export const QUEUE_LIST_MAX_HEIGHT = 160

export type MobileQueueDragViewport = {
  scrollRef: AnimatedRef<Animated.ScrollView>
  scrollOffsetY: SharedValue<number>
  scrollContentHeight: SharedValue<number>
  onDragActiveChange: (active: boolean) => void
}

type Metrics = { offset: number; viewport: number; content: number }

/** Compact cards are capped; the full editor yields to the available chat viewport. */
export function MobileQueuedMessageScrollList({
  messageIds,
  newestIsPersons,
  inlineEditorActive = false,
  children
}: {
  messageIds: readonly string[]
  /** The newest card has no sender: the person just queued it, so the list shows it. */
  newestIsPersons: boolean
  inlineEditorActive?: boolean
  /** Rows, given `reveal` to keep an edited row in view. */
  children: (reveal: (row: LayoutRectangle) => void, drag: MobileQueueDragViewport) => ReactNode
}): React.JSX.Element {
  const scrollRef = useAnimatedRef<Animated.ScrollView>()
  const scrollOffsetY = useSharedValue(0)
  const scrollContentHeight = useSharedValue(0)
  const [dragging, setDragging] = useState(false)
  const metrics = useRef<Metrics>({ offset: 0, viewport: 0, content: 0 })
  const shownIds = useRef<ReadonlySet<string>>(new Set(messageIds))
  const flashed = useRef(false)
  const [cues, setCues] = useState({ above: false, below: false })
  useLayoutEffect(() => {
    const newest = messageIds.at(-1)
    const appended =
      shownIds.current.size > 0 && newest !== undefined && !shownIds.current.has(newest)
    shownIds.current = new Set(messageIds)
    // Reveal the committed own append even when a simultaneous removal keeps content size equal.
    if (appended && newestIsPersons) {
      scrollRef.current?.scrollToEnd({ animated: false })
    }
  }, [messageIds, newestIsPersons])
  const measure = (next: Partial<Metrics>) => {
    const m = Object.assign(metrics.current, next)
    const above = m.offset > 1
    const below = m.offset + m.viewport < m.content - 1
    setCues((prev) => (prev.above === above && prev.below === below ? prev : { above, below }))
    if (!flashed.current && m.viewport > 0 && m.content > m.viewport + 1) {
      flashed.current = true
      if (Platform.OS === 'ios') {
        scrollRef.current?.flashScrollIndicators()
      }
    }
  }
  // The nearest scroll that shows the whole row, as desktop's editor does, clear of the edge fades.
  const reveal = (row: LayoutRectangle) => {
    const { offset, viewport, content } = metrics.current
    if (viewport === 0) {
      return
    }
    const rowBottom = row.y + row.height
    // Never past the content's end, which may not have caught up with a row that just grew.
    const top = Math.max(0, row.y - spacing.lg)
    const bottom = Math.min(Math.max(content, rowBottom), rowBottom + spacing.lg)
    if (row.height > viewport || top < offset) {
      scrollRef.current?.scrollTo({ y: top, animated: false })
    } else if (bottom > offset + viewport) {
      scrollRef.current?.scrollTo({ y: bottom - viewport, animated: false })
    }
  }
  return (
    <View style={inlineEditorActive && styles.inline}>
      <Animated.ScrollView
        ref={scrollRef}
        style={inlineEditorActive ? styles.inline : styles.scroll}
        scrollEnabled={!dragging}
        nestedScrollEnabled
        showsVerticalScrollIndicator
        persistentScrollbar
        keyboardShouldPersistTaps="handled"
        scrollEventThrottle={32}
        onScroll={(event) => {
          scrollOffsetY.value = event.nativeEvent.contentOffset.y
          measure({ offset: event.nativeEvent.contentOffset.y })
        }}
        onLayout={(event) => measure({ viewport: event.nativeEvent.layout.height })}
        onContentSizeChange={(_width, height) => {
          scrollContentHeight.value = height
          measure({ content: height })
        }}
      >
        {children(reveal, {
          scrollRef,
          scrollOffsetY,
          scrollContentHeight,
          onDragActiveChange: setDragging
        })}
      </Animated.ScrollView>
      {cues.above ? <ListEdgeFade edge="top" /> : null}
      {cues.below ? <ListEdgeFade edge="bottom" /> : null}
    </View>
  )
}

/** Fades the cards into the box's fill at an edge more content lies past. */
function ListEdgeFade({ edge }: { edge: 'top' | 'bottom' }): React.JSX.Element {
  useMobileColorScheme()
  const id = `queued-list-fade-${edge}`
  const top = edge === 'top'
  return (
    <View pointerEvents="none" style={[styles.fade, top ? styles.fadeTop : styles.fadeBottom]}>
      <Svg width="100%" height="100%">
        <Defs>
          <LinearGradient id={id} x1="0" y1="0" x2="0" y2="1">
            <Stop offset="0" stopColor={colors.bgPanel} stopOpacity={top ? 1 : 0} />
            <Stop offset="1" stopColor={colors.bgPanel} stopOpacity={top ? 0 : 1} />
          </LinearGradient>
        </Defs>
        <Rect width="100%" height="100%" fill={`url(#${id})`} />
      </Svg>
    </View>
  )
}

const styles = StyleSheet.create({
  scroll: { maxHeight: QUEUE_LIST_MAX_HEIGHT },
  inline: { flexShrink: 1, minHeight: 0 },
  fade: { position: 'absolute', left: 0, right: 0, height: spacing.lg },
  fadeTop: { top: 0 },
  fadeBottom: { bottom: 0 }
})