openclaw/apps/expo-github-mobile/src/hooks/useGitHub.ts
semihpolat a591c51010 feat(expo): add GitHub integration and WebSocket client
- Add GitHub API service for fetching user repos and branches
- Add settings view for GitHub username and gateway URL
- Update NewSessionSheet to use real GitHub data
- Add Gateway WebSocket client for real-time communication
- Add useGateway hook for agent event streaming
- Prepare for real-time tool call streaming in ChatView

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-23 14:16:02 -08:00

78 lines
2.2 KiB
TypeScript

import { useState, useEffect, useCallback } from 'react'
import { GitHubAPI, type GitHubRepo, type GitHubBranch } from '../services/github'
import { useSettings } from './useSettings'
export const useGitHubRepos = () => {
const { settings, loading: settingsLoading } = useSettings()
const [repos, setRepos] = useState<GitHubRepo[]>([])
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const fetchRepos = useCallback(async () => {
if (!settings.githubUsername) {
setRepos([])
setError(null)
return
}
setLoading(true)
setError(null)
try {
const fetchedRepos = await GitHubAPI.getUserRepos(settings.githubUsername)
// Filter out forks optionally, sort by updated
const sortedRepos = fetchedRepos.sort(
(a, b) => new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime()
)
setRepos(sortedRepos)
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to fetch repositories')
setRepos([])
} finally {
setLoading(false)
}
}, [settings.githubUsername])
useEffect(() => {
if (!settingsLoading) {
fetchRepos()
}
}, [settingsLoading, fetchRepos])
return { repos, loading: loading || settingsLoading, error, refetch: fetchRepos }
}
export const useGitHubBranches = (owner: string, repo: string) => {
const [branches, setBranches] = useState<GitHubBranch[]>([])
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const fetchBranches = useCallback(async () => {
if (!owner || !repo) {
setBranches([])
return
}
setLoading(true)
setError(null)
try {
const fetchedBranches = await GitHubAPI.getRepoBranches(owner, repo)
setBranches(fetchedBranches)
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to fetch branches')
setBranches([])
} finally {
setLoading(false)
}
}, [owner, repo])
useEffect(() => {
fetchBranches()
}, [fetchBranches])
return { branches, loading, error, refetch: fetchBranches }
}
export { type GitHubRepo, type GitHubBranch }