From 04b0ed32cd07503c5f6c8f873116bbb538908061 Mon Sep 17 00:00:00 2001 From: Elec0 Date: Thu, 1 May 2025 12:25:29 -0700 Subject: [PATCH 1/3] Refactor workspace listing logic to include all open window IDs and update CSS class for current workspace --- src/popup-actions.ts | 10 ++++--- src/popup.css | 2 +- src/popup.js | 53 ++---------------------------------- src/utils.ts | 18 ++++++++++++ src/workspace-entry-logic.ts | 29 ++++++-------------- 5 files changed, 36 insertions(+), 76 deletions(-) diff --git a/src/popup-actions.ts b/src/popup-actions.ts index fbb9e15..4f06f7c 100644 --- a/src/popup-actions.ts +++ b/src/popup-actions.ts @@ -28,7 +28,7 @@ export class PopupActions { private static async handleNewWorkspaceResponse(response: MessageResponse, windowId: number): Promise { if (response.message === MessageResponses.SUCCESS.message) { console.debug("Workspace added successfully, refreshing list"); - WorkspaceEntryLogic.listWorkspaces(await getWorkspaceStorage()); + WorkspaceEntryLogic.listWorkspaces(await getWorkspaceStorage(), await Utils.getAllWindowIds()); } else { LogHelper.errorAlert("Workspace could not be added\n" + response.message); @@ -108,6 +108,8 @@ export class PopupActions { return; } // We don't need to do anything with the response, since all the data should now be in sync + // Update the workspace list + WorkspaceEntryLogic.listWorkspaces(await getWorkspaceStorage(), await Utils.getAllWindowIds()); }); } @@ -174,7 +176,7 @@ export class PopupActions { PopupMessageHelper.sendClearWorkspaces().then(async response => { if (response.message === MessageResponses.SUCCESS.message) { LogHelper.successAlert("Workspace data cleared."); - WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces()); + WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces(), await Utils.getAllWindowIds()); } else { LogHelper.errorAlert("Error clearing workspace data. Check the console for more details."); @@ -193,7 +195,7 @@ export class PopupActions { PopupMessageHelper.sendDeleteWorkspace(workspace.uuid).then(async response => { if (response.message === MessageResponses.SUCCESS.message) { console.log("Workspace deleted", workspace); - WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces()); + WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces(), await Utils.getAllWindowIds()); } else { LogHelper.errorAlert("Error deleting workspace. Check the console for more details."); @@ -212,7 +214,7 @@ export class PopupActions { PopupMessageHelper.sendRenameWorkspace(workspace.uuid, newName).then(async response => { if (response.message === MessageResponses.SUCCESS.message) { console.log("Workspace renamed", workspace); - WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces()); + WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces(), await Utils.getAllWindowIds()); } else { LogHelper.errorAlert("Error renaming workspace. Check the console for more details."); diff --git a/src/popup.css b/src/popup.css index 8e831f4..e29026c 100644 --- a/src/popup.css +++ b/src/popup.css @@ -206,7 +206,7 @@ img#addIcon:hover { background-color: var(--surface-container-color); } -.workspace-current { +.workspace-open { font-weight: 700; } diff --git a/src/popup.js b/src/popup.js index de55f39..596c478 100644 --- a/src/popup.js +++ b/src/popup.js @@ -5,6 +5,7 @@ import { PageAddWorkspace } from "./pages/page-add-workspace"; import { PageSettings } from "./pages/page-settings"; import "./popup.css"; import { StorageHelper } from "./storage-helper"; +import { Utils } from "./utils"; import { WorkspaceEntryLogic } from "./workspace-entry-logic"; import { WorkspaceStorage } from "./workspace-storage"; @@ -13,46 +14,13 @@ import { WorkspaceStorage } from "./workspace-storage"; * Setup the listeners for the buttons */ async function documentLoaded() { - chrome.tabs.onRemoved.addListener(WorkspaceEntryLogic.tabRemoved); - chrome.tabs.onUpdated.addListener(WorkspaceEntryLogic.tabUpdated); chrome.windows.onRemoved.addListener(windowRemoved); const workspaceStorage = await getWorkspaceStorage(); - const curWindow = await chrome.windows.getCurrent(); - - // Check if the popup is opened from a workspace or not - const isWorkspace = isWindowWorkspace(curWindow.id, workspaceStorage); - console.debug("Window is workspace:", isWorkspace, curWindow); - if (isWorkspace) { - loadWorkspacePopup(workspaceStorage, curWindow.id); - } - else { - loadNonWorkspacePopup(workspaceStorage); - } -} - -/** - * Load the popup page for a non0workspace window. - */ -async function loadNonWorkspacePopup(workspaceStorage) { document.getElementById("addWorkspace").addEventListener("click", addWorkspaceButtonClicked); document.getElementById("settings-button").addEventListener("click", settingsButtonClicked); - WorkspaceEntryLogic.listWorkspaces(workspaceStorage); -} - -/** - * Load the popup page for a workspace window. - * Highlight the current workspace name, and do nothing if the workspace is clicked. - * @param {WorkspaceStorage} workspaceStorage - * @param {number} curWindowId - */ -async function loadWorkspacePopup(workspaceStorage, curWindowId) { - console.log("Loading workspace popup") - document.getElementById("addWorkspace").style.display = "none"; - document.getElementById("settings-button").style.display = "none"; - - WorkspaceEntryLogic.listWorkspaces(workspaceStorage, workspaceStorage.get(curWindowId)); + WorkspaceEntryLogic.listWorkspaces(workspaceStorage, await Utils.getAllWindowIds()); } /** @@ -64,31 +32,16 @@ async function addWorkspaceButtonClicked() { pageAddWorkspace.open(); } -/** - * Check if the window is a workspace window. - * @param {number} windowId - * @param {WorkspaceStorage} workspaceStorage - * @returns {boolean} - */ -function isWindowWorkspace(windowId, workspaceStorage) { - return workspaceStorage.get(windowId) !== undefined; -} - /** * Present a popup asking for confirmation, then clear all workspace data. */ async function settingsButtonClicked() { const pageSettings = new PageSettings(); pageSettings.open(); - - // Open basic javascript ok cancel prompt - // if (confirm("Clear all workspace data?")) { - // PopupActions.clearWorkspaceData(); - // } } /** - * + * When a window is added or removed, update the workspace list. * @param {chrome.windows.window} window */ async function windowRemoved(window) { diff --git a/src/utils.ts b/src/utils.ts index 4df35de..e599cdd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,6 +25,24 @@ export class Utils { }); } + /** + * Retrieves all Chrome window IDs. + * @returns A Promise that resolves with an array of window IDs. + */ + public static async getAllWindowIds(): Promise<(number | undefined)[]> { + return new Promise((resolve) => { + chrome.windows.getAll().then((windows) => { + const windowIds = windows.map((window) => window.id); + resolve(windowIds); + }) + // If the windows are not found, the promise will still resolve, just with an empty array. + // Catch the error to prevent the console from logging it. + .catch(() => { + resolve([]); + }); + }); + } + /** * Focuses a Chrome window by its ID. * @param windowId - The ID of the window to focus. diff --git a/src/workspace-entry-logic.ts b/src/workspace-entry-logic.ts index a844c5a..dbc0267 100644 --- a/src/workspace-entry-logic.ts +++ b/src/workspace-entry-logic.ts @@ -10,7 +10,7 @@ import { WorkspaceStorage } from "./workspace-storage"; */ export class WorkspaceEntryLogic { - public static listWorkspaces(workspaces: WorkspaceStorage, curOpenWorkspace: Workspace | undefined = undefined) { + public static listWorkspaces(workspaces: WorkspaceStorage, allWindowIds: (number | undefined)[]) { console.debug("listWorkspaces", workspaces) const workspaceDiv = document.getElementById("workspaces-list"); @@ -19,7 +19,7 @@ export class WorkspaceEntryLogic { return; } workspaceDiv.innerHTML = ""; - + // Add each workspace to the list, and add event listeners to the buttons. for (const workspace of Array.from(workspaces.values())) { const workspaceElement = this.addWorkspace(workspaceDiv, workspace); @@ -28,20 +28,13 @@ export class WorkspaceEntryLogic { const editWorkspace = workspaceElement.querySelector('#edit-button'); const deleteWorkspace = workspaceElement.querySelector('#delete-button'); - // Add a class to the workspace if this window is the current workspace. - if (curOpenWorkspace?.uuid === workspace.uuid) { - workspaceElement.classList.add('workspace-current'); - } - else { - // Don't allow the user to re-open the current workspace. - openWorkspace?.addEventListener('click', () => { - this.workspaceClicked(workspace); - }); + // Add a class to the workspace if it is open in a window. + if (allWindowIds?.includes(workspace.windowId)) { + workspaceElement.classList.add('workspace-open'); } - - // settingsWorkspace?.addEventListener('click', () => { - // this.workspaceSettingsClicked(workspace); - // }); + openWorkspace?.addEventListener('click', () => { + this.workspaceClicked(workspace); + }); editWorkspace?.addEventListener('click', (event) => { event.stopPropagation(); @@ -126,10 +119,4 @@ export class WorkspaceEntryLogic { } PopupActions.deleteWorkspace(workspace); } - - public static async tabRemoved(tabId: number, removeInfo: chrome.tabs.TabRemoveInfo) { - } - - public static async tabUpdated(tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) { - } } \ No newline at end of file From 3978bc70224f2ea13b2dd80d6bada3afc191e7a7 Mon Sep 17 00:00:00 2001 From: Aaron Date: Thu, 1 May 2025 12:38:13 -0700 Subject: [PATCH 2/3] Remove unneeded optional chain Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/workspace-entry-logic.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/workspace-entry-logic.ts b/src/workspace-entry-logic.ts index dbc0267..fd88ac4 100644 --- a/src/workspace-entry-logic.ts +++ b/src/workspace-entry-logic.ts @@ -29,7 +29,7 @@ export class WorkspaceEntryLogic { const deleteWorkspace = workspaceElement.querySelector('#delete-button'); // Add a class to the workspace if it is open in a window. - if (allWindowIds?.includes(workspace.windowId)) { + if (allWindowIds.includes(workspace.windowId)) { workspaceElement.classList.add('workspace-open'); } openWorkspace?.addEventListener('click', () => { From f3e4781f932342bbe6e4d320bd491c3bd6648afc Mon Sep 17 00:00:00 2001 From: Elec0 Date: Thu, 1 May 2025 12:45:25 -0700 Subject: [PATCH 3/3] Update bug report link to include template --- public-flavors/chrome/popup.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public-flavors/chrome/popup.html b/public-flavors/chrome/popup.html index cb89be2..fdff394 100644 --- a/public-flavors/chrome/popup.html +++ b/public-flavors/chrome/popup.html @@ -22,7 +22,7 @@

Edge Workspaces

If you see this message, it means that the workspaces list is still loading.
- If this message persists, please file a bug report here. + If this message persists, please file a bug report here.