-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuseGitHubFolderTree.js
104 lines (104 loc) · 4.71 KB
/
useGitHubFolderTree.js
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
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { useState } from 'react';
import axios from 'axios';
export const useGitHubFolderTree = (folderUrl, apiKey) => {
const [repoFiles, setRepoFiles] = useState([]);
const [error, setError] = useState('');
const [log, setLog] = useState('');
const fetchFolderData = (folderUrl) => __awaiter(void 0, void 0, void 0, function* () {
const contentIndex = folderUrl.indexOf('contents/') + 'contents/'.length;
const decodedUrl = decodeURIComponent(contentIndex > 0 ? folderUrl.substring(contentIndex) : folderUrl);
setLog(`Fetching data from ${decodedUrl}`);
const options = {};
if (apiKey) {
options.headers = {
Authorization: `Bearer ${apiKey}`,
};
}
const { data: response } = yield axios.get(folderUrl, options);
setLog(`Data fetched from ${decodedUrl}`);
return response;
});
const processFolderContents = (folder) => __awaiter(void 0, void 0, void 0, function* () {
const filePromises = folder.map((item) => __awaiter(void 0, void 0, void 0, function* () {
if (item.type === 'file') {
setLog(`Processing ${item.name}`);
const extension = item.name.split('.').pop() || 'unknown';
const sizeInKB = Math.round(parseInt(item.size) / 1024);
let size;
if (sizeInKB >= 1024) {
const sizeInMB = (sizeInKB / 1024).toFixed(2);
size = sizeInMB + ' MB';
}
else {
size = sizeInKB + ' KB';
}
return {
name: item.name,
file_type: extension,
download_url: item.download_url,
sha: item.sha,
size: size,
path: item.path,
};
}
else if (item.type === 'dir') {
setLog(`Processing ${item.name}`);
const subFolder = yield fetchFolderData(item.url);
setLog(`Subfolder data fetched from ${item.url}`);
const subFolderFiles = yield processFolderContents(subFolder);
setLog(`Processed ${item.name}`);
return subFolderFiles;
}
return null;
}));
const files = yield Promise.all(filePromises);
const flattenedFiles = files.flat();
setLog('Processing complete');
return flattenedFiles.filter(Boolean);
});
const fetchRepositoryContents = () => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
try {
const urlRegex = /https:\/\/github.com\/([^\/]+)\/([^\/]+)\/tree\/([^\/]+)\/?(.*)/;
if (!folderUrl) {
setError('Please enter a GitHub folder URL');
return;
}
const matches = folderUrl.match(urlRegex);
if (!matches) {
setError('Invalid GitHub folder URL');
return;
}
const user = matches[1];
const repo = matches[2];
const branch = matches[3];
const dir = matches[4];
setLog(`Extracted user: ${user}, repo: ${repo}, branch: ${branch}, dir: ${dir}`);
const apiUrl = `https://api.github.com/repos/${user}/${repo}/contents/${dir}?ref=${branch}`;
setLog(`Fetching repository contents from ${apiUrl}`);
const folderData = yield fetchFolderData(apiUrl);
setLog('Folder data fetched');
const processedFiles = yield processFolderContents(folderData);
setRepoFiles(prevFiles => [...prevFiles, ...processedFiles].filter(Boolean));
}
catch (error) {
setError(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || 'An error occurred');
}
});
return {
repoFiles,
error,
log,
fetchRepositoryContents,
};
};
//# sourceMappingURL=useGitHubFolderTree.js.map