({ children }: { children: ReactNode })
| 49 | } |
| 50 | |
| 51 | export function AppProvider({ children }: { children: ReactNode }) { |
| 52 | const [config, setConfig] = useState<OpencodeConfig | null>(null); |
| 53 | const [skills, setSkills] = useState<SkillInfo[]>([]); |
| 54 | const [plugins, setPlugins] = useState<PluginInfo[]>([]); |
| 55 | const [loading, setLoading] = useState(true); |
| 56 | const [error, setError] = useState<string | null>(null); |
| 57 | const [connected, setConnected] = useState(false); |
| 58 | const [pendingAction, setPendingAction] = useState<PendingAction | null>(null); |
| 59 | const [versionInfo, setVersionInfo] = useState<VersionCheck | null>(null); |
| 60 | const healthCheckRef = useRef<NodeJS.Timeout | null>(null); |
| 61 | const loadingRef = useRef(false); |
| 62 | const checkedPendingRef = useRef(false); |
| 63 | const versionCheckedRef = useRef(false); |
| 64 | const autoSyncCheckedRef = useRef(false); |
| 65 | |
| 66 | const refreshData = useCallback(async () => { |
| 67 | // Prevent multiple simultaneous refreshes |
| 68 | if (loadingRef.current) return; |
| 69 | |
| 70 | try { |
| 71 | loadingRef.current = true; |
| 72 | setLoading(true); |
| 73 | setError(null); |
| 74 | const [configData, skillsData, pluginsData] = await Promise.all([ |
| 75 | getConfig(), |
| 76 | getSkills(), |
| 77 | getPlugins(), |
| 78 | ]); |
| 79 | setConfig(configData); |
| 80 | setSkills(skillsData); |
| 81 | setPlugins(pluginsData); |
| 82 | // We are connected because the requests succeeded |
| 83 | setConnected(true); |
| 84 | } catch (err: any) { |
| 85 | let errorMessage = 'Failed to load data from backend'; |
| 86 | |
| 87 | if (err.code === 'ERR_NETWORK') { |
| 88 | errorMessage = 'Backend server is unreachable. Ensure "npm start" is running.'; |
| 89 | } else if (err.response) { |
| 90 | const status = err.response.status; |
| 91 | const data = err.response.data; |
| 92 | |
| 93 | if (status === 404) { |
| 94 | errorMessage = 'OpenCode configuration not found. Please run "opencode --version" in your terminal to initialize it.'; |
| 95 | setConfig(null); |
| 96 | setConnected(true); |
| 97 | } else if (status === 500) { |
| 98 | errorMessage = `Server Error (500): ${data?.error || data?.message || 'Check backend logs'}`; |
| 99 | } else if (status === 403) { |
| 100 | errorMessage = 'Access Denied (403): Check backend CORS or permission settings.'; |
| 101 | } else { |
| 102 | errorMessage = `HTTP Error ${status}: ${data?.error || data?.message || 'Unknown server error'}`; |
| 103 | } |
| 104 | } else if (err.message) { |
| 105 | errorMessage = `Error: ${err.message}`; |
| 106 | } |
| 107 | |
| 108 | setError(errorMessage); |
nothing calls this directly
no test coverage detected