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.

9.7 KB
Raw Blame
import { useMemo, useState } from 'react'
import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native'
import { openExternalLink } from '../../platform/external-link'
import { ChevronDown, ChevronRight } from 'lucide-react-native'
import { colors, radii, spacing, typography } from '../../theme/mobile-theme'
import { themedStyleSheet } from '../../theme/themed-styles'
import { useMobileColorScheme } from '../../theme/use-mobile-color-scheme'
import { MermaidDiagram } from './MermaidDiagram'
import { isAllowedMarkdownLinkUrl } from './markdown-link-scheme'
import {
  parseInline,
  parseMarkdownBlocks,
  type CellAlign,
  type InlineToken,
  type MarkdownBlock
} from './markdown-blocks'

type Props = {
  content: string
  // PR body uses a slightly larger base than inline comment cards (mirrors desktop).
  variant?: 'document' | 'comment'
}

// Use small themed block parser with plain-text fallback: prior third-party renderer hung JS thread on mount; parse failure must not crash comment list.
export function CommentMarkdown({ content, variant = 'comment' }: Props) {
  useMobileColorScheme()
  const base = variant === 'document' ? typography.bodySize : 13
  const blocks = useMemo<MarkdownBlock[] | null>(() => {
    try {
      return parseMarkdownBlocks(content)
    } catch {
      return null
    }
  }, [content])

  if (!blocks) {
    return (
      <Text style={[styles.paragraph, { fontSize: base, lineHeight: base + 7 }]}>{content}</Text>
    )
  }

  return (
    <View>
      {blocks.map((block, index) => (
        <BlockView key={index} block={block} base={base} />
      ))}
    </View>
  )
}

function DetailsBlock({
  summary,
  body,
  base
}: {
  summary: string
  body: MarkdownBlock[]
  base: number
}) {
  useMobileColorScheme()
  const [open, setOpen] = useState(false)
  const Chevron = open ? ChevronDown : ChevronRight
  return (
    <View style={styles.details}>
      <Pressable
        style={styles.detailsSummary}
        onPress={() => setOpen((v) => !v)}
        accessibilityRole="button"
      >
        <Chevron size={14} color={colors.textSecondary} strokeWidth={2.2} />
        <Text style={[styles.detailsSummaryText, { fontSize: base }]}>{summary}</Text>
      </Pressable>
      {open ? (
        <View style={styles.detailsBody}>
          {body.map((b, i) => (
            <BlockView key={i} block={b} base={base} />
          ))}
        </View>
      ) : null}
    </View>
  )
}

function BlockView({ block, base }: { block: MarkdownBlock; base: number }) {
  useMobileColorScheme()
  switch (block.kind) {
    case 'details':
      return <DetailsBlock summary={block.summary} body={block.body} base={base} />
    case 'heading':
      return (
        <Text style={[styles.heading, { fontSize: base + Math.max(0, 4 - block.level) }]}>
          <Inline text={block.text} base={base} />
        </Text>
      )
    case 'code':
      // Mermaid fences render as diagrams (WebView), not as raw code.
      if (block.lang === 'mermaid') {
        return <MermaidDiagram source={block.text} base={base} />
      }
      return (
        <View style={styles.codeBlock}>
          <Text style={[styles.codeText, { fontSize: base - 1 }]}>{block.text}</Text>
        </View>
      )
    case 'table':
      return <TableBlock block={block} base={base} />
    case 'quote':
      return (
        <View style={styles.quote}>
          <Text style={[styles.paragraph, { fontSize: base, lineHeight: base + 7 }]}>
            <Inline text={block.text} base={base} />
          </Text>
        </View>
      )
    case 'hr':
      return <View style={styles.hr} />
    case 'list':
      return (
        <View style={styles.list}>
          {block.items.map((item, i) => (
            <View key={i} style={styles.listItem}>
              <Text style={[styles.bullet, { fontSize: base }]}>
                {block.ordered ? `${i + 1}.` : '•'}
              </Text>
              <Text
                style={[
                  styles.paragraph,
                  styles.listItemText,
                  { fontSize: base, lineHeight: base + 7 }
                ]}
              >
                <Inline text={item} base={base} />
              </Text>
            </View>
          ))}
        </View>
      )
    case 'paragraph':
      return (
        <Text style={[styles.paragraph, { fontSize: base, lineHeight: base + 7 }]}>
          <Inline text={block.text} base={base} />
        </Text>
      )
  }
}

function openMarkdownLink(url: string): void {
  if (!isAllowedMarkdownLinkUrl(url)) {
    return
  }
  openExternalLink(url)
}

function alignToFlex(align: CellAlign | undefined): 'flex-start' | 'center' | 'flex-end' {
  if (align === 'center') {
    return 'center'
  }
  if (align === 'right') {
    return 'flex-end'
  }
  return 'flex-start'
}

