-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathviews-fragment.tsx
More file actions
43 lines (36 loc) · 1004 Bytes
/
views-fragment.tsx
File metadata and controls
43 lines (36 loc) · 1004 Bytes
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
import { getEvents } from 'basehub/events'
import { IncrementViews } from './increment-views'
import { unstable_noStore } from 'next/cache'
import { draftMode } from 'next/headers'
import { fragmentOn } from 'basehub'
export const viewsFragment = fragmentOn('Views', {
adminKey: true,
ingestKey: true,
})
export type ViewsFragment = fragmentOn.infer<typeof viewsFragment>
export const ViewsFragment = async ({
ingestKey,
adminKey,
increment,
}: {
increment?: boolean
} & ViewsFragment) => {
unstable_noStore()
const { isEnabled: isDraftMode } = await draftMode()
const getEventsAction = async () => {
'use server'
const views = await getEvents(adminKey, {
type: 'time-series',
range: 'all-time',
})
return views.success ? views.data : 0
}
return (
<IncrementViews
ingestKey={ingestKey}
getEventsAction={getEventsAction}
increment={Boolean(increment && !isDraftMode)}
initialCount={await getEventsAction()}
/>
)
}