-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); |
There was a problem hiding this comment.
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