-
Notifications
You must be signed in to change notification settings - Fork 5
/
route.tsx
105 lines (93 loc) · 3.36 KB
/
route.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { json, LoaderFunction, MetaFunction } from "@remix-run/node"
import { useLoaderData, useOutletContext } from "@remix-run/react"
import React from "react"
import invariant from "tiny-invariant"
import { ErrorBoundaryView } from "~/components/ErrorBoundaryView"
import {
getAggregateRelays,
getRealtimeDataChains,
getTotalRelays,
} from "~/models/portal/dwh.server"
import { initPortalClient } from "~/models/portal/portal.server"
import { Account, D2Chain, D2Stats, PortalApp } from "~/models/portal/sdk"
import { AccountIdLoaderData } from "~/routes/account.$accountId/route"
import AccountInsightsView from "~/routes/account.$accountId._index/view"
import { getErrorMessage } from "~/utils/catchError"
import { byHourPeriods, getDwhParams, validatePeriod } from "~/utils/dwhUtils.server"
import { seo_title_append } from "~/utils/seo"
import { requireUser } from "~/utils/user.server"
export const meta: MetaFunction = () => {
return [
{
title: `Account Insights ${seo_title_append}`,
},
]
}
export type AccountInsightsData = {
account: Account
total: D2Stats
aggregate: D2Stats[]
realtimeDataChains: D2Chain[]
}
export const loader: LoaderFunction = async ({ request, params }) => {
const user = await requireUser(request)
const portal = initPortalClient({ token: user.accessToken })
const url = new URL(request.url)
const { period, chainParam, appParam } = getDwhParams(url)
// Prevent manually entering an invalid period
validatePeriod({ period, url })
try {
const { accountId } = params
invariant(typeof accountId === "string", "AccountId must be a set url parameter")
const account = await portal.getUserAccount({ accountID: accountId, accepted: true })
const getAggregateRelaysResponse = await getAggregateRelays({
period,
accountId,
portalClient: portal,
byHour: byHourPeriods.includes(period),
...(chainParam && chainParam !== "all" && { chainIDs: [chainParam] }),
...(appParam && appParam !== "all" && { applicationIDs: [appParam] }),
})
const getTotalRelaysResponse = await getTotalRelays({
period,
accountId,
portalClient: portal,
...(chainParam && chainParam !== "all" && { chainIDs: [chainParam] }),
...(appParam && appParam !== "all" && { applicationIDs: [appParam] }),
})
const getRealtimeDataChainsResponse = await getRealtimeDataChains({
period,
accountId,
portalClient: portal,
...(appParam && appParam !== "all" && { applicationIDs: [appParam] }),
})
return json<AccountInsightsData>({
account: account.getUserAccount as Account,
realtimeDataChains: getRealtimeDataChainsResponse,
total: getTotalRelaysResponse,
aggregate: getAggregateRelaysResponse,
})
} catch (error) {
throw new Response(getErrorMessage(error), {
status: 500,
})
}
}
export default function AccountInsights() {
const { account, total, aggregate, realtimeDataChains } =
useLoaderData() as AccountInsightsData
const { blockchains, userRole } = useOutletContext<AccountIdLoaderData>()
return (
<AccountInsightsView
aggregate={aggregate}
apps={account?.portalApps as PortalApp[]}
blockchains={blockchains}
realtimeDataChains={realtimeDataChains}
total={total}
userRole={userRole}
/>
)
}
export function ErrorBoundary() {
return <ErrorBoundaryView />
}