diff --git a/apps/android/src/ui/chat-image.tsx b/apps/android/src/ui/chat-image.tsx new file mode 100644 index 0000000..49d0f5a --- /dev/null +++ b/apps/android/src/ui/chat-image.tsx @@ -0,0 +1,70 @@ +import { createContext, useContext, useEffect, useMemo, useState } from 'react' +import { ActivityIndicator, Image, Modal, Pressable, StyleSheet, Text, View } from 'react-native' +import { strings } from '../locales/i18n' +import type { ChatImage } from '../types' +import type { ChatImageLoader } from '../services/chat-image-loader' +import { validatedImageDataUri } from '../services/chat-image-loader' +import { useTheme } from './theme-context' + +const imageLoaderContext = createContext(undefined) +export const ChatImageProvider = imageLoaderContext.Provider + +/** Host-backed images are memory-only. A new scope immediately hides old pixels and aborts reads. */ +export function ChatImageView({ image, remoteSource = false }: { image: ChatImage; remoteSource?: boolean }) { + const load = useContext(imageLoaderContext) + const { colors } = useTheme() + const [attempt, setAttempt] = useState(0) + const [expanded, setExpanded] = useState(false) + const source = useMemo(() => image, [image.sessionId, image.attachmentId, image.uri, image.name]) + const [result, setResult] = useState<{ source: ChatImage; load: typeof load; uri?: string; failed?: boolean }>() + const current = result?.source === source && result.load === load ? result : undefined + const label = image.name || strings.chat.unnamedImage + useEffect(() => { + const controller = new AbortController() + setResult(undefined) + setExpanded(false) + const resolve = async () => { + // Only optimistic picker images can use a device-local URI. Markdown never can. + if (!remoteSource && /^(?:file|content):/u.test(image.uri ?? '')) return image.uri! + if (load !== undefined) return load(image, controller.signal) + if (image.uri?.startsWith('data:')) return validatedImageDataUri(image.uri) + throw new Error('Image unavailable') + } + void resolve().then(uri => { + if (!controller.signal.aborted) setResult({ source, load, uri }) + }).catch(() => { + if (!controller.signal.aborted) setResult({ source, load, failed: true }) + }) + return () => controller.abort() + }, [source, load, attempt, remoteSource]) + const fail = () => setResult({ source, load, failed: true }) + return + current?.uri ? setExpanded(true) : current?.failed && setAttempt(value => value + 1)} style={styles.frame}> + {current?.uri + ? + : + {current?.failed ? {strings.common.unavailable} · {strings.common.retry} + : } + {label} + } + + setExpanded(false)}> + + setExpanded(false)} style={styles.close}> + {strings.common.close} + + {current?.uri && } + + + +} + +const styles = StyleSheet.create({ + frame: { width: 220, height: 160, borderRadius: 8, overflow: 'hidden' }, + placeholder: { flex: 1, alignItems: 'center', justifyContent: 'center', padding: 12, gap: 8 }, + backdrop: { flex: 1, backgroundColor: '#000', paddingTop: 52, paddingBottom: 24 }, + close: { alignSelf: 'flex-end', padding: 16 }, + closeText: { color: '#fff', fontSize: 16 }, + full: { flex: 1, width: '100%' }, +})