File size: 12,658 Bytes
f5a3d5a
81814ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5a3d5a
 
 
81814ba
f5a3d5a
81814ba
 
f5a3d5a
 
 
 
 
d040477
 
 
 
 
 
 
81814ba
 
 
f5a3d5a
 
 
 
 
 
 
 
 
 
 
12bfb7e
f5a3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d040477
 
 
81814ba
d040477
 
 
 
 
 
 
 
 
 
81814ba
d040477
12bfb7e
d040477
 
 
81814ba
 
 
 
f5a3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
81814ba
d040477
f5a3d5a
 
d040477
 
 
 
 
81814ba
 
 
 
 
 
 
 
 
 
 
 
f5a3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d040477
f5a3d5a
 
 
 
 
 
 
 
 
 
81814ba
4fa42d6
f5a3d5a
529f0aa
 
81814ba
f5a3d5a
 
529f0aa
f5a3d5a
 
 
 
 
 
 
 
 
529f0aa
 
81814ba
f5a3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81814ba
f5a3d5a
81814ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5a3d5a
 
 
81814ba
f5a3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81814ba
 
f5a3d5a
 
81814ba
 
 
f5a3d5a
 
 
 
 
 
 
 
 
81814ba
 
 
 
 
 
 
f5a3d5a
81814ba
 
 
 
d040477
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import React, { useState, useEffect, useRef } from "react";
import axios from "axios";
import {
  Box,
  Button,
  CircularProgress,
  InputLabel,
  MenuItem,
  Select,
  Typography,
  FormControl,
  Grid,
  Paper,
  Snackbar,
  Alert,
  Dialog,
  DialogTitle,
  DialogContent,
  DialogActions,
  useMediaQuery,
  Menu,
  MenuItem as ContextMenuItem,
} from "@mui/material";
import { useTheme } from "@mui/material/styles";
import CloudUploadIcon from "@mui/icons-material/CloudUpload";
import DownloadIcon from "@mui/icons-material/Download";
import FolderIcon from "@mui/icons-material/Folder";
import InsertDriveFileIcon from "@mui/icons-material/InsertDriveFile";
import { SimpleTreeView, TreeItem } from "@mui/x-tree-view";
import Prism from './prism-setup';
import { detectLanguage } from './utils/languageDetect';

const FileUploadForm = () => {
  const [file, setFile] = useState(null);
  const [frontend, setFrontend] = useState("React");
  const [backend, setBackend] = useState("Flask");
  const [database, setDatabase] = useState("PostgreSQL");
  const [loading, setLoading] = useState(false);
  const [showSnackbar, setShowSnackbar] = useState(false);
  const [showModal, setShowModal] = useState(false);
  const [zipBlob, setZipBlob] = useState(null);
  const [fileListInfo, setFileListInfo] = useState({});
  const [selectedFile, setSelectedFile] = useState(null);
  const [expandedIds, setExpandedIds] = useState([]); // βœ… array, not Set

  const [contextAnchor, setContextAnchor] = useState(null);
  const [contextFile, setContextFile] = useState(null);

  const codeRef = useRef(null);

 


  

  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down("sm"));

  const buildTree = (files) => {
    const root = {};
    for (const path in files) {
      const parts = path.split("/");
      let current = root;
      for (let i = 0; i < parts.length; i++) {
        const part = parts[i];
        if (!current[part]) {
          current[part] = i === parts.length - 1 ? files[path] : {};
        }
        current = current[part];
      }
    }
    return root;
  };

  const renderTree = (node, path = "") => {
    if (!node || typeof node !== "object") return null;

    return Object.entries(node).map(([key, value]) => {
      const fullPath = path ? `${path}/${key}` : key;
      const itemId = fullPath.replaceAll("/", "-");
      const isFile = value && typeof value.preview !== "undefined";

      return (
        <TreeItem
          key={fullPath}
          itemId={itemId}
          label={
            <Box
              display="flex"
              alignItems="center"
              onContextMenu={(e) => handleContextMenu(e, fullPath, value)}
            >
              {isFile ? <InsertDriveFileIcon sx={{ mr: 1 }} /> : <FolderIcon sx={{ mr: 1 }} />}
              <Typography variant="body2">
                {key}
                {isFile ? ` (${(value.size / 1024).toFixed(1)} KB)` : ""}
              </Typography>
            </Box>
          }
          onClick={() => {
            if (isFile) {
        setSelectedFile({ name: fullPath, content: value.preview });
        Prism.highlightAll();
      } else {
        setExpandedIds((prev) =>
          prev.includes(itemId)
            ? prev.filter((id) => id !== itemId) // collapse
            : [...prev, itemId] // expand
        );
      }
          }}
        >
          {!isFile && renderTree(value, fullPath)}
        </TreeItem>
      );
    });
  };

  const handleDownload = async (e) => {
    e.preventDefault();
    if (!file) return alert("Please select a requirement document!");

    const formData = new FormData();
    formData.append("file", file);
    formData.append("frontend", frontend);
    formData.append("backend", backend);
    formData.append("database", database);

    try {
      setLoading(true);
      const response = await axios.post(
        "https://fredericksundeep-aichatmatedev.hf.space/chat-stream-doc",
        formData,
        { responseType: "blob" }
      );

      const blob = new Blob([response.data], { type: "application/zip" });
      setZipBlob(blob);

      const JSZip = (await import("jszip")).default;
      const zip = await JSZip.loadAsync(blob);
      const fileInfo = {};
      await Promise.all(
        Object.entries(zip.files).map(async ([name, file]) => {
          if (!file.dir) {
            const content = await file.async("string");
            fileInfo[name] = {
              size: file._data.uncompressedSize,
              preview: content.slice(0, 1000),
            };
          }
        })
      );
      setFileListInfo(fileInfo);
      setExpandedIds([]); // reset tree state
      setShowModal(true);
    } catch (error) {
      console.error("Error:", error);
      alert("Failed to generate project.");
    } finally {
      setLoading(false);
    }
  };

  const handleZipDownload = () => {
    const url = window.URL.createObjectURL(zipBlob);
    const a = document.createElement("a");
    a.href = url;
    a.download = "generated_project.zip";
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(url);
    setShowSnackbar(true);
    setShowModal(false);
  };

  const handleContextMenu = (event, filename, value) => {
    event.preventDefault();
    if (typeof value.preview === "undefined") return; // not a file
    setContextFile({ name: filename, content: value.preview });
    setContextAnchor(event.currentTarget);
  };

  const handleDownloadFile = () => {
    if (!contextFile) return;
    const blob = new Blob([contextFile.content], { type: "text/plain" });
    const a = document.createElement("a");
    a.href = URL.createObjectURL(blob);
    a.download = contextFile.name.split("/").pop();
    a.click();
    setContextAnchor(null);
  };


