import { useEffect, useState } from 'react' import type { ComponentType } from 'react' import { BackHandler, Keyboard, Pressable, ScrollView, StyleSheet, Text, useWindowDimensions, View } from 'react-native' import { radius, spacing, type } from './theme' import { useTheme, type ThemeColors } from './theme-context' import { useThemedStyles } from './use-themed-styles' export * from './mention-helpers' export interface MentionItem { id: string icon: ComponentType<{ size?: number; color?: string }> title: string description?: string /** Right-aligned trailing meta, e.g. a relative update time. */ meta?: string /** Keyboard-highlighted row (the web marks the active candidate). */ highlighted?: boolean onPress: () => void } export interface MentionGroup { key: string title: string items: MentionItem[] } /** Suggestion surface for the `/` and `@` triggers, mounted above the composer card. */ export function MentionPopover({ groups, onDismiss, emptyText }: { groups: MentionGroup[] onDismiss: () => void emptyText: string }) { const { colors } = useTheme() const styles = useThemedStyles(createStyles) // Android Back closes the popover first instead of leaving the chat screen. useEffect(() => { const subscription = BackHandler.addEventListener('hardwareBackPress', () => { onDismiss() return true }) return () => subscription.remove() }, [onDismiss]) const total = groups.reduce((sum, group) => sum + group.items.length, 0) const { width, height } = useWindowDimensions() const landscape = width > height const [keyboardTop, setKeyboardTop] = useState() useEffect(() => { const shown = Keyboard.addListener('keyboardDidShow', event => setKeyboardTop(event.endCoordinates.screenY)) const hidden = Keyboard.addListener('keyboardDidHide', () => setKeyboardTop(undefined)) setKeyboardTop(Keyboard.metrics()?.screenY) return () => { shown.remove(); hidden.remove() } }, []) // Rotation moves the keyboard's top edge without necessarily emitting a new // show event; re-read the live metrics so the popover does not keep the // previous orientation's screenY. useEffect(() => { setKeyboardTop(Keyboard.isVisible() ? Keyboard.metrics()?.screenY : undefined) }, [width, height]) const availableHeight = Math.min(height, keyboardTop ?? height) const maxHeight = Math.max(80, Math.min(240, Math.round(availableHeight * (landscape ? 0.45 : 0.32)))) return ( {total === 0 ? {emptyText} : groups.map(group => group.items.length === 0 ? null : ( {group.title} {group.items.map(item => { const Icon = item.icon return ( [styles.row, item.highlighted === true && styles.rowActive, pressed && styles.rowActive]} > {item.title} {item.meta !== undefined && {item.meta}} {item.description !== undefined && ( {item.description} )} ) })} ))} ) } function createStyles(colors: ThemeColors) { return StyleSheet.create({ popover: { // Keep the native hit rectangle inside its parent. An absolute child // above the short composer is visible but not touchable on Android. position: 'relative', marginBottom: spacing.xs, maxHeight: 260, borderRadius: radius.sm, borderWidth: 1, borderColor: colors.border, backgroundColor: colors.background, overflow: 'hidden', shadowColor: colors.shadow, shadowOpacity: 0.18, shadowRadius: 12, shadowOffset: { width: 0, height: 4 }, elevation: 8, }, scroll: { maxHeight: 260 }, scrollContent: { paddingBottom: spacing.xxs }, groupHeader: { backgroundColor: colors.background, paddingHorizontal: spacing.sm, paddingVertical: spacing.xxs }, groupTitle: { ...type.caption, color: colors.muted }, iconWrap: { width: 28, height: 28, borderRadius: 4, backgroundColor: colors.primarySoft, alignItems: 'center', justifyContent: 'center', flexShrink: 0, }, row: { flexDirection: 'row', alignItems: 'center', gap: spacing.sm, paddingHorizontal: spacing.sm, minHeight: 46, paddingVertical: spacing.xxs }, rowActive: { backgroundColor: colors.background, opacity: 0.8 }, rowCopy: { flex: 1, minWidth: 0 }, rowLine: { flexDirection: 'row', alignItems: 'center', gap: spacing.xs }, rowTitle: { ...type.small, color: colors.ink, flexShrink: 1 }, rowMeta: { ...type.caption, color: colors.muted, marginLeft: 'auto', flexShrink: 1 }, rowDescription: { ...type.caption, color: colors.muted, marginTop: 1 }, empty: { ...type.small, color: colors.muted, padding: spacing.md, textAlign: 'center' }, }) }