-
Notifications
You must be signed in to change notification settings - Fork 0
/
usePost.ts
61 lines (54 loc) · 1.39 KB
/
usePost.ts
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
import { useState } from "preact/hooks";
import { markdown, MarkdownInit } from "src/md";
type FetchPost = {
mdStr: string;
meta: {};
};
type Post = {
id: string;
title: string;
description: string;
createdAt: string;
content: string;
};
type Response = Post | Error;
const CACHE = new Map();
async function fetchPost(url?: string): Promise<FetchPost> {
const mdStr = await fetch(url).then((res) => res.text());
const meta = {};
return { mdStr, meta };
}
export function usePost(id: string): Response {
const url = `/contents/${id}.md`;
const [, setPost] = useState(0);
let post = CACHE.get(url);
if (post === undefined) {
post = fetchPost(url);
CACHE.set(url, post);
post.then(
(value: FetchPost) => {
const md = new MarkdownInit(value.mdStr);
const title = md.getTitle();
const createdAt = md.getCreatedAt();
const description = `${title} について書きました。`;
markdown(md.getContent()).then((content) => {
post.value = {
id,
title,
content,
description,
createdAt,
};
setPost(post);
});
},
(error: any) => {
post.error = error;
setPost(post);
}
);
}
if (post.value !== undefined) return post.value;
if (post.error !== undefined) throw new Error(post.error);
throw post;
}