// βœ… Expand/collapse logic
const expandAll = () => {
  const allPaths = Object.keys(fileListInfo); // files only
  const allIds = new Set();

  allPaths.forEach((filePath) => {
    const parts = filePath.split("/");
    for (let i = 1; i <= parts.length; i++) {
      const partialPath = parts.slice(0, i).join("/");
      allIds.add(partialPath.replaceAll("/", "-")); // folder or file
    }
  });

  setExpandedIds(Array.from(allIds));
};


const collapseAll = () => {
  setExpandedIds([]); // βœ… empty array
};




const { lang, code } = React.useMemo(() => {
  if (selectedFile && selectedFile.content) {
    const lines = selectedFile.content.split('\n');
    const firstLine = lines[0].trim();
    const hasLang = /^[a-zA-Z]+$/.test(firstLine);
    const lang = hasLang ? firstLine.toLowerCase() : detectLanguage(selectedFile.content) || 'markdown';
    const code = hasLang ? lines.slice(1).join('\n') : selectedFile.content;
    return { lang, code };
  }
  return { lang: 'markdown', code: '' };
}, [selectedFile]);


useEffect(() => {
    if (codeRef.current) {
      Prism.highlightElement(codeRef.current);
    }
  }, [code, lang]);
  return (
    <Box sx={{
      backgroundColor: "#121212",
      minHeight: "100vh",
      color: "#fff",
      padding: 4,
      display: "flex",
      justifyContent: "center",
      alignItems: "center",
      flexDirection: "column",
    }}>
      <Paper elevation={6} sx={{ padding: 4, backgroundColor: "#1e1e1e", maxWidth: 600, width: "100%" }}>
        <Box textAlign="center" my={4}>
          <Typography variant="h4" component="h1">🧠 AI - Full-Stack Project Generator</Typography>
        </Box>

        <form onSubmit={handleDownload}>
          <Grid container spacing={2} direction="column">
            <Grid item>
              <FormControl fullWidth>
                <Button
                  variant="contained"
                  component="label"
                  startIcon={<CloudUploadIcon />}
                  fullWidth
                >
                  {file ? file.name : "Upload Requirement Document"}
                  <input type="file" hidden required onChange={(e) => setFile(e.target.files[0])} accept=".pdf,.txt" />
                </Button>
              </FormControl>
            </Grid>

            {["Frontend", "Backend", "Database"].map((label, idx) => (
              <Grid item key={label}>
                <FormControl fullWidth>
                  <InputLabel>{label}</InputLabel>
                  <Select
                    value={label === "Frontend" ? frontend : label === "Backend" ? backend : database}
                    onChange={(e) => {
                      if (label === "Frontend") setFrontend(e.target.value);
                      else if (label === "Backend") setBackend(e.target.value);
                      else setDatabase(e.target.value);
                    }}
                    label={label}
                  >
                    {label === "Frontend" && ["React", "Angular", "Vue", "Next.js", "Svelte", "HTML/CSS/JS"].map(opt => <MenuItem key={opt} value={opt}>{opt}</MenuItem>)}
                    {label === "Backend" && ["Flask", "Node.js", "Django", "Express.js", "Spring Boot", "FastAPI"].map(opt => <MenuItem key={opt} value={opt}>{opt}</MenuItem>)}
                    {label === "Database" && ["PostgreSQL", "MongoDB", "MySQL", "SQLite", "Firebase", "Supabase"].map(opt => <MenuItem key={opt} value={opt}>{opt}</MenuItem>)}
                  </Select>
                </FormControl>
              </Grid>
            ))}

            <Grid item>
              <Button
                type="submit"
                variant="contained"
                color="secondary"
                startIcon={loading ? <CircularProgress size={20} /> : <DownloadIcon />}
                fullWidth
                disabled={loading}
              >
                {loading ? "Generating..." : "Generate Project ZIP"}
              </Button>
            </Grid>
          </Grid>
        </form>
      </Paper>

      {/* πŸ“¦ Preview Dialog */}
      <Dialog open={showModal} onClose={() => setShowModal(false)} maxWidth="md" fullWidth>
        <DialogTitle>πŸ“¦ Generated Files</DialogTitle>
        <DialogContent dividers>
          <Grid container spacing={2}>
            <Grid item xs={12} md={5}>
              <Box mb={1} display="flex" gap={1}>
                <Button size="small" variant="outlined" onClick={expandAll}>Expand All</Button>
                <Button size="small" variant="outlined" onClick={collapseAll}>Collapse All</Button>
              </Box>
              <SimpleTreeView
  expandedItems={expandedIds}
  onExpandedItemsChange={(newItems) => {
    if (Array.isArray(newItems)) {
      setExpandedIds(newItems); // βœ… directly set array
    } else {
      console.warn("Unexpected expandedItems:", newItems);
      setExpandedIds([]);
    }
  }}
   onItemToggle={(itemId, isExpanded) => {
    setExpandedIds((prev) =>
      isExpanded ? [...prev, itemId] : prev.filter((id) => id !== itemId)
    );
  }}
>
  {renderTree(buildTree(fileListInfo))}
</SimpleTreeView>
            </Grid>

            <Grid item xs={12} md={7}>
              {selectedFile ? (
                <>
                  <Typography variant="subtitle1" gutterBottom>{selectedFile.name}</Typography>
                  <Paper variant="outlined" sx={{ p: 2, maxHeight: 400, overflowY: "auto", backgroundColor: "#1e1e1e" }}>
                    <pre className={`line-numbers language-${lang}`} style={{ borderRadius: '8px', overflowX: 'auto' }}>
  <code ref={codeRef} className={`language-${lang}`}>
    {code}
  </code>
</pre>
                  </Paper>
                </>
              ) : (
                <Typography color="text.secondary">Select a file to preview its content</Typography>
              )}
            </Grid>
          </Grid>
        </DialogContent>
        <DialogActions>
          <Button onClick={handleZipDownload} variant="contained">Download ZIP</Button>
          <Button onClick={() => setShowModal(false)}>Close</Button>
        </DialogActions>
      </Dialog>

      {/* 🎯 Context Menu for Download */}
      <Menu
        anchorEl={contextAnchor}
        open={Boolean(contextAnchor)}
        onClose={() => setContextAnchor(null)}
      >
        <ContextMenuItem onClick={handleDownloadFile}>πŸ“₯ Download File</ContextMenuItem>
      </Menu>

      {/* βœ… Success Snackbar */}
      <Snackbar
        open={showSnackbar}
        autoHideDuration={4000}
        onClose={() => setShowSnackbar(false)}
        anchorOrigin={{ vertical: "bottom", horizontal: "center" }}
      >
        <Alert severity="success" sx={{ width: "100%" }} onClose={() => setShowSnackbar(false)}>
          βœ… ZIP downloaded successfully!
        </Alert>
      </Snackbar>
    </Box>
  );
};

export default FileUploadForm;