import { useRef, useState } from 'react' import { ActivityIndicator, Pressable, Text, View } from 'react-native' import { ChevronDown, Keyboard as KeyboardIcon, RefreshCw } from 'lucide-react-native' import { MobileRichMarkdownEditor, type MobileRichMarkdownEditorHandle } from '../components/MobileRichMarkdownEditor' import { colors, spacing } from '../theme/mobile-theme' import { resolveMarkdownFloatingActionsBottom, shouldShowMarkdownFloatingActions } from './markdown-floating-actions-layout' import type { MarkdownDocState } from './mobile-session-route-types' import { styles } from './mobile-session-styles' type Props = { documentId: string doc: MarkdownDocState | undefined onRefresh: () => void onChange: (content: string) => void onSave: () => void onCopy: () => void onDiscard: () => void keyboardLift: number } export function MobileMarkdownReader({ documentId, doc, onRefresh, onChange, onSave, onCopy, onDiscard, keyboardLift }: Props) { const editorRef = useRef(null) // Native Keyboard events under-report the WebView editor's covered area, so prefer the larger WebView-measured inset. const [webviewKeyboardInset, setWebviewKeyboardInset] = useState(0) const effectiveKeyboardLift = Math.max(keyboardLift, webviewKeyboardInset) const keyboardOpen = effectiveKeyboardLift > 0 if (!doc || doc.status === 'loading') { return ( ) } if (doc.status === 'error') { return ( {doc.message} Retry ) } const statusText = doc.saveError ? doc.saveError : doc.readOnlyReason ? 'Read only' : doc.stale ? 'Changed on desktop' : null const showRefresh = Boolean((doc.stale && !doc.isDirty) || !doc.editable) const showCopy = Boolean(doc.saveError || !doc.editable) const showSave = Boolean(doc.isDirty || doc.saving) const showFloatingActions = shouldShowMarkdownFloatingActions({ keyboardLift: effectiveKeyboardLift, hasStatus: statusText != null, showRefresh, showCopy, showSave }) return ( {showFloatingActions ? ( {statusText ? ( {statusText} ) : null} {keyboardOpen ? ( editorRef.current?.dismissKeyboard()} hitSlop={8} accessibilityRole="button" accessibilityLabel="Dismiss keyboard" accessibilityHint="Hides the software keyboard and keeps the markdown editor open." > ) : null} {showCopy ? ( Copy ) : null} {showRefresh ? ( Refresh ) : null} {doc.isDirty ? ( Discard ) : null} {showSave ? ( {doc.saving ? ( ) : ( Save )} ) : null} ) : null} ) }