-
Notifications
You must be signed in to change notification settings - Fork 18
feat: Support switching timezone of log event timestamp (resolves #207). #251
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
Draft
zzxthehappiest
wants to merge
12
commits into
y-scope:main
Choose a base branch
from
zzxthehappiest:zzx/feat-207
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8720b2f
Init, add some TODOs
zzxthehappiest c154ff1
Add a timezone drop down
zzxthehappiest be8e0c3
Add the selected timezone to the URL parameter
zzxthehappiest 1bc4ed4
Pass the selected timezone to worker so that we can format the tz there
zzxthehappiest 8808871
Finish the basic timestamp switching
zzxthehappiest 546eb3c
Fix lint
zzxthehappiest 6702581
Merge branch 'main' into zzx/feat-207
zzxthehappiest c721100
Apply on the latest code
zzxthehappiest 6c67f2e
Fix lint
zzxthehappiest 16e4639
Disable prettify and timezone button when fast loading to fix #249)
zzxthehappiest 654212a
Merge remote-tracking branch 'yscope/main' into zzx/feat-207
zzxthehappiest e703ddb
Fix
zzxthehappiest 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,31 @@ | ||
/* Since timezone is not multi select, and multi select has a different style in Joy components, so we have to manually | ||
tune the style | ||
*/ | ||
.timezone-select { | ||
margin-right: 1px; | ||
} | ||
|
||
.timezone-select-render-value-box { | ||
display: flex; | ||
gap: 2px; | ||
} | ||
|
||
.timezone-select-render-value-box-label { | ||
padding: 0 !important; | ||
|
||
font-size: 0.95rem !important; | ||
font-weight: 500 !important; | ||
line-height: 1.5 !important; | ||
|
||
background-color: initial !important; | ||
border: none !important; | ||
box-shadow: none !important; | ||
} | ||
|
||
.timezone-select-render-value-box-label-disabled { | ||
color: #686f76 !important; | ||
} | ||
|
||
.timezone-select-listbox { | ||
max-height: calc(100vh - var(--ylv-menu-bar-height) - var(--ylv-status-bar-height)) !important; | ||
} |
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,222 @@ | ||
import React, { | ||
useCallback, | ||
useEffect, | ||
useState, | ||
} from "react"; | ||
|
||
import {SelectValue} from "@mui/base/useSelect"; | ||
import { | ||
Box, | ||
Chip, | ||
ListDivider, | ||
ListItemContent, | ||
Option, | ||
Select, | ||
SelectOption, | ||
} from "@mui/joy"; | ||
|
||
import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp"; | ||
|
||
import useUiStore from "../../../stores/uiStore"; | ||
import useViewStore from "../../../stores/viewStore.ts"; | ||
import {UI_ELEMENT} from "../../../typings/states"; | ||
import {HASH_PARAM_NAMES} from "../../../typings/url"; | ||
import {isDisabled} from "../../../utils/states"; | ||
import {updateWindowUrlHashParams} from "../../../utils/url"; | ||
|
||
import "./index.css"; | ||
|
||
|
||
const LOGGER_TIMEZONE = "Logger Timezone"; | ||
const COMMON_TIMEZONES = [ | ||
"America/New_York", | ||
"Asia/Shanghai", | ||
"Asia/Tokyo", | ||
"Australia/Sydney", | ||
"Pacific/Honolulu", | ||
"America/Los_Angeles", | ||
"America/Chicago", | ||
"America/Denver", | ||
"Asia/Kolkata", | ||
"Europe/Berlin", | ||
"Europe/Moscow", | ||
"Asia/Dubai", | ||
"Asia/Singapore", | ||
"Asia/Seoul", | ||
"Pacific/Auckland", | ||
]; | ||
|
||
/** | ||
* Convert the timezone string to GMT +/- minutes | ||
* | ||
* @param tz | ||
* @return The GMT +/- minutes shown before the timezone string | ||
*/ | ||
const getLongOffsetOfTimezone = (tz: string): string => { | ||
return new Intl.DateTimeFormat("default", { | ||
timeZone: tz, | ||
timeZoneName: "longOffset", | ||
}).formatToParts() | ||
.find((p) => "timeZoneName" === p.type)?.value ?? "Unknown timezone"; | ||
}; | ||
|
||
/** | ||
* Render the selected timezone option in the status bar | ||
* | ||
* @param selected | ||
* @return The selected timezone shown in the status bar | ||
*/ | ||
const handleRenderValue = (selected: SelectValue<SelectOption<string>, false>) => ( | ||
<Box className={"timezone-select-render-value-box"}> | ||
<Chip className={"timezone-select-render-value-box-label"}>Timezone</Chip> | ||
<Chip> | ||
{selected?.label} | ||
</Chip> | ||
</Box> | ||
); | ||
|
||
/** | ||
* Render the timezone options in the dropdown menu | ||
* | ||
* @param value | ||
* @param label | ||
* @param onClick | ||
* @param suffix | ||
* @return An option box in the dropdown menu | ||
*/ | ||
const renderTimezoneOption = ( | ||
value: string, | ||
label: string, | ||
onClick: React.MouseEventHandler, | ||
suffix?: string | ||
) => ( | ||
<Option | ||
data-value={value} | ||
key={value} | ||
value={value} | ||
onClick={onClick} | ||
> | ||
{LOGGER_TIMEZONE !== value && | ||
<ListItemContent> | ||
( | ||
{getLongOffsetOfTimezone(value)} | ||
) | ||
{" "} | ||
{label} | ||
{" "} | ||
{suffix ?? ""} | ||
</ListItemContent>} | ||
|
||
{LOGGER_TIMEZONE === value && | ||
<ListItemContent> | ||
{LOGGER_TIMEZONE} | ||
</ListItemContent>} | ||
</Option> | ||
); | ||
|
||
/** | ||
* The timezone select dropdown menu, the selectable options can be classified as three types: | ||
* - Default (use the origin timezone of the log events) | ||
* - Browser Timezone (use the timezone that the browser is currently using) | ||
* - Frequently-used Timezone | ||
* | ||
* @return A timezone select dropdown menu | ||
*/ | ||
const TimezoneSelect = () => { | ||
const uiState = useUiStore((state) => state.uiState); | ||
|
||
const {logTimezone} = useViewStore.getState(); | ||
const updateLogTimezone = useViewStore((state) => state.updateLogTimezone); | ||
|
||
const [browserTimezone, setBrowserTimezone] = useState<string | null>(null); | ||
const [selectedTimezone, setSelectedTimezone] = useState<string | null>(null); | ||
|
||
const disabled = isDisabled(uiState, UI_ELEMENT.TIMEZONE_SETTER); | ||
|
||
useEffect(() => { | ||
const tz = new Intl.DateTimeFormat().resolvedOptions().timeZone; | ||
setBrowserTimezone(tz); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!disabled && selectedTimezone !== logTimezone) { | ||
const updatedTimezone = (LOGGER_TIMEZONE === selectedTimezone) ? | ||
"" : | ||
(selectedTimezone ?? ""); | ||
|
||
updateWindowUrlHashParams({ | ||
[HASH_PARAM_NAMES.LOG_TIMEZONE]: updatedTimezone, | ||
}); | ||
updateLogTimezone(updatedTimezone); | ||
} | ||
}, [ | ||
disabled, | ||
logTimezone, | ||
selectedTimezone, | ||
updateLogTimezone, | ||
]); | ||
|
||
const handleOptionClick = useCallback((ev: React.MouseEvent) => { | ||
const currentTarget = ev.currentTarget as HTMLElement; | ||
const value = currentTarget.dataset.value ?? LOGGER_TIMEZONE; | ||
setSelectedTimezone(value); | ||
}, []); | ||
|
||
return ( | ||
<Select | ||
className={"timezone-select"} | ||
disabled={disabled} | ||
indicator={<KeyboardArrowUpIcon/>} | ||
renderValue={handleRenderValue} | ||
size={"sm"} | ||
value={selectedTimezone} | ||
variant={"soft"} | ||
placeholder={ | ||
<Box className={"timezone-select-render-value-box"}> | ||
<Chip | ||
className={`timezone-select-render-value-box-label ${disabled ? | ||
"timezone-select-render-value-box-label-disabled" : | ||
""}`} | ||
> | ||
Timezone | ||
</Chip> | ||
</Box> | ||
} | ||
slotProps={{ | ||
listbox: { | ||
className: "timezone-select-listbox", | ||
placement: "top-end", | ||
modifiers: [ | ||
{name: "equalWidth", enabled: false}, | ||
{name: "offset", enabled: false}, | ||
], | ||
}, | ||
}} | ||
onChange={(_, value) => { | ||
if (value) { | ||
setSelectedTimezone(value); | ||
} | ||
}} | ||
> | ||
{renderTimezoneOption(LOGGER_TIMEZONE, LOGGER_TIMEZONE, handleOptionClick)} | ||
|
||
{browserTimezone && | ||
renderTimezoneOption( | ||
browserTimezone, | ||
browserTimezone, | ||
handleOptionClick, | ||
"(Browser Timezone)" | ||
)} | ||
|
||
<ListDivider | ||
inset={"gutter"} | ||
role={"separator"}/> | ||
|
||
{COMMON_TIMEZONES.map( | ||
(label) => renderTimezoneOption(label, label, handleOptionClick) | ||
)} | ||
</Select> | ||
); | ||
}; | ||
|
||
export default TimezoneSelect; |
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
Oops, something went wrong.
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.
🛠️ Refactor suggestion
Add error handling for invalid timezone strings.
The function doesn't handle invalid timezone strings gracefully. While there's a fallback to "Unknown timezone", it would be better to validate the timezone before attempting to use it.
📝 Committable suggestion