Skip to content

Commit c0b32b8

Browse files
authored
Move Metrics component to Relay Hooks (#508)
1 parent b6eb5c3 commit c0b32b8

File tree

5 files changed

+49
-57
lines changed

5 files changed

+49
-57
lines changed

schema.gql

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ scalar Int
343343
type MetricsChart {
344344
title: String!
345345
points: [TimePoint]!
346-
dataUnits: String!
346+
dataUnits: String
347347
}
348348

349349
input MetricsQueryParameters {

src/App.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createTheme, StyledEngineProvider, Theme, ThemeProvider } from '@mui/ma
66
import CirrusFavicon from './components/common/CirrusFavicon';
77
import { CssBaseline } from '@mui/material';
88
import { useRecoilValue } from 'recoil';
9+
import * as Sentry from '@sentry/react';
910

1011
declare module '@mui/styles/defaultTheme' {
1112
// eslint-disable-next-line @typescript-eslint/no-empty-interface
@@ -22,7 +23,9 @@ export default function App(): JSX.Element {
2223
<ThemeProvider theme={theme}>
2324
<CirrusFavicon />
2425
<CssBaseline />
25-
<Routes />
26+
<Sentry.ErrorBoundary>
27+
<Routes />
28+
</Sentry.ErrorBoundary>
2629
</ThemeProvider>
2730
</StyledEngineProvider>
2831
);

src/components/common/AccountSwitch.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ const AccountsSwitch = ({ viewer }: AccountSwitchProps) => {
4444
</Button>
4545
<Menu anchorEl={menuAnchorEl} open={menuOpen} onClose={handleMenuClose}>
4646
{viewer.relatedOwners.map(viewer => {
47-
return <MenuItem onClick={e => handleMenuItemClick(e, viewer.name)}>{viewer.name}</MenuItem>;
47+
return (
48+
<MenuItem key={viewer.name} onClick={e => handleMenuItemClick(e, viewer.name)}>
49+
{viewer.name}
50+
</MenuItem>
51+
);
4852
})}
4953
</Menu>
5054
</>

src/components/metrics/MetricsChart.tsx

+24-25
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import React, { useState } from 'react';
22
import { graphql } from 'babel-plugin-relay/macro';
33
import Typography from '@mui/material/Typography';
44
import { makeStyles } from '@mui/styles';
5-
import { createFragmentContainer } from 'react-relay';
5+
import { useFragment } from 'react-relay';
66
import 'react-vis/dist/style.css';
77
import { FlexibleWidthXYPlot, Hint, LineSeries, VerticalGridLines, XAxis, YAxis } from 'react-vis';
88
import Chip from '@mui/material/Chip';
99
import { formatDuration } from '../../utils/time';
10-
import { MetricsChart_chart } from './__generated__/MetricsChart_chart.graphql';
10+
import { MetricsChart_chart$key } from './__generated__/MetricsChart_chart.graphql';
1111

1212
const useStyles = makeStyles(theme => {
1313
return {
@@ -16,7 +16,7 @@ const useStyles = makeStyles(theme => {
1616
});
1717

1818
interface Props {
19-
chart: MetricsChart_chart;
19+
chart: MetricsChart_chart$key;
2020
}
2121

2222
function intervals(intervalIncrement, maxValue) {
@@ -29,32 +29,48 @@ function intervals(intervalIncrement, maxValue) {
2929
return result;
3030
}
3131

32-
function MetricsChart(props: Props) {
32+
export default function MetricsChart(props: Props) {
3333
let [hoveredPointIndex, setHoveredPointIndex] = useState(null);
34-
let { chart } = props;
3534
let classes = useStyles();
35+
let chart = useFragment(
36+
graphql`
37+
fragment MetricsChart_chart on MetricsChart {
38+
title
39+
dataUnits
40+
points {
41+
date {
42+
year
43+
month
44+
day
45+
}
46+
value
47+
}
48+
}
49+
`,
50+
props.chart,
51+
);
3652

3753
function _onNearestX(value, { index }) {
3854
setHoveredPointIndex(index);
3955
}
4056

4157
function formatValue(value) {
42-
if (props.chart.dataUnits === 'seconds') {
58+
if (chart.dataUnits === 'seconds') {
4359
return formatDuration(value);
4460
}
4561
return value;
4662
}
4763

4864
function intermediateValues() {
49-
let points = props.chart.points || [];
65+
let points = chart.points || [];
5066
let maxValue = 0;
5167
for (let point of points) {
5268
if (maxValue < point.value) {
5369
maxValue = point.value;
5470
}
5571
}
5672

57-
if (props.chart.dataUnits === 'seconds') {
73+
if (chart.dataUnits === 'seconds') {
5874
if (maxValue < 60) return null;
5975
// 10 minutes
6076
if (maxValue < 10 * 60) return intervals(60, maxValue);
@@ -118,20 +134,3 @@ function MetricsChart(props: Props) {
118134
</div>
119135
);
120136
}
121-
122-
export default createFragmentContainer(MetricsChart, {
123-
chart: graphql`
124-
fragment MetricsChart_chart on MetricsChart {
125-
title
126-
dataUnits
127-
points {
128-
date {
129-
year
130-
month
131-
day
132-
}
133-
value
134-
}
135-
}
136-
`,
137-
});
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
import React from 'react';
22

3-
import { QueryRenderer } from 'react-relay';
3+
import { useLazyLoadQuery } from 'react-relay';
44
import { graphql } from 'babel-plugin-relay/macro';
5-
6-
import environment from '../../createRelayEnvironment';
75
import MetricsChart from './MetricsChart';
8-
import CirrusLinearProgress from '../common/CirrusLinearProgress';
9-
import { RepositoryMetricsChartsQuery } from './__generated__/RepositoryMetricsChartsQuery.graphql';
6+
import {
7+
RepositoryMetricsChartsQuery,
8+
RepositoryMetricsChartsQueryVariables,
9+
} from './__generated__/RepositoryMetricsChartsQuery.graphql';
1010

11-
const RepositoryMetricsCharts = props => (
12-
<QueryRenderer<RepositoryMetricsChartsQuery>
13-
environment={environment}
14-
variables={{
15-
repositoryId: props.repositoryId,
16-
parameters: props.parameters,
17-
}}
18-
query={graphql`
11+
export default function RepositoryMetricsCharts(props: RepositoryMetricsChartsQueryVariables) {
12+
const response = useLazyLoadQuery<RepositoryMetricsChartsQuery>(
13+
graphql`
1914
query RepositoryMetricsChartsQuery($repositoryId: ID!, $parameters: MetricsQueryParameters!) {
2015
repository(id: $repositoryId) {
2116
metrics(parameters: $parameters) {
2217
...MetricsChart_chart
2318
}
2419
}
2520
}
26-
`}
27-
render={({ error, props }) => {
28-
if (error) {
29-
console.log(error);
30-
}
31-
if (!props) {
32-
return <CirrusLinearProgress />;
33-
}
34-
let metrics = props.repository.metrics || [];
35-
let chartComponents = metrics.map((chart, index) => <MetricsChart key={index} chart={chart} />);
36-
return <div>{chartComponents}</div>;
37-
}}
38-
/>
39-
);
40-
41-
export default RepositoryMetricsCharts;
21+
`,
22+
props,
23+
);
24+
let metrics = response.repository.metrics || [];
25+
let chartComponents = metrics.map((chart, index) => <MetricsChart key={index} chart={chart} />);
26+
return <div>{chartComponents}</div>;
27+
}

0 commit comments

Comments
 (0)