Files
orca/mobile/app/troubleshoot.tsx
Neil 5f75b247b9 Extract mobile troubleshoot screen styles (#17146)
* Split speech session lifecycle

* Split terminal output scheduler pipeline

* Split mobile browser pane modules

* Prune resolved max-lines suppressions

* Split pane tree equalization logic

* Extract mobile troubleshoot screen styles

* Fix F3-speech for #17123

* Fix F1-cycle for #17131
2026-08-29 20:07:23 -07:00

277 lines
8.8 KiB
TypeScript

import { useState, useCallback, useRef } from 'react'
import { View, Text, Pressable, ScrollView, ActivityIndicator, Platform } from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { useRouter } from 'expo-router'
import {
ChevronLeft,
ChevronDown,
ChevronUp,
Activity,
CheckCircle2,
ScrollText,
XCircle,
AlertTriangle
} from 'lucide-react-native'
import { colors, spacing } from '../src/theme/mobile-theme'
import { loadHosts } from '../src/transport/host-store'
import {
startDiagnosticFetchTimeout,
type DiagnosticFetchTimeout
} from '../src/diagnostics/diagnostic-fetch-timeout'
import {
formatEndpoint,
testHostReachability,
unreachableHostDetail
} from '../src/diagnostics/host-reachability'
import { troubleshootCommonIssues } from '../src/diagnostics/troubleshoot-common-issues'
import { troubleshootScreenStyles as styles } from '../src/diagnostics/troubleshoot-screen-styles'
type DiagnosticStatus = 'idle' | 'running' | 'done'
type CheckResult = {
label: string
status: 'pass' | 'fail' | 'warn'
detail: string
}
function StatusIcon({ status }: { status: CheckResult['status'] }) {
switch (status) {
case 'pass':
return <CheckCircle2 size={14} color={colors.statusGreen} />
case 'fail':
return <XCircle size={14} color={colors.statusRed} />
case 'warn':
return <AlertTriangle size={14} color={colors.textMuted} />
}
}
export default function TroubleshootScreen() {
const router = useRouter()
const insets = useSafeAreaInsets()
const [expandedId, setExpandedId] = useState<string | null>(null)
const [diagnosticStatus, setDiagnosticStatus] = useState<DiagnosticStatus>('idle')
const [checks, setChecks] = useState<CheckResult[]>([])
const abortRef = useRef(false)
const diagnosticRunRef = useRef(0)
const activeInternetCheckRef = useRef<DiagnosticFetchTimeout | null>(null)
const setTroubleshootRootRef = useCallback((node: View | null): void => {
if (node !== null) {
return
}
// Why: diagnostics can outlive the screen; cancel the active run when the
// route detaches without a passive cleanup-only Effect.
abortRef.current = true
diagnosticRunRef.current += 1
activeInternetCheckRef.current?.dispose()
activeInternetCheckRef.current = null
}, [])
const toggleSection = useCallback((id: string) => {
setExpandedId((prev) => (prev === id ? null : id))
}, [])
const runDiagnostics = useCallback(async () => {
const runId = diagnosticRunRef.current + 1
diagnosticRunRef.current = runId
abortRef.current = false
activeInternetCheckRef.current?.dispose()
activeInternetCheckRef.current = null
setDiagnosticStatus('running')
setChecks([])
const results: CheckResult[] = []
const isCurrentRun = () => !abortRef.current && diagnosticRunRef.current === runId
try {
const hosts = await loadHosts()
results.push(
hosts.length > 0
? { label: 'Paired hosts', status: 'pass', detail: `${hosts.length} paired` }
: { label: 'Paired hosts', status: 'fail', detail: 'None — scan a QR to pair' }
)
} catch {
results.push({ label: 'Paired hosts', status: 'warn', detail: 'Could not read host data' })
}
if (!isCurrentRun()) {
return
}
setChecks([...results])
const internetCheck = startDiagnosticFetchTimeout(5000)
activeInternetCheckRef.current = internetCheck
try {
const resp = await fetch('https://dns.google/resolve?name=example.com&type=A', {
signal: internetCheck.signal
})
if (!isCurrentRun()) {
return
}
results.push(
resp.ok
? { label: 'Internet', status: 'pass', detail: 'Connected' }
: { label: 'Internet', status: 'warn', detail: 'Unexpected response' }
)
} catch {
if (!isCurrentRun()) {
return
}
results.push({ label: 'Internet', status: 'fail', detail: 'No connection' })
} finally {
internetCheck.dispose()
if (activeInternetCheckRef.current === internetCheck) {
activeInternetCheckRef.current = null
}
}
if (!isCurrentRun()) {
return
}
setChecks([...results])
try {
const hosts = await loadHosts()
for (const host of hosts) {
if (!isCurrentRun()) {
return
}
const reachable = await testHostReachability(host.endpoint)
if (!isCurrentRun()) {
return
}
results.push({
label: host.name,
status: reachable ? 'pass' : 'fail',
detail: reachable
? `Reachable at ${formatEndpoint(host.endpoint)}`
: unreachableHostDetail(host.endpoint)
})
setChecks([...results])
}
} catch {
results.push({ label: 'Hosts', status: 'warn', detail: 'Could not test' })
}
if (!isCurrentRun()) {
return
}
results.push({
label: 'Platform',
status: 'pass',
detail: `${Platform.OS} ${Platform.Version ?? ''}`
})
setChecks([...results])
setDiagnosticStatus('done')
}, [])
return (
<View
ref={setTroubleshootRootRef}
style={[styles.container, { paddingTop: insets.top + spacing.sm }]}
>
<View style={styles.topRow}>
<Pressable style={styles.backButton} onPress={() => router.back()}>
<ChevronLeft size={22} color={colors.textSecondary} />
</Pressable>
<Text style={styles.heading}>Troubleshooting</Text>
</View>
<ScrollView
style={styles.scroll}
contentContainerStyle={styles.scrollContent}
showsVerticalScrollIndicator={false}
>
<Pressable
style={({ pressed }) => [
styles.diagnosticButton,
pressed && styles.diagnosticButtonPressed,
diagnosticStatus === 'running' && styles.diagnosticButtonDisabled
]}
onPress={runDiagnostics}
disabled={diagnosticStatus === 'running'}
>
{diagnosticStatus === 'running' ? (
<ActivityIndicator size="small" color={colors.textPrimary} />
) : (
<Activity size={16} color={colors.textPrimary} />
)}
<Text style={styles.diagnosticButtonLabel}>
{diagnosticStatus === 'running'
? 'Running…'
: diagnosticStatus === 'done'
? 'Run again'
: 'Run diagnostics'}
</Text>
</Pressable>
<Pressable
style={({ pressed }) => [
styles.diagnosticButton,
pressed && styles.diagnosticButtonPressed
]}
onPress={() => router.push('/connection-log')}
>
<ScrollText size={16} color={colors.textPrimary} />
<Text style={styles.diagnosticButtonLabel}>View network diagnostics</Text>
</Pressable>
{checks.length > 0 && (
<View style={styles.section}>
{checks.map((check, i) => (
<View key={i}>
{i > 0 && <View style={styles.separator} />}
<View style={styles.checkRow}>
<StatusIcon status={check.status} />
<Text style={styles.checkLabel}>{check.label}</Text>
<Text
style={[styles.checkDetail, check.status === 'fail' && styles.checkDetailFail]}
>
{check.detail}
</Text>
</View>
</View>
))}
</View>
)}
<Text style={styles.sectionHeading}>Common issues</Text>
<View style={styles.section}>
{troubleshootCommonIssues.map((section, i) => (
<View key={section.id}>
{i > 0 && <View style={styles.separator} />}
<Pressable
style={({ pressed }) => [styles.accordionHeader, pressed && styles.rowPressed]}
onPress={() => toggleSection(section.id)}
>
{section.icon}
<Text style={styles.accordionTitle}>{section.title}</Text>
{expandedId === section.id ? (
<ChevronUp size={16} color={colors.textMuted} />
) : (
<ChevronDown size={16} color={colors.textMuted} />
)}
</Pressable>
{expandedId === section.id && (
<View style={styles.accordionBody}>
{section.steps.map((step, j) => (
<View key={j} style={styles.stepRow}>
<Text style={styles.bullet}>•</Text>
<Text style={styles.stepText}>{step}</Text>
</View>
))}
</View>
)}
</View>
))}
</View>
<View style={{ height: spacing.xl }} />
</ScrollView>
</View>
)
}