import { useEffect, useState } from 'react' import { ActivityIndicator, ScrollView, StyleSheet, Text, View } from 'react-native' import { Check, RefreshCw, SlidersHorizontal, X } from 'lucide-react-native' import { useSafeAreaInsets } from 'react-native-safe-area-context' import { useAppStore } from '../state/store' import { strings } from '../locales/i18n' import type { Messages } from '../locales/types' import type { ConnectionPhase, RemoteDevice } from '../types' import { ConnectionScreen } from './device-screens' import { platformIcon, switcherDeviceSubtitle } from '../lib/device-display' import { ErrorBanner, GroupCard, GroupRow, IconButton } from '../ui/components' import { useResponsiveLayout } from '../ui/responsive' import { radius, spacing, type, type ThemeColors } from '../ui/theme' import { useTheme } from '../ui/theme-context' import { useThemedStyles } from '../ui/use-themed-styles' import { SheetModal } from '../ui/sheet' export interface HostSwitcherSheetProps { visible: boolean initialConnectingDevice?: RemoteDevice | undefined onClose: () => void onSelectDevice: (device: RemoteDevice) => void onManageHosts: () => void onConnected: () => void } /** * Bottom half-sheet that only lists remote Hosts. The dismiss target and the * sheet are siblings so Android's responder negotiation cannot swallow taps * on rows or the close button. Every row is a `GroupRow`, so the icon column, * the label start, the inset separators and the right edge match the cards * used elsewhere, and the trailing status text lands on one baseline. */ export function HostSwitcherSheet({ visible, onClose, onSelectDevice, onManageHosts, onConnected, initialConnectingDevice }: HostSwitcherSheetProps) { useAppStore(state => state.language) const devices = useAppStore(state => state.devices) const selectedDevice = useAppStore(state => state.selectedDevice) const connection = useAppStore(state => state.connection) const refreshing = useAppStore(state => state.refreshing) const refreshDevices = useAppStore(state => state.refreshDevices) const error = useAppStore(state => state.error) const clearError = useAppStore(state => state.clearError) const [chosenDevice, setChosenDevice] = useState() const connectingDevice = chosenDevice ?? initialConnectingDevice const busy = connectingDevice !== undefined const dismiss = () => { setChosenDevice(undefined) onClose() } useEffect(() => { if (!visible) setChosenDevice(undefined) }, [visible]) const { colors } = useTheme() const styles = useThemedStyles(createStyles) const { sheetMaxHeight } = useResponsiveLayout() const insets = useSafeAreaInsets() // Opening the sheet quietly refreshes presence so the status dots are current. useEffect(() => { if (!visible) return if (useAppStore.getState().refreshing) return void refreshDevices() }, [visible, refreshDevices]) return ( {strings.remoteHosts.switchTitle} {refreshing ? : void refreshDevices()} />} {error !== undefined && connectingDevice === undefined && ( void refreshDevices()} onDismiss={clearError} /> )} {connectingDevice !== undefined ? ( { setChosenDevice(undefined) if (initialConnectingDevice !== undefined) onClose() }} onConnected={() => { setChosenDevice(undefined) onConnected() }} /> ) : {devices.length === 0 ? {strings.devices.emptyTitle} {strings.devices.emptyBody} : devices.map((device, index) => { const isCurrent = selectedDevice?.deviceId === device.deviceId const isConnected = isCurrent && connection.phase === 'connected' const Icon = platformIcon(device.platform) const status = hostStatus(device, isCurrent, connection.phase, colors, strings) return ( { if (isConnected) onClose() else if (device.trusted && device.online) setChosenDevice(device) else onSelectDevice(device) }} accessory={ // The 20dp check slot is reserved on every row, so the // online/offline text cannot drift between rows. {status.label} {isConnected ? : null} } /> ) })} {strings.remoteHosts.singleHostHint} } {connectingDevice === undefined && } ) } interface HostStatus { dot: string label: string } function hostStatus(device: RemoteDevice, isCurrent: boolean, phase: ConnectionPhase, colors: ThemeColors, s: Messages): HostStatus { const connected = isCurrent && phase === 'connected' const connecting = isCurrent && (phase === 'connecting' || phase === 'reconnecting') if (connected) return { dot: colors.success, label: s.status.online } if (connecting) return { dot: colors.warning, label: s.status.waiting } if (device.online) return { dot: colors.success, label: s.status.online } return { dot: colors.disabled, label: s.status.offline } } function createStyles(colors: ThemeColors) { return StyleSheet.create({ backdrop: { flex: 1, backgroundColor: colors.modalBackdrop, justifyContent: 'flex-end' }, sheet: { zIndex: 1, elevation: 1, backgroundColor: colors.background, borderTopLeftRadius: radius.xxl, borderTopRightRadius: radius.xxl, // Rows and the footer own their horizontal insets; the sheet itself has // never added any, so keep the shared surface's padding out of the way. paddingHorizontal: 0, paddingTop: spacing.sm, gap: spacing.md, }, grabber: { width: 36, height: 4, borderRadius: 2, backgroundColor: colors.subtle, opacity: 0.45, alignSelf: 'center' }, sheetHeader: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', gap: spacing.xs, paddingHorizontal: spacing.lg }, headerActions: { flexDirection: 'row', alignItems: 'center', gap: 4 }, title: { ...type.heading, fontSize: 18, fontWeight: '700', color: colors.ink, flex: 1 }, scroll: { flexGrow: 0, flexShrink: 1 }, scrollContent: { gap: spacing.md, paddingHorizontal: spacing.lg }, hint: { ...type.caption, color: colors.muted, paddingHorizontal: spacing.md }, trailingGroup: { flexDirection: 'row', alignItems: 'center', gap: spacing.xs }, statusRow: { flexDirection: 'row', alignItems: 'center', gap: 5 }, statusDot: { width: 6, height: 6, borderRadius: 3 }, statusText: { ...type.caption, color: colors.muted }, checkSlot: { width: 20, alignItems: 'center', justifyContent: 'center' }, bottomCard: { paddingHorizontal: spacing.lg }, emptyState: { paddingVertical: spacing.xl, paddingHorizontal: spacing.md, alignItems: 'center', gap: spacing.xs }, emptyTitle: { ...type.bodyStrong, color: colors.ink, textAlign: 'center' }, emptyBody: { ...type.small, color: colors.muted, textAlign: 'center' }, }) }