Skip to content

Commit

Permalink
use BrowserStateDao in containers view
Browse files Browse the repository at this point in the history
  • Loading branch information
metastable-void committed May 6, 2023
1 parent ffeadb8 commit 966b56d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
40 changes: 23 additions & 17 deletions src/pages/popup-v2/fragments/ContainerDetailsFragmentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
import browser from "webextension-polyfill";
import { CookieStore, DisplayedContainer } from "weeg-containers";

import { AbstractFragmentBuilder } from "./AbstractFragmentBuilder";
import { ContainerTabOpenerService } from "../../../lib/tabGroups/ContainerTabOpenerService";
import { BrowserStateDao } from "../../../lib/states/BrowserStateDao";
import { DisplayedContainerDao } from "../../../lib/states/DisplayedContainerDao";
import { TabDao } from "../../../lib/states/TabDao";

import { IndexTab } from "../../../legacy-lib/modules/IndexTab";

import { MenulistWindowElement } from "../../../components/menulist-window";
import { CtgFragmentElement } from "../../../components/ctg/ctg-fragment";
import { CtgTopBarElement } from "../../../components/ctg/ctg-top-bar";
import { CtgMenuItemElement } from "../../../components/ctg/ctg-menu-item";

import { AbstractFragmentBuilder } from "./AbstractFragmentBuilder";
import { PopupRendererService } from "../PopupRendererService";
import { BrowserStateSnapshot } from "../../../legacy-lib/tabs/BrowserStateSnapshot";
import { MenulistWindowElement } from "../../../components/menulist-window";
import { ContainerTabOpenerService } from "../../../lib/tabGroups/ContainerTabOpenerService";
import { IndexTab } from "../../../legacy-lib/modules/IndexTab";


export class ContainerDetailsFragmentBuilder extends AbstractFragmentBuilder {
Expand All @@ -40,7 +45,7 @@ export class ContainerDetailsFragmentBuilder extends AbstractFragmentBuilder {
private readonly _popupRenderer = PopupRendererService.getInstance().popupRenderer;
private _containerName = browser.i18n.getMessage('noContainer');
private _cookieStoreId = CookieStore.DEFAULT.id;
private _browserStateSnapshot: BrowserStateSnapshot | null = null;
private _browserState: BrowserStateDao | null = null;
private _selectedDisplayedContainer: DisplayedContainer | null = null;

public getFragmentId(): string {
Expand Down Expand Up @@ -86,9 +91,9 @@ export class ContainerDetailsFragmentBuilder extends AbstractFragmentBuilder {
topBarElement.headingText = this._containerName;
}

public render(browserStateSnapshot: BrowserStateSnapshot): void {
public render(browserState: BrowserStateDao): void {
// todo
this._browserStateSnapshot = browserStateSnapshot;
this._browserState = browserState;
this.setContainer();
if (this.active) {
this.renderTopBarWithGlobalItems();
Expand All @@ -98,14 +103,14 @@ export class ContainerDetailsFragmentBuilder extends AbstractFragmentBuilder {
}

private setContainer(): void {
if (this._browserStateSnapshot == null) {
if (this._browserState == null) {
return;
}
const containersStateSnapshot = this._browserStateSnapshot.getContainersStateSnapshot();
const browserState = this._browserState;
let selectedDisplayedContainer: DisplayedContainer | null = null;
for (const containerAttributes of containersStateSnapshot.displayedContainers) {
if (containerAttributes.cookieStore.id === this._cookieStoreId) {
selectedDisplayedContainer = containerAttributes;
for (const displayedContainer of browserState.displayedContainers.map((dao) => DisplayedContainerDao.toDisplayedContainer(dao))) {
if (displayedContainer.cookieStore.id === this._cookieStoreId) {
selectedDisplayedContainer = displayedContainer;
break;
}
}
Expand All @@ -117,14 +122,15 @@ export class ContainerDetailsFragmentBuilder extends AbstractFragmentBuilder {
}

public renderContainer(): void {
if (this._browserStateSnapshot == null || this._selectedDisplayedContainer == null) {
if (this._browserState == null || this._selectedDisplayedContainer == null) {
return;
}

const browserState = this._browserState;
const fragment = this.getFragment();
fragment.textContent = '';
const containersStateSnapshot = this._browserStateSnapshot.getContainersStateSnapshot();
const tabs = containersStateSnapshot.getTabsByContainer(this._cookieStoreId);
const tabIds = browserState.tabIdsByContainer[this._cookieStoreId] ?? [];
const tabs = tabIds.map((tabId) => TabDao.toCompatTab(browserState.tabs[tabId] as TabDao));
const displayedContainer = this._selectedDisplayedContainer;
let windowId: number = browser.windows.WINDOW_ID_NONE;
for (const tab of tabs) {
Expand All @@ -133,7 +139,7 @@ export class ContainerDetailsFragmentBuilder extends AbstractFragmentBuilder {
}
if (windowId != tab.windowId) {
const windowElement = new MenulistWindowElement();
windowElement.windowName = this._browserStateSnapshot.currentWindowId == tab.windowId
windowElement.windowName = this._browserState.currentWindowId == tab.windowId
? browser.i18n.getMessage('currentWindow', tab.windowId.toString())
: browser.i18n.getMessage('windowLabel', tab.windowId.toString());
fragment.appendChild(windowElement);
Expand Down
35 changes: 20 additions & 15 deletions src/pages/popup-v2/fragments/ContainersFragmentBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import { TabQueryService } from "../../../lib/tabs/TabQueryService";
import { TabService } from "../../../lib/tabs/TabService";
import { IndexTabService } from "../../../lib/tabs/IndexTabService";
import { CompatConsole } from "../../../lib/console/CompatConsole";
import { BrowserStateDao } from "../../../lib/states/BrowserStateDao";
import { TabGroupDirectorySnapshot } from "../../../lib/tabGroups/TabGroupDirectorySnapshot";
import { DisplayedContainerDao } from "../../../lib/states/DisplayedContainerDao";
import { TabDao } from "../../../lib/states/TabDao";

import { CtgFragmentElement } from "../../../components/ctg/ctg-fragment";
import { CtgTopBarElement } from "../../../components/ctg/ctg-top-bar";
Expand All @@ -40,8 +44,6 @@ import { MenulistSupergroupElement } from "../../../components/menulist-supergro
import { AbstractFragmentBuilder } from "./AbstractFragmentBuilder";
import { PopupRendererService } from "../PopupRendererService";

import { ContainersStateSnapshot } from "../../../legacy-lib/tabs/ContainersStateSnapshot";

const console = new CompatConsole(CompatConsole.tagFromFilename(__filename));

export class ContainersFragmentBuilder extends AbstractFragmentBuilder {
Expand Down Expand Up @@ -123,19 +125,20 @@ export class ContainersFragmentBuilder extends AbstractFragmentBuilder {
topBarElement.headingText = `${labelText} (${this._containerCount.toFixed(0)})`;
}

public render(containersStateSnapshot: ContainersStateSnapshot): void {
this._containerCount = containersStateSnapshot.displayedContainers.length;
public render(browserState: BrowserStateDao): void {
this._containerCount = browserState.displayedContainers.length;
if (this.active) {
this.renderTopBarWithGlobalItems();
}
const tabGroupDirectorySnapshot = containersStateSnapshot.tabGroupDirectorySnapshot;
const tabGroupDirectorySnapshot = new TabGroupDirectorySnapshot(browserState.supergroups);
const displayedContainers = browserState.displayedContainers.map((dao) => DisplayedContainerDao.toDisplayedContainer(dao));
const fragment = this.getFragment();
const activeContainersElement = fragment.querySelector('.active-containers') as HTMLDivElement;
const inactiveContainersElement = fragment.querySelector('.inactive-containers') as HTMLDivElement;
activeContainersElement.textContent = '';
inactiveContainersElement.textContent = '';
const privateContainers = containersStateSnapshot.displayedContainers.filter((container) => container.cookieStore.isPrivate);
const normalContainers = containersStateSnapshot.displayedContainers.filter((container) => !container.cookieStore.isPrivate);
const privateContainers = displayedContainers.filter((container) => container.cookieStore.isPrivate);
const normalContainers = displayedContainers.filter((container) => !container.cookieStore.isPrivate);
const privateUserContexts = [... privateContainers];
const normalUserContexts = [... normalContainers].sort((a, b) => {
return tabGroupDirectorySnapshot.cookieStoreIdSortingCallback(a.cookieStore.id, b.cookieStore.id);
Expand All @@ -145,13 +148,13 @@ export class ContainersFragmentBuilder extends AbstractFragmentBuilder {
userContextMap.set(normalUserContext.cookieStore.id, normalUserContext);
}
const rootSupergroup = tabGroupDirectorySnapshot.getSupergroup(TabGroupAttributes.getRootSupergroupTabGroupId()) as SupergroupType;
this.renderPrivateContainers(containersStateSnapshot, privateUserContexts, activeContainersElement);
this.renderSupergroup(0, rootSupergroup, containersStateSnapshot, userContextMap, activeContainersElement);
this.renderPrivateContainers(browserState, privateUserContexts, activeContainersElement);
this.renderSupergroup(0, rootSupergroup, browserState, userContextMap, activeContainersElement);
}

private renderSupergroup(nestingCount: number, supergroup: SupergroupType, containersStateSnapshot: ContainersStateSnapshot, userContextMap: Map<string, DisplayedContainer>, parentElement: HTMLElement) {
private renderSupergroup(nestingCount: number, supergroup: SupergroupType, browserState: BrowserStateDao, userContextMap: Map<string, DisplayedContainer>, parentElement: HTMLElement) {
let tabCount = 0;
const tabGroupDirectorySnapshot = containersStateSnapshot.tabGroupDirectorySnapshot;
const tabGroupDirectorySnapshot = new TabGroupDirectorySnapshot(browserState.supergroups);
const tabGroupIds = supergroup.members;
for (const tabGroupId of tabGroupIds) {
const attributes = new TabGroupAttributes(tabGroupId);
Expand All @@ -160,7 +163,8 @@ export class ContainersFragmentBuilder extends AbstractFragmentBuilder {
const userContext = userContextMap.get(tabGroupId);
if (!userContext) continue;
const isPrivate = cookieStore.isPrivate;
const tabs = this._indexTabService.filterOutIndexTabs([... containersStateSnapshot.getTabsByContainer(cookieStore.id)]);
const tabIds = browserState.tabIdsByContainer[userContext.cookieStore.id] ?? [];
const tabs = this._indexTabService.filterOutIndexTabs(tabIds.map((tabId) => TabDao.toCompatTab(browserState.tabs[tabId] as TabDao)));
const containerElement = this._popupRenderer.renderPartialContainerElement(userContext, isPrivate);
containerElement.tabCount = tabs.length;
tabCount += tabs.length;
Expand All @@ -187,7 +191,7 @@ export class ContainersFragmentBuilder extends AbstractFragmentBuilder {
supergroupElement.groupName = supergroup.name;
parentElement.appendChild(supergroupElement);

const subTabCount = this.renderSupergroup(nestingCount + 1, supergroup, containersStateSnapshot, userContextMap, supergroupElement);
const subTabCount = this.renderSupergroup(nestingCount + 1, supergroup, browserState, userContextMap, supergroupElement);
supergroupElement.tabCount = subTabCount;
supergroupElement.groupHideButton.disabled = true;
supergroupElement.groupUnhideButton.disabled = true;
Expand All @@ -207,10 +211,11 @@ export class ContainersFragmentBuilder extends AbstractFragmentBuilder {
return tabCount;
}

private renderPrivateContainers(containersStateSnapshot: ContainersStateSnapshot, userContexts: DisplayedContainer[], parentElement: HTMLElement) {
private renderPrivateContainers(browserState: BrowserStateDao, userContexts: DisplayedContainer[], parentElement: HTMLElement) {
for (const userContext of userContexts) {
const isPrivate = userContext.cookieStore.isPrivate;
const tabs = containersStateSnapshot.getTabsByContainer(userContext.cookieStore.id);
const tabIds = browserState.tabIdsByContainer[userContext.cookieStore.id] ?? [];
const tabs = tabIds.map((tabId) => TabDao.toCompatTab(browserState.tabs[tabId] as TabDao));
const containerElement = this._popupRenderer.renderPartialContainerElement(userContext, isPrivate);
containerElement.tabCount = tabs.length;
parentElement.appendChild(containerElement);
Expand Down
4 changes: 2 additions & 2 deletions src/pages/popup-v2/popup-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,9 @@ const renderer = new ViewRefreshHandler(async () => {
if (browserSnapshotTime > 500) {
console.info(`Browser snapshot took ${browserSnapshotTime}ms`);
}
containersBuilder.render(browserStateSnapshot.getContainersStateSnapshot());
const currentWindowSnapshot = browserStateSnapshot.getWindowStateSnapshot(browserStateSnapshot.currentWindowId);
sitesBuilder.render(browserStateSnapshot.getFirstPartyStateSnapshot(currentWindowSnapshot.isPrivate));

containerDetailsBuilder.render(browserStateSnapshot);
siteDetailsBuilder.render(browserStateSnapshot, currentWindowSnapshot.isPrivate);
} finally {
topBarElement.endSpinnerTransaction('popup-rendering');
Expand Down Expand Up @@ -303,5 +301,7 @@ messagingService.addListener('tab-sorting-ended', () => {
const store = new BrowserStateStore();
store.onChanged.addListener(() => {
windowsBuilder.render(store.value);
containersBuilder.render(store.value);
containerDetailsBuilder.render(store.value);
console.debug('BrowserStateStore changed:', store.value);
});

0 comments on commit 966b56d

Please sign in to comment.