This commit is contained in:
Auto
2025-12-30 11:56:39 +02:00
parent dd7c1ddd82
commit a2efec159d
40 changed files with 9112 additions and 3 deletions

172
ui/src/hooks/useProjects.ts Normal file
View File

@@ -0,0 +1,172 @@
/**
* React Query hooks for project data
*/
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import * as api from '../lib/api'
import type { FeatureCreate } from '../lib/types'
// ============================================================================
// Projects
// ============================================================================
export function useProjects() {
return useQuery({
queryKey: ['projects'],
queryFn: api.listProjects,
})
}
export function useProject(name: string | null) {
return useQuery({
queryKey: ['project', name],
queryFn: () => api.getProject(name!),
enabled: !!name,
})
}
export function useCreateProject() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: ({ name, specMethod }: { name: string; specMethod?: 'claude' | 'manual' }) =>
api.createProject(name, specMethod),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['projects'] })
},
})
}
export function useDeleteProject() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (name: string) => api.deleteProject(name),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['projects'] })
},
})
}
// ============================================================================
// Features
// ============================================================================
export function useFeatures(projectName: string | null) {
return useQuery({
queryKey: ['features', projectName],
queryFn: () => api.listFeatures(projectName!),
enabled: !!projectName,
refetchInterval: 5000, // Refetch every 5 seconds for real-time updates
})
}
export function useCreateFeature(projectName: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (feature: FeatureCreate) => api.createFeature(projectName, feature),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['features', projectName] })
},
})
}
export function useDeleteFeature(projectName: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (featureId: number) => api.deleteFeature(projectName, featureId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['features', projectName] })
},
})
}
export function useSkipFeature(projectName: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (featureId: number) => api.skipFeature(projectName, featureId),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['features', projectName] })
},
})
}
// ============================================================================
// Agent
// ============================================================================
export function useAgentStatus(projectName: string | null) {
return useQuery({
queryKey: ['agent-status', projectName],
queryFn: () => api.getAgentStatus(projectName!),
enabled: !!projectName,
refetchInterval: 3000, // Poll every 3 seconds
})
}
export function useStartAgent(projectName: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: () => api.startAgent(projectName),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
},
})
}
export function useStopAgent(projectName: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: () => api.stopAgent(projectName),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
},
})
}
export function usePauseAgent(projectName: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: () => api.pauseAgent(projectName),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
},
})
}
export function useResumeAgent(projectName: string) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: () => api.resumeAgent(projectName),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['agent-status', projectName] })
},
})
}
// ============================================================================
// Setup
// ============================================================================
export function useSetupStatus() {
return useQuery({
queryKey: ['setup-status'],
queryFn: api.getSetupStatus,
staleTime: 60000, // Cache for 1 minute
})
}
export function useHealthCheck() {
return useQuery({
queryKey: ['health'],
queryFn: api.healthCheck,
retry: false,
})
}

View File

@@ -0,0 +1,161 @@
/**
* WebSocket Hook for Real-time Updates
*/
import { useEffect, useRef, useState, useCallback } from 'react'
import type { WSMessage, AgentStatus } from '../lib/types'
interface WebSocketState {
progress: {
passing: number
total: number
percentage: number
}
agentStatus: AgentStatus
logs: Array<{ line: string; timestamp: string }>
isConnected: boolean
}
const MAX_LOGS = 100 // Keep last 100 log lines
export function useProjectWebSocket(projectName: string | null) {
const [state, setState] = useState<WebSocketState>({
progress: { passing: 0, total: 0, percentage: 0 },
agentStatus: 'stopped',
logs: [],
isConnected: false,
})
const wsRef = useRef<WebSocket | null>(null)
const reconnectTimeoutRef = useRef<number | null>(null)
const reconnectAttempts = useRef(0)
const connect = useCallback(() => {
if (!projectName) return
// Build WebSocket URL
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
const host = window.location.host
const wsUrl = `${protocol}//${host}/ws/projects/${encodeURIComponent(projectName)}`
try {
const ws = new WebSocket(wsUrl)
wsRef.current = ws
ws.onopen = () => {
setState(prev => ({ ...prev, isConnected: true }))
reconnectAttempts.current = 0
}
ws.onmessage = (event) => {
try {
const message: WSMessage = JSON.parse(event.data)
switch (message.type) {
case 'progress':
setState(prev => ({
...prev,
progress: {
passing: message.passing,
total: message.total,
percentage: message.percentage,
},
}))
break
case 'agent_status':
setState(prev => ({
...prev,
agentStatus: message.status,
}))
break
case 'log':
setState(prev => ({
...prev,
logs: [
...prev.logs.slice(-MAX_LOGS + 1),
{ line: message.line, timestamp: message.timestamp },
],
}))
break
case 'feature_update':
// Feature updates will trigger a refetch via React Query
break
case 'pong':
// Heartbeat response
break
}
} catch {
console.error('Failed to parse WebSocket message')
}
}
ws.onclose = () => {
setState(prev => ({ ...prev, isConnected: false }))
wsRef.current = null
// Exponential backoff reconnection
const delay = Math.min(1000 * Math.pow(2, reconnectAttempts.current), 30000)
reconnectAttempts.current++
reconnectTimeoutRef.current = window.setTimeout(() => {
connect()
}, delay)
}
ws.onerror = () => {
ws.close()
}
} catch {
// Failed to connect, will retry via onclose
}
}, [projectName])
// Send ping to keep connection alive
const sendPing = useCallback(() => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
wsRef.current.send(JSON.stringify({ type: 'ping' }))
}
}, [])
// Connect when project changes
useEffect(() => {
if (!projectName) {
// Disconnect if no project
if (wsRef.current) {
wsRef.current.close()
wsRef.current = null
}
return
}
connect()
// Ping every 30 seconds
const pingInterval = setInterval(sendPing, 30000)
return () => {
clearInterval(pingInterval)
if (reconnectTimeoutRef.current) {
clearTimeout(reconnectTimeoutRef.current)
}
if (wsRef.current) {
wsRef.current.close()
wsRef.current = null
}
}
}, [projectName, connect, sendPing])
// Clear logs function
const clearLogs = useCallback(() => {
setState(prev => ({ ...prev, logs: [] }))
}, [])
return {
...state,
clearLogs,
}
}