fix: correct API endpoint URLs in workspace file browser
The workspace was using /api/projects/... but the API routes are mounted at /projects without the /api prefix. Switched from raw fetch() to the apiClient which already has the correct baseURL configured. Also fixed TypeScript types and removed unused variables.
This commit is contained in:
@@ -1,15 +1,29 @@
|
|||||||
import { useCallback, useEffect, useState } from "react";
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
|
||||||
import { Link, useNavigate, useParams, useSearchParams } from "react-router-dom";
|
import { Link, useParams, useSearchParams } from "react-router-dom";
|
||||||
|
|
||||||
|
import { apiClient } from "../api/client";
|
||||||
import { listRepositories, type GitRepository } from "../api/git_repositories";
|
import { listRepositories, type GitRepository } from "../api/git_repositories";
|
||||||
|
|
||||||
type WorkspaceStatus = "loading" | "ready" | "error" | "empty";
|
type WorkspaceStatus = "loading" | "ready" | "error" | "empty";
|
||||||
|
|
||||||
|
interface FileTreeEntry {
|
||||||
|
name: string;
|
||||||
|
type: "file" | "directory";
|
||||||
|
path: string;
|
||||||
|
size?: number;
|
||||||
|
mode?: string;
|
||||||
|
last_commit?: {
|
||||||
|
hash: string;
|
||||||
|
message: string;
|
||||||
|
author: string;
|
||||||
|
date: string;
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
|
||||||
export const RepoWorkspace = () => {
|
export const RepoWorkspace = () => {
|
||||||
const { projectId } = useParams<{ projectId: string }>();
|
const { projectId } = useParams<{ projectId: string }>();
|
||||||
const [searchParams, setSearchParams] = useSearchParams();
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
const navigate = useNavigate();
|
|
||||||
|
|
||||||
const [status, setStatus] = useState<WorkspaceStatus>("loading");
|
const [status, setStatus] = useState<WorkspaceStatus>("loading");
|
||||||
const [repositories, setRepositories] = useState<GitRepository[]>([]);
|
const [repositories, setRepositories] = useState<GitRepository[]>([]);
|
||||||
@@ -151,7 +165,7 @@ const FileBrowser = ({
|
|||||||
repoId: string;
|
repoId: string;
|
||||||
}) => {
|
}) => {
|
||||||
const [searchParams, setSearchParams] = useSearchParams();
|
const [searchParams, setSearchParams] = useSearchParams();
|
||||||
const [entries, setEntries] = useState<any[]>([]);
|
const [entries, setEntries] = useState<FileTreeEntry[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
@@ -162,17 +176,16 @@ const FileBrowser = ({
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await apiClient.get(
|
||||||
`/api/projects/${projectId}/repositories/${repoId}/files?branch=${encodeURIComponent(
|
`/projects/${projectId}/repositories/${repoId}/files`,
|
||||||
branch
|
{
|
||||||
)}&path=${encodeURIComponent(path)}`,
|
params: {
|
||||||
{ credentials: "include" }
|
branch,
|
||||||
|
path,
|
||||||
|
},
|
||||||
|
}
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
setEntries(response.data.entries || []);
|
||||||
throw new Error("Failed to load files");
|
|
||||||
}
|
|
||||||
const data = await response.json();
|
|
||||||
setEntries(data.entries || []);
|
|
||||||
} catch {
|
} catch {
|
||||||
setError("Failed to load files");
|
setError("Failed to load files");
|
||||||
} finally {
|
} finally {
|
||||||
@@ -184,7 +197,7 @@ const FileBrowser = ({
|
|||||||
void loadFiles();
|
void loadFiles();
|
||||||
}, [loadFiles]);
|
}, [loadFiles]);
|
||||||
|
|
||||||
const handleEntryClick = (entry: any) => {
|
const handleEntryClick = (entry: FileTreeEntry) => {
|
||||||
if (entry.type === "directory") {
|
if (entry.type === "directory") {
|
||||||
const newParams = new URLSearchParams(searchParams);
|
const newParams = new URLSearchParams(searchParams);
|
||||||
newParams.set("path", entry.path);
|
newParams.set("path", entry.path);
|
||||||
@@ -240,7 +253,7 @@ const FileViewer = ({
|
|||||||
projectId: string;
|
projectId: string;
|
||||||
repoId: string;
|
repoId: string;
|
||||||
}) => {
|
}) => {
|
||||||
const [searchParams, setSearchParams] = useSearchParams();
|
const [searchParams] = useSearchParams();
|
||||||
const [content, setContent] = useState<string | null>(null);
|
const [content, setContent] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
@@ -258,16 +271,16 @@ const FileViewer = ({
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const response = await fetch(
|
const response = await apiClient.get(
|
||||||
`/api/projects/${projectId}/repositories/${repoId}/files/content?branch=${encodeURIComponent(
|
`/projects/${projectId}/repositories/${repoId}/files/content`,
|
||||||
branch
|
{
|
||||||
)}&path=${encodeURIComponent(filePath)}`,
|
params: {
|
||||||
{ credentials: "include" }
|
branch,
|
||||||
|
path: filePath,
|
||||||
|
},
|
||||||
|
}
|
||||||
);
|
);
|
||||||
if (!response.ok) {
|
const data = response.data;
|
||||||
throw new Error("Failed to load file");
|
|
||||||
}
|
|
||||||
const data = await response.json();
|
|
||||||
if (data.is_binary) {
|
if (data.is_binary) {
|
||||||
setIsBinary(true);
|
setIsBinary(true);
|
||||||
setContent("Binary file - cannot display");
|
setContent("Binary file - cannot display");
|
||||||
|
|||||||
Reference in New Issue
Block a user