import React, { useMemo } from "react"; import Markdown from "react-native-markdown-display"; import type { OperoChatBubbleProps } from "../types"; import { View, Text, StyleSheet } from "react-native"; import { OperoSourcesDisplay } from "./OperoSourcesDisplay"; import { OperoMessageActions } from "./OperoMessageActions"; import { CopyableSection } from "./CopyableSection"; import { MarkdownRenderer } from "../utils/markdownRenderer"; import { mergeStyles } from "../utils/helpers"; export const OperoChatBubble: React.FC = ({ message, customStyles, onReaction, }) => { const isUser = message.role === "user"; const styles = createStyles(mergeStyles(customStyles)); const mergedStyles = mergeStyles(customStyles); // Parse markdown into sections with headings (memoized for performance) const sections = useMemo( () => MarkdownRenderer.parseIntoSections(message.content), [message.content], ); const hasSections = sections.length > 0; // Custom renderers for markdown with copy buttons (memoized for performance) const markdownRules = useMemo(() => { if (hasSections) { return {}; // No custom rules needed when rendering sections } return { // Code blocks with backticks (```) fence: (node: any) => { const content = node.content || ""; return ( {content} ); }, // Code blocks (indented) code_block: (node: any) => { const content = node.content || ""; return ( {content} ); }, // Tables table: (node: any, children: any) => { const tableContent = MarkdownRenderer.extractTableContent(node); return ( {children} ); }, }; }, [hasSections, mergedStyles]); return ( {isUser ? ( {message.content} ) : hasSections ? ( <> {/* Render sections with copy buttons */} {sections.map((section, index) => ( {section.content} ))} {/* Sources as citations */} {message.sources && message.sources.length > 0 && ( )} ) : ( <> {message.content} {/* Sources as citations */} {message.sources && message.sources.length > 0 && ( )} )} {/* Action buttons for assistant messages */} {!isUser && !message.isStreaming && ( { if (message.message_id && onReaction) { onReaction(message.message_id, reactionType); } }} customStyles={customStyles} /> )} ); }; const createStyles = (customStyles: any) => StyleSheet.create({ container: { marginVertical: 4, paddingHorizontal: 16, }, userContainer: { alignItems: "flex-end", }, assistantContainer: { alignItems: "flex-start", }, bubble: { maxWidth: "80%", padding: 12, borderRadius: customStyles?.chatBubbleRadius || 12, }, userBubble: { backgroundColor: customStyles?.userBubbleColor || "#000000", }, assistantBubble: { backgroundColor: customStyles?.assistantBubbleColor || "#f3f4f6", }, userText: { color: "#ffffff", fontSize: 16, lineHeight: 20, }, actionButtons: { flexDirection: "row", justifyContent: "space-between", alignItems: "center", marginTop: 8, paddingHorizontal: 4, }, leftButtons: { flexDirection: "row", gap: 8, }, actionButton: { flexDirection: "row", alignItems: "center", paddingVertical: 4, paddingHorizontal: 8, backgroundColor: "#f9fafb", borderRadius: 8, borderWidth: 1, borderColor: "#e5e7eb", gap: 4, }, actionButtonText: { fontSize: 12, color: "#6b7280", fontWeight: "500", }, }); const markdownStyles = StyleSheet.create({ body: { color: "#1f2937", fontSize: 16, lineHeight: 20, }, heading1: { fontSize: 20, fontWeight: "bold", color: "#1f2937", marginVertical: 4, }, heading2: { fontSize: 18, fontWeight: "bold", color: "#1f2937", marginVertical: 4, }, heading3: { fontSize: 16, fontWeight: "bold", color: "#1f2937", marginVertical: 4, }, paragraph: { marginVertical: 2, }, strong: { fontWeight: "bold", }, em: { fontStyle: "italic", }, code_inline: { backgroundColor: "rgba(0, 0, 0, 0.1)", paddingHorizontal: 4, paddingVertical: 2, borderRadius: 4, fontFamily: "monospace", fontSize: 14, }, code_block: { backgroundColor: "rgba(0, 0, 0, 0.1)", padding: 8, borderRadius: 4, marginVertical: 4, }, bullet_list: { marginVertical: 2, }, ordered_list: { marginVertical: 2, }, list_item: { marginVertical: 1, }, blockquote: { borderLeftWidth: 3, borderLeftColor: "#e5e7eb", paddingLeft: 8, marginVertical: 4, fontStyle: "italic", color: "#6b7280", }, hr: { backgroundColor: "#e5e7eb", height: 1, marginVertical: 8, }, link: { color: "#3b82f6", textDecorationLine: "underline", }, }); const codeBlockStyles = StyleSheet.create({ text: { fontFamily: "monospace", fontSize: 14, color: "#1f2937", }, });