fix: 🐛 timezones not localised
This commit is contained in:
@@ -1,31 +1,50 @@
|
|||||||
|
import type { AgChartOptions, FormatterParams } from "ag-charts-community";
|
||||||
|
import { AgCharts } from "ag-charts-react";
|
||||||
import {
|
import {
|
||||||
AllCommunityModule,
|
AllCommunityModule,
|
||||||
ColDef,
|
type ColDef,
|
||||||
ModuleRegistry,
|
ModuleRegistry,
|
||||||
|
type PaginationChangedEvent,
|
||||||
|
type RowDataUpdatedEvent,
|
||||||
ValidationModule,
|
ValidationModule,
|
||||||
ValueFormatterParams,
|
type ValueFormatterParams,
|
||||||
RowDataUpdatedEvent,
|
|
||||||
PaginationChangedEvent,
|
|
||||||
} from "ag-grid-community";
|
} from "ag-grid-community";
|
||||||
import { AgChartOptions, FormatterParams } from "ag-charts-community";
|
|
||||||
import { AgCharts } from "ag-charts-react";
|
|
||||||
|
|
||||||
// Register all Community features
|
// Register all Community features
|
||||||
|
|
||||||
import { AgGridReact } from "ag-grid-react";
|
import { AgGridReact } from "ag-grid-react";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { IRow, Row, autumnTheme, paginationOptions } from "./components/AGGrid";
|
|
||||||
import { useAnalyticsContext } from "./AnalyticsContext";
|
import { useAnalyticsContext } from "./AnalyticsContext";
|
||||||
|
import {
|
||||||
|
autumnTheme,
|
||||||
|
type IRow,
|
||||||
|
paginationOptions,
|
||||||
|
type Row,
|
||||||
|
} from "./components/AGGrid";
|
||||||
import { RowClickDialog } from "./components/RowClickDialog";
|
import { RowClickDialog } from "./components/RowClickDialog";
|
||||||
|
|
||||||
|
const userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||||
|
|
||||||
|
// Helper function to parse UTC timestamps from the backend
|
||||||
|
const parseUTCTimestamp = (timestamp: string): Date => {
|
||||||
|
// If the timestamp doesn't end with 'Z' or have timezone info, assume it's UTC
|
||||||
|
if (!timestamp.includes('Z') && !timestamp.includes('+') && !timestamp.includes('-', 10)) {
|
||||||
|
// Add 'Z' to indicate UTC if it's missing
|
||||||
|
return new Date(timestamp + (timestamp.includes('T') ? 'Z' : ' UTC'));
|
||||||
|
}
|
||||||
|
return new Date(timestamp);
|
||||||
|
};
|
||||||
|
|
||||||
const dateFormatter = new Intl.DateTimeFormat(navigator.language || "en-US", {
|
const dateFormatter = new Intl.DateTimeFormat(navigator.language || "en-US", {
|
||||||
month: "short",
|
month: "short",
|
||||||
day: "numeric",
|
day: "numeric",
|
||||||
|
timeZone: userTimeZone,
|
||||||
});
|
});
|
||||||
|
|
||||||
const hourFormatter = new Intl.DateTimeFormat(navigator.language || "en-US", {
|
const hourFormatter = new Intl.DateTimeFormat(navigator.language || "en-US", {
|
||||||
hour: "numeric",
|
hour: "numeric",
|
||||||
minute: "numeric",
|
minute: "numeric",
|
||||||
|
timeZone: userTimeZone,
|
||||||
});
|
});
|
||||||
|
|
||||||
const timestampFormatter = new Intl.DateTimeFormat(
|
const timestampFormatter = new Intl.DateTimeFormat(
|
||||||
@@ -36,7 +55,8 @@ const timestampFormatter = new Intl.DateTimeFormat(
|
|||||||
hour: "numeric",
|
hour: "numeric",
|
||||||
minute: "numeric",
|
minute: "numeric",
|
||||||
second: "numeric",
|
second: "numeric",
|
||||||
}
|
timeZone: userTimeZone,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
export function EventsBarChart({
|
export function EventsBarChart({
|
||||||
@@ -101,8 +121,8 @@ export function EventsBarChart({
|
|||||||
x: (params: FormatterParams<any, unknown>) => {
|
x: (params: FormatterParams<any, unknown>) => {
|
||||||
if (params.type !== "category") return;
|
if (params.type !== "category") return;
|
||||||
return selectedInterval === "24h"
|
return selectedInterval === "24h"
|
||||||
? hourFormatter.format(new Date(params.value as string))
|
? hourFormatter.format(parseUTCTimestamp(params.value as string))
|
||||||
: dateFormatter.format(new Date(params.value as string));
|
: dateFormatter.format(parseUTCTimestamp(params.value as string));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
legend: {
|
legend: {
|
||||||
@@ -110,14 +130,12 @@ export function EventsBarChart({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const chartData = data.data;
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setOptions({
|
setOptions((prevOptions) => ({
|
||||||
...options,
|
...prevOptions,
|
||||||
data: data.data,
|
data: data.data,
|
||||||
series: chartConfig,
|
series: chartConfig,
|
||||||
});
|
}));
|
||||||
}, [chartConfig, data]);
|
}, [chartConfig, data]);
|
||||||
|
|
||||||
return <AgCharts options={options} className="h-full w-full" />;
|
return <AgCharts options={options} className="h-full w-full" />;
|
||||||
@@ -132,7 +150,7 @@ export function EventsAGGrid({ data }: { data: any }) {
|
|||||||
field: "timestamp",
|
field: "timestamp",
|
||||||
flex: 1,
|
flex: 1,
|
||||||
valueFormatter: (params: ValueFormatterParams<any, unknown>) => {
|
valueFormatter: (params: ValueFormatterParams<any, unknown>) => {
|
||||||
return timestampFormatter.format(new Date(params.value as string));
|
return timestampFormatter.format(parseUTCTimestamp(params.value as string));
|
||||||
},
|
},
|
||||||
cellStyle: {
|
cellStyle: {
|
||||||
paddingLeft: "2.5rem",
|
paddingLeft: "2.5rem",
|
||||||
|
|||||||
Reference in New Issue
Block a user