-
Notifications
You must be signed in to change notification settings - Fork 22
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
#2865: Various notification improvements #3531
Changes from all commits
a00e28d
9b5666c
bb024d3
9be44f1
7933796
d58a629
e0fdec8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import reportError from "@/telemetry/reportError"; | |
import { Except, RequireAtLeastOne } from "type-fest"; | ||
import { getErrorMessage } from "@/errors"; | ||
import { truncate } from "lodash"; | ||
import { SIDEBAR_WIDTH_CSS_PROPERTY } from "@/contentScript/sidebar"; | ||
|
||
const MINIMUM_NOTIFICATION_DURATION = 2000; | ||
|
||
|
@@ -71,8 +72,8 @@ const Message: React.VoidFunctionComponent<{ | |
id: string; | ||
dismissable: boolean; | ||
}> = ({ message, id, dismissable }) => ( | ||
<span> | ||
{message} | ||
<> | ||
<span className={styles.message}>{message}</span> | ||
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. |
||
{dismissable ? ( | ||
<button | ||
className={styles.closeButton} | ||
|
@@ -83,12 +84,12 @@ const Message: React.VoidFunctionComponent<{ | |
× | ||
</button> | ||
) : undefined} | ||
</span> | ||
</> | ||
); | ||
|
||
function getMessageDisplayTime(message: string): number { | ||
const wpm = 100; // 180 is the average words read per minute, make it slower | ||
return Math.min( | ||
return Math.max( | ||
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. Fix Notification timer (it was being ignored)Fixed: All the notifications without a specified duration would last 2 seconds regardless of length |
||
MINIMUM_NOTIFICATION_DURATION, | ||
(message.split(" ").length / wpm) * 60_000 | ||
); | ||
|
@@ -115,8 +116,6 @@ export function showNotification({ | |
/** Only errors are reported by default */ | ||
reportError: willReport = type === "error", | ||
}: RequireAtLeastOne<Notification, "message" | "error">): string { | ||
const options = { id, duration }; | ||
|
||
if (error) { | ||
if (!message) { | ||
message = getErrorMessage(error); | ||
|
@@ -136,16 +135,24 @@ export function showNotification({ | |
type = "error"; | ||
} | ||
|
||
const options = { | ||
id, | ||
duration, | ||
// Keep the notification centered on the document even when the sidebar is open | ||
style: { marginLeft: `calc(var(${SIDEBAR_WIDTH_CSS_PROPERTY}, 0) * -1)` }, | ||
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. |
||
}; | ||
const component = <Message {...{ message, id, dismissable }} />; | ||
|
||
switch (type) { | ||
case "error": | ||
case "success": | ||
case "loading": | ||
// eslint-disable-next-line security/detect-object-injection -- Filtered | ||
toast[type](<Message {...{ message, id, dismissable }} />, options); | ||
toast[type](component, options); | ||
break; | ||
|
||
default: | ||
toast(<Message {...{ message, id, dismissable }} />, options); | ||
toast(component, options); | ||
} | ||
|
||
if (willReport) { | ||
|
@@ -162,38 +169,22 @@ export function hideNotification(id: string): void { | |
export const DEFAULT_ACTION_RESULTS = { | ||
error: { | ||
message: "Error running action", | ||
config: { | ||
className: "error", | ||
}, | ||
type: "error", | ||
reportError: false, | ||
}, | ||
cancel: { | ||
message: "The action was cancelled", | ||
config: { | ||
className: "info", | ||
}, | ||
type: "info", | ||
}, | ||
success: { | ||
message: "Successfully ran action", | ||
config: { | ||
className: "success", | ||
}, | ||
type: "success", | ||
}, | ||
}; | ||
} as const; | ||
|
||
export interface MessageConfig { | ||
message: string; | ||
config: Partial<NotificationOptions>; | ||
} | ||
|
||
export function notifyResult( | ||
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. Dropped, |
||
extensionId: string, | ||
{ message, config: { className } }: MessageConfig | ||
): void { | ||
showNotification({ | ||
message, | ||
type: className as NotificationType, | ||
reportError: false, | ||
}); | ||
type: NotificationType; | ||
} | ||
|
||
// Private method to prevent adding logic to the `notify.*` helpers. | ||
|
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.
Drop outdated types (requires review)
I think that Notify.js’ types are somehow part of the public Alert brick’s config and the whole object was being passed as was to the notifier. However we kind of broke that months ago when we dropped the library. In practice I don’t think this was used for anything more than changing the className/type.
Now I'm making the last step of replacing the
className
setting with justtype
.There's no migration path and I don't think it was ever necessary since error events should use
error
type, and so on…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.
It's probably fine, but I'd have to see if anyone (probably not) is using the following affordance in production: https://github.com/pixiebrix/pixiebrix-extension/pull/3531/files#r885082918
Given your comment here, it sounds like if anyone was using it, we would have heard about it? If no one is, I will need to drop the support from menu item extension
What you're referring to is it that the old code destructured className? Or what exactly are you referring to?
{ message, config: { className } }: MessageConfig
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.
That's correct. When I changed library months ago I mapped the
className
to thetype
property because that's how our code was using the library.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.
Re-read some of our old documentation. With the old library you could pass props for controlling whether it was dismissible, etc. It wasn't very well documented and wasn't exposed in the Page Editor, though