( projectId?: string | null, serverId?: string | null, rootPath?: string | null )
| 30 | } |
| 31 | |
| 32 | export function useFileBrowser( |
| 33 | projectId?: string | null, |
| 34 | serverId?: string | null, |
| 35 | rootPath?: string | null |
| 36 | ) { |
| 37 | const [path, setPath] = useState('/') |
| 38 | const [items, setItems] = useState<CliFileItem[]>([]) |
| 39 | const [isLoading, setIsLoading] = useState(false) |
| 40 | const [error, setError] = useState<string | null>(null) |
| 41 | const normalizedRoot = normalizePath(rootPath || '/') |
| 42 | |
| 43 | const load = useCallback( |
| 44 | async (nextPath: string, refresh = false) => { |
| 45 | if (!projectId || !serverId) return |
| 46 | const normalizedPath = normalizePath(nextPath) |
| 47 | setIsLoading(true) |
| 48 | setError(null) |
| 49 | try { |
| 50 | const response = await listCliFiles(projectId, serverId, normalizedPath, refresh) |
| 51 | const normalizedItems = response.items.map((item) => ({ |
| 52 | ...item, |
| 53 | path: normalizePath(item.path), |
| 54 | })) |
| 55 | setItems(normalizedItems) |
| 56 | setPath(normalizePath(response.path || normalizedPath)) |
| 57 | } catch (err) { |
| 58 | console.error('[CLI] Failed to load files:', err) |
| 59 | setError(resolveErrorMessage(err, 'Failed to load files')) |
| 60 | } finally { |
| 61 | setIsLoading(false) |
| 62 | } |
| 63 | }, |
| 64 | [projectId, serverId] |
| 65 | ) |
| 66 | |
| 67 | const readFile = useCallback( |
| 68 | async (filePath: string, tailBytes?: number): Promise<CliFileContentResponse | null> => { |
| 69 | if (!projectId || !serverId) return null |
| 70 | try { |
| 71 | return await readCliFile(projectId, serverId, filePath, tailBytes) |
| 72 | } catch (err) { |
| 73 | console.error('[CLI] Failed to read file:', err) |
| 74 | return null |
| 75 | } |
| 76 | }, |
| 77 | [projectId, serverId] |
| 78 | ) |
| 79 | |
| 80 | const writeFile = useCallback( |
| 81 | async (filePath: string, content: string) => { |
| 82 | if (!projectId || !serverId) return |
| 83 | await writeCliFile(projectId, serverId, { path: filePath, content, operation: 'write' }) |
| 84 | await load(path, true) |
| 85 | }, |
| 86 | [projectId, serverId, load, path] |
| 87 | ) |
| 88 | |
| 89 | const removeFile = useCallback( |
no test coverage detected