Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {VictoryChartRenderArgsProvider} from '@components/HTMLEngineProvider/HTM
import getChartDesignWidth from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getChartDesignWidth';
import getChartLayoutModeProps from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getChartLayoutModeProps';
import getHierarchyID from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getHierarchyID';
import useCurrentTimezone from '@hooks/useCurrentTimezone';
import VictoryChartLabel from './VictoryChartLabel';
import VictoryChartLegend from './VictoryChartLegend';
import VictoryChartSeries from './VictoryChartSeries';
Expand All @@ -21,6 +22,7 @@ type VictoryChartCartesianProps = {
*/
function VictoryChartCartesian({explicitSize, headless}: VictoryChartCartesianProps) {
Comment thread
luacmartins marked this conversation as resolved.
Resolved
Comment thread
luacmartins marked this conversation as resolved.
Resolved
const {tnode, data, xKey, yKeys, xAxis, yAxis, domain, domainPadding, padding, isHorizontal, labelItems, legendItems, chartContentStyles} = useVictoryChartContext();
const timezone = useCurrentTimezone();
const designWidth = getChartDesignWidth(explicitSize, chartContentStyles.width);

return (
Expand All @@ -39,8 +41,9 @@ function VictoryChartCartesian({explicitSize, headless}: VictoryChartCartesianPr
<VictoryChartRenderArgsProvider value={renderArgs}>
{labelItems.map((labelItem) => (
<VictoryChartLabel
key={`label-${labelItem.x}-${labelItem.y}`}
key={`label-${labelItem.x}-${labelItem.y}-${timezone}`}
{...labelItem}
timezone={timezone}
/>
))}
{legendItems.map((legendItem) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import {useChartTypefaces} from '@components/Charts/context/ChartFontsContext';
import getChartSkiaTypeface from '@components/Charts/utils/getChartSkiaTypeface';
import type {LabelItem} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/types';
import computeTextAnchorPosition from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/computeTextAnchorPosition';
import {getLocalizedVictoryChartLabelText} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/localizeVictoryChartLabelText';
import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';

type VictoryChartLabelsProps = LabelItem;
type VictoryChartLabelsProps = LabelItem & {
timezone?: SelectedTimezone;
};

type ProcessedLine = {
lineX: number;
Expand All @@ -21,9 +25,10 @@ type ProcessedLine = {
* Renders floating Skia text labels (from `<victorylabel>` nodes) over the chart canvas.
* Intended for use inside CartesianChart's `renderOutside` callback.
*/
function VictoryChartLabel({x, y, text, color, fontSize, fontWeight, fontFamily, fontStyle, lineHeight, textAnchor = 'start', verticalAnchor = 'middle'}: VictoryChartLabelsProps) {
function VictoryChartLabel({x, y, text, color, fontSize, fontWeight, fontFamily, fontStyle, lineHeight, textAnchor = 'start', verticalAnchor = 'middle', timezone}: VictoryChartLabelsProps) {
const typefaces = useChartTypefaces();
const processedLines = text.split('\n').reduce(
const displayText = getLocalizedVictoryChartLabelText(text, timezone);
const processedLines = displayText.split('\n').reduce(
Comment thread
luacmartins marked this conversation as resolved.
Resolved
(acc, line, index) => {
const lineColor = color?.[index];
const lineFontSize = fontSize?.[index];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {useVictoryChartContext} from '@components/HTMLEngineProvider/HTMLRendere
import getChartDesignWidth from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getChartDesignWidth';
import getChartLayoutModeProps from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getChartLayoutModeProps';
import getHierarchyID from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/getHierarchyID';
import useCurrentTimezone from '@hooks/useCurrentTimezone';
import VictoryChartCategories from './VictoryChartCategories';
import VictoryChartLabel from './VictoryChartLabel';
import VictoryChartLegend from './VictoryChartLegend';
Expand All @@ -20,6 +21,7 @@ type VictoryChartPolarProps = {
*/
function VictoryChartPolar({explicitSize, headless}: VictoryChartPolarProps) {
Comment thread
luacmartins marked this conversation as resolved.
Resolved
Comment thread
luacmartins marked this conversation as resolved.
Resolved
const {tnode, data, labelItems, legendItems, chartContentStyles} = useVictoryChartContext();
const timezone = useCurrentTimezone();
const designWidth = getChartDesignWidth(explicitSize, chartContentStyles.width);

const chartContent = (
Expand All @@ -32,8 +34,9 @@ function VictoryChartPolar({explicitSize, headless}: VictoryChartPolarProps) {
))}
{labelItems.map((labelItem) => (
<VictoryChartLabel
key={`label-${labelItem.x}-${labelItem.y}`}
key={`label-${labelItem.x}-${labelItem.y}-${timezone}`}
{...labelItem}
timezone={timezone}
/>
))}
{legendItems.map((legendItem) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {isValid, parse} from 'date-fns';
import DateUtils from '@libs/DateUtils';
import CONST from '@src/CONST';
import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';

const AS_OF_LABEL_PATTERN = /^As of:\s*(.+)$/i;
const CHART_DISPLAY_FORMAT = `MMM d, yyyy 'at' ${CONST.DATE.LOCAL_TIME_FORMAT}`;

/** Matches server `EXP_CHAT_COMMENT_DATETIME` (`M j, Y \a\t h:i A`) with and without a leading zero on the hour. */
const SERVER_AS_OF_PARSE_FORMATS = ['MMM d, yyyy hh:mm aa', 'MMM d, yyyy h:mm aa'] as const;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make sure that server formatted date doesn't have timezone at the end (i.e. "As of: May 30, 2026 12:30 PM PT"


/**
* Parses a Victory chart "As of" datetime label text into a UTC date.
*/
function parseDateAsUTC(sourceText: string): Date | null {
const normalizedText = sourceText
.trim()
.replace(/\s+at\s+/i, ' ')
.trim();

for (const formatStr of SERVER_AS_OF_PARSE_FORMATS) {
const parsed = parse(normalizedText, formatStr, new Date());
Comment thread
luacmartins marked this conversation as resolved.
Resolved

if (!isValid(parsed)) {
continue;
}

return new Date(Date.UTC(parsed.getFullYear(), parsed.getMonth(), parsed.getDate(), parsed.getHours(), parsed.getMinutes(), parsed.getSeconds()));
}

return null;
}

/**
* Rewrites a `<victorylabel>` "As of: ..." string in the viewer's timezone.
* Returns the original text when the label does not match or cannot be parsed.
*/
function getLocalizedVictoryChartLabelText(text: string, timezone?: SelectedTimezone): string {
if (!timezone) {
return text;
}

const match = text.trim().match(AS_OF_LABEL_PATTERN);
if (!match) {
return text;
}

const utcDate = parseDateAsUTC(match[1]);
if (!utcDate) {
return text;
}

const localizedDate = DateUtils.formatInTimeZoneWithFallback(utcDate, timezone, CHART_DISPLAY_FORMAT);
return `As of: ${localizedDate}`;
}

export {getLocalizedVictoryChartLabelText, parseDateAsUTC};
11 changes: 11 additions & 0 deletions src/hooks/useCurrentTimezone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import DateUtils from '@libs/DateUtils';
import CONST from '@src/CONST';
import type {SelectedTimezone} from '@src/types/onyx/PersonalDetails';
import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails';

function useCurrentTimezone(): SelectedTimezone {
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
return DateUtils.getCurrentTimezone(currentUserPersonalDetails?.timezone ?? CONST.DEFAULT_TIME_ZONE).selected;
}

export default useCurrentTimezone;
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {getLocalizedVictoryChartLabelText, parseDateAsUTC} from '@components/HTMLEngineProvider/HTMLRenderers/VictoryChartRenderer/utils/localizeVictoryChartLabelText';

describe('localizeVictoryChartLabelText', () => {
describe('parseDateAsUTC', () => {
it('parses server UTC chart timestamps', () => {
const utcDate = parseDateAsUTC('Jun 5, 2026 at 06:47 PM');
expect(utcDate?.toISOString()).toBe('2026-06-05T18:47:00.000Z');
});

it('parses server UTC chart timestamps without a leading zero on the hour', () => {
const utcDate = parseDateAsUTC('Jun 12, 2026 at 8:48 AM');
expect(utcDate?.toISOString()).toBe('2026-06-12T08:48:00.000Z');
});

it('parses server UTC chart timestamps with a leading zero on the hour', () => {
expect(parseDateAsUTC('Jun 12, 2026 at 08:48 AM')?.toISOString()).toBe('2026-06-12T08:48:00.000Z');
expect(parseDateAsUTC('Jun 12, 2026 at 02:48 PM')?.toISOString()).toBe('2026-06-12T14:48:00.000Z');
});

it('returns null for invalid timestamps', () => {
expect(parseDateAsUTC('not a date')).toBeNull();
});
});

describe('getLocalizedVictoryChartLabelText', () => {
it('rewrites As of labels in the viewer timezone without a timezone label', () => {
expect(getLocalizedVictoryChartLabelText('As of: Jun 5, 2026 at 06:47 PM', 'America/Los_Angeles')).toBe('As of: Jun 5, 2026 at 11:47 AM');
expect(getLocalizedVictoryChartLabelText('As of: Jun 5, 2026 at 06:47 PM', 'Asia/Tokyo')).toBe('As of: Jun 6, 2026 at 3:47 AM');
expect(getLocalizedVictoryChartLabelText('As of: Jun 12, 2026 at 8:48 AM', 'America/Edmonton')).toBe('As of: Jun 12, 2026 at 2:48 AM');
expect(getLocalizedVictoryChartLabelText('As of: Jun 12, 2026 at 8:48 AM', 'Asia/Tokyo')).toBe('As of: Jun 12, 2026 at 5:48 PM');
expect(getLocalizedVictoryChartLabelText('As of: Jun 12, 2026 at 02:48 PM', 'America/Edmonton')).toBe('As of: Jun 12, 2026 at 8:48 AM');
expect(getLocalizedVictoryChartLabelText('As of: Jun 12, 2026 at 02:48 PM', 'Asia/Tokyo')).toBe('As of: Jun 12, 2026 at 11:48 PM');
});

it('leaves non-As-of labels unchanged', () => {
expect(getLocalizedVictoryChartLabelText('Top employees by spend', 'America/Los_Angeles')).toBe('Top employees by spend');
});

it('returns the original text when no timezone is provided', () => {
expect(getLocalizedVictoryChartLabelText('As of: Jun 5, 2026 at 06:47 PM')).toBe('As of: Jun 5, 2026 at 06:47 PM');
});

it('returns the original text when the timestamp cannot be parsed', () => {
expect(getLocalizedVictoryChartLabelText('As of: not a date', 'America/Los_Angeles')).toBe('As of: not a date');
});
});
});
Loading