// Keep wide GFM tables in horizontal ScrollView with fixed-width columns so sidebar layout stays intact.
function TableBlock({
  block,
  base
}: {
  block: Extract<MarkdownBlock, { kind: 'table' }>
  base: number
}) {
  useMobileColorScheme()
  const columnCount = Math.max(block.headers.length, ...block.rows.map((r) => r.length), 1)
  const columns = Array.from({ length: columnCount }, (_, c) => c)
  return (
    <ScrollView
      horizontal
      showsHorizontalScrollIndicator={false}
      style={styles.tableScroll}
      contentContainerStyle={styles.table}
    >
      <View>
        <View style={[styles.tableRow, styles.tableHeaderRow]}>
          {columns.map((c) => (
            <View key={c} style={[styles.tableCell, { alignItems: alignToFlex(block.align[c]) }]}>
              <Text style={[styles.tableHeaderText, { fontSize: base - 1 }]}>
                <Inline text={block.headers[c] ?? ''} base={base} />
              </Text>
            </View>
          ))}
        </View>
        {block.rows.map((row, r) => (
          <View key={r} style={styles.tableRow}>
            {columns.map((c) => (
              <View key={c} style={[styles.tableCell, { alignItems: alignToFlex(block.align[c]) }]}>
                <Text style={[styles.tableCellText, { fontSize: base - 1 }]}>
                  <Inline text={row[c] ?? ''} base={base} />
                </Text>
              </View>
            ))}
          </View>
        ))}
      </View>
    </ScrollView>
  )
}

function Inline({ text, base }: { text: string; base: number }) {
  useMobileColorScheme()
  const tokens = useMemo<InlineToken[]>(() => {
    try {
      return parseInline(text)
    } catch {
      return [{ kind: 'text', text }]
    }
  }, [text])
  return (
    <>
      {tokens.map((token, i) => {
        if (token.kind === 'bold') {
          return (
            <Text key={i} style={styles.bold}>
              {token.text}
            </Text>
          )
        }
        if (token.kind === 'italic') {
          return (
            <Text key={i} style={styles.italic}>
              {token.text}
            </Text>
          )
        }
        if (token.kind === 'code') {
          return (
            <Text key={i} style={[styles.codeInline, { fontSize: base - 1 }]}>
              {token.text}
            </Text>
          )
        }
        if (token.kind === 'link') {
          return (
            <Text key={i} style={styles.link} onPress={() => openMarkdownLink(token.url)}>
              {token.text}
            </Text>
          )
        }
        return <Text key={i}>{token.text}</Text>
      })}
    </>
  )
}

const styles = themedStyleSheet(() => ({
  paragraph: { color: colors.textPrimary, marginBottom: spacing.sm },
  heading: { color: colors.textPrimary, fontWeight: '700', marginBottom: spacing.xs },
  bold: { fontWeight: '700' },
  italic: { fontStyle: 'italic' },
  link: { color: colors.textPrimary, textDecorationLine: 'underline' },
  codeInline: {
    color: colors.textPrimary,
    fontFamily: typography.monoFamily,
    backgroundColor: colors.bgRaised
  },
  codeBlock: {
    backgroundColor: colors.bgRaised,
    borderWidth: StyleSheet.hairlineWidth,
    borderColor: colors.borderSubtle,
    borderRadius: radii.row,
    padding: spacing.sm,
    marginBottom: spacing.sm
  },
  codeText: { color: colors.textPrimary, fontFamily: typography.monoFamily },
  quote: {
    borderLeftWidth: 3,
    borderLeftColor: colors.borderSubtle,
    backgroundColor: colors.bgRaised,
    paddingHorizontal: spacing.sm,
    paddingVertical: spacing.xs,
    marginBottom: spacing.sm
  },
  hr: { height: 1, backgroundColor: colors.borderSubtle, marginVertical: spacing.sm },
  list: { marginBottom: spacing.sm },
  listItem: { flexDirection: 'row', gap: spacing.xs },
  listItemText: { flex: 1, marginBottom: 2 },
  bullet: { color: colors.textSecondary },
  details: {
    borderWidth: StyleSheet.hairlineWidth,
    borderColor: colors.borderSubtle,
    borderRadius: radii.row,
    marginBottom: spacing.sm,
    overflow: 'hidden'
  },
  detailsSummary: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: spacing.xs,
    paddingHorizontal: spacing.sm,
    paddingVertical: spacing.xs,
    backgroundColor: colors.bgRaised
  },
  detailsSummaryText: { color: colors.textPrimary, fontWeight: '600', flexShrink: 1 },
  detailsBody: { paddingHorizontal: spacing.sm, paddingTop: spacing.xs },
  tableScroll: { marginBottom: spacing.sm },
  table: {
    borderWidth: StyleSheet.hairlineWidth,
    borderColor: colors.borderSubtle,
    borderRadius: radii.row,
    overflow: 'hidden'
  },
  tableRow: {
    flexDirection: 'row',
    borderTopWidth: StyleSheet.hairlineWidth,
    borderTopColor: colors.borderSubtle
  },
  tableHeaderRow: { borderTopWidth: 0, backgroundColor: colors.bgRaised },
  tableCell: {
    minWidth: 96,
    paddingHorizontal: spacing.sm,
    paddingVertical: spacing.xs,
    borderLeftWidth: StyleSheet.hairlineWidth,
    borderLeftColor: colors.borderSubtle
  },
  tableHeaderText: { color: colors.textPrimary, fontWeight: '700' },
  tableCellText: { color: colors.textPrimary }
}))