-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscourse.ts
More file actions
88 lines (81 loc) · 2.49 KB
/
Copy pathdiscourse.ts
File metadata and controls
88 lines (81 loc) · 2.49 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
export interface ForumTopic {
id: number
title: string
slug: string
postsCount: number
replyCount: number
lastPostedAt: string
imageUrl: string | null
excerpt: string | null
}
const FORUM_BASE = 'https://forum.256foundation.org'
export async function fetchForumTopics(count = 6): Promise<ForumTopic[]> {
try {
const res = await fetch(`${FORUM_BASE}/latest.json`, {
next: { revalidate: 3600 },
headers: { Accept: 'application/json' },
})
if (!res.ok) return []
const data = await res.json()
const topics: unknown[] = data?.topic_list?.topics ?? []
return (topics as Record<string, unknown>[])
.filter(() => true)
.slice(0, count)
.map((t) => ({
id: t.id as number,
title: t.title as string,
slug: t.slug as string,
postsCount: t.posts_count as number,
replyCount: t.reply_count as number,
lastPostedAt: t.last_posted_at as string,
imageUrl: (t.image_url as string | null) ?? null,
excerpt: (t.excerpt as string | null) ?? null,
}))
} catch {
return []
}
}
export async function fetchProjectForumTopics(
categoryApiUrl: string,
count = 4,
): Promise<ForumTopic[]> {
try {
const res = await fetch(categoryApiUrl, {
next: { revalidate: 3600 },
headers: { Accept: 'application/json' },
})
if (!res.ok) return []
const data = await res.json()
const topics: unknown[] = data?.topic_list?.topics ?? []
return (topics as Record<string, unknown>[])
.filter(() => true)
.slice(0, count)
.map((t) => ({
id: t.id as number,
title: t.title as string,
slug: t.slug as string,
postsCount: t.posts_count as number,
replyCount: t.reply_count as number,
lastPostedAt: t.last_posted_at as string,
imageUrl: (t.image_url as string | null) ?? null,
excerpt: (t.excerpt as string | null) ?? null,
}))
} catch {
return []
}
}
export function forumTopicUrl(slug: string, id: number): string {
return `${FORUM_BASE}/t/${slug}/${id}`
}
export function timeAgo(dateStr: string): string {
const diff = Date.now() - new Date(dateStr).getTime()
const mins = Math.floor(diff / 60000)
if (mins < 1) return 'just now'
if (mins < 60) return `${mins}m ago`
const hours = Math.floor(mins / 60)
if (hours < 24) return `${hours}h ago`
const days = Math.floor(hours / 24)
if (days < 30) return `${days}d ago`
const months = Math.floor(days / 30)
return `${months}mo ago`
}