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
fix: Made timestamp consistant #46
fix: Made timestamp consistant #46
Conversation
const selectedRow = selectedEl.closest('tr'); | ||
if (!selectedEl) { | ||
return; | ||
} |
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.
When we have multiple activity log page opened at a time, multiple context menu event listeners are being assigned but we get only one selectedEl
(from where the context event has been called)
Update: opened another PR #47 , as this change is unrelated to this PR. Removed this change from here.
dbbf38d
to
ba05db8
Compare
src/lib/ext-listen.js
Outdated
export function dateTimeFormat(timestamp) { | ||
const date = new Date(timestamp); | ||
const month = date.getMonth() + 1; | ||
return `${date.getDate()}-${month}-${date.getFullYear()} ${date.toLocaleTimeString()}`; |
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.
dd-MM-YYYY
looks reasonable to me because the format is the same as where I am from (The Netherlands), but it is not universally true.
Since you're trying to have locale-aware time strings, I suggest to be consistent.
Use the Intl.DateTimeFormat
API to format the date in a locale-aware way - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat
@@ -33,14 +35,15 @@ export class FilterTimestamp extends HTMLElement { | |||
|
|||
const timeStamp = this.timeStamp || {}; | |||
|
|||
const chosenTimestamp = selectedRow.querySelector('.timestamp').textContent; | |||
const chosenTimestamp = selectedRow.querySelector('.timestamp')?._timestamp; |
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.
Use selectedRow._log.timeStamp
instead.
I would rather not have an external class using an internal _log
property, but it is preferable to your current approach, of adding a _timeStamp
property to an unrelated DOM element.
To reduce the risk of future regressions, it would be nice to have a unit test that verifies that the time labels are rendered as expected.
561655c
to
bcf8de9
Compare
105def4
to
511fd3f
Compare
511fd3f
to
f52a0f0
Compare
src/lib/ext-listen.js
Outdated
|
||
export function dateTimeFormat(timestamp, options) { | ||
const dateTime = new Date(timestamp); | ||
const time = dateTime.toLocaleTimeString(); |
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.
DateTimeFormat
also supports parameters to include the time in the generated string. Do not use toLocaleTimeString
, but only DateTimeFormat
.
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.
done :-)
tests/ext-activitylog.test.js
Outdated
}); | ||
|
||
document.body.innerHTML = activityLogBody; | ||
const dateTimeFormatFn = jest.spyOn(ExtListen, 'dateTimeFormat'); |
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.
You shouldn't mock your function, but the API that you're using. The only changes should be:
- fix
'en-US'
as the locale instead ofundefined
. - fix the
timeZone
option.
By doing both, the generated time strings become fully deterministic / predictable, and you can put the expected times in strings below.
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.
done :-) Thanks
tests/ext-activitylog.test.js
Outdated
expect(dateTimeFormatFn).toHaveBeenCalled(); | ||
|
||
const dateTime = new Date(logTimestamp); | ||
const expectedTime = dateTime.toLocaleTimeString(); |
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.
Hard-code the expected strings instead of generating it based on the dynamic objects.
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.
done :-)
src/lib/ext-listen.js
Outdated
@@ -31,3 +31,19 @@ export function openActivityLogPage() { | |||
url: getActivityLogPageURL(), | |||
}); | |||
} | |||
|
|||
export function dateTimeFormat(timestamp, options) { |
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.
Why did you put this function in ext-listen.js
instead of a new file called, say, formatters.js
?
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.
yes, made a new file formatters.js
src/lib/formatters.js
Outdated
dateTime | ||
); | ||
|
||
return `${date} ${time}`; |
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.
Don't create two DateTimeFormat
instances followed by string concatenation. Let the value of timeFormatOptions
be conditional based on options?.timeOnly
.
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.
done :-)
tests/ext-activitylog.test.js
Outdated
// To have consistant date time format, we choose "en-US" date time formatting and UTC timezone. | ||
IntlDateTimeFormatFn.mockImplementation(function (zone, options) { | ||
Object.assign(options, timeZone); | ||
this.format = (date) => |
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.
Don't replace the format
method; just let the constructor return new originalIntlDateTimeFormat('en-US', options);
.
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.
done :-)
0f67e05
to
b613cef
Compare
tests/ext-activitylog.test.js
Outdated
const { activityLog } = new ActivityLog(); | ||
// To have consistant date time format, we choose "en-US" date time formatting and UTC timezone. | ||
IntlDateTimeFormatFn.mockImplementation((zone, options) => { | ||
Object.assign(options, timeZone); |
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.
Mocks should not modify the input object, as that may have side effects.
Do this instead:
options = {...options, timeZone: 'UTC'}
@@ -34,6 +34,9 @@ export default class ExtensionMonitor { | |||
|
|||
createLogListener() { | |||
return async (details) => { | |||
// set a timestamp(number) as the value of timeStamp property | |||
details.timeStamp = Date.parse(details.timeStamp); |
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.
Could you add the next comment:
// TODO: Stop using `Date.parse` when `details.timeStamp` is a numeric timestamp.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1660460
Fixes #41
log.timeStamp
contains timestamp (number).8:49:31 PM
(hh:mm:ss am/pm
)log-view
, it shows tooltip in the format17-8-2020 8:49:31 PM
17-8-2020 8:49:31 PM
Screenshot