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

PM-17963 remove ts ignore from bar.ts #13249

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all 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
173 changes: 121 additions & 52 deletions apps/browser/src/autofill/notification/bar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { render } from "lit";

import { Theme, ThemeTypes } from "@bitwarden/common/platform/enums";
Expand All @@ -18,6 +16,14 @@
NotificationType,
} from "./abstractions/notification-bar";

type AttributeSetter = {
textContent?: string;
title?: string;
hidden?: boolean;
ariaLabel?: string;
onClick?: (e: Event) => void;
};

const logService = new ConsoleLogService(false);
let notificationBarIframeInitData: NotificationBarIframeInitData = {};
let windowMessageOrigin: string;
Expand All @@ -44,6 +50,30 @@
postMessageToParent({ command: "initNotificationBar" });
}

function setAttributes(
element: HTMLElement | null,
{ textContent, title, hidden, ariaLabel, onClick }: AttributeSetter,
) {
if (!element) {
return;

Check warning on line 58 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L58

Added line #L58 was not covered by tests
}
if (textContent) {
element.textContent = textContent;

Check warning on line 61 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L61

Added line #L61 was not covered by tests
}
if (title) {
element.title = title;

Check warning on line 64 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L64

Added line #L64 was not covered by tests
}
if (hidden) {
element.hidden = hidden;

Check warning on line 67 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L67

Added line #L67 was not covered by tests
}
if (ariaLabel) {
element.setAttribute("aria-label", ariaLabel);

Check warning on line 70 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L70

Added line #L70 was not covered by tests
}
if (onClick) {
element.addEventListener("click", onClick);

Check warning on line 73 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L73

Added line #L73 was not covered by tests
}
}

