Skip to content
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

Replace content script notifications #2102

Merged
merged 11 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"react-beautiful-dnd": "^13.1.0",
"react-bootstrap": "^1.6.1",
"react-dom": "^17.0.2",
"react-hot-toast": "^2.1.1",
"react-hotkeys": "^2.0.0",
"react-json-tree": "^0.15.0",
"react-outside-click-handler": "^1.3.0",
Expand Down
5 changes: 2 additions & 3 deletions src/background/contextMenus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,19 @@ async function dispatchMenu(
});
void showNotification(target, {
message: "Ran content menu item action",
className: "success",
type: "success",
});
} catch (error) {
if (hasCancelRootCause(error)) {
void showNotification(target, {
message: "The action was cancelled",
className: "info",
});
} else {
const message = `Error handling context menu action: ${getErrorMessage(
error
)}`;
reportError(new Error(message));
void showNotification(target, { message, className: "error" });
void showNotification(target, { message, type: "error" });
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import registerBuiltinBlocks from "@/blocks/registerBuiltinBlocks";
import registerContribBlocks from "@/contrib/registerContribBlocks";
import { handleNavigate } from "@/contentScript/lifecycle";
import "@/messaging/external";
import "@/vendors/notify";
import { markReady, updateTabInfo } from "@/contentScript/context";
import { initTelemetry } from "@/telemetry/events";
import { markTabAsReady, whoAmI } from "@/background/messenger/api";
Expand Down
36 changes: 9 additions & 27 deletions src/contentScript/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,19 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { NotificationCallbacks } from "@/contentScript/notify";
import { hideNotification, showNotification } from "@/contentScript/notify";

let _hooks: NotificationCallbacks = null;
const id = "connection-lost";

export function showConnectionLost(): void {
if (_hooks) {
// Don't show connection notification if it's already being displayed
return null;
}

const element = $.notify(
"Connection to PixieBrix lost. Please reload the page",
{
autoHide: false,
clickToHide: false,
className: "error",
}
);

const $element = $(element);

_hooks = {
hide: () => {
$element.trigger("notify-hide");
},
};
showNotification({
id,
message: "Connection to PixieBrix lost. Please reload the page",
type: "error",
duration: Number.POSITIVE_INFINITY,
});
}

export function hideConnectionLost(): void {
if (_hooks) {
_hooks.hide();
_hooks = null;
}
hideNotification(id);
}
82 changes: 53 additions & 29 deletions src/contentScript/notify.ts → src/contentScript/notify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,59 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import React from "react";
import { merge } from "lodash";
import { render } from "react-dom";
import { toast, Toaster } from "react-hot-toast";
import { uuidv4 } from "@/types/helpers";

type NotificationType = "info" | "success" | "error" | "loading";
interface Notification {
message: string;
className: "error" | "info" | "success";
type?: NotificationType;
id?: string;
duration?: number;
}
export async function showNotification(
notification: Notification
): Promise<void> {
$.notify(notification.message, {
className: notification.className,
});

let root: Element | null;
function initToaster(): void {
if (!root) {
root = document.createElement("div");
}

if (root.isConnected) {
return;
}

document.body.append(root);
render(<Toaster />, root);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think isolating these would only take:

  • add shadow DOM to avoid inheriting styles
  • that's it

I doubt it uses any rem and if it does we can override them via inline styles.

I just need some sites to test this on.

Copy link
Contributor

@twschiller twschiller Dec 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me see if I can put together a list of public-ish places we've had issues. The icon alignment issue for the old library occurred across a number of sites

  • LinkedIn uses rem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LinkedIn:

Screen.Recording.1.mov

LinkedIn uses rem

Whether the host page uses them doesn't affect us, it's all good as long as we don't use rem.

The good part of hot-toast is that it only uses div and generated classes, so chances of conflict are slim:

Screen Shot 2

Should we encounter issues, then yes, we can just wrap it in a shadow DOM in 2 lines. However it currently does not support it:

}

export function showNotification({
message,
type,
id = uuidv4(),
duration,
}: Notification): string {
initToaster();
const options = { id, duration };
fregante marked this conversation as resolved.
Show resolved Hide resolved
switch (type) {
case "error":
case "success":
case "loading":
// eslint-disable-next-line security/detect-object-injection -- Filtered
toast[type](message, options);
break;

default:
toast(message, options);
}

return id;
}

export function hideNotification(id: string): void {
toast.remove(id);
}

export const DEFAULT_ACTION_RESULTS = {
Expand Down Expand Up @@ -72,29 +113,12 @@ export interface NotificationCallbacks {
}

export function notifyError(message: string): void {
// Call getErrorMessage on err and include in the details
$.notify(message, {
className: "error",
});
showNotification({ message, type: "error" });
}

export function notifyResult(extensionId: string, config: MessageConfig): void {
$.notify(config.message, config.config);
}

export function notifyProgress(
export function notifyResult(
extensionId: string,
message: string
): NotificationCallbacks {
const element = $.notify(message, {
autoHide: false,
clickToHide: false,
className: "info",
});

return {
hide: () => {
$(element).trigger("notify-hide");
},
};
{ message, config: { className } }: MessageConfig
): void {
showNotification({ message, type: className as NotificationType });
}
25 changes: 19 additions & 6 deletions src/contrib/pipedrive/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Effect } from "@/types";
import { proxyService } from "@/background/messenger/api";
import { propertiesToSchema } from "@/validators/generic";
import { BlockArg } from "@/core";
import { showNotification } from "@/contentScript/notify";

export class AddOrganization extends Effect {
// https://developers.pipedrive.com/docs/api/v1/#!/Organizations/post_organizations
Expand Down Expand Up @@ -61,7 +62,7 @@ export class AddOrganization extends Effect {
});

if (data.items.length > 0) {
$.notify(`Organization already exists for ${name}`, "info");
showNotification({ message: `Organization already exists for ${name}` });
return;
}

Expand All @@ -71,9 +72,15 @@ export class AddOrganization extends Effect {
method: "post",
data: { name, owner_id },
});
$.notify(`Added ${name} to Pipedrive`, "success");
showNotification({
message: `Added ${name} to Pipedrive`,
type: "success",
});
} catch {
$.notify(`Error adding ${name} to Pipedrive`, "error");
showNotification({
message: `Error adding ${name} to Pipedrive`,
type: "error",
});
}
}
}
Expand Down Expand Up @@ -133,7 +140,7 @@ export class AddPerson extends Effect {
});

if (data.items.length > 0) {
$.notify(`Person record already exists for ${name}`, "info");
showNotification({ message: `Person record already exists for ${name}` });
return;
}

Expand All @@ -148,9 +155,15 @@ export class AddPerson extends Effect {
phone: phone ? [phone] : undefined,
},
});
$.notify(`Added ${name} to Pipedrive`, "success");
showNotification({
message: `Added ${name} to Pipedrive`,
type: "success",
});
} catch {
$.notify(`Error adding ${name} to Pipedrive`, "error");
showNotification({
message: `Error adding ${name} to Pipedrive`,
type: "error",
});
}
}
}
16 changes: 7 additions & 9 deletions src/runtime/reducePipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { castArray, isPlainObject } from "lodash";
import { BusinessError, ContextError } from "@/errors";
import { requestRun } from "@/background/messenger/api";
import { getLoggingConfig } from "@/background/logging";
import { NotificationCallbacks, notifyProgress } from "@/contentScript/notify";
import { hideNotification, showNotification } from "@/contentScript/notify";
import { sendDeploymentAlert } from "@/background/telemetry";
import { serializeError } from "serialize-error";
import { HeadlessModeError } from "@/blocks/errors";
Expand Down Expand Up @@ -362,13 +362,13 @@ export async function runBlock(
await throwIfInvalidInput(block, props.args);
}

let progressCallbacks: NotificationCallbacks;
let notification: string;

if (stage.notifyProgress) {
progressCallbacks = notifyProgress(
logger.context.extensionId,
stage.label ?? block.name
);
notification = showNotification({
message: stage.label ?? block.name,
type: "loading",
});
}

if (type === "renderer" && headless) {
Expand All @@ -385,9 +385,7 @@ export async function runBlock(
const validatedProps = (props as unknown) as BlockProps<BlockArg>;
return await execute(resolvedConfig, validatedProps, options);
} finally {
if (progressCallbacks) {
progressCallbacks.hide();
}
hideNotification(notification);
}
}

Expand Down
Loading