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.

7.1 KB
Raw Blame
import { useState } from 'react'
import { ActivityIndicator, Pressable, Text, View } from 'react-native'
import { ArrowRight, ExternalLink, Pencil } from 'lucide-react-native'
import { colors } from '../../theme/mobile-theme'
import { useMobileColorScheme } from '../../theme/use-mobile-color-scheme'
import { useMobileTranslation } from '../../i18n/use-mobile-translation'
import type { PRInfo } from '../../../../src/shared/github/pull-request-types'
import type { GitHubWorkItemDetails } from '../../../../src/shared/github/work-item-types'
import type { MobilePrTitleAction } from '../../session/use-mobile-pr-title-action'
import { prStateBadge } from './pr-checks-presentation'
import { statusColor } from './pr-sidebar-status-color'
import { canEditPRTitle } from '../../session/pr-title-edit'
import { openMobilePrUrl } from '../mobile-pr-url'
import { mobilePrSidebarStyles as styles } from './mobile-pr-sidebar-styles'
import { prTitleEditStyles as editStyles } from './pr-title-edit-styles'
import { TextField } from '../composer-input/TextField'

type Props = {
  pr: PRInfo
  details: GitHubWorkItemDetails | null
  // Inline title-edit action; the pencil affordance only shows when the PR is editable.
  titleAction: MobilePrTitleAction
  // Hub chrome already surfaces open-on-web; hide the duplicate icon in that case.
  showOpenOnWeb?: boolean
  // When true, render without section chrome so identity can share a card with actions.
  bare?: boolean
}

// Keep state/#/author on one meta row, then title and head→base; print number only once.
export function PRSidebarHeader({
  pr,
  details,
  titleAction,
  showOpenOnWeb = true,
  bare = false
}: Props) {
  useMobileColorScheme()
  const t = useMobileTranslation()
  const item = details?.item
  const badge = prStateBadge(pr.state)
  const badgeColor = statusColor(badge.token)
  const title = item?.title ?? pr.title
  const author = item?.author ?? null
  const baseRef = item?.baseRefName ?? null
  const headRef = item?.branchName ?? null
  const editable = canEditPRTitle(pr.state)
  const openPr = pr.url ? () => openMobilePrUrl(pr.url) : undefined

  const body = (
    <>
      <View style={styles.metaRow}>
        <View style={styles.metaLeft}>
          <Pressable
            onPress={openPr}
            disabled={!openPr}
            accessibilityRole="link"
            accessibilityLabel={t(
              'pullRequest.prSidebarHeader.openOnWeb',
              'Open pull request #{{number}} on the web',
              { number: pr.number }
            )}
            style={({ pressed }) => [
              styles.badge,
              { borderColor: badgeColor },
              pressed && { opacity: 0.6 }
            ]}
          >
            <Text style={[styles.badgeText, { color: badgeColor }]}>{badge.label}</Text>
          </Pressable>
          <Text
            style={styles.prMetaStrong}
            onPress={openPr}
            accessibilityRole="link"
            accessibilityLabel={t(
              'pullRequest.prSidebarHeader.openOnWeb',
              'Open pull request #{{number}} on the web',
              { number: pr.number }
            )}
          >
            #{pr.number}
          </Text>
          {author ? <Text style={styles.prMeta}>· {author}</Text> : null}
        </View>
        {showOpenOnWeb && openPr ? (
          <Pressable
            onPress={openPr}
            hitSlop={8}
            accessibilityRole="link"
            accessibilityLabel={t(
              'pullRequest.prSidebarHeader.openInBrowser',
              'Open pull request #{{number}} in browser',
              { number: pr.number }
            )}
            style={({ pressed }) => [styles.iconButton, pressed && { opacity: 0.6 }]}
          >
            <ExternalLink size={16} color={colors.textSecondary} strokeWidth={2.2} />
          </Pressable>
        ) : null}
      </View>
      <PRTitle title={title} editable={editable} titleAction={titleAction} />
      {baseRef && headRef ? (
        <View style={styles.branchRow}>
          <Text style={styles.branchPill} numberOfLines={1}>
            {headRef}
          </Text>
          <ArrowRight size={12} color={colors.textSecondary} strokeWidth={2.2} />
          <Text style={styles.branchPill} numberOfLines={1}>
            {baseRef}
          </Text>
        </View>
      ) : null}
    </>
  )

  if (bare) {
    return <View style={styles.identityBlock}>{body}</View>
  }
  return (
    <View style={styles.section}>
      <View style={styles.sectionBody}>{body}</View>
    </View>
  )
}

function PRTitle({
  title,
  editable,
  titleAction
}: {
  title: string
  editable: boolean
  titleAction: MobilePrTitleAction
}) {
  useMobileColorScheme()
  const t = useMobileTranslation()
  const [editing, setEditing] = useState(false)
  const [draft, setDraft] = useState(title)

  const startEdit = () => {
    titleAction.clearError()
    setDraft(title)
    setEditing(true)
  }
  const cancel = () => {
    titleAction.clearError()
    setEditing(false)
  }
  const save = async () => {
    // setTitle handles trim/empty/unchanged/refetch; collapse editor on successful result.
    const ok = await titleAction.setTitle(draft, title)
    if (ok) {
      setEditing(false)
    }
  }

  if (editing) {
    return (
      <View style={editStyles.container}>
        <TextField value={draft} onChangeText={setDraft} editable={!titleAction.saving} autoFocus />
        {titleAction.error ? <Text style={editStyles.error}>{titleAction.error}</Text> : null}
        <View style={editStyles.actions}>
          <Pressable
            style={({ pressed }) => [editStyles.cancel, pressed && editStyles.pressed]}
            onPress={cancel}
            disabled={titleAction.saving}
            accessibilityRole="button"
            accessibilityLabel={t(
              'pullRequest.prSidebarHeader.cancelEditingTitle',
              'Cancel editing title'
            )}
          >
            <Text style={editStyles.cancelText}>{t('common.cancel', 'Cancel')}</Text>
          </Pressable>
          <Pressable
            style={({ pressed }) => [editStyles.submit, pressed && editStyles.pressed]}
            onPress={() => void save()}
            disabled={titleAction.saving}
            accessibilityRole="button"
            accessibilityLabel={t('pullRequest.prSidebarHeader.saveTitle', 'Save title')}
          >
            {titleAction.saving ? (
              <ActivityIndicator size="small" color={colors.bgBase} />
            ) : (
              <Text style={editStyles.submitText}>{t('common.save', 'Save')}</Text>
            )}
          </Pressable>
        </View>
      </View>
    )
  }

  return (
    <Pressable
      style={styles.titleRow}
      onPress={editable ? startEdit : undefined}
      disabled={!editable}
      accessibilityRole={editable ? 'button' : undefined}
      accessibilityLabel={
        editable ? t('pullRequest.prSidebarHeader.editTitle', 'Edit pull request title') : undefined
      }
    >
      <Text style={styles.prTitle}>{title}</Text>
      {editable ? (
        <View style={styles.titleEditButton}>
          <Pencil size={14} color={colors.textSecondary} strokeWidth={2} />
        </View>
      ) : null}
    </Pressable>
  )
}