-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseProjectContent.js
More file actions
41 lines (34 loc) · 1.08 KB
/
useProjectContent.js
File metadata and controls
41 lines (34 loc) · 1.08 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
import { useEffect, useState } from 'react';
export const useProjectContent = (slug) => {
const [content, setContent] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
if (!slug) {
setLoading(false);
return;
}
const fetchContent = async () => {
try {
const response = await fetch(`/projects/${slug}.txt`);
if (!response.ok) {
throw new Error(
`HTTP error! status: ${response.status} for ${slug}.txt`,
);
}
const text = await response.text();
// After removing the leading '---' from the .txt files,
// the full content is simply the entire text of the file.
const fullContent = text.trim();
// shortDescription is now provided by projects.json, so we don't extract it here.
setContent({ fullContent });
} catch (e) {
setError(e);
} finally {
setLoading(false);
}
};
fetchContent();
}, [slug]);
return { content, loading, error };
};