-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectParser.js
More file actions
64 lines (57 loc) · 2.25 KB
/
projectParser.js
File metadata and controls
64 lines (57 loc) · 2.25 KB
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
import { useEffect, useState } from 'react';
import piml from 'piml';
export const useProjects = (fetchPinnedOnly = false) => {
const [projects, setProjects] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
const fetchProjects = async () => {
try {
const url = '/projects/projects.piml';
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status} for ${url}`);
}
const pimlText = await response.text();
const parsedData = piml.parse(pimlText);
// Robust extraction: handle different possible PIML structures
let projectList = [];
if (parsedData.projects && Array.isArray(parsedData.projects)) {
projectList = parsedData.projects;
} else if (parsedData.item && Array.isArray(parsedData.item)) {
// If they are collected under 'item' tag
projectList = parsedData.item;
} else if (Array.isArray(parsedData)) {
projectList = parsedData;
} else if (typeof parsedData === 'object') {
// Fallback: look for any array property
projectList =
Object.values(parsedData).find((val) => Array.isArray(val)) || [];
}
// Post-process project list to handle types and arrays
projectList = projectList.map((project) => ({
...project,
size: project.size ? parseInt(project.size, 10) : 1,
pinned: String(project.pinned).toLowerCase() === 'true',
isActive: String(project.isActive).toLowerCase() === 'true',
technologies: project.technologies
? typeof project.technologies === 'string'
? project.technologies.split(',').map((t) => t.trim())
: project.technologies
: [],
}));
if (fetchPinnedOnly) {
projectList = projectList.filter((p) => p.pinned);
}
setProjects(projectList);
} catch (e) {
console.error('Error parsing projects.piml:', e);
setError(e);
} finally {
setLoading(false);
}
};
fetchProjects();
}, [fetchPinnedOnly]);
return { projects, loading, error };
};