function initNotificationBar(message: NotificationBarWindowMessage) {
const { initData } = message;
if (!initData) {
Expand Down Expand Up @@ -117,44 +147,59 @@
const addTemplate = document.getElementById("template-add") as HTMLTemplateElement;

const neverButton = addTemplate.content.getElementById("never-save");
neverButton.textContent = i18n.never;
setAttributes(neverButton, {

Check warning on line 150 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L150

Added line #L150 was not covered by tests
textContent: i18n.never,
});

const selectFolder = addTemplate.content.getElementById("select-folder");
selectFolder.hidden = isVaultLocked || removeIndividualVault();
selectFolder.setAttribute("aria-label", i18n.folder);
setAttributes(selectFolder, {

Check warning on line 155 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L155

Added line #L155 was not covered by tests
hidden: isVaultLocked || removeIndividualVault(),
ariaLabel: i18n.folder,
});

const addButton = addTemplate.content.getElementById("add-save");
addButton.textContent = i18n.notificationAddSave;
setAttributes(addButton, {

Check warning on line 161 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L161

Added line #L161 was not covered by tests
textContent: i18n.notificationAddSave,
});

const addEditButton = addTemplate.content.getElementById("add-edit");
// If Remove Individual Vault policy applies, "Add" opens the edit tab, so we hide the Edit button
addEditButton.hidden = removeIndividualVault();
addEditButton.textContent = i18n.notificationEdit;
setAttributes(addEditButton, {

Check warning on line 166 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L166

Added line #L166 was not covered by tests
hidden: removeIndividualVault(),
textContent: i18n.notificationEdit,
});

addTemplate.content.getElementById("add-text").textContent = i18n.notificationAddDesc;
// If Remove Individual Vault policy applies, "Add" opens the edit tab, so we hide the Edit button
setAttributes(addTemplate.content.getElementById("add-text"), {

Check warning on line 172 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L172

Added line #L172 was not covered by tests
textContent: i18n.notificationAddDesc,
});

// i18n for "Change" (update password) template
const changeTemplate = document.getElementById("template-change") as HTMLTemplateElement;

const changeButton = changeTemplate.content.getElementById("change-save");
changeButton.textContent = i18n.notificationChangeSave;
setAttributes(changeButton, { textContent: i18n.notificationChangeSave });

Check warning on line 180 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L180

Added line #L180 was not covered by tests

const changeEditButton = changeTemplate.content.getElementById("change-edit");
changeEditButton.textContent = i18n.notificationEdit;
setAttributes(changeEditButton, {

Check warning on line 183 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L183

Added line #L183 was not covered by tests
textContent: i18n.notificationEdit,
});

changeTemplate.content.getElementById("change-text").textContent = i18n.notificationChangeDesc;
setAttributes(changeTemplate.content.getElementById("change-text"), {

Check warning on line 187 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L187

Added line #L187 was not covered by tests
textContent: i18n.notificationChangeDesc,
});

// i18n for "Unlock" (unlock extension) template
const unlockTemplate = document.getElementById("template-unlock") as HTMLTemplateElement;

const unlockButton = unlockTemplate.content.getElementById("unlock-vault");
unlockButton.textContent = i18n.notificationUnlock;
setAttributes(unlockButton, { textContent: i18n.notificationUnlock });

Check warning on line 195 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L195

Added line #L195 was not covered by tests

unlockTemplate.content.getElementById("unlock-text").textContent = i18n.notificationUnlockDesc;
setAttributes(unlockTemplate.content.getElementById("unlock-text"), {

Check warning on line 197 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L197

Added line #L197 was not covered by tests
textContent: i18n.notificationUnlockDesc,
});

// i18n for body content
const closeButton = document.getElementById("close-button");
closeButton.title = i18n.close;

const notificationType = initData.type;
if (notificationType === "add") {
Expand All @@ -165,7 +210,7 @@
handleTypeUnlock();
}

closeButton.addEventListener("click", handleCloseNotification);
setAttributes(closeButton, { title: i18n.close, onClick: handleCloseNotification });

Check warning on line 213 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L213

Added line #L213 was not covered by tests

globalThis.addEventListener("resize", adjustHeight);
adjustHeight();
Expand All @@ -182,11 +227,12 @@
setContent(document.getElementById("template-add") as HTMLTemplateElement);

const addButton = document.getElementById("add-save");
addButton.addEventListener("click", (e) => {
e.preventDefault();

// If Remove Individual Vault policy applies, "Add" opens the edit tab
sendSaveCipherMessage(removeIndividualVault(), getSelectedFolder());
setAttributes(addButton, {

Check warning on line 230 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L230

Added line #L230 was not covered by tests
onClick: (e) => {
e.preventDefault();

Check warning on line 232 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L232

Added line #L232 was not covered by tests
// If Remove Individual Vault policy applies, "Add" opens the edit tab
sendSaveCipherMessage(removeIndividualVault(), getSelectedFolder());

Check warning on line 234 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L234

Added line #L234 was not covered by tests
},
});

if (removeIndividualVault()) {
Expand All @@ -195,18 +241,21 @@
}

const editButton = document.getElementById("add-edit");
editButton.addEventListener("click", (e) => {
e.preventDefault();

sendSaveCipherMessage(true, getSelectedFolder());
setAttributes(editButton, {

Check warning on line 244 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L244

Added line #L244 was not covered by tests
onClick: (e) => {
e.preventDefault();
sendSaveCipherMessage(true, getSelectedFolder());

Check warning on line 247 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L246-L247

Added lines #L246 - L247 were not covered by tests
},
});

const neverButton = document.getElementById("never-save");
neverButton.addEventListener("click", (e) => {
e.preventDefault();
sendPlatformMessage({
command: "bgNeverSave",
});
setAttributes(neverButton, {

Check warning on line 252 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L252

Added line #L252 was not covered by tests
onClick: (e) => {
e.preventDefault();
sendPlatformMessage({

Check warning on line 255 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L254-L255

Added lines #L254 - L255 were not covered by tests
command: "bgNeverSave",
});
},
});

loadFolderSelector();
Expand All @@ -215,17 +264,19 @@
function handleTypeChange() {
setContent(document.getElementById("template-change") as HTMLTemplateElement);
const changeButton = document.getElementById("change-save");
changeButton.addEventListener("click", (e) => {
e.preventDefault();

sendSaveCipherMessage(false);
setAttributes(changeButton, {

Check warning on line 267 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L267

Added line #L267 was not covered by tests
onClick: (e) => {
e.preventDefault();
sendSaveCipherMessage(false);

Check warning on line 270 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L269-L270

Added lines #L269 - L270 were not covered by tests
},
});

const editButton = document.getElementById("change-edit");
editButton.addEventListener("click", (e) => {
e.preventDefault();

sendSaveCipherMessage(true);
setAttributes(editButton, {

Check warning on line 275 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L275

Added line #L275 was not covered by tests
onClick: (e) => {
e.preventDefault();
sendSaveCipherMessage(false);

Check warning on line 278 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L277-L278

Added lines #L277 - L278 were not covered by tests
},
});
}

Expand All @@ -244,7 +295,9 @@
addSaveButtonContainers.forEach((element) => {
element.textContent = chrome.i18n.getMessage("saveCipherAttemptFailed");
element.classList.add("error-message");
notificationBarOuterWrapper.classList.add("error-event");
if (notificationBarOuterWrapper) {
notificationBarOuterWrapper.classList.add("error-event");

Check warning on line 299 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L299

Added line #L299 was not covered by tests
}
});

adjustHeight();
Expand All @@ -258,7 +311,9 @@
element.textContent = chrome.i18n.getMessage(messageName);
element.prepend(buildSvgDomElement(circleCheckIcon));
element.classList.add("success-message");
notificationBarOuterWrapper.classList.add("success-event");
if (notificationBarOuterWrapper) {
notificationBarOuterWrapper.classList.add("success-event");

Check warning on line 315 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L315

Added line #L315 was not covered by tests
}
});
adjustHeight();
globalThis.setTimeout(
Expand All @@ -271,21 +326,27 @@
setContent(document.getElementById("template-unlock") as HTMLTemplateElement);

const unlockButton = document.getElementById("unlock-vault");
unlockButton.addEventListener("click", (e) => {
sendPlatformMessage({
command: "bgReopenUnlockPopout",
});
setAttributes(unlockButton, {

Check warning on line 329 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L329

Added line #L329 was not covered by tests
onClick: (e) => {
sendPlatformMessage({

Check warning on line 331 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L331

Added line #L331 was not covered by tests
command: "bgReopenUnlockPopout",
});
},
});
}

function setContent(template: HTMLTemplateElement) {
const content = document.getElementById("content");
while (content.firstChild) {
content.removeChild(content.firstChild);
if (content) {
while (content.firstChild) {
content.removeChild(content.firstChild);

Check warning on line 342 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L341-L342

Added lines #L341 - L342 were not covered by tests
}
}

const newElement = template.content.cloneNode(true) as HTMLElement;
content.appendChild(newElement);
if (content) {
content.appendChild(newElement);

Check warning on line 348 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L348

Added line #L348 was not covered by tests
}
}

function sendPlatformMessage(
Expand All @@ -302,13 +363,17 @@
function loadFolderSelector() {
const populateFolderData = (folderData: FolderView[]) => {
const select = document.getElementById("select-folder");
if (!select) {
return;

Check warning on line 367 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L367

Added line #L367 was not covered by tests
}

if (!folderData?.length) {
select.appendChild(new Option(chrome.i18n.getMessage("noFoldersFound"), null, true));
select.appendChild(new Option(chrome.i18n.getMessage("noFoldersFound"), undefined, true));

Check warning on line 371 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L371

Added line #L371 was not covered by tests
select.setAttribute("disabled", "true");
return;
}

select.appendChild(new Option(chrome.i18n.getMessage("selectFolder"), null, true));
select.appendChild(new Option(chrome.i18n.getMessage("selectFolder"), undefined, true));

Check warning on line 376 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L376

Added line #L376 was not covered by tests
folderData.forEach((folder: FolderView) => {
// Select "No Folder" (id=null) folder by default
select.appendChild(new Option(folder.name, folder.id || "", false));
Expand All @@ -323,12 +388,16 @@
}

function removeIndividualVault(): boolean {
return notificationBarIframeInitData.removeIndividualVault;
return notificationBarIframeInitData.removeIndividualVault || false;
}

function adjustHeight() {
const body = document.querySelector("body");

Check warning on line 395 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L395

Added line #L395 was not covered by tests
if (!body) {
return;

Check warning on line 397 in apps/browser/src/autofill/notification/bar.ts

View check run for this annotation

Codecov / codecov/patch

apps/browser/src/autofill/notification/bar.ts#L397

Added line #L397 was not covered by tests
}
const data: AdjustNotificationBarMessageData = {
height: document.querySelector("body").scrollHeight,
height: body.scrollHeight,
};
sendPlatformMessage({
command: "bgAdjustNotificationBar",
Expand Down