Skip to content

Commit

Permalink
fix(poll): Wrap overly long poll options
Browse files Browse the repository at this point in the history
  • Loading branch information
JoVictorNunes committed Jan 13, 2025
1 parent eae6fcb commit f425e30
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';

function getAverageCharacterWidth(text: string, font: string) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');

if (!ctx) return null;

ctx.font = font;

const textWidth = ctx.measureText(text).width;
const averageAdvance = textWidth / text.length;

return averageAdvance;
}

const TICK_SIZE = 6;
const AVERAGE_CHAR_WIDTH = getAverageCharacterWidth('0', 'Source Sans Pro') ?? 6;
const ELLIPSIS = '...';

const CustomizedAxisTick = (props: any) => {
const { payload, ...restProps } = props;
const { width } = restProps;
const numberOfChars = Math.floor((width - TICK_SIZE) / AVERAGE_CHAR_WIDTH);
const restValue = payload.value.substring(numberOfChars, payload.value.length);

return (
<g>
<text {...restProps}>
{payload.value.substring(0, restValue.length > 0 ? numberOfChars - ELLIPSIS.length : numberOfChars)}
{restValue.length > 0 && ELLIPSIS}
</text>
</g>
);
};

export default CustomizedAxisTick;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React from 'react'
import { Bar, BarChart, ResponsiveContainer, XAxis, YAxis } from 'recharts'
import { TLUiTranslationKey } from '../../../ui/hooks/useTranslation/TLUiTranslationKey'
import CustomizedAxisTick from './CustomizedAxisTick';
import Styled from './styles'

const caseInsensitiveReducer = (acc: any[], item: { key: string; numVotes: number }) => {
Expand Down Expand Up @@ -89,7 +90,7 @@ const ChatPollContent: React.FC<ChatPollContentProps> = ({
<ResponsiveContainer width="90%" height={useHeight}>
<BarChart data={translatedAnswers} layout="vertical">
<XAxis type="number" allowDecimals={false} />
<YAxis width={80} type="category" dataKey="pollAnswer" />
<YAxis width={80} type="category" dataKey="pollAnswer" tick={<CustomizedAxisTick />} />
<Bar dataKey="numVotes" fill="#0C57A7" />
</BarChart>
</ResponsiveContainer>
Expand Down

0 comments on commit f425e30

Please sign in to comment.