/
project-analytics.tsx
81 lines (74 loc) · 2.01 KB
/
project-analytics.tsx
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
import { dub } from "@/lib/dub";
import { EnrichedProjectProps } from "@/lib/types";
import { LoadingSpinner } from "@dub/ui";
import { Suspense } from "react";
import ProjectAnalyticsClient from "./project-analytics-client";
export default function ProjectAnalytics({
project,
}: {
project: EnrichedProjectProps;
}) {
return (
<Suspense fallback={<LoadingSpinner />}>
<ProjectAnalyticsRSC project={project} />
</Suspense>
);
}
async function ProjectAnalyticsRSC({
project,
}: {
project: EnrichedProjectProps;
}) {
const { links } = project;
// if project created less than 3 days ago, it's a newly added project
const newlyAddedProject =
new Date(project.createdAt).getTime() >
Date.now() - 3 * 24 * 60 * 60 * 1000;
console.log("Refreshing analytics data");
const analytics = await Promise.all(
links
.sort(
// show GITHUB first, then WEBSITE
(a, _) => (a.type === "GITHUB" ? -1 : 1),
)
.map(async (link) => {
return await dub.analytics.clicks
.timeseries({
externalId: `ext_${link.id}`,
interval: newlyAddedProject ? "24h" : "30d",
})
.catch(() => []);
}),
);
const chartData = analytics[0].map(
(data: { start: string; clicks: number }, i) => {
return {
start: new Date(data.start).toLocaleDateString(
"en-US",
newlyAddedProject
? {
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
}
: {
month: "short",
day: "numeric",
},
),
[links[0].type]: analytics[0][i]?.clicks,
...(links[1] && {
[links[1].type]: analytics[1][i]?.clicks,
}),
};
},
);
return (
<ProjectAnalyticsClient
chartData={chartData}
categories={[links[0].type, links[1] ? links[1].type : undefined]}
startEndOnly={newlyAddedProject}
/>
);
}