Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize timezone dropdown with virtualization and memoization #9896

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -3,6 +3,7 @@ import { findAvailableTimeZoneOption } from '@/localization/utils/findAvailableT
import { AVAILABLE_TIMEZONE_OPTIONS } from '@/settings/accounts/constants/AvailableTimezoneOptions';

import { Select } from '@/ui/input/components/Select';
import { VirtualizedSelect } from '@/ui/input/components/VirtualizedSelect';

type SettingsAccountsCalendarTimeZoneSelectProps = {
value?: string;
Expand All @@ -13,7 +14,17 @@ export const SettingsAccountsCalendarTimeZoneSelect = ({
value = detectTimeZone(),
onChange,
}: SettingsAccountsCalendarTimeZoneSelectProps) => (
<Select
// <Select
// dropdownId="settings-accounts-calendar-time-zone"
// dropdownWidth={416}
// label="Time zone"
// fullWidth
// value={findAvailableTimeZoneOption(value)?.value}
// options={AVAILABLE_TIMEZONE_OPTIONS}
// onChange={onChange}
// withSearchInput
// />
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Remove commented out code since it's in version control

<VirtualizedSelect
dropdownId="settings-accounts-calendar-time-zone"
dropdownWidth={416}
label="Time zone"
Expand All @@ -22,5 +33,7 @@ export const SettingsAccountsCalendarTimeZoneSelect = ({
options={AVAILABLE_TIMEZONE_OPTIONS}
onChange={onChange}
withSearchInput
itemHeight={40}
maxHeight={300}
/>
);
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
/* eslint-disable @nx/workspace-max-consts-per-file */
import { memoize } from 'lodash';
import { getTimezoneOffset } from 'date-fns-tz';
import { IANA_TIME_ZONES } from '@/localization/constants/IanaTimeZones';
import { formatTimeZoneLabel } from '@/localization/utils/formatTimeZoneLabel';
import { SelectOption } from '@/ui/input/components/Select';

import { AVAILABLE_TIME_ZONE_OPTIONS_BY_LABEL } from '@/settings/accounts/constants/AvailableTimezoneOptionsByLabel';
type TimezoneSelectOption = SelectOption<string> & {
offset: number; // Add the offset property
};

export const AVAILABLE_TIMEZONE_OPTIONS = Object.values(
AVAILABLE_TIME_ZONE_OPTIONS_BY_LABEL,
).sort((optionA, optionB) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const difference =
getTimezoneOffset(optionA.value) - getTimezoneOffset(optionB.value);
export const createTimeZoneOptions = memoize(() => {
const timeZoneOptionsMap = IANA_TIME_ZONES.reduce<Record<string, TimezoneSelectOption>>(
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: memoizing this function won't help with performance since it's only called once at module initialization - the AVAILABLE_TIMEZONE_OPTIONS constant already serves as a cache

(result, ianaTimeZone) => {
const timeZoneLabel = formatTimeZoneLabel(ianaTimeZone);
const timeZoneName = timeZoneLabel.slice(11);

Copy link
Contributor

Choose a reason for hiding this comment

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

style: slicing at index 11 is fragile - consider using a regex or split() to extract the timezone name more reliably

return difference === 0
? // Sort alphabetically if the time zone offsets are the same.
optionA.label.localeCompare(optionB.label)
: // Sort by time zone offset if different.
difference;
if (
timeZoneName.includes('GMT') ||
timeZoneName.includes('UTC') ||
timeZoneName.includes('UCT') ||
timeZoneLabel in result
) {
return result;
}

return {
...result,
[timeZoneLabel]: {
label: timeZoneLabel,
value: ianaTimeZone,
// Pre-calculate offset to avoid doing it during sorting
offset: getTimezoneOffset(ianaTimeZone)
},
};
},
{}
);

return Object.values(timeZoneOptionsMap).sort((a, b) => {
const difference = a.offset - b.offset;
return difference === 0 ? a.label.localeCompare(b.label) : difference;
});

});

export const AVAILABLE_TIMEZONE_OPTIONS = createTimeZoneOptions();
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const StyledLabel = styled.span`
margin-bottom: ${({ theme }) => theme.spacing(1)};
`;

export const Select = <Value extends SelectValue>({
export const Select = <Value extends SelectValue,>({
className,
disabled: disabledFromProps,
selectSizeVariant,
Expand Down
Loading
Loading