From 66356d511c114c58f8f561c65954eb0a215b1c1d Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:13:28 +0800 Subject: [PATCH] Import apps/android/src/screens/bot/reply-view.tsx --- apps/android/src/screens/bot/reply-view.tsx | 212 ++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 apps/android/src/screens/bot/reply-view.tsx diff --git a/apps/android/src/screens/bot/reply-view.tsx b/apps/android/src/screens/bot/reply-view.tsx new file mode 100644 index 0000000..1062b9f --- /dev/null +++ b/apps/android/src/screens/bot/reply-view.tsx @@ -0,0 +1,212 @@ +import { useState } from 'react' +import * as Clipboard from 'expo-clipboard' +import { Check, Copy } from 'lucide-react-native' +import { StyleSheet, Text, View, Pressable } from 'react-native' +import { strings as s } from '../../locales/i18n' +import { text, type BotObject } from '../../services/bot-catalog' +import { NativeMarkdown } from '../../ui/markdown' +import { radius, spacing, type } from '../../ui/theme' +import { useTheme, type ThemeColors } from '../../ui/theme-context' +import { useThemedStyles } from '../../ui/use-themed-styles' +import { stackBotReplies } from './replies' +import { botMentionSpans } from './mentions' +import type { BotEntry } from '../../services/bot-catalog' +import { BotAvatar } from './avatar' +import { botMessageClock } from './clock' +import { createBotStyles } from './styles' + +function formatRelayEvent(reply: BotObject, speakerName: string): { isEvent: boolean; isError?: boolean; body: string } { + const kind = text(reply.kind) + if (!kind || kind === 'user') { + return { isEvent: false, body: text(reply.text) } + } + if (kind === 'failed') { + const reason = text(reply.text) || text(reply.code) || s.bots.unknownOutcome + return { + isEvent: true, + isError: true, + body: s.bots.relayFailed(speakerName, reason), + } + } + if (kind === 'folder') return { isEvent: true, body: text(reply.text) } + const label = (s.bots.relayEvents as Record)[kind] + return { isEvent: true, body: label ? `${speakerName} ยท ${label}` : '' } +} + +export function BotViaPill({ platform }: { platform?: string }) { + const localStyles = useThemedStyles(createReplyStyles) + if (!platform) return null + return ( + + {s.bots.viaPlatform(platform)} + + ) +} + +type AvatarBot = { id: string; avatar?: { shape?: string; image?: string }; color?: string; engine?: string; model?: string; appliedModel?: string; role?: string } + +/** What a mention needs: the roster to resolve names against and where a tap goes. */ +export interface BotMentionContext { bots: BotEntry[]; selfId?: string; onPress: (botId: string) => void } + +export function BotReplies({ rows, botOf, mentions, adminId }: { + rows: BotObject[]; botOf?: (botId: string) => AvatarBot | undefined; mentions?: BotMentionContext + /** The room's admin, badged as on the Host's own chat. */ + adminId?: string +}) { + const styles = useThemedStyles(createBotStyles) + const localStyles = useThemedStyles(createReplyStyles) + return ( + + {stackBotReplies(rows).map(({ reply, texts }, i) => { + const speakerName = text(reply.name) || (reply.kind === 'user' ? s.bots.yourAnswer : s.bots.title) + const via = text(reply.via) + const speakerId = text(reply.botId) + const role = reply.kind === 'user' || speakerId === '' ? '' + : speakerId === adminId ? 'Admin' : (botOf?.(speakerId)?.role ?? '') + const formatted = formatRelayEvent(reply, speakerName) + if (formatted.isEvent) { + if (!formatted.body) return null + return ( + + + {formatted.body} + + + ) + } + return ( + + {reply.kind !== 'user' && ( + + + + )} + + {reply.kind !== 'user' && + {speakerName} + {role !== '' && {role}} + + {typeof reply.time === 'number' && {botMessageClock(reply.time)}} + {typeof reply.duration === 'number' && reply.duration > 0 && ( + {Math.max(1, Math.round(reply.duration / 1000))}s + )} + {via !== '' && } + } + + {texts.map((body, index) => )} + + + + {reply.kind === 'user' && via !== '' && } + + + + ) + })} + + ) +} + +/** + * A member's line, with each `@Name` drawn like the web chat: the mentioned member's + * character followed by their name, inline with the sentence. + */ +function MentionBody({ body, mentions }: { body: string; mentions?: BotMentionContext }) { + const styles = useThemedStyles(createBotStyles) + const localStyles = useThemedStyles(createReplyStyles) + const spans = mentions ? botMentionSpans(body, mentions.bots, mentions.selfId) : [] + if (mentions === undefined || spans.length === 0) return + const runs: React.ReactNode[] = [] + let cursor = 0 + spans.forEach((span, index) => { + if (span.start > cursor) runs.push({body.slice(cursor, span.start)}) + runs.push( + mentions.onPress(span.bot.id)} + accessibilityRole="link" + accessibilityLabel={`@${span.bot.name}`} + > + + + {span.bot.name} + + , + ) + cursor = span.end + }) + if (cursor < body.length) runs.push({body.slice(cursor)}) + return {runs} +} + +const createReplyStyles = (colors: ThemeColors) => + StyleSheet.create({ + userBubble: { + alignSelf: 'flex-end', maxWidth: '90%', padding: spacing.md, + borderRadius: radius.lg, backgroundColor: colors.surfaceStrong, + }, + memberRow: { flexDirection: 'row', alignItems: 'flex-start', gap: spacing.sm }, + memberAvatar: { width: 26, height: 26, flexShrink: 0 }, + mentionFlow: { flexDirection: 'row', flexWrap: 'wrap', alignItems: 'center', columnGap: 2, rowGap: 2 }, + mention: { flexDirection: 'row', alignItems: 'center', gap: 3, paddingHorizontal: 1 }, + mentionName: { color: colors.warning, fontWeight: '600' }, + memberBody: { flexShrink: 1, flexGrow: 1, gap: spacing.xs }, + memberBubble: { + paddingHorizontal: spacing.md, paddingVertical: spacing.sm, + borderRadius: radius.lg, backgroundColor: colors.surfaceStrong, + }, + userRow: { alignItems: 'flex-end' }, + footerRow: { flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-end', gap: spacing.xs }, + headerRow: { + flexDirection: 'row', + alignItems: 'center', + gap: spacing.xs, + }, + headerSpacer: { flexGrow: 1 }, + roleBadge: { + paddingHorizontal: 6, paddingVertical: 1, borderRadius: radius.sm, + backgroundColor: colors.surfaceStrong, + }, + roleBadgeText: { fontSize: 11, fontWeight: '600', color: colors.muted }, + viaPill: { + paddingHorizontal: 7, + paddingVertical: 2, + borderRadius: radius.pill, + backgroundColor: colors.surfaceStrong, + }, + viaPillText: { + ...type.caption, + fontSize: 11, + color: colors.muted, + }, + eventRow: { + paddingHorizontal: spacing.md, + paddingVertical: 8, + borderRadius: radius.lg, + backgroundColor: colors.surfaceStrong, + }, + eventRowError: { + backgroundColor: colors.dangerSoft, + }, + eventText: { + ...type.small, + color: colors.muted, + }, + eventTextError: { + color: colors.danger, + }, + }) + +function ReplyCopy({ text }: { text: string }) { + const { colors } = useTheme() + const [copied, setCopied] = useState(false) + return { await Clipboard.setStringAsync(text); setCopied(true) }}> + {copied ? : } + +}