1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00
unleash.unleash/frontend/src/component/insights/componentsChart/useBatchedTooltipDate.ts
Thomas Heartman 7044cd4b1a
feat: Add batching functionality to new flags in production (#10756)
Adds the same batching functionality that was added to the
archived:created chart to the new flags in production chart.

In doing so, I've extracted the batching algorithm and the batched
tooltip title creation, as well as the ChartDataResult type (though
naming suggestions are still welcome on that front).

Locale 'ja':
<img width="1143" height="370" alt="image"
src="https://github.com/user-attachments/assets/827b41c6-0e67-46f4-8f82-4ba12e2120bb"
/>

Locale 'no':
<img width="1475" height="554" alt="image"
src="https://github.com/user-attachments/assets/6125c318-25fb-42bd-a520-44e6a7f7ece7"
/>
2025-10-08 12:40:11 +00:00

36 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useLocationSettings } from 'hooks/useLocationSettings';
import { getDateFnsLocale } from '../getDateFnsLocale.ts';
import type { ChartTypeRegistry, TooltipItem } from 'chart.js';
import { format, startOfWeek } from 'date-fns';
export const useBatchedTooltipDate = <T extends keyof ChartTypeRegistry>(
fallback: string = 'Unknown date range',
) => {
const { locationSettings } = useLocationSettings();
const locale = getDateFnsLocale(locationSettings.locale);
return (datapoints: TooltipItem<T>[]) => {
const dataPoint = datapoints[0].raw as any;
if (
'date' in dataPoint &&
typeof dataPoint.date === 'string' &&
'endDate' in dataPoint &&
typeof dataPoint.endDate === 'string'
) {
const startDate = format(
startOfWeek(new Date(dataPoint.date), {
locale,
weekStartsOn: 1,
}),
`PP`,
{ locale },
);
const endDate = format(new Date(dataPoint.endDate), `PP`, {
locale,
});
return `${startDate} ${endDate}`;
}
return fallback;
};
};