diff --git a/apps/desktop/src/locales/en/messages.json b/apps/desktop/src/locales/en/messages.json index d8cfc0eb8d4..7562d6a5ea0 100644 --- a/apps/desktop/src/locales/en/messages.json +++ b/apps/desktop/src/locales/en/messages.json @@ -2438,6 +2438,10 @@ } } }, + "itemSingular": { + "message": "1 item", + "description": "Count of exactly one item, e.g. on a folder card holding a single item." + }, "all": { "message": "All" }, diff --git a/apps/desktop/src/vault/app/vault-v3/vault.component.html b/apps/desktop/src/vault/app/vault-v3/vault.component.html index fd281e02481..4b7a2669438 100644 --- a/apps/desktop/src/vault/app/vault-v3/vault.component.html +++ b/apps/desktop/src/vault/app/vault-v3/vault.component.html @@ -64,6 +64,7 @@ class="tw-block tw-shrink-0 tw-px-8" [collections]="scopedCollections" [folders]="folders()" + [ciphers]="allCiphers" [scope]="vaultScope()" > diff --git a/apps/desktop/src/vault/app/vault-v3/vault.component.ts b/apps/desktop/src/vault/app/vault-v3/vault.component.ts index 3fa107dbb43..3994622a135 100644 --- a/apps/desktop/src/vault/app/vault-v3/vault.component.ts +++ b/apps/desktop/src/vault/app/vault-v3/vault.component.ts @@ -539,6 +539,8 @@ export class VaultComponent implements OnInit, OnDestr this.i18nService.t(this.calculateSearchBarLocalizationString(this.activeFilter())), ); protected ciphers: C[] = []; + /** Every allowed item, unfiltered by the page, for the card grid's item counts. */ + protected allCiphers: CipherViewLike[] = []; protected isEmpty: boolean; protected currentSearchText$: Observable = this.route.queryParams.pipe( map((queryParams) => queryParams.search), @@ -843,12 +845,21 @@ export class VaultComponent implements OnInit, OnDestr collections$, this.vfo1Foundation$, this.vaultScope$, + allowedCiphers$, ]), ), takeUntil(this.destroy$), ) .subscribe( - ([allCollections, allOrganizations, ciphers, collections, vfo1Foundation, scope]) => { + ([ + allCollections, + allOrganizations, + ciphers, + collections, + vfo1Foundation, + scope, + allowedCiphers, + ]) => { this.allCollections = allCollections; this.allOrganizations = allOrganizations; this.scopedCollections = vfo1Foundation @@ -861,6 +872,7 @@ export class VaultComponent implements OnInit, OnDestr ? allOrganizations.filter((organization) => organizationInScope(organization, scope)) : allOrganizations; this.ciphers = ciphers; + this.allCiphers = allowedCiphers; this.collectionsToDisplay = collections; this.isEmpty = collections?.length === 0 && ciphers?.length === 0; const initialLoad = this.performingInitialLoad; diff --git a/apps/web/src/app/vault/individual-vault/vault-next.component.html b/apps/web/src/app/vault/individual-vault/vault-next.component.html index 0f3503528c1..43dfa157d46 100644 --- a/apps/web/src/app/vault/individual-vault/vault-next.component.html +++ b/apps/web/src/app/vault/individual-vault/vault-next.component.html @@ -63,6 +63,7 @@ diff --git a/apps/web/src/app/vault/individual-vault/vault-next.component.ts b/apps/web/src/app/vault/individual-vault/vault-next.component.ts index 47d2f5c7e26..0d5d32f1d9a 100644 --- a/apps/web/src/app/vault/individual-vault/vault-next.component.ts +++ b/apps/web/src/app/vault/individual-vault/vault-next.component.ts @@ -331,7 +331,7 @@ export class VaultNextComponent implements OnInit { /** `undefined` until the ciphers stream first emits, which is what drives {@link loading}. */ private readonly loadedCiphers = toSignal(this.allCiphers$); - private readonly allCiphers = computed(() => this.loadedCiphers() ?? []); + protected readonly allCiphers = computed(() => this.loadedCiphers() ?? []); private readonly failedCiphers = toSignal(this.failedCiphers$, { initialValue: [] }); diff --git a/apps/web/src/locales/en/messages.json b/apps/web/src/locales/en/messages.json index 46d7139ae67..a3acaf8bef6 100644 --- a/apps/web/src/locales/en/messages.json +++ b/apps/web/src/locales/en/messages.json @@ -1877,6 +1877,10 @@ } } }, + "itemSingular": { + "message": "1 item", + "description": "Count of exactly one item, e.g. on a folder card holding a single item." + }, "clearAll": { "message": "Clear all" }, diff --git a/libs/vault/src/components/card-grid/card-grid-source.spec.ts b/libs/vault/src/components/card-grid/card-grid-source.spec.ts index 63d9a577f38..52158abbca5 100644 --- a/libs/vault/src/components/card-grid/card-grid-source.spec.ts +++ b/libs/vault/src/components/card-grid/card-grid-source.spec.ts @@ -1,5 +1,6 @@ import { CollectionView } from "@bitwarden/common/admin-console/models/collections"; import { CollectionId, OrganizationId } from "@bitwarden/common/types/guid"; +import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; import { VaultScope, VaultScopeType } from "../../models/vault-scope"; @@ -16,6 +17,10 @@ function folder(id: string, name: string): FolderView { return Object.assign(new FolderView(), { id, name }); } +let nextCipherId = 0; +const cipher = (overrides: Partial): CipherView => + Object.assign(new CipherView(), { id: `cipher-${nextCipherId++}` }, overrides); + const sharedScope = (collectionId?: CollectionId): VaultScope => ({ type: VaultScopeType.Organization, organizationId, @@ -36,6 +41,7 @@ describe("resolveCardGridSource", () => { const source = resolveCardGridSource(sharedScope("eng" as CollectionId), { collections, folders: [], + ciphers: [], }); expect(source).toEqual({ @@ -47,6 +53,7 @@ describe("resolveCardGridSource", () => { commands: ["/vault", "org-1", "shared-folders", "platform"], icon: "bwi-shared-folder", nestedCount: 1, + itemCount: 0, }, ], titleKey: "sharedFoldersInParent", @@ -59,18 +66,58 @@ describe("resolveCardGridSource", () => { const source = resolveCardGridSource(sharedScope("eng" as CollectionId), { collections, folders: [folder("banking", "Banking"), folder("checking", "Banking/Checking")], + ciphers: [], }); expect(source?.cards.map((card) => card.id)).toEqual(["platform"]); }); + it("counts the items in each card's folder and every folder beneath it, each once", () => { + const source = resolveCardGridSource(sharedScope("eng" as CollectionId), { + collections: [...collections, collection("web", "Engineering/Web")], + folders: [], + ciphers: [ + cipher({ organizationId, collectionIds: ["platform" as CollectionId] }), + cipher({ organizationId, collectionIds: ["api" as CollectionId] }), + // In two folders of one card's subtree, so counted toward it once. + cipher({ + organizationId, + collectionIds: ["platform" as CollectionId, "api" as CollectionId], + }), + // Directly in the folder in view, so no card's. + cipher({ organizationId, collectionIds: ["eng" as CollectionId] }), + cipher({ + organizationId, + collectionIds: ["web" as CollectionId], + deletedDate: new Date(), + }), + cipher({ + organizationId, + collectionIds: ["web" as CollectionId], + archivedDate: new Date(), + }), + ], + }); + + expect(source?.cards.map(({ id, itemCount }) => ({ id, itemCount }))).toEqual([ + { id: "platform", itemCount: 3 }, + { id: "web", itemCount: 0 }, + ]); + }); + it("resolves nothing for a vault scope that names no folder", () => { - expect(resolveCardGridSource(sharedScope(), { collections, folders: [] })).toBeUndefined(); + expect( + resolveCardGridSource(sharedScope(), { collections, folders: [], ciphers: [] }), + ).toBeUndefined(); }); it("resolves nothing for a folder the collections do not hold", () => { expect( - resolveCardGridSource(sharedScope("missing" as CollectionId), { collections, folders: [] }), + resolveCardGridSource(sharedScope("missing" as CollectionId), { + collections, + folders: [], + ciphers: [], + }), ).toBeUndefined(); }); }); @@ -84,7 +131,11 @@ describe("resolveCardGridSource", () => { ]; it("resolves the children of the folder in view with the My folders copy", () => { - const source = resolveCardGridSource(folderScope("banking"), { collections: [], folders }); + const source = resolveCardGridSource(folderScope("banking"), { + collections: [], + folders, + ciphers: [], + }); expect(source).toEqual({ parentName: "Banking", @@ -95,6 +146,7 @@ describe("resolveCardGridSource", () => { commands: ["/folders", "checking"], icon: "bwi-folder", nestedCount: 0, + itemCount: 0, }, { id: "savings", @@ -102,6 +154,7 @@ describe("resolveCardGridSource", () => { commands: ["/folders", "savings"], icon: "bwi-folder", nestedCount: 1, + itemCount: 0, }, ], titleKey: "foldersInParent", @@ -110,6 +163,27 @@ describe("resolveCardGridSource", () => { }); }); + it("counts the items in each card's folder and every folder beneath it", () => { + const source = resolveCardGridSource(folderScope("banking"), { + collections: [], + folders, + ciphers: [ + cipher({ folderId: "savings" }), + cipher({ folderId: "cards" }), + cipher({ folderId: "cards" }), + // Directly in the folder in view, so no card's. + cipher({ folderId: "banking" }), + cipher({ folderId: "checking", deletedDate: new Date() }), + cipher({ folderId: "checking", archivedDate: new Date() }), + ], + }); + + expect(source?.cards.map(({ id, itemCount }) => ({ id, itemCount }))).toEqual([ + { id: "checking", itemCount: 0 }, + { id: "savings", itemCount: 3 }, + ]); + }); + it("ignores the collections", () => { const source = resolveCardGridSource(folderScope("banking"), { collections: [ @@ -117,6 +191,7 @@ describe("resolveCardGridSource", () => { collection("platform", "Engineering/Platform"), ], folders, + ciphers: [], }); expect(source?.cards.map((card) => card.id)).toEqual(["checking", "savings"]); @@ -126,6 +201,7 @@ describe("resolveCardGridSource", () => { const source = resolveCardGridSource(folderScope("banking"), { collections: [], folders: [folder("banking", "Banking"), folder("deep", "Banking/Next/Deeper")], + ciphers: [], }); expect(source?.cards.map((card) => card.name)).toEqual(["Deeper"]); @@ -135,6 +211,7 @@ describe("resolveCardGridSource", () => { const source = resolveCardGridSource(folderScope("banking"), { collections: [], folders: [...folders, folder("unnamed", "")], + ciphers: [], }); expect(source?.cards.map((card) => card.id)).toEqual(["checking", "savings"]); @@ -142,7 +219,7 @@ describe("resolveCardGridSource", () => { it("resolves nothing for a folder the folders do not hold", () => { expect( - resolveCardGridSource(folderScope("missing"), { collections: [], folders }), + resolveCardGridSource(folderScope("missing"), { collections: [], folders, ciphers: [] }), ).toBeUndefined(); }); }); @@ -157,6 +234,7 @@ describe("resolveCardGridSource", () => { resolveCardGridSource(scope, { collections: [collection("eng", "Engineering")], folders: [folder("banking", "Banking")], + ciphers: [], }), ).toBeUndefined(); }); diff --git a/libs/vault/src/components/card-grid/card-grid-source.ts b/libs/vault/src/components/card-grid/card-grid-source.ts index c1555014e64..ce9fe20a838 100644 --- a/libs/vault/src/components/card-grid/card-grid-source.ts +++ b/libs/vault/src/components/card-grid/card-grid-source.ts @@ -1,11 +1,16 @@ import { CollectionView } from "@bitwarden/common/admin-console/models/collections"; -import { getNestedCollectionTree } from "@bitwarden/common/admin-console/utils/collection-utils"; +import { + getFlatCollectionTree, + getNestedCollectionTree, +} from "@bitwarden/common/admin-console/utils/collection-utils"; import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node"; import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; import { ServiceUtils } from "@bitwarden/common/vault/service-utils"; +import { CipherViewLike } from "@bitwarden/common/vault/utils/cipher-view-like-utils"; import { getNestedFolderTree } from "@bitwarden/common/vault/utils/folder-utils"; import { BitwardenIcon } from "@bitwarden/components"; +import { myFolderItemCounts, subtreeIds } from "../../models/folder-table-row"; import { folderCommands, scopedFolderId, @@ -15,6 +20,7 @@ import { VaultScopeType, } from "../../models/vault-scope"; import { sharedFolderName } from "../../utils/shared-folder-name"; +import { sharedFolderItemCounts } from "../shared-folders/shared-folder-rows"; /** A single child folder, resolved to the route its card links to. */ export type CardGridCard = { @@ -25,6 +31,9 @@ export type CardGridCard = { /** How many folders sit directly inside this one. */ nestedCount: number; + + /** Items in this folder and every folder nested beneath it, each counted once. */ + itemCount: number; }; /** The i18n keys of a message that counts folders: one for exactly one, one for any other number. */ @@ -60,6 +69,9 @@ export type CardGridSource = { export type CardGridData = { collections: CollectionView[]; folders: FolderView[]; + + /** Every item the user holds, for each card's item count. */ + ciphers: CipherViewLike[]; }; /** @@ -72,17 +84,18 @@ export function resolveCardGridSource( ): CardGridSource | undefined { switch (scope.type) { case VaultScopeType.Organization: - return sharedFolderSource(scope, data.collections); + return sharedFolderSource(scope, data.collections, data.ciphers); case VaultScopeType.Folder: - return myFolderSource(scope, data.folders); + return myFolderSource(scope, data.folders, data.ciphers); default: return undefined; } } function sharedFolderSource( - scope: VaultScope, + scope: Extract, collections: CollectionView[], + ciphers: CipherViewLike[], ): CardGridSource | undefined { const collectionId = scopedSharedFolderId(scope); if (collectionId == null) { @@ -94,6 +107,17 @@ function sharedFolderSource( return undefined; } + // Each descendant pooled under the child card it sits beneath. + const cardOf = new Map(); + for (const child of node.children) { + for (const descendant of getFlatCollectionTree([child])) { + cardOf.set(descendant.id, child.node.id); + } + } + const itemCounts = sharedFolderItemCounts(ciphers, scope.organizationId, (collectionId) => + cardOf.get(collectionId), + ); + return { // Read through `sharedFolderName`: the tree names a node whose parent is missing by the joined // path segments, not its own name. @@ -104,6 +128,7 @@ function sharedFolderSource( commands: sharedFolderRoute(scope, child), icon: "bwi-shared-folder", nestedCount: children.length, + itemCount: itemCounts.get(child.id) ?? 0, })), titleKey: "sharedFoldersInParent", countKeys: { singular: "sharedFolderSingular", plural: "sharedFolderCount" }, @@ -123,7 +148,11 @@ function sharedFolderRoute(scope: VaultScope, folder: CollectionView): string[] ); } -function myFolderSource(scope: VaultScope, folders: FolderView[]): CardGridSource | undefined { +function myFolderSource( + scope: VaultScope, + folders: FolderView[], + ciphers: CipherViewLike[], +): CardGridSource | undefined { const folderId = scopedFolderId(scope); if (folderId == null) { return undefined; @@ -136,6 +165,15 @@ function myFolderSource(scope: VaultScope, folders: FolderView[]): CardGridSourc return undefined; } + // Each descendant pooled under the child card it sits beneath. + const cardOf = new Map(); + for (const child of node.children) { + for (const id of subtreeIds(child)) { + cardOf.set(id, child.node.id); + } + } + const itemCounts = myFolderItemCounts(ciphers, (folderId) => cardOf.get(folderId)); + return { parentName: sharedFolderName(node.node), cards: node.children.map(({ node: child, children }) => ({ @@ -144,6 +182,7 @@ function myFolderSource(scope: VaultScope, folders: FolderView[]): CardGridSourc commands: folderCommands(child.id), icon: "bwi-folder", nestedCount: children.length, + itemCount: itemCounts.get(child.id) ?? 0, })), titleKey: "foldersInParent", countKeys: { singular: "folderSingular", plural: "folderCount" }, diff --git a/libs/vault/src/components/card-grid/card-grid.component.html b/libs/vault/src/components/card-grid/card-grid.component.html index 8ef14c31af8..48a5d067a8f 100644 --- a/libs/vault/src/components/card-grid/card-grid.component.html +++ b/libs/vault/src/components/card-grid/card-grid.component.html @@ -65,6 +65,9 @@ @if (folder.nestedCount > 0) { {{ nestedLabel(folder.nestedCount) }} } + {{ + itemLabel(folder.itemCount) + }} diff --git a/libs/vault/src/components/card-grid/card-grid.component.spec.ts b/libs/vault/src/components/card-grid/card-grid.component.spec.ts index a718cbd02d0..4a81d5452df 100644 --- a/libs/vault/src/components/card-grid/card-grid.component.spec.ts +++ b/libs/vault/src/components/card-grid/card-grid.component.spec.ts @@ -4,6 +4,7 @@ import { provideRouter } from "@angular/router"; import { CollectionView } from "@bitwarden/common/admin-console/models/collections"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { CollectionId, OrganizationId } from "@bitwarden/common/types/guid"; +import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; import { VaultScope, VaultScopeType } from "../../models/vault-scope"; @@ -146,6 +147,13 @@ describe("CardGridComponent", () => { return Array.from(fixture.nativeElement.querySelectorAll("a[bit-item-content]")); } + /** A card's folder name, without the item count in its trailing slot. */ + function cardName(card: Element | null): string | undefined { + const clone = card?.cloneNode(true) as HTMLElement | undefined; + clone?.querySelectorAll('[slot="end"]').forEach((end) => end.remove()); + return clone?.textContent?.trim(); + } + function trigger(): HTMLButtonElement | null { return fixture.nativeElement.querySelector(TRIGGER_SELECTOR); } @@ -207,7 +215,7 @@ describe("CardGridComponent", () => { createComponent(children(2)); expect(cards()).toHaveLength(2); - expect(cards().map((card) => card.textContent?.trim())).toEqual(["Folder 0", "Folder 1"]); + expect(cards().map(cardName)).toEqual(["Folder 0", "Folder 1"]); cards().forEach((card) => { expect(card.querySelector("bit-icon-tile bit-icon")?.classList).toContain( @@ -261,13 +269,38 @@ describe("CardGridComponent", () => { ).toEqual(["nestedSharedFolderCount:2", "nestedSharedFolderSingular", undefined]); }); + it("counts the items in and beneath each card in its trailing slot", () => { + const inFolder = (id: string, collectionId: string) => + Object.assign(new CipherView(), { + id, + organizationId, + collectionIds: [collectionId as CollectionId], + }); + fixture = TestBed.createComponent(CardGridComponent); + fixture.componentRef.setInput("collections", [ + ...children(3), + collection("backend", `${PARENT.name}/Folder 0/Backend`), + ]); + fixture.componentRef.setInput("ciphers", [ + inFolder("1", `${PARENT.id}-folder-0`), + inFolder("2", "backend"), + inFolder("3", `${PARENT.id}-folder-1`), + ]); + fixture.componentRef.setInput("scope", scopeTo(PARENT.id)); + fixture.detectChanges(); + + expect( + cards().map((card) => card.querySelector('span[slot="end"]')?.textContent?.trim()), + ).toEqual(["itemCount:2", "itemSingular", "itemCount:0"]); + }); + it("names a child whose intermediate parent is missing by its own name, not its path", () => { createComponent([ collection(PARENT.id, PARENT.name), collection("deepest", `${PARENT.name}/Next/Deeper/Deepest`), ]); - expect(cards().map((card) => card.textContent?.trim())).toEqual(["Deepest"]); + expect(cards().map(cardName)).toEqual(["Deepest"]); }); it("caps the grid at three columns, each at least 240px wide, with 12px spacing", () => { @@ -292,7 +325,7 @@ describe("CardGridComponent", () => { createComponent(nested); expect(accordionTitle()).toBe("sharedFoldersInParent:Engineering"); - expect(cards().map((card) => card.textContent?.trim())).toEqual(["Platform"]); + expect(cards().map(cardName)).toEqual(["Platform"]); }); it("renders nothing until the scope drills into a folder", () => { @@ -414,7 +447,7 @@ describe("CardGridComponent", () => { // The seventh card, first past the six the two-column grid shows, rather than the tenth a // three-column grid would have cut off at. expect(document.activeElement).toBe(cards()[6]); - expect(document.activeElement?.textContent?.trim()).toBe("Folder 6"); + expect(cardName(document.activeElement)).toBe("Folder 6"); }); }); @@ -635,7 +668,7 @@ describe("CardGridComponent", () => { folder("other", "Other"), ]); - expect(cards().map((card) => card.textContent?.trim())).toEqual(["Checking", "Savings"]); + expect(cards().map(cardName)).toEqual(["Checking", "Savings"]); cards().forEach((card) => { expect(card.querySelector("bit-icon-tile bit-icon")?.classList).toContain("bwi-folder"); expect(card.querySelector("bit-icon-tile bit-icon")?.classList).not.toContain( diff --git a/libs/vault/src/components/card-grid/card-grid.component.stories.ts b/libs/vault/src/components/card-grid/card-grid.component.stories.ts index b77e1ffcb6f..260e2508e9e 100644 --- a/libs/vault/src/components/card-grid/card-grid.component.stories.ts +++ b/libs/vault/src/components/card-grid/card-grid.component.stories.ts @@ -4,6 +4,7 @@ import { Meta, StoryObj, componentWrapperDecorator, moduleMetadata } from "@stor import { CollectionView } from "@bitwarden/common/admin-console/models/collections"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { CollectionId, OrganizationId } from "@bitwarden/common/types/guid"; +import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; import { I18nMockService } from "@bitwarden/components"; @@ -49,6 +50,26 @@ const DEFAULT_FOLDERS = childFolders([ "People Ops", ]); +/** `count` items in the shared folder `collectionId`, for the cards' item counts. */ +function itemsIn(collectionId: string, count: number): CipherView[] { + return Array.from({ length: count }, (_, i) => + Object.assign(new CipherView(), { + id: `${collectionId}-item-${i}`, + organizationId, + collectionIds: [collectionId as CollectionId], + }), + ); +} + +/** Engineering's count includes the items in Backend and Frontend beneath it. */ +const DEFAULT_CIPHERS = [ + ...itemsIn("folder-0", 4), + ...itemsIn("folder-1", 3), + ...itemsIn("folder-2", 3), + ...itemsIn("folder-3", 5), + ...itemsIn("folder-5", 1), +]; + /** * Fourteen children — more than the three rows on show hold at any width, so the rest always * collapse behind the trigger: five of them at three columns, eight at two, eleven at one. @@ -96,6 +117,18 @@ const MY_FOLDERS = [ myFolder("taxes", "Banking/Taxes"), ]; +/** Checking's and Savings' counts include the items in the folders nested beneath them. */ +const MY_FOLDER_CIPHERS = [ + "checking", + "joint", + "joint", + "savings", + "cards", + "cards", + "cards", + "taxes", +].map((folderId, i) => Object.assign(new CipherView(), { id: `my-item-${i}`, folderId })); + const LONG_NAME_FOLDERS = childFolders([ "Engineering — Platform, Infrastructure, and Developer Experience", "Design — Brand, Product, and Marketing Systems", @@ -124,6 +157,8 @@ export default { folderSingular: (count) => `${count} folder`, nestedFolderCount: (count) => `${count} nested folders`, nestedFolderSingular: "1 nested folder", + itemCount: (count) => `${count} items`, + itemSingular: "1 item", showAll: "Show all", showLess: "Show less", }), @@ -134,6 +169,7 @@ export default { ], args: { collections: DEFAULT_FOLDERS, + ciphers: DEFAULT_CIPHERS, scope: SCOPE, }, } as Meta; @@ -147,7 +183,7 @@ type Story = StoryObj; /** * Five children — one full row of three plus a partial row, no overflow. Engineering and Design - * count the folders nested inside them beneath their names. + * count the folders nested inside them beneath their names, and each card its items at its end. */ export const Default: Story = {}; @@ -166,6 +202,7 @@ export const MyFolders: Story = { args: { collections: [], folders: MY_FOLDERS, + ciphers: MY_FOLDER_CIPHERS, scope: { type: VaultScopeType.Folder, folderId: "banking" }, }, }; diff --git a/libs/vault/src/components/card-grid/card-grid.component.ts b/libs/vault/src/components/card-grid/card-grid.component.ts index 1b7a7567d61..c1bb3fad776 100644 --- a/libs/vault/src/components/card-grid/card-grid.component.ts +++ b/libs/vault/src/components/card-grid/card-grid.component.ts @@ -19,6 +19,7 @@ import { RouterLink } from "@angular/router"; import { CollectionView } from "@bitwarden/common/admin-console/models/collections"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; +import { CipherViewLike } from "@bitwarden/common/vault/utils/cipher-view-like-utils"; import { IconComponent, IconTileComponent, @@ -118,6 +119,9 @@ export class CardGridComponent { */ readonly folders = input([]); + /** Every item the user holds, for each card's count of the items in and beneath it. */ + readonly ciphers = input([]); + /** * The page the grid is on, and the folder within it the URL has drilled into. The folder is what * the grid renders the children of, so a scope naming none renders nothing. @@ -204,6 +208,7 @@ export class CardGridComponent { resolveCardGridSource(this.scope(), { collections: this.collections(), folders: this.folders(), + ciphers: this.ciphers(), }), ); @@ -262,6 +267,13 @@ export class CardGridComponent { return count === 1 ? this.i18nService.t(keys.singular) : this.i18nService.t(keys.plural, count); } + /** The trailing text of a card, counting the items in it and every folder nested beneath it. */ + protected itemLabel(count: number): string { + return count === 1 + ? this.i18nService.t("itemSingular") + : this.i18nService.t("itemCount", count); + } + protected readonly overflowCards = computed(() => this.cards().slice(this.collapsedCardCount())); /** diff --git a/libs/vault/src/models/folder-table-row.spec.ts b/libs/vault/src/models/folder-table-row.spec.ts index ad0251deab4..9dcf349c426 100644 --- a/libs/vault/src/models/folder-table-row.spec.ts +++ b/libs/vault/src/models/folder-table-row.spec.ts @@ -23,7 +23,7 @@ describe("buildFolderRows", () => { ]); }); - it("excludes trashed ciphers from the count but keeps archived ones", () => { + it("excludes trashed and archived ciphers from the count", () => { const rows = buildFolderRows( [folder("1", "Banking")], [ @@ -33,7 +33,7 @@ describe("buildFolderRows", () => { ], ); - expect(rows[0].itemCount).toBe(2); + expect(rows[0].itemCount).toBe(1); }); it("excludes the synthetic no-folder entry", () => { diff --git a/libs/vault/src/models/folder-table-row.ts b/libs/vault/src/models/folder-table-row.ts index 67f462eef4a..40b1055cd08 100644 --- a/libs/vault/src/models/folder-table-row.ts +++ b/libs/vault/src/models/folder-table-row.ts @@ -44,20 +44,7 @@ export function buildFolderRows( } } - const itemCounts = new Map(); - for (const cipher of ciphers) { - if ( - CipherViewLikeUtils.isDeleted(cipher) || - cipher.folderId == null || - cipher.folderId === "" - ) { - continue; - } - const rootId = rootOf.get(String(cipher.folderId)); - if (rootId !== undefined) { - itemCounts.set(rootId, (itemCounts.get(rootId) ?? 0) + 1); - } - } + const itemCounts = myFolderItemCounts(ciphers, (folderId) => rootOf.get(folderId)); return [ ...roots.map((root) => ({ @@ -77,6 +64,36 @@ export function buildFolderRows( ]; } -function subtreeIds(node: TreeNode): string[] { +/** + * How many active (not trashed or archived) items each folder holds, keyed by folder id. Folders with no items are + * absent rather than zero. + * + * `groupOf` pools folders under a shared key — e.g. each folder under its top-level ancestor. A + * folder it maps to `undefined` is left uncounted. Defaults to each folder being its own group. + */ +export function myFolderItemCounts( + ciphers: CipherViewLike[], + groupOf: (folderId: string) => string | undefined = (folderId) => folderId, +): Map { + const counts = new Map(); + for (const cipher of ciphers) { + if ( + CipherViewLikeUtils.isDeleted(cipher) || + CipherViewLikeUtils.isArchived(cipher) || + cipher.folderId == null || + cipher.folderId === "" + ) { + continue; + } + const group = groupOf(String(cipher.folderId)); + if (group !== undefined) { + counts.set(group, (counts.get(group) ?? 0) + 1); + } + } + return counts; +} + +/** The ids of {@link node} and every folder nested beneath it. */ +export function subtreeIds(node: TreeNode): string[] { return [node.node.id, ...node.children.flatMap(subtreeIds)]; }