Files
opencode-manager/frontend/src/api/files.ts
T
Chris Scott 1212e6c6fb refactor: add desktop sidebar navigation and consolidate header actions (#199)
* refactor: add desktop sidebar navigation and consolidate header actions

- Add DesktopSidebar component with collapsible navigation
- Add useDesktop and useSidebarAction hooks for sidebar state management
- Add useSidebarCollapsed hook with localStorage persistence and error handling
- Integrate sidebar into App layout with tooltip support
- Remove repoUrl and isRepoWorktree props from SourceControlPanel
- BranchesTab now fetches repo data internally using getRepo query
- Remove unused to prop from SidebarItem
- Simplify RepoList conditional rendering
- Add comprehensive test coverage for new components

* refactor: remove LSP status button and memory plugin status hook

- Delete LspStatusButton component and its test file
- Remove unused useMemoryPluginStatus hook from RepoDetail
- Clean up dead code as part of desktop header refactor
2026-04-25 10:36:14 -04:00

91 lines
3.0 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { fetchWrapper, fetchWrapperBlob } from './fetchWrapper'
import { API_BASE_URL } from '@/config'
import type { FileInfo, ChunkedFileInfo, PatchOperation } from '@/types/files'
interface FileApiUrlOptions {
route?: string
params?: Record<string, string | number | boolean | undefined>
}
function pathRequiresQuery(path: string): boolean {
return path.split('/').some(segment => segment === '.' || segment === '..')
}
export function getFileApiUrl(path: string, options: FileApiUrlOptions = {}): string {
const searchParams = new URLSearchParams()
Object.entries(options.params ?? {}).forEach(([key, value]) => {
if (value !== undefined) {
searchParams.append(key, String(value))
}
})
const routePath = options.route ? `/${options.route}` : ''
if (pathRequiresQuery(path)) {
searchParams.set('path', path)
const query = searchParams.toString()
return `${API_BASE_URL}/api/files${routePath}${query ? `?${query}` : ''}`
}
const encodedPath = path.split('/').map(encodeURIComponent).join('/')
const query = searchParams.toString()
return `${API_BASE_URL}/api/files/${encodedPath}${routePath}${query ? `?${query}` : ''}`
}
async function fetchFile(path: string): Promise<FileInfo> {
return fetchWrapper(getFileApiUrl(path))
}
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> {
return fetchWrapper(getFileApiUrl(path), {
params: { startLine, endLine },
})
}
export async function applyFilePatches(path: string, patches: PatchOperation[]): Promise<{ success: boolean; totalLines: number }> {
return fetchWrapper(getFileApiUrl(path, { route: 'patches' }), {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ patches }),
})
}
export async function getIgnoredPaths(path: string): Promise<{ ignoredPaths: string[] }> {
return fetchWrapper(getFileApiUrl(path, { route: 'ignored-paths' }))
}
export interface DownloadOptions {
includeGit?: boolean
includePaths?: string[]
}
export async function downloadDirectoryAsZip(path: string, options?: DownloadOptions): Promise<void> {
const url = getFileApiUrl(path, {
route: 'download-zip',
params: {
includeGit: options?.includeGit || undefined,
includePaths: options?.includePaths?.length ? options.includePaths.join(',') : undefined,
},
})
const blob = await fetchWrapperBlob(url)
const urlObj = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = urlObj
const dirName = path.split('/').pop() || 'download'
a.download = `${dirName}.zip`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(urlObj)
}