-
Notifications
You must be signed in to change notification settings - Fork 153
/
page.js
52 lines (46 loc) · 1.21 KB
/
page.js
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
'use client'
import { Suspense, useEffect, useState } from 'react'
// Client Components
import Page from '../../components/page.client'
import Story from '../../components/story.client'
// Utils
import fetchData from '../../lib/fetch-data'
import { transform } from '../../lib/get-item'
import useData from '../../lib/use-data'
import Skeletons from '../../components/skeletons'
function StoryWithData({ id }) {
if (typeof window === 'undefined') return <Skeletons />
const { data } = useData(`s-${id}`, () => fetchData(`item/${id}`).then(transform))
return <Story {...data} />
}
function NewsWithData() {
const { data: storyIds } = useData('top', () => fetchData('topstories'))
return (
<>
{storyIds.slice(0, 30).map((id) => {
return (
<Suspense key={id} fallback={<Skeletons />}>
<StoryWithData id={id} />
</Suspense>
)
})}
</>
)
}
export default function News() {
const [mounted, setMounted] = useState(false)
useEffect(() => {
setMounted(true)
}, [])
return (
<Page>
{mounted ? (
<Suspense fallback={<Skeletons />}>
<NewsWithData />
</Suspense>
) : (
<Skeletons />
)}
</Page>
)
}