fix: update backend response for /list route (#186)

* update structure of list directory response

* fmt
This commit is contained in:
Gabriel Gordon-Hall
2025-07-15 11:38:13 +01:00
committed by GitHub
parent ed781b552a
commit d994622a99
2 changed files with 22 additions and 3 deletions

View File

@@ -20,6 +20,13 @@ pub struct DirectoryEntry {
pub is_git_repo: bool,
}
#[derive(Debug, Serialize, TS)]
#[ts(export)]
pub struct DirectoryListResponse {
pub entries: Vec<DirectoryEntry>,
pub current_path: String,
}
#[derive(Debug, Deserialize)]
pub struct ListDirectoryQuery {
path: Option<String>,
@@ -27,7 +34,7 @@ pub struct ListDirectoryQuery {
pub async fn list_directory(
Query(query): Query<ListDirectoryQuery>,
) -> Result<ResponseJson<ApiResponse<Vec<DirectoryEntry>>>, StatusCode> {
) -> Result<ResponseJson<ApiResponse<DirectoryListResponse>>, StatusCode> {
let path_str = query.path.unwrap_or_else(|| {
// Default to user's home directory
dirs::home_dir()
@@ -103,7 +110,10 @@ pub async fn list_directory(
Ok(ResponseJson(ApiResponse {
success: true,
data: Some(directory_entries),
data: Some(DirectoryListResponse {
entries: directory_entries,
current_path: path.to_string_lossy().to_string(),
}),
message: None,
}))
}

View File

@@ -66,7 +66,14 @@ export function FolderPicker({
try {
const result = await fileSystemApi.list(path);
setEntries(result.entries || []);
// Ensure result exists and has the expected structure
if (!result || typeof result !== 'object') {
throw new Error('Invalid response from file system API');
}
// Safely access entries, ensuring it's an array
const entries = Array.isArray(result.entries) ? result.entries : [];
setEntries(entries);
const newPath = result.current_path || '';
setCurrentPath(newPath);
// Update manual path if we have a specific path (not for initial home directory load)
@@ -75,6 +82,8 @@ export function FolderPicker({
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load directory');
// Reset entries to empty array on error
setEntries([]);
} finally {
setLoading(false);
}