Skip to content

Commit 1cfbb66

Browse files
authored
basic chart with Recharts. lazy load page due to bundle size (#509)
1 parent 73afc21 commit 1cfbb66

File tree

4 files changed

+297
-4
lines changed

4 files changed

+297
-4
lines changed

app/pages/instances/Metrics.tsx

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react'
2+
import {
3+
Area,
4+
CartesianGrid,
5+
ComposedChart,
6+
Line,
7+
XAxis,
8+
YAxis,
9+
} from 'recharts'
10+
11+
const data = [
12+
{ name: '9:50', amt: 200, limit: 600 },
13+
{ name: '10:00', amt: 300, limit: 600 },
14+
{ name: '10:10', amt: 500, limit: 600 },
15+
{ name: '10:20', amt: 450, limit: 600 },
16+
{ name: '10:30', amt: 650, limit: 800 },
17+
{ name: '10:40', amt: 550, limit: 800 },
18+
{ name: '10:50', amt: 600, limit: 800 },
19+
]
20+
21+
export default function InstanceMetrics() {
22+
return (
23+
<ComposedChart
24+
width={600}
25+
height={300}
26+
data={data}
27+
margin={{ top: 5, right: 20, bottom: 5, left: 0 }}
28+
className="mt-16"
29+
>
30+
{/* TODO: pull these colors from TW config */}
31+
<CartesianGrid stroke="#1D2427" vertical={false} />
32+
<Area
33+
dataKey="amt"
34+
stroke="#2F8865"
35+
fillOpacity={1}
36+
fill="#112725"
37+
isAnimationActive={false}
38+
/>
39+
<Line
40+
dataKey="limit"
41+
stroke="#48D597"
42+
strokeDasharray="3 3"
43+
dot={false}
44+
type="stepBefore"
45+
isAnimationActive={false}
46+
/>
47+
<XAxis dataKey="name" />
48+
<YAxis orientation="right" />
49+
</ComposedChart>
50+
)
51+
}

app/routes.tsx

+24-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1-
import React from 'react'
1+
import React, { Suspense } from 'react'
22

33
import type { RouteMatch, RouteObject } from 'react-router'
44
import { Navigate, Route, Routes } from 'react-router-dom'
55

6+
// Needs to take import callback (as opposed to taking the path and passing it
7+
// to import()) because Rollup doesn't find the file properly otherwise. I tried
8+
// to follow the rules for dynamic import specified here but it didn't work:
9+
//
10+
// https://github.com/rollup/plugins/tree/02fb349d/packages/dynamic-import-vars#limitations
11+
// Rules imply () => import(`./{path}.tsx`) should work
12+
function lazyLoad(importFunc: () => Promise<{ default: React.ComponentType }>) {
13+
const Inner = React.lazy(importFunc)
14+
return () => (
15+
// TODO: nicer fallback
16+
<Suspense fallback="loading">
17+
<Inner />
18+
</Suspense>
19+
)
20+
}
21+
622
import InstanceCreatePage from './pages/instances/create'
723
import InstanceStorage from './pages/instances/Storage'
24+
// Recharts is 350 KB
25+
const InstanceMetrics = lazyLoad(() => import('./pages/instances/Metrics'))
826
import OrgPage from './pages/OrgPage'
927
import ProjectPage from './pages/project'
1028
import ProjectAccessPage from './pages/project/Access'
@@ -109,7 +127,11 @@ export const routes = (
109127
crumb={instanceCrumb}
110128
>
111129
<Route index />
112-
<Route path="metrics" crumb="Metrics" />
130+
<Route
131+
path="metrics"
132+
crumb="Metrics"
133+
element={<InstanceMetrics />}
134+
/>
113135
<Route path="activity" crumb="Activity" />
114136
<Route path="access" crumb="Access" />
115137
<Route path="resize" crumb="Resize" />

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"react-router-dom": "^6.0.0",
4545
"react-table": "^7.7.0",
4646
"react-transition-group": "^4.4.1",
47+
"recharts": "^2.1.6",
4748
"tslib": "^2.0.0",
4849
"uuid": "^8.3.2"
4950
},

0 commit comments

Comments
 (0)