import { useTranslation } from 'react-i18next'; import styled from '@emotion/styled';…
- User
import { useTranslation } from 'react-i18next'; import styled from '@emotion/styled'; import { useTheme } from '@mui/material'; import { MouseEventHandler, PieTooltipProps, ResponsivePie } from '@nivo/pie'; import { formatCurrency, formatValue } from 'app/utils/numbers'; import { useLocale } from 'app/features/Localisation';
const LegendContainer = styled('div', { shouldForwardProp: (prop) => !['pointColor'].includes(prop), })<{ pointColor: string }>` width: 250px; @media only screen and (max-width: 550px) { width: 215px; } display: flex; flex-direction: column; align-items: center; justify-content: center;
padding: 15px; border-radius: 4px; font-size: 14px; {({ theme }) => ` box-shadow: {theme.shadows[3]}; background-color: ${theme.palette.background.paper}; `}
div.label { font-weight: 700; padding-bottom: {({ theme }) => theme.spacing(3)}; ::after { height: 18px; width: 18px; content: ' '; position: absolute; display: inline; margin: auto; margin-left: 12px; border-radius: 50px; background-color: {({ pointColor }) => pointColor}; box-shadow: 0 0 0 2px rgb(230, 230, 230), 0 0 0 3px rgb(160, 160, 160); } } div.values { display: flex; flex-direction: column; justify-content: center; align-items: center; gap: ${({ theme }) => theme.spacing(1)}; } `;
export type NivoPieChartData = { id: string; label: string; value?: number; quantity: number; color?: string; };
const ChartTooltip = ({ datum, }: PieTooltipProps): JSX.Element => { const { t } = useTranslation(['common', 'journals']); const { locale } = useLocale();
return ( {datum.label}
<div className="values"> {datum.data.value ? ( <span> {t('journals:hub-view.pie-chart.tooltip.value', { value: t('common:display.value.currency', { symbol: locale.currency.symbol, value: formatCurrency(datum.data.value, locale.type), }), })} </span> ) : null} <span> {t('journals:hub-view.pie-chart.tooltip.quantity', { value: datum.data.quantity, })} </span> </div> </LegendContainer>); };
export const NivoPieChart = ({ data, displayValue, onClick, }: { data: NivoPieChartData[]; displayValue: 'value' | 'quantity'; onClick: MouseEventHandler<NivoPieChartData, SVGPathElement>; }): JSX.Element => { const theme = useTheme(); const { locale } = useLocale(); const { t } = useTranslation(['common', 'journals']);
return ( <ResponsivePie activeOuterRadiusOffset={8} arcLinkLabel={({ label, value }) =>
${label} (${ displayValue === 'value' ? t('common:display.value.currency', { symbol: locale.currency.symbol, value: formatCurrency(value, locale.type), }) : formatValue(value, locale.type) })} value={displayValue} arcLinkLabelsColor={{ from: 'color' }} arcLinkLabelsDiagonalLength={6} arcLinkLabelsOffset={6} arcLinkLabelsSkipAngle={13} arcLinkLabelsStraightLength={6} arcLinkLabelsTextColor={{ theme: 'labels.text.fill' }} arcLinkLabelsThickness={2} colors={{ scheme: 'category10' }} cornerRadius={10} data={data} enableArcLabels={false} innerRadius={0.25} margin={{ top: 25, right: 50, bottom: 50, left: 50 }} padAngle={2} sortByValue theme={theme.nivo} tooltip={ChartTooltip} onClick={onClick} /> ); }; - Assistant
Your code looks well-organized and follows best practices for structuring React code. Here are a few tips for further improvement:
-
Utilize TypeScript types: You can define types for props and state to make your code more explicit and readable.
-
Extract repeated strings into a separate file: If you have repeated strings that are used throughout the component, it might be better to extract them into a separate file and import them instead of typing them manually every time.
-
Use localization libraries: While your code uses localization, you could consider using popular libraries like react-intl or i18next to manage translations in a more comprehensive way.
-
Break down into smaller components: Your component has multiple levels of presentation (e.g. LegendContainer, ChartTooltip) and functionality (e.g. NivoPieChart). Consider breaking these down into smaller, reusable components for a more modular codebase.
-
Use CSS-in-JS libraries: Your component uses Emotion for styling, which is a great choice. However, other CSS-in-JS libraries like styled-components, Fela, or Glamorous can also be used to achieve similar results.
-
- User
- Assistant
Your code looks mostly good, but there are a few bits of repetition that could be reduced:
-
Method for handling filter data: Your
getFilterValuemethod has a lot of repetition in its switch statement that could be condensed. Instead of usingreturnfor each case, you could list out the case values and return only once at the end. -
Repeated property structure: The properties of your
ChartDatatype seem to have a similar structure (e.g. count and value). You could consider extracting this structure into a separate type and using it as a property type. -
Repeated error handling: Instead of having separate checks for
financialSalesData.errorandgraphData.groupedByStore.length, you could combine these into one statement using optional chaining and nullish coalescing. For example:financialSalesData.error ?? (graphData.groupedByStore.length === 0 && t('common:response.no-data')).
By implementing these changes, you could make the code more concise and DRY.
-
- User
- Method for handling filter data: Your getFilterValue method has a lot of repetition in its switch statement that could be condensed. Instead of using return for each case, you could list out the case values and return only once at the end.
How do you do this? Give an example
- Assistant
Sure! Here's an example of how you could modify the
getFilterValuemethod:function getFilterValue(key: SelectedKey) { const filterValues: Record<SelectedKey, string[] | string> = { discount: ['DISCA##', 'DISCO##', 'DISCP##', 'MARKDOWN##'], noSale: 'NOSALE##', reduced: 'PriceOverride##', refund: 'REFUND##', void: 'VOID##', voidAll: 'VOIDA##', }; return filterValues[key]; }In this example, we define an object called
filterValuesthat maps eachSelectedKeyvalue to its corresponding filter value(s). We then simply return the value from this object based on thekeyinput, eliminating the need for a switch statement.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.