Repository navigation
feat(desktop): searchable timezone dropdown in settings #38607
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import { useMemo, useState } from 'react' | ||
|
|
||
| import { Command, CommandEmpty, CommandInput, CommandItem, CommandList } from '@/components/ui/command' | ||
| import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' | ||
| import { Check, ChevronDown, World } from '@/lib/icons' | ||
| import { cn } from '@/lib/utils' | ||
|
|
||
| // Maximum rows rendered at once. cmdk isn't virtualized and the full IANA list | ||
| // is ~400 entries, so we cap the visible set and lean on the search box to | ||
| // narrow things down. A footer hint appears whenever the list is capped. | ||
| const MAX_VISIBLE = 150 | ||
|
|
||
| interface TimezoneOption { | ||
| /** IANA identifier, e.g. "America/New_York". */ | ||
| id: string | ||
| /** Human label including the current UTC offset, e.g. "America/New_York (GMT-5)". */ | ||
| label: string | ||
| } | ||
|
|
||
| function offsetLabel(timeZone: string): string { | ||
| try { | ||
| const parts = new Intl.DateTimeFormat('en-US', { timeZone, timeZoneName: 'shortOffset' }).formatToParts(new Date()) | ||
|
|
||
| return parts.find(p => p.type === 'timeZoneName')?.value ?? '' | ||
| } catch { | ||
| return '' | ||
| } | ||
| } | ||
|
|
||
| function listTimeZones(): string[] { | ||
| // supportedValuesOf is available in modern Electron/Chromium; guard anyway so | ||
| // an older runtime degrades to a plain text entry rather than throwing. | ||
| const supported = (Intl as { supportedValuesOf?: (key: string) => string[] }).supportedValuesOf | ||
|
|
||
| if (typeof supported === 'function') { | ||
| try { | ||
| return supported('timeZone') | ||
| } catch { | ||
| /* fall through */ | ||
| } | ||
| } | ||
|
|
||
| return [] | ||
| } | ||
|
|
||
| function toOption(id: string): TimezoneOption { | ||
| const offset = offsetLabel(id) | ||
|
|
||
| return { id, label: offset ? `${id} (${offset})` : id } | ||
| } | ||
|
|
||
| export function TimezoneCombobox({ | ||
| value, | ||
| onChange, | ||
| className | ||
| }: { | ||
| value: string | ||
| onChange: (value: string) => void | ||
| className?: string | ||
| }) { | ||
| const [open, setOpen] = useState(false) | ||
| const [search, setSearch] = useState('') | ||
|
|
||
| // The IANA list never changes for the lifetime of the runtime, so build it | ||
| // (and its offset labels) once rather than on every selection. | ||
| const baseOptions = useMemo<TimezoneOption[]>(() => listTimeZones().map(toOption), []) | ||
|
|
||
| const options = useMemo<TimezoneOption[]>(() => { | ||
| // Surface a previously-saved value that the runtime doesn't enumerate | ||
| // (custom or deprecated zone) so it stays selectable. | ||
| if (value && !baseOptions.some(o => o.id === value)) { | ||
| return [toOption(value), ...baseOptions] | ||
| } | ||
|
|
||
| return baseOptions | ||
| }, [baseOptions, value]) | ||
|
|
||
| const query = search.trim().toLowerCase() | ||
|
|
||
| const filtered = useMemo(() => { | ||
| if (!query) { | ||
| return options.slice(0, MAX_VISIBLE) | ||
| } | ||
|
|
||
| const hits: TimezoneOption[] = [] | ||
|
|
||
| for (const option of options) { | ||
| if (option.label.toLowerCase().includes(query) || option.id.replace(/_/g, ' ').toLowerCase().includes(query)) { | ||
| hits.push(option) | ||
|
|
||
| if (hits.length >= MAX_VISIBLE) { | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return hits | ||
| }, [options, query]) | ||
|
|
||
| // Keep "System default" filterable so a non-matching query can reach the | ||
| // empty state instead of leaving a lone, always-present row. | ||
| const showSystemDefault = !query || 'system default'.includes(query) | ||
| const trimmed = filtered.length >= MAX_VISIBLE | ||
|
|
||
| const select = (next: string) => { | ||
| onChange(next) | ||
| setOpen(false) | ||
| setSearch('') | ||
| } | ||
|
|
||
| const triggerLabel = value ? (options.find(o => o.id === value)?.label ?? value) : 'System default' | ||
|
|
||
| return ( | ||
| <Popover | ||
| onOpenChange={next => { | ||
| setOpen(next) | ||
|
|
||
| if (!next) { | ||
| setSearch('') | ||
| } | ||
| }} | ||
| open={open} | ||
| > | ||
| <PopoverTrigger | ||
| aria-expanded={open} | ||
| className={cn( | ||
| 'flex h-8 w-full items-center justify-between gap-2 rounded-lg border border-input bg-background px-3 py-2 text-sm whitespace-nowrap shadow-xs outline-none transition-[color,box-shadow] focus-visible:border-ring focus-visible:ring-[0.1875rem] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50', | ||
| className | ||
| )} | ||
| role="combobox" | ||
| type="button" | ||
| > | ||
| <span className={cn('flex min-w-0 items-center gap-2', !value && 'text-muted-foreground')}> | ||
| <World className="size-3.5 shrink-0 opacity-60" /> | ||
| <span className="truncate">{triggerLabel}</span> | ||
| </span> | ||
| <ChevronDown className="size-4 shrink-0 opacity-60" /> | ||
| </PopoverTrigger> | ||
| <PopoverContent align="end" className="w-[min(22rem,90vw)] p-0"> | ||
| <Command shouldFilter={false}> | ||
| <CommandInput onValueChange={setSearch} placeholder="Search timezones..." value={search} /> | ||
|
Collaborator
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. Please source this and the other new visible strings (System default, empty state, truncation hint) from the desktop i18n catalog. The desktop has Japanese and Chinese catalogs, whose existing timezone descriptions also need the System-default wording update. |
||
| <CommandList> | ||
| <CommandEmpty>No timezone found.</CommandEmpty> | ||
| {showSystemDefault && ( | ||
| <CommandItem onSelect={() => select('')} value="__system_default__"> | ||
| <Check className={cn('size-4 shrink-0', value ? 'opacity-0' : 'opacity-100')} /> | ||
| <span className="truncate">System default</span> | ||
| </CommandItem> | ||
| )} | ||
| {filtered.map(option => ( | ||
| <CommandItem className="font-mono" key={option.id} onSelect={() => select(option.id)} value={option.id}> | ||
| <Check className={cn('size-4 shrink-0', value === option.id ? 'opacity-100' : 'opacity-0')} /> | ||
| <span className="truncate">{option.label}</span> | ||
| </CommandItem> | ||
| ))} | ||
| {trimmed && ( | ||
| <div className="px-3 py-2 text-xs text-muted-foreground"> | ||
| Showing the first {MAX_VISIBLE}. Keep typing to narrow results. | ||
| </div> | ||
| )} | ||
| </CommandList> | ||
| </Command> | ||
| </PopoverContent> | ||
| </Popover> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { Popover as PopoverPrimitive } from 'radix-ui' | ||
| import * as React from 'react' | ||
|
|
||
| import { cn } from '@/lib/utils' | ||
|
|
||
| function Popover({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Root>) { | ||
| return <PopoverPrimitive.Root data-slot="popover" {...props} /> | ||
| } | ||
|
|
||
| function PopoverTrigger({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Trigger>) { | ||
| return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} /> | ||
| } | ||
|
|
||
| function PopoverAnchor({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Anchor>) { | ||
| return <PopoverPrimitive.Anchor data-slot="popover-anchor" {...props} /> | ||
| } | ||
|
|
||
| function PopoverContent({ | ||
| className, | ||
| align = 'center', | ||
| sideOffset = 4, | ||
| ...props | ||
| }: React.ComponentProps<typeof PopoverPrimitive.Content>) { | ||
| return ( | ||
| <PopoverPrimitive.Portal> | ||
| <PopoverPrimitive.Content | ||
| align={align} | ||
| className={cn( | ||
| 'z-[140] w-72 origin-(--radix-popover-content-transform-origin) rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=top]:slide-in-from-bottom-2 data-[side=right]:slide-in-from-left-2', | ||
| className | ||
| )} | ||
| data-slot="popover-content" | ||
| sideOffset={sideOffset} | ||
| {...props} | ||
| /> | ||
| </PopoverPrimitive.Portal> | ||
| ) | ||
| } | ||
|
|
||
| export { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This does not provide the comment's promised plain-text fallback: with no
Intl.supportedValuesOf, the option list is empty and the user can only choose System default. Please retain a validated text-entry path or supply a fallback canonical zone list, and cover this branch with a test.