mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
* feat(mobile): keep commit, PR and review drafts across kills * refactor(mobile): build commit, PR and review drafts on the shared draft fields, and clear only the text that was sent * fix(mobile): keep a toggle made during Write with AI, the edit note while its sheet closes, and a newer PR edit after an earlier save * test(mobile): wait for the saved review note row instead of sleeping * perf(mobile): keep a hash of the PR text an edit was typed against, not a second copy
337 lines
11 KiB
TypeScript
337 lines
11 KiB
TypeScript
import { MobileFileMediaPreview } from '../MobileFileMediaPreview'
|
|
import type { MobileFilePreviewRpcSender } from '../mobile-file-preview-operations'
|
|
import { useState, useEffect, useCallback, useMemo } from 'react'
|
|
import {
|
|
FlatList,
|
|
View,
|
|
Text,
|
|
ScrollView,
|
|
Pressable,
|
|
Platform,
|
|
type ListRenderItem
|
|
} from 'react-native'
|
|
import { Copy, MessageSquare, Send } from 'lucide-react-native'
|
|
import { MobileSyntaxSegments } from '../../components/MobileSyntaxSegments'
|
|
import {
|
|
buildPlainMobileDiffSyntaxLines,
|
|
highlightMobileCode,
|
|
highlightMobileDiffLines,
|
|
resolveMobileSyntaxLanguage
|
|
} from './mobile-file-syntax'
|
|
import { MobileHtmlPreview } from '../../components/MobileHtmlPreview'
|
|
import { MobilePanZoomImage } from '../../components/MobilePanZoomImage'
|
|
import { MobileSvgPreview } from '../../components/MobileSvgPreview'
|
|
import { colors, pressedStyle } from '../../theme/mobile-theme'
|
|
import { useMobileColorScheme } from '../../theme/use-mobile-color-scheme'
|
|
import { useMobileTranslation } from '../../i18n/use-mobile-translation'
|
|
import { styles } from '../../session/mobile-session-styles'
|
|
import type { DiffComment } from '../../../../src/shared/diff-comment-types'
|
|
import type {
|
|
DiffCommentActions,
|
|
DiffSyntaxState,
|
|
RenderableDiffLine
|
|
} from '../../source-control/review/mobile-review-comment-state'
|
|
import type { FileDocState, FileSyntaxState } from './session-document-state'
|
|
import { DiffLineRow } from '../../source-control/review/MobileSessionDiffLineRow'
|
|
import { sendableMobileDiffComments } from '../../source-control/review/mobile-diff-comments'
|
|
import { MobileLoadErrorState, MobileStateView } from '../../components/MobileStateView'
|
|
import { useDurableDraftField } from '../../drafts/durable-draft-fields'
|
|
|
|
type FileReaderProps = {
|
|
doc: FileDocState | undefined
|
|
client?: MobileFilePreviewRpcSender | null
|
|
title: string
|
|
relativePath: string
|
|
documentId?: string
|
|
language?: string
|
|
diffCommentActions?: DiffCommentActions
|
|
// Where an unsaved note waits through leaving the file and the app being killed.
|
|
noteDraftKey?: string
|
|
}
|
|
|
|
export function FileReader(props: FileReaderProps) {
|
|
return (
|
|
<FileReaderDocument key={JSON.stringify([props.documentId, props.relativePath])} {...props} />
|
|
)
|
|
}
|
|
|
|
function FileReaderDocument({
|
|
doc,
|
|
client = null,
|
|
title,
|
|
relativePath,
|
|
language,
|
|
diffCommentActions,
|
|
noteDraftKey
|
|
}: FileReaderProps) {
|
|
useMobileColorScheme()
|
|
const t = useMobileTranslation()
|
|
const syntaxLanguage = useMemo(
|
|
() => resolveMobileSyntaxLanguage(relativePath || title, language),
|
|
[language, relativePath, title]
|
|
)
|
|
const [fileSyntax, setFileSyntax] = useState<FileSyntaxState | null>(null)
|
|
const [diffSyntax, setDiffSyntax] = useState<DiffSyntaxState | null>(null)
|
|
const [activeCommentLine, setActiveCommentLine] = useState<number | null>(null)
|
|
const [keptNote, setKeptNote] = useDurableDraftField(noteDraftKey ?? null, 'note')
|
|
const [localNote, setLocalNote] = useState('')
|
|
const [commentDraft, setCommentDraft] = noteDraftKey
|
|
? [keptNote, setKeptNote]
|
|
: [localNote, setLocalNote]
|
|
const plainDiffLines = useMemo(
|
|
() =>
|
|
doc?.status === 'ready' && doc.kind === 'diff'
|
|
? buildPlainMobileDiffSyntaxLines(doc.lines)
|
|
: [],
|
|
[doc]
|
|
)
|
|
const diffCommentsForFile = useMemo(
|
|
() =>
|
|
diffCommentActions?.comments.filter(
|
|
(comment) => comment.filePath === relativePath && comment.source !== 'markdown'
|
|
) ?? [],
|
|
[diffCommentActions?.comments, relativePath]
|
|
)
|
|
const diffCommentsByLine = useMemo(() => {
|
|
const map = new Map<number, DiffComment[]>()
|
|
for (const comment of diffCommentsForFile) {
|
|
const list = map.get(comment.lineNumber) ?? []
|
|
list.push(comment)
|
|
map.set(comment.lineNumber, list)
|
|
}
|
|
for (const list of map.values()) {
|
|
list.sort((a, b) => a.createdAt - b.createdAt)
|
|
}
|
|
return map
|
|
}, [diffCommentsForFile])
|
|
|
|
// The draft follows the reader to another line and survives Cancel; only a saved note clears it.
|
|
const cancelComment = useCallback(() => setActiveCommentLine(null), [])
|
|
|
|
const submitComment = useCallback(
|
|
(lineNumber: number) => {
|
|
if (!diffCommentActions) {
|
|
return
|
|
}
|
|
const note = commentDraft
|
|
void diffCommentActions.onAdd(relativePath, lineNumber, note).then((added) => {
|
|
if (added) {
|
|
setActiveCommentLine(null)
|
|
// Only the saved text; a note typed since in a reopened reader stays.
|
|
setCommentDraft((current) => (current === note ? '' : current))
|
|
}
|
|
})
|
|
},
|
|
[commentDraft, diffCommentActions, relativePath]
|
|
)
|
|
|
|
const renderDiffLine: ListRenderItem<RenderableDiffLine> = useCallback(
|
|
({ item, index }) => (
|
|
<DiffLineRow
|
|
line={item}
|
|
title={title}
|
|
index={index}
|
|
comments={
|
|
item.newLineNumber !== undefined ? (diffCommentsByLine.get(item.newLineNumber) ?? []) : []
|
|
}
|
|
activeCommentLine={activeCommentLine}
|
|
commentDraft={commentDraft}
|
|
commentsBusy={diffCommentActions?.busy === true}
|
|
onStartComment={setActiveCommentLine}
|
|
onCancelComment={cancelComment}
|
|
onDraftChange={setCommentDraft}
|
|
onSubmitComment={submitComment}
|
|
onDeleteComment={(commentId) => {
|
|
if (diffCommentActions) {
|
|
void diffCommentActions.onDelete(commentId)
|
|
}
|
|
}}
|
|
/>
|
|
),
|
|
[
|
|
activeCommentLine,
|
|
cancelComment,
|
|
commentDraft,
|
|
diffCommentActions,
|
|
diffCommentsByLine,
|
|
submitComment,
|
|
title
|
|
]
|
|
)
|
|
|
|
useEffect(() => {
|
|
if (doc?.status !== 'ready') {
|
|
return undefined
|
|
}
|
|
|
|
// Defer highlighting one tick so large files appear as plain text immediately.
|
|
const timer = setTimeout(() => {
|
|
if (doc.kind === 'file' || doc.kind === 'html' || doc.kind === 'svg') {
|
|
setFileSyntax({
|
|
doc,
|
|
language: syntaxLanguage,
|
|
segments: highlightMobileCode(doc.content, syntaxLanguage).segments
|
|
})
|
|
return
|
|
}
|
|
if (doc.kind === 'diff') {
|
|
setDiffSyntax({
|
|
doc,
|
|
language: syntaxLanguage,
|
|
lines: highlightMobileDiffLines(doc.lines, syntaxLanguage)
|
|
})
|
|
}
|
|
}, 0)
|
|
|
|
return () => clearTimeout(timer)
|
|
}, [doc, syntaxLanguage])
|
|
|
|
if (!doc || doc.status === 'loading') {
|
|
return <MobileStateView loading title={t('session.fileReader.loading', 'Loading file…')} />
|
|
}
|
|
if (doc.status === 'error') {
|
|
return <MobileLoadErrorState error={doc.message} />
|
|
}
|
|
|
|
if (doc.kind === 'diff') {
|
|
const activeDiffSyntax =
|
|
diffSyntax?.doc === doc && diffSyntax.language === syntaxLanguage ? diffSyntax.lines : null
|
|
const count = diffCommentActions?.comments.length ?? 0
|
|
const unsentCommentCount = diffCommentActions
|
|
? sendableMobileDiffComments(
|
|
diffCommentActions.comments,
|
|
diffCommentActions.sendingCommentIds
|
|
).length
|
|
: 0
|
|
const commentsBusy = diffCommentActions?.busy === true
|
|
const canCopyNotes = count > 0 && !commentsBusy
|
|
const canSendNotes = unsentCommentCount > 0 && !commentsBusy
|
|
return (
|
|
<View style={styles.markdownEditor}>
|
|
{diffCommentActions ? (
|
|
<View style={styles.diffNotesToolbar}>
|
|
<View style={styles.diffNotesTitleRow}>
|
|
<MessageSquare size={14} color={colors.textSecondary} strokeWidth={2.2} />
|
|
<Text style={styles.diffNotesTitle}>
|
|
{count === 0
|
|
? t('session.fileReader.noReviewNotes', 'No review notes')
|
|
: count === 1
|
|
? t('session.fileReader.reviewNotes_one', '{{count}} review note', { count })
|
|
: t('session.fileReader.reviewNotes_other', '{{count}} review notes', {
|
|
count
|
|
})}
|
|
</Text>
|
|
</View>
|
|
<View style={styles.diffNotesActions}>
|
|
<Pressable
|
|
style={({ pressed }) => [
|
|
styles.diffNotesActionButton,
|
|
!canCopyNotes && styles.diffCommentButtonDisabled,
|
|
pressed && pressedStyle
|
|
]}
|
|
disabled={!canCopyNotes}
|
|
onPress={() => void diffCommentActions.onCopyAll()}
|
|
accessibilityLabel={t('session.fileReader.copyReviewNotes', 'Copy review notes')}
|
|
>
|
|
<Copy size={13} color={colors.textSecondary} strokeWidth={2.2} />
|
|
<Text style={styles.diffNotesActionText}>{t('common.copy', 'Copy')}</Text>
|
|
</Pressable>
|
|
<Pressable
|
|
style={({ pressed }) => [
|
|
styles.diffNotesActionButton,
|
|
!canSendNotes && styles.diffCommentButtonDisabled,
|
|
pressed && pressedStyle
|
|
]}
|
|
disabled={!canSendNotes}
|
|
onPress={diffCommentActions.onSendAll}
|
|
accessibilityLabel={t(
|
|
'session.fileReader.sendReviewNotes',
|
|
'Send review notes to AI'
|
|
)}
|
|
>
|
|
<Send size={13} color={colors.textSecondary} strokeWidth={2.2} />
|
|
<Text style={styles.diffNotesActionText}>
|
|
{t('session.fileReader.send', 'Send')}
|
|
</Text>
|
|
</Pressable>
|
|
</View>
|
|
</View>
|
|
) : null}
|
|
<FlatList
|
|
data={activeDiffSyntax ?? plainDiffLines}
|
|
style={styles.filePreviewScroll}
|
|
contentContainerStyle={styles.filePreviewContent}
|
|
keyExtractor={(line, index) =>
|
|
`${index}:${line.kind}:${line.oldLineNumber ?? ''}:${line.newLineNumber ?? ''}`
|
|
}
|
|
renderItem={renderDiffLine}
|
|
initialNumToRender={32}
|
|
maxToRenderPerBatch={48}
|
|
windowSize={7}
|
|
removeClippedSubviews={Platform.OS !== 'web'}
|
|
keyboardShouldPersistTaps="handled"
|
|
/>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
if (doc.kind === 'media') {
|
|
return <MobileFileMediaPreview media={doc.media} client={client} title={title} />
|
|
}
|
|
if (doc.kind === 'image') {
|
|
return (
|
|
<View style={styles.imagePreviewContainer}>
|
|
<MobilePanZoomImage
|
|
uri={doc.dataUri}
|
|
label={t('session.fileReader.imageLabel', '{{title}} image', { title })}
|
|
/>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
const renderSourceText = (content: string) => (
|
|
<View style={styles.markdownEditor}>
|
|
<ScrollView
|
|
style={styles.filePreviewScroll}
|
|
contentContainerStyle={styles.filePreviewContent}
|
|
>
|
|
<Text
|
|
selectable
|
|
style={styles.filePreviewText}
|
|
accessibilityLabel={t('session.fileReader.previewLabel', '{{title}} preview', { title })}
|
|
>
|
|
<MobileSyntaxSegments
|
|
segments={
|
|
fileSyntax?.doc === doc && fileSyntax.language === syntaxLanguage
|
|
? fileSyntax.segments
|
|
: [{ text: content, kind: 'plain' }]
|
|
}
|
|
/>
|
|
</Text>
|
|
</ScrollView>
|
|
</View>
|
|
)
|
|
|
|
if (doc.kind === 'html') {
|
|
return (
|
|
<View style={styles.markdownEditor}>
|
|
<MobileHtmlPreview html={doc.content} renderSource={() => renderSourceText(doc.content)} />
|
|
</View>
|
|
)
|
|
}
|
|
|
|
if (doc.kind === 'svg' && !doc.truncated) {
|
|
return (
|
|
<View style={styles.markdownEditor}>
|
|
<MobileSvgPreview
|
|
markup={doc.content}
|
|
title={title}
|
|
renderSource={() => renderSourceText(doc.content)}
|
|
/>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
return renderSourceText(doc.content)
|
|
}
|