|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useCallback, useMemo, useState } from "react" |
|
4
|
import { Button, Hr } from "docs-ui" |
|
5
|
import ChangelogEntry from "./Entry" |
|
6
|
import { basePathUrl } from "../../utils/base-path-url" |
|
7
|
import type { ChangelogPage, PublicChangelogEntry } from "../../utils/changelog" |
|
8
|
|
|
9
|
type ChangelogProps = { |
|
10
|
/** |
|
11
|
* The first page of entries, loaded on the server so the changelog is |
|
12
|
* rendered and indexed without waiting for a client request. |
|
13
|
*/ |
|
14
|
initialPage: ChangelogPage |
|
15
|
} |
|
16
|
|
|
17
|
const Changelog = ({ initialPage }: ChangelogProps) => { |
|
18
|
const [entries, setEntries] = useState<PublicChangelogEntry[]>( |
|
19
|
initialPage.entries |
|
20
|
) |
|
21
|
const [page, setPage] = useState(initialPage.page) |
|
22
|
const [hasMore, setHasMore] = useState(initialPage.has_more) |
|
23
|
const [isLoading, setIsLoading] = useState(false) |
|
24
|
const [error, setError] = useState<string | undefined>() |
|
25
|
|
|
26
|
const endpoint = useMemo(() => basePathUrl("/api/changelog"), []) |
|
27
|
|
|
28
|
const loadMore = useCallback(async () => { |
|
29
|
if (isLoading) { |
|
30
|
return |
|
31
|
} |
|
32
|
|
|
33
|
setIsLoading(true) |
|
34
|
setError(undefined) |
|
35
|
|
|
36
|
try { |
|
37
|
const nextPage = page + 1 |
|
38
|
const response = await fetch( |
|
39
|
`${endpoint}?page=${nextPage}&limit=${initialPage.limit}` |
|
40
|
) |
|
41
|
|
|
42
|
if (!response.ok) { |
|
43
|
throw new Error(`Received a ${response.status} response.`) |
|
44
|
} |
|
45
|
|
|
46
|
const data = (await response.json()) as ChangelogPage |
|
47
|
|
|
48
|
// Guard against a duplicate entry if an entry was added between the two |
|
49
|
// requests, which would shift the pages. |
|
50
|
setEntries((previous) => { |
|
51
|
const seen = new Set(previous.map((entry) => entry.date)) |
|
52
|
return [ |
|
53
|
...previous, |
|
54
|
...data.entries.filter((entry) => !seen.has(entry.date)), |
|
55
|
] |
|
56
|
}) |
|
57
|
setPage(data.page) |
|
58
|
setHasMore(data.has_more) |
|
59
|
} catch { |
|
60
|
setError("Couldn't load more changelog entries. Please try again.") |
|
61
|
} finally { |
|
62
|
setIsLoading(false) |
|
63
|
} |
|
64
|
}, [endpoint, initialPage.limit, isLoading, page]) |
|
65
|
|
|
66
|
return ( |
|
67
|
<div> |
|
68
|
{entries.map((entry, index) => ( |
|
69
|
<React.Fragment key={entry.date}> |
|
70
|
{index > 0 && <Hr />} |
|
71
|
<ChangelogEntry entry={entry} /> |
|
72
|
</React.Fragment> |
|
73
|
))} |
|
74
|
{hasMore && ( |
|
75
|
<div className="flex flex-col items-center gap-docs_0.5 mt-docs_2"> |
|
76
|
<Button variant="secondary" onClick={loadMore} disabled={isLoading}> |
|
77
|
{isLoading ? "Loading..." : "Load more"} |
|
78
|
</Button> |
|
79
|
{error && ( |
|
80
|
<span className="text-medusa-fg-error text-compact-small"> |
|
81
|
{error} |
|
82
|
</span> |
|
83
|
)} |
|
84
|
</div> |
|
85
|
)} |
|
86
|
</div> |
|
87
|
) |
|
88
|
} |
|
89
|
|
|
90
|
export default Changelog |