tfrere commited on
Commit
f8362f3
·
1 Parent(s): 603401f

minor design fixes | update upload url api calls

Browse files
frontend/src/components/Benchmark/CreateForm.jsx CHANGED
@@ -30,7 +30,7 @@ import CloseIcon from "@mui/icons-material/Close";
30
  import PublicIcon from "@mui/icons-material/Public";
31
  import { useThemeMode } from "../../hooks/useThemeMode";
32
  import getTheme from "../../config/theme";
33
- import API_CONFIG from "../../config/api";
34
  import { useDocumentSelection } from "./hooks/useDocumentSelection";
35
 
36
  /**
@@ -123,8 +123,8 @@ function CreateForm({ onStartGeneration }) {
123
  // Mettre à jour l'état du document pour la modale
124
  setModalDocument(doc);
125
 
126
- const response = await fetch(`/${doc.id}.${extension}`);
127
- const text = await response.text();
128
 
129
  setDocumentContent(text);
130
  setOpenContentModal(true);
@@ -151,27 +151,17 @@ function CreateForm({ onStartGeneration }) {
151
  const handleDownloadDocument = async (doc) => {
152
  setIsDownloading(true);
153
  try {
154
- const link = document.createElement("a");
155
- link.href = `/${doc.id}.${
156
- doc.id === "the-bitter-lesson"
157
- ? "html"
158
- : doc.id === "hurricane-faq"
159
- ? "md"
160
- : "txt"
161
- }`;
162
- link.setAttribute(
163
- "download",
164
- `${doc.name}.${
165
- doc.id === "the-bitter-lesson"
166
- ? "html"
167
- : doc.id === "hurricane-faq"
168
- ? "md"
169
- : "txt"
170
- }`
171
- );
172
- document.body.appendChild(link);
173
- link.click();
174
- document.body.removeChild(link);
175
  } catch (error) {
176
  console.error("Error downloading document:", error);
177
  setUploadStatus({
@@ -388,7 +378,7 @@ function CreateForm({ onStartGeneration }) {
388
  fullWidth
389
  variant="outlined"
390
  label="Enter a new URL"
391
- placeholder="ex: https://en.wikipedia.org/wiki/Real_Madrid_CF"
392
  value={urlInput}
393
  onChange={handleUrlInputChange}
394
  disabled={isLoading}
@@ -412,7 +402,7 @@ function CreateForm({ onStartGeneration }) {
412
  fullWidth
413
  variant="outlined"
414
  label="Website URL"
415
- placeholder="ex:https://en.wikipedia.org/wiki/Real_Madrid_CF"
416
  value={urlInput}
417
  onChange={handleUrlInputChange}
418
  disabled={isLoading}
 
30
  import PublicIcon from "@mui/icons-material/Public";
31
  import { useThemeMode } from "../../hooks/useThemeMode";
32
  import getTheme from "../../config/theme";
33
+ import { API_CONFIG, apiService } from "../../config/api";
34
  import { useDocumentSelection } from "./hooks/useDocumentSelection";
35
 
36
  /**
 
123
  // Mettre à jour l'état du document pour la modale
124
  setModalDocument(doc);
125
 
126
+ // Utiliser apiService au lieu de fetch
127
+ const text = await apiService.getDocumentContent(doc.id, extension);
128
 
129
  setDocumentContent(text);
130
  setOpenContentModal(true);
 
151
  const handleDownloadDocument = async (doc) => {
152
  setIsDownloading(true);
153
  try {
154
+ let extension = "";
155
+ if (doc.id === "the-bitter-lesson") {
156
+ extension = "html";
157
+ } else if (doc.id === "hurricane-faq") {
158
+ extension = "md";
159
+ } else {
160
+ extension = "txt";
161
+ }
162
+
163
+ // Utiliser le service API pour télécharger le document
164
+ await apiService.downloadDocument(doc.id, extension, doc.name);
 
 
 
 
 
 
 
 
 
 
165
  } catch (error) {
166
  console.error("Error downloading document:", error);
167
  setUploadStatus({
 
378
  fullWidth
379
  variant="outlined"
380
  label="Enter a new URL"
381
+ placeholder="e.g. https://en.wikipedia.org/wiki/Real_Madrid_CF"
382
  value={urlInput}
383
  onChange={handleUrlInputChange}
384
  disabled={isLoading}
 
402
  fullWidth
403
  variant="outlined"
404
  label="Website URL"
405
+ placeholder="e.g. https://en.wikipedia.org/wiki/Real_Madrid_CF"
406
  value={urlInput}
407
  onChange={handleUrlInputChange}
408
  disabled={isLoading}
frontend/src/components/Benchmark/Generator.jsx CHANGED
@@ -500,8 +500,6 @@ const Generator = ({ sessionId, isDefaultDocument, onComplete }) => {
500
  elevation={3}
501
  sx={{
502
  p: 4,
503
- mt: 3,
504
- mb: 3,
505
  display: "flex",
506
  flexDirection: "column",
507
  alignItems: "center",
 
500
  elevation={3}
501
  sx={{
502
  p: 4,
 
 
503
  display: "flex",
504
  flexDirection: "column",
505
  alignItems: "center",
frontend/src/components/Benchmark/hooks/useDocumentSelection.js CHANGED
@@ -1,5 +1,5 @@
1
  import { useState, useRef } from "react";
2
- import API_CONFIG from "../../../config/api";
3
 
4
  /**
5
  * Hook personnalisé pour gérer la sélection et l'upload de documents
@@ -76,40 +76,27 @@ export const useDocumentSelection = (onStartGeneration) => {
76
  setSelectedDocument(null);
77
 
78
  try {
79
- const formData = new FormData();
80
- formData.append("file", file);
81
 
82
- const response = await fetch(`${API_CONFIG.BASE_URL}/upload`, {
83
- method: "POST",
84
- body: formData,
85
  });
86
-
87
- const result = await response.json();
88
-
89
- if (response.ok) {
90
- setUploadStatus({
91
- success: true,
92
- message: "File uploaded successfully",
93
- });
94
- setSessionId(result.session_id);
95
- setSelectedDocument({ name: file.name });
96
- // Fichier uploadé avec succès, donc on désactive les autres options
97
- setIsDefaultDocument(false);
98
- setUrlSelected(false);
99
- return { success: true };
100
- } else {
101
- setUploadStatus({
102
- success: false,
103
- message: result.detail || "Upload failed",
104
- });
105
- return { success: false, error: result.detail || "Upload failed" };
106
- }
107
  } catch (error) {
108
  setUploadStatus({
109
  success: false,
110
- message: "Server connection error",
111
  });
112
- return { success: false, error: "Server connection error" };
 
 
 
113
  } finally {
114
  setIsLoading(false);
115
  }
@@ -240,55 +227,37 @@ export const useDocumentSelection = (onStartGeneration) => {
240
  setIsDefaultDocument(false);
241
 
242
  try {
243
- // Création d'un FormData pour envoyer l'URL
244
- const formData = new FormData();
245
- formData.append("url", url);
246
 
247
- const response = await fetch(`${API_CONFIG.BASE_URL}/upload-url`, {
248
- method: "POST",
249
- body: formData,
250
  });
251
-
252
- const result = await response.json();
253
-
254
- if (response.ok) {
255
- setUploadStatus({
256
- success: true,
257
- message: "Content from URL uploaded successfully",
258
- });
259
- setSessionId(result.session_id);
260
-
261
- // Extraire le domaine racine de l'URL
262
- let domain = url;
263
- try {
264
- // Utiliser URL API pour extraire le hostname
265
- const urlObj = new URL(
266
- url.startsWith("http") ? url : `https://${url}`
267
- );
268
- domain = urlObj.hostname;
269
- } catch (e) {
270
- console.log("Error parsing URL, using original:", e);
271
- }
272
-
273
- setSelectedDocument({ name: url, domain: domain });
274
- setUrlSelected(true);
275
- return { success: true };
276
- } else {
277
- setUploadStatus({
278
- success: false,
279
- message: result.detail || "URL processing failed",
280
- });
281
- return {
282
- success: false,
283
- error: result.detail || "URL processing failed",
284
- };
285
  }
 
 
 
 
 
 
 
 
286
  } catch (error) {
287
  setUploadStatus({
288
  success: false,
289
- message: "Server connection error",
290
  });
291
- return { success: false, error: "Server connection error" };
292
  } finally {
293
  setIsLoading(false);
294
  }
 
1
  import { useState, useRef } from "react";
2
+ import { API_CONFIG, apiService } from "../../../config/api";
3
 
4
  /**
5
  * Hook personnalisé pour gérer la sélection et l'upload de documents
 
76
  setSelectedDocument(null);
77
 
78
  try {
79
+ const result = await apiService.uploadFile(file);
 
80
 
81
+ setUploadStatus({
82
+ success: true,
83
+ message: "File uploaded successfully",
84
  });
85
+ setSessionId(result.session_id);
86
+ setSelectedDocument({ name: file.name });
87
+ // Fichier uploadé avec succès, donc on désactive les autres options
88
+ setIsDefaultDocument(false);
89
+ setUrlSelected(false);
90
+ return { success: true };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  } catch (error) {
92
  setUploadStatus({
93
  success: false,
94
+ message: error.message || "Server connection error",
95
  });
96
+ return {
97
+ success: false,
98
+ error: error.message || "Server connection error",
99
+ };
100
  } finally {
101
  setIsLoading(false);
102
  }
 
227
  setIsDefaultDocument(false);
228
 
229
  try {
230
+ const result = await apiService.uploadUrl(url);
 
 
231
 
232
+ setUploadStatus({
233
+ success: true,
234
+ message: "Content from URL uploaded successfully",
235
  });
236
+ setSessionId(result.session_id);
237
+
238
+ // Extraire le domaine racine de l'URL
239
+ let domain = url;
240
+ try {
241
+ // Utiliser URL API pour extraire le hostname
242
+ const urlObj = new URL(url.startsWith("http") ? url : `https://${url}`);
243
+ domain = urlObj.hostname;
244
+ } catch (e) {
245
+ console.error("Error parsing URL:", e);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  }
247
+
248
+ setSelectedDocument({
249
+ name: "URL Content",
250
+ domain: domain,
251
+ source_url: result.source_url || url,
252
+ });
253
+
254
+ return { success: true };
255
  } catch (error) {
256
  setUploadStatus({
257
  success: false,
258
+ message: error.message || "Error processing URL",
259
  });
260
+ return { success: false, error: error.message || "Error processing URL" };
261
  } finally {
262
  setIsLoading(false);
263
  }
frontend/src/config/api.js CHANGED
@@ -1,4 +1,4 @@
1
- // API Configuration
2
  const API_CONFIG = {
3
  // Use the current origin in production
4
  // In development, use localhost:3001 explicitly
@@ -9,4 +9,181 @@ const API_CONFIG = {
9
  : window.location.origin,
10
  };
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  export default API_CONFIG;
 
1
+ // API Configuration and Service
2
  const API_CONFIG = {
3
  // Use the current origin in production
4
  // In development, use localhost:3001 explicitly
 
9
  : window.location.origin,
10
  };
11
 
12
+ // API Service for making HTTP requests
13
+ const apiService = {
14
+ // Generic API call method with error handling
15
+ async call(endpoint, options = {}) {
16
+ try {
17
+ const url = endpoint.startsWith("http")
18
+ ? endpoint
19
+ : `${API_CONFIG.BASE_URL}${endpoint}`;
20
+ const response = await fetch(url, {
21
+ ...options,
22
+ headers: {
23
+ "Content-Type": "application/json",
24
+ ...options.headers,
25
+ },
26
+ });
27
+
28
+ // If response is not ok, throw an error
29
+ if (!response.ok) {
30
+ const errorData = await response.json().catch(() => ({}));
31
+ throw new Error(errorData.detail || `API error: ${response.status}`);
32
+ }
33
+
34
+ // Check if response is JSON or text
35
+ const contentType = response.headers.get("content-type");
36
+ if (contentType && contentType.includes("application/json")) {
37
+ return await response.json();
38
+ }
39
+ return await response.text();
40
+ } catch (error) {
41
+ console.error("API call failed:", error);
42
+ throw error;
43
+ }
44
+ },
45
+
46
+ // GET request
47
+ async get(endpoint, options = {}) {
48
+ return await this.call(endpoint, {
49
+ method: "GET",
50
+ ...options,
51
+ });
52
+ },
53
+
54
+ // POST request with JSON data
55
+ async post(endpoint, data, options = {}) {
56
+ return await this.call(endpoint, {
57
+ method: "POST",
58
+ body: JSON.stringify(data),
59
+ ...options,
60
+ });
61
+ },
62
+
63
+ // POST request with FormData
64
+ async postFormData(endpoint, formData, options = {}) {
65
+ try {
66
+ const url = endpoint.startsWith("http")
67
+ ? endpoint
68
+ : `${API_CONFIG.BASE_URL}${endpoint}`;
69
+ const response = await fetch(url, {
70
+ method: "POST",
71
+ body: formData,
72
+ ...options,
73
+ headers: {
74
+ // Ne pas définir Content-Type pour FormData
75
+ ...(options.headers || {}),
76
+ },
77
+ });
78
+
79
+ // Si response is not ok, throw an error
80
+ if (!response.ok) {
81
+ const errorData = await response.json().catch(() => ({}));
82
+ throw new Error(errorData.detail || `API error: ${response.status}`);
83
+ }
84
+
85
+ // Check if response is JSON or text
86
+ const contentType = response.headers.get("content-type");
87
+ if (contentType && contentType.includes("application/json")) {
88
+ return await response.json();
89
+ }
90
+ return await response.text();
91
+ } catch (error) {
92
+ console.error("API FormData call failed:", error);
93
+ throw error;
94
+ }
95
+ },
96
+
97
+ // PUT request
98
+ async put(endpoint, data, options = {}) {
99
+ return await this.call(endpoint, {
100
+ method: "PUT",
101
+ body: JSON.stringify(data),
102
+ ...options,
103
+ });
104
+ },
105
+
106
+ // DELETE request
107
+ async delete(endpoint, options = {}) {
108
+ return await this.call(endpoint, {
109
+ method: "DELETE",
110
+ ...options,
111
+ });
112
+ },
113
+
114
+ // Upload a file
115
+ async uploadFile(file) {
116
+ const formData = new FormData();
117
+ formData.append("file", file);
118
+
119
+ // Utilisation directe de fetch pour éviter les problèmes avec les en-têtes de FormData
120
+ try {
121
+ const response = await fetch(`${API_CONFIG.BASE_URL}/upload`, {
122
+ method: "POST",
123
+ body: formData,
124
+ // Ne pas définir le Content-Type pour FormData
125
+ });
126
+
127
+ // Si response is not ok, throw an error
128
+ if (!response.ok) {
129
+ const errorData = await response.json().catch(() => ({}));
130
+ throw new Error(errorData.detail || `API error: ${response.status}`);
131
+ }
132
+
133
+ return await response.json();
134
+ } catch (error) {
135
+ console.error("Upload file failed:", error);
136
+ throw error;
137
+ }
138
+ },
139
+
140
+ // Upload content from a URL
141
+ async uploadUrl(url) {
142
+ const formData = new FormData();
143
+ formData.append("url", url);
144
+
145
+ // Utilisation directe de fetch pour éviter les problèmes avec les en-têtes de FormData
146
+ try {
147
+ const response = await fetch(`${API_CONFIG.BASE_URL}/upload-url`, {
148
+ method: "POST",
149
+ body: formData,
150
+ // Ne pas définir le Content-Type pour FormData
151
+ });
152
+
153
+ // Si response is not ok, throw an error
154
+ if (!response.ok) {
155
+ const errorData = await response.json().catch(() => ({}));
156
+ throw new Error(errorData.detail || `API error: ${response.status}`);
157
+ }
158
+
159
+ return await response.json();
160
+ } catch (error) {
161
+ console.error("Upload URL failed:", error);
162
+ throw error;
163
+ }
164
+ },
165
+
166
+ // Get static document content
167
+ async getDocumentContent(docId, extension) {
168
+ return await this.get(`/${docId}.${extension}`);
169
+ },
170
+
171
+ // Download document
172
+ downloadDocument(docId, extension, documentName) {
173
+ try {
174
+ const link = document.createElement("a");
175
+ link.href = `/${docId}.${extension}`;
176
+ link.setAttribute("download", `${documentName}.${extension}`);
177
+ document.body.appendChild(link);
178
+ link.click();
179
+ document.body.removeChild(link);
180
+ return true;
181
+ } catch (error) {
182
+ console.error("Download document failed:", error);
183
+ throw error;
184
+ }
185
+ },
186
+ };
187
+
188
+ export { API_CONFIG, apiService };
189
  export default API_CONFIG;