Skip to content

Commit

Permalink
refactor: Realtime Doughnout Charts to TS (#33092)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinSchoeler authored Aug 23, 2024
1 parent 927710d commit c2609d7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
13 changes: 6 additions & 7 deletions apps/meteor/app/livechat/client/lib/chartHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,9 @@ export const drawDoughnutChart = async (
chartContext: { destroy: () => void } | undefined,
dataLabels: string[],
dataPoints: number[],
): Promise<ChartType<'doughnut', number[], string> | void> => {
): Promise<ChartType> => {
if (!chart) {
console.error('No chart element');
return;
throw new Error('No chart element');
}
if (chartContext) {
chartContext.destroy();
Expand All @@ -200,7 +199,7 @@ export const drawDoughnutChart = async (
],
},
options: doughnutChartConfiguration(title),
});
}) as ChartType;
};

/**
Expand All @@ -209,12 +208,12 @@ export const drawDoughnutChart = async (
* @param {String} label [chart label]
* @param {Array(Double)} data [updated data]
*/
export const updateChart = async (c: ChartType, label: string, data: { [x: string]: number }): Promise<void> => {
export const updateChart = async (c: ChartType, label: string, data: number[]): Promise<void> => {
const chart = await c;
if (chart.data?.labels?.indexOf(label) === -1) {
// insert data
chart.data.labels.push(label);
chart.data.datasets.forEach((dataset: { data: any[] }, idx: string | number) => {
chart.data.datasets.forEach((dataset: { data: any[] }, idx: number) => {
dataset.data.push(data[idx]);
});
} else {
Expand All @@ -224,7 +223,7 @@ export const updateChart = async (c: ChartType, label: string, data: { [x: strin
return;
}

chart.data.datasets.forEach((dataset: { data: { [x: string]: any } }, idx: string | number) => {
chart.data.datasets.forEach((dataset: { data: { [x: string]: any } }, idx: number) => {
dataset.data[index] = data[idx];
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { OperationParams } from '@rocket.chat/rest-typings';
import type { TranslationContextValue, TranslationKey } from '@rocket.chat/ui-contexts';
import { useTranslation } from '@rocket.chat/ui-contexts';
import type { Chart as ChartType } from 'chart.js';
import type { MutableRefObject } from 'react';
import React, { useRef, useEffect } from 'react';

import { drawDoughnutChart } from '../../../../../app/livechat/client/lib/chartHandler';
Expand All @@ -16,20 +20,25 @@ const initialData = {
offline: 0,
};

const init = (canvas, context, t) =>
const init = (canvas: HTMLCanvasElement, context: ChartType | undefined, t: TranslationContextValue['translate']): Promise<ChartType> =>
drawDoughnutChart(
canvas,
t('Agents'),
context,
labels.map((l) => t(l)),
labels.map((l) => t(l as TranslationKey)),
Object.values(initialData),
);

const AgentStatusChart = ({ params, reloadRef, ...props }) => {
type AgentStatusChartsProps = {
params: OperationParams<'GET', '/v1/livechat/analytics/dashboards/charts/agents-status'>;
reloadRef: MutableRefObject<{ [x: string]: () => void }>;
};

const AgentStatusChart = ({ params, reloadRef, ...props }: AgentStatusChartsProps) => {
const t = useTranslation();

const canvas = useRef();
const context = useRef();
const canvas: MutableRefObject<HTMLCanvasElement | null> = useRef(null);
const context: MutableRefObject<ChartType | undefined> = useRef();

const updateChartData = useUpdateChartData({
context,
Expand All @@ -46,7 +55,9 @@ const AgentStatusChart = ({ params, reloadRef, ...props }) => {

useEffect(() => {
const initChart = async () => {
context.current = await init(canvas.current, context.current, t);
if (canvas?.current) {
context.current = await init(canvas.current, context.current, t);
}
};
initChart();
}, [t]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { OperationParams } from '@rocket.chat/rest-typings';
import type { TranslationContextValue, TranslationKey } from '@rocket.chat/ui-contexts';
import { useTranslation } from '@rocket.chat/ui-contexts';
import type { Chart as ChartType } from 'chart.js';
import type { MutableRefObject } from 'react';
import React, { useRef, useEffect } from 'react';

import { drawDoughnutChart } from '../../../../../app/livechat/client/lib/chartHandler';
Expand All @@ -16,20 +20,25 @@ const initialData = {
closed: 0,
};

const init = (canvas, context, t) =>
const init = (canvas: HTMLCanvasElement, context: ChartType | undefined, t: TranslationContextValue['translate']) =>
drawDoughnutChart(
canvas,
t('Chats'),
context,
labels.map((l) => t(l)),
labels.map((l) => t(l as TranslationKey)),
Object.values(initialData),
);

const ChatsChart = ({ params, reloadRef, ...props }) => {
type ChatsChartProps = {
params: OperationParams<'GET', '/v1/livechat/analytics/dashboards/charts/chats'>;
reloadRef: MutableRefObject<{ [x: string]: () => void }>;
};

const ChatsChart = ({ params, reloadRef, ...props }: ChatsChartProps) => {
const t = useTranslation();

const canvas = useRef();
const context = useRef();
const canvas: MutableRefObject<HTMLCanvasElement | null> = useRef(null);
const context: MutableRefObject<ChartType | undefined> = useRef();

const updateChartData = useUpdateChartData({
context,
Expand All @@ -46,7 +55,9 @@ const ChatsChart = ({ params, reloadRef, ...props }) => {

useEffect(() => {
const initChart = async () => {
context.current = await init(canvas.current, context.current, t);
if (canvas?.current) {
context.current = await init(canvas.current, context.current, t);
}
};
initChart();
}, [t]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import type { TranslationContextValue } from '@rocket.chat/ui-contexts';
import { type Chart } from 'chart.js';
import { type TFunction } from 'i18next';
import { type RefObject } from 'react';
import { type MutableRefObject } from 'react';

import { updateChart } from '../../../../../app/livechat/client/lib/chartHandler';

type UseUpdateChartDataOptions = {
context: RefObject<Chart | undefined>;
canvas: RefObject<HTMLCanvasElement | null>;
init: (canvas: HTMLCanvasElement, context: undefined, t: TFunction) => Promise<Chart>;
t: TFunction;
context: MutableRefObject<Chart | undefined>;
canvas: MutableRefObject<HTMLCanvasElement | null>;
init: (canvas: HTMLCanvasElement, context: undefined, t: TranslationContextValue['translate']) => Promise<Chart>;
t: TranslationContextValue['translate'];
};

export const useUpdateChartData = ({ context: contextRef, canvas: canvasRef, init, t }: UseUpdateChartDataOptions) =>
useMutableCallback(async (label: string, data: { [x: string]: number }) => {
useMutableCallback(async (label: string, data: number[]) => {
const canvas = canvasRef.current;

if (!canvas) {
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/locales/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@
"Channels_list": "List of public channels",
"Channel_what_is_this_channel_about": "What is this channel about?",
"Chart": "Chart",
"Chats": "Chats",
"Chat_button": "Chat button",
"Chat_close": "Chat Close",
"Chat_closed": "Chat closed",
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/locales/pt-BR.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@
"Channels_list": "Lista de canais públicos",
"Channel_what_is_this_channel_about": "Sobre o que é este canal?",
"Chart": "Gráfico",
"Chats": "Conversas",
"Chat_button": "Botão da conversa",
"Chat_close": "Fechar conversa",
"Chat_closed": "Conversa encerrada",
Expand Down

0 comments on commit c2609d7

Please sign in to comment.