mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { useQuery } from '@tanstack/react-query'
|
|
import { API_BASE_URL } from '@/config'
|
|
import type { FileInfo, ChunkedFileInfo, PatchOperation } from '@/types/files'
|
|
|
|
async function fetchFile(path: string): Promise<FileInfo> {
|
|
const response = await fetch(`${API_BASE_URL}/api/files/${path}`)
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to load file: ${response.statusText}`)
|
|
}
|
|
|
|
return response.json()
|
|
}
|
|
|
|
export function useFile(path: string | undefined) {
|
|
return useQuery({
|
|
queryKey: ['file', path],
|
|
queryFn: () => path ? fetchFile(path) : Promise.reject(new Error('No file path provided')),
|
|
enabled: !!path,
|
|
})
|
|
}
|
|
|
|
export async function fetchFileRange(path: string, startLine: number, endLine: number): Promise<ChunkedFileInfo> {
|
|
const response = await fetch(`${API_BASE_URL}/api/files/${path}?startLine=${startLine}&endLine=${endLine}`)
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to load file range: ${response.statusText}`)
|
|
}
|
|
|
|
return response.json()
|
|
}
|
|
|
|
export async function applyFilePatches(path: string, patches: PatchOperation[]): Promise<{ success: boolean; totalLines: number }> {
|
|
const response = await fetch(`${API_BASE_URL}/api/files/${path}`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ patches }),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`Failed to apply patches: ${response.statusText}`)
|
|
}
|
|
|
|
return response.json()
|
|
} |