Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
majakomel committed Aug 18, 2023
1 parent 230baa8 commit b55a298
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 49 deletions.
1 change: 0 additions & 1 deletion components/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const Chart = React.memo(function Chart({testGroup = null, queryParams = {}, set
data={chartData}
rowKeys={rowKeys}
rowLabels={rowLabels}
isGrouped={false}
/>
{!!chartData?.size && <MATLink query={queryParams} />}
</>
Expand Down
1 change: 0 additions & 1 deletion components/DomainChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const Chart = React.memo(function Chart({testGroup = null, queryParams = {}, set
data={chartData}
rowKeys={rowKeys}
rowLabels={rowLabels}
isGrouped={false}
/>
{!!chartData?.size && <MATLink query={linkParams} />}
</>
Expand Down
35 changes: 23 additions & 12 deletions components/MATChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import { axiosResponseTime } from 'components/axios-plugins'
import axios from 'axios'
import { MATContextProvider } from 'components/aggregation/mat/MATContext'
import { useIntl } from 'react-intl'
import { ResizableBox } from './aggregation/mat/Resizable'
import FormattedMarkdown from './FormattedMarkdown'

const baseURL = process.env.NEXT_PUBLIC_OONI_API
axiosResponseTime(axios)

const swrOptions = {
Expand Down Expand Up @@ -42,7 +43,7 @@ const fetcher = (query) => {
})
}

export const MATChartReportWrapper = ({link}) => {
export const MATChartReportWrapper = ({link, caption}) => {
let searchParams
const today = dayjs.utc().add(1, 'day')
const monthAgo = dayjs.utc(today).subtract(1, 'month')
Expand All @@ -64,29 +65,39 @@ export const MATChartReportWrapper = ({link}) => {
...searchParams,
}

return searchParams ? <MATChart query={query} /> : <bold>WRONG LINK FORMAT</bold>
return searchParams ?
<>
<MATChart query={query} />
{caption && <FormattedMarkdown>{caption}</FormattedMarkdown>}
</> : <bold>WRONG LINK FORMAT</bold>
}

const MATChart = ({ query }) => {
const intl = useIntl()
const { data, error, isValidating } = useSWR(query ? query : null, fetcher, swrOptions)

const showLoadingIndicator = useMemo(() => isValidating, [isValidating])

return (
<Box my={3}>
<Box>
<MATContextProvider queryParams={query}>
{error && <NoCharts message={error?.info ?? JSON.stringify(error)} />}
<Box sx={{ minHeight: '500px' }}>
{showLoadingIndicator && (
<Box>
{showLoadingIndicator ? (
<Box>
<h2>{intl.formatMessage({ id: 'General.Loading' })}</h2>
</Box>
)}
{data && data.data.dimension_count == 0 && <FunnelChart data={data.data.result} />}
{data && data.data.dimension_count == 1 && <StackedBarChart data={data} query={query} />}
{data && data.data.dimension_count > 1 && (
<TableView data={data.data.result} query={query} />
) : (
<>
{data?.data?.result?.length > 0 ?
<Box sx={{ minHeight: '500px' }}>
{data && data.data.dimension_count == 0 && <FunnelChart data={data.data.result} />}
{data && data.data.dimension_count == 1 && <StackedBarChart data={data.data.result} query={query} />}
{data && data.data.dimension_count > 1 && (
<TableView data={data.data.result} query={query} />
)}
</Box> : <ResizableBox><NoCharts /></ResizableBox>
}
</>
)}
</Box>
</MATContextProvider>
Expand Down
6 changes: 1 addition & 5 deletions components/aggregation/mat/GridChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ export const prepareDataForGridChart = (data, query, locale) => {
*
* selectedRows - a subset of `rowKeys` representing which rows to render in the grid
*
* isGrouped - Whether the data is already grouped by y-axis value
* If `false`, `reshapeChartData()` will group the data as required
*
* height - uses a specific height provided by the container (e.g ResizableBox)
* If not speicied, it calculates a height based on the number of rows, capped
* at GRID_MAX_HEIGHT, which allows <VirtualRows> to render a subset of the data
Expand All @@ -89,7 +86,7 @@ export const prepareDataForGridChart = (data, query, locale) => {
* header - an element showing some summary information on top of the charts
}
*/
const GridChart = ({ data, rowKeys, rowLabels, isGrouped = true, height = 'auto', header, selectedRows = null, noLabels = false }) => {
const GridChart = ({ data, rowKeys, rowLabels, height = 'auto', header, selectedRows = null, noLabels = false }) => {

// Fetch query state from context instead of router
// because some params not present in the URL are injected in the context
Expand Down Expand Up @@ -178,7 +175,6 @@ GridChart.propTypes = {
rowKeys: PropTypes.arrayOf(PropTypes.string),
rowLabels: PropTypes.objectOf(PropTypes.string),
selectedRows: PropTypes.arrayOf(PropTypes.string),
isGrouped: PropTypes.bool,
height: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
Expand Down
30 changes: 16 additions & 14 deletions components/aggregation/mat/Resizable.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import React, { useEffect, useCallback, useState } from 'react'
import { Box } from 'ooni-components'
import styled from 'styled-components'
import { useResizeDetector } from 'react-resize-detector'
// import { useResizeDetector } from 'react-resize-detector'

export const ResizableYBox = styled(Box)`
position: relative;
border: 2px solid ${props => props.theme.colors.gray1};
// border: 2px solid ${props => props.theme.colors.gray1};
/* Disabled resizability because of the onResize loop bug */
/* resize: vertical; */
min-height: ${props => props.minHeight ?? 250}px;
// min-height: ${props => props.minHeight ?? 250}px;
/* max-height: 100vh; */
padding: 16px;
// padding: 16px;
`

export const ResizableBox = ({ children, onResize, ...props}) => {
const { width, height, ref } = useResizeDetector({
onResize,
handleWidth: false,
skipOnMount: true,
refreshMode: 'debounce',
refreshRate: 500,
})
// const { width, height, ref } = useResizeDetector({
// onResize,
// handleWidth: false,
// skipOnMount: true,
// refreshMode: 'debounce',
// refreshRate: 500,
// })
return (
<ResizableYBox ref={ref} {...props}>
// <ResizableYBox ref={ref} {...props}>
// {children}
// </ResizableYBox>
<Box>
{children}
</ResizableYBox>
</Box>
)
}
6 changes: 3 additions & 3 deletions components/aggregation/mat/RowChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ const chartProps1D = (query, intl) => ({
round: false
},
margin: {
top: 50,
right: 30,
top: 30,
right: 20,
bottom: 100,
left: 80
left: 70
},
padding: 0.3,
borderColor: { from: 'color', modifiers: [ [ 'darker', 1.6 ] ] },
Expand Down
6 changes: 3 additions & 3 deletions components/aggregation/mat/StackedBarChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { NoCharts } from './NoCharts'

const ChartContainer = styled(Flex)`
position: relative;
border: 2px solid ${props => props.theme.colors.gray1};
padding: 16px;
// border: 2px solid ${props => props.theme.colors.gray1};
// padding: 16px;
`

export const StackedBarChart = ({ data, query }) => {
const intl = useIntl()

try {
const [gridData, rows ] = prepareDataForGridChart(data.data.result, query, intl.locale)
const [gridData, rows ] = prepareDataForGridChart(data, query, intl.locale)

return (
<ChartContainer flexDirection={['column']}>
Expand Down
1 change: 0 additions & 1 deletion components/aggregation/mat/TableView.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const TableView = ({ data, query }) => {
selectedRows={dataForCharts}
rowKeys={rowKeys}
rowLabels={rowLabels}
isGrouped={true}
/>
</ResizableBox>
</Flex>
Expand Down
1 change: 0 additions & 1 deletion components/dashboard/Charts.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ const Chart = React.memo(function Chart({ testName }) {
data={chartData}
rowKeys={rowKeys}
rowLabels={rowLabels}
isGrouped={false}
header={headerOptions}
/>
)}
Expand Down
30 changes: 23 additions & 7 deletions components/incidents/ReportDisplay.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Heading, Flex, Box } from 'ooni-components'
import { Heading, Flex, Box, Text } from 'ooni-components'
import { Badge } from 'components/Badge'
import Flag from 'components/Flag'
import Markdown from 'markdown-to-jsx'
import { MATChartReportWrapper } from 'components/MATChart'
import { getLocalisedRegionName } from '../../utils/i18nCountries'
import { useIntl } from 'react-intl'

const FormattedMarkdown = ({ children }) => {
return (
Expand All @@ -19,14 +22,29 @@ const FormattedMarkdown = ({ children }) => {
)
}

const formatDate = (date, locale) => {
return new Intl.DateTimeFormat(locale, { dateStyle: 'long' }).format(new Date(date))
}

const ReportDisplay = ({ report }) => {
const intl = useIntl()

return (
<>
<Heading h={1}>{report?.title}</Heading>
<Heading h={1} mt={4} mb={3} sx={{borderBottom: '1px solid', borderColor: 'gray3'}}>{report?.title}</Heading>
{/* <p>Published: {report.published}</p> */}
<p>Created by {report?.reported_by}</p>
<p>Start time: {report?.start_time}</p>
<p>End time: {report?.end_time}</p>
{/* {!!report?.CCs?.length && <p>Countries: {report.CCs.join(', ')}</p>} */}
{/* {!!report?.ASNs?.length && <p>ASNs: {report.ASNs.join(', ')}</p>} */}
{!!report?.CCs?.length && (
<Flex alignItems="center">
<Flag countryCode={report.CCs[0]} size={32} />
<Heading h={3} ml={2}>
{getLocalisedRegionName(report.CCs[0], intl.locale)} {!!report?.ASNs?.length && <>({report.ASNs.map((as) => (`AS${as}`)).join(', ')})</>}
</Heading>
</Flex>
)}
<p>{formatDate(report?.start_time, intl.locale)} - {report?.end_time ? formatDate(report?.end_time, intl.locale) : 'ongoing'}</p>
<p>created by {report?.reported_by}</p>
{!!report?.tags?.length && (
<Flex>
{report.tags.map((tag) => (
Expand All @@ -36,8 +54,6 @@ const ReportDisplay = ({ report }) => {
))}
</Flex>
)}
{!!report?.ASNs?.length && <p>ASNs: {report.ASNs.join(', ')}</p>}
{!!report?.CCs?.length && <p>Countries: {report.CCs.join(', ')}</p>}
{!!report?.domains?.length && <p>Domains: {report.domains.join(', ')}</p>}
<Box mt={3}>{report?.text && <FormattedMarkdown>{report.text}</FormattedMarkdown>}</Box>
</>
Expand Down
1 change: 0 additions & 1 deletion components/measurement/nettests/WebConnectivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { Tick, Cross } from 'ooni-components/icons'
import deepmerge from 'deepmerge'
import styled from 'styled-components'
import dayjs from 'services/dayjs'

import { FormattedMessage, defineMessages, useIntl } from 'react-intl'

Expand Down

0 comments on commit b55a298

Please sign in to comment.