From 26a81ea0e516bb154e60701826e1da0c278d9c8c Mon Sep 17 00:00:00 2001 From: mister-ideas <36502095+mister-ideas@users.noreply.github.com> Date: Sat, 31 Oct 2026 11:40:22 +0100 Subject: [PATCH] Feature/add sorting to holdings table of home overview --- CHANGELOG.md | 8 +++ .../accounts-table.component.html | 1 + .../accounts-table.component.ts | 2 + .../holdings-table.component.html | 11 ++- .../holdings-table.component.ts | 13 ++-- libs/ui/src/lib/persist-sort/index.ts | 1 + .../persist-sort.directive.spec.ts | 70 +++++++++++++++++++ .../persist-sort/persist-sort.directive.ts | 57 +++++++++++++++ 8 files changed, 147 insertions(+), 16 deletions(-) create mode 100644 libs/ui/src/lib/persist-sort/index.ts create mode 100644 libs/ui/src/lib/persist-sort/persist-sort.directive.spec.ts create mode 100644 libs/ui/src/lib/persist-sort/persist-sort.directive.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index bc3e536b6f..2070ebe2b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Changed + +- Improved the holdings table of the home overview by adding the sorting by name and performance +- Changed the default sorting of the holdings table to the performance in descending order +- Persisted the sorting of the holdings and accounts tables on the device + ## 3.100.0 - 2026-11-15 ### Changed diff --git a/libs/ui/src/lib/accounts-table/accounts-table.component.html b/libs/ui/src/lib/accounts-table/accounts-table.component.html index 43a6a429b4..00b544bf4e 100644 --- a/libs/ui/src/lib/accounts-table/accounts-table.component.html +++ b/libs/ui/src/lib/accounts-table/accounts-table.component.html @@ -20,6 +20,7 @@ matSortActive="name" matSortDirection="asc" [dataSource]="dataSource" + [gfPersistSort]="'accounts'" > @@ -210,11 +211,7 @@ - + { - return this.mode() === 'default' - ? 'allocationInPercentage' - : 'assetProfile.name'; - }); - - protected readonly sortDirection = computed(() => { - return this.mode() === 'default' ? 'desc' : 'asc'; - }); + protected readonly sortActive = 'netPerformancePercentWithCurrencyEffect'; + protected readonly sortDirection: SortDirection = 'desc'; public constructor() { this.dataSource.sortingDataAccessor = (holding, path) => { diff --git a/libs/ui/src/lib/persist-sort/index.ts b/libs/ui/src/lib/persist-sort/index.ts new file mode 100644 index 0000000000..22d5db3391 --- /dev/null +++ b/libs/ui/src/lib/persist-sort/index.ts @@ -0,0 +1 @@ +export * from './persist-sort.directive'; diff --git a/libs/ui/src/lib/persist-sort/persist-sort.directive.spec.ts b/libs/ui/src/lib/persist-sort/persist-sort.directive.spec.ts new file mode 100644 index 0000000000..7fa7a7f811 --- /dev/null +++ b/libs/ui/src/lib/persist-sort/persist-sort.directive.spec.ts @@ -0,0 +1,70 @@ +import { Component } from '@angular/core'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { MatSort, MatSortModule } from '@angular/material/sort'; + +import { GfPersistSortDirective } from './persist-sort.directive'; + +@Component({ + imports: [GfPersistSortDirective, MatSortModule], + template: `
` +}) +class TestHostComponent {} + +describe('GfPersistSortDirective', () => { + let fixture: ComponentFixture; + + const create = () => { + fixture = TestBed.createComponent(TestHostComponent); + fixture.detectChanges(); + + return fixture.debugElement.children[0].injector.get(MatSort); + }; + + beforeEach(() => { + localStorage.clear(); + TestBed.configureTestingModule({ imports: [TestHostComponent] }); + }); + + it('keeps the default sorting without a stored value', () => { + const sort = create(); + + expect(sort.active).toBe('name'); + expect(sort.direction).toBe('asc'); + }); + + it('stores the sorting on change', () => { + const sort = create(); + + sort.sort({ id: 'value', start: 'desc', disableClear: false }); + + expect(JSON.parse(localStorage.getItem('gf-sort-test') ?? '{}')).toEqual({ + active: 'value', + direction: 'desc' + }); + }); + + it('restores a stored sorting', () => { + localStorage.setItem( + 'gf-sort-test', + JSON.stringify({ active: 'value', direction: 'desc' }) + ); + + const sort = create(); + + expect(sort.active).toBe('value'); + expect(sort.direction).toBe('desc'); + }); + + it('ignores an invalid stored sorting', () => { + localStorage.setItem('gf-sort-test', '{"active":"value","direction":"x"}'); + + const sort = create(); + + expect(sort.active).toBe('name'); + }); +}); diff --git a/libs/ui/src/lib/persist-sort/persist-sort.directive.ts b/libs/ui/src/lib/persist-sort/persist-sort.directive.ts new file mode 100644 index 0000000000..d7e70f81ae --- /dev/null +++ b/libs/ui/src/lib/persist-sort/persist-sort.directive.ts @@ -0,0 +1,57 @@ +import { DestroyRef, Directive, inject, input, OnInit } from '@angular/core'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; +import { MatSort, SortDirection } from '@angular/material/sort'; + +const STORAGE_KEY_PREFIX = 'gf-sort-'; + +/** + * Remembers the sorting of a `matSort` table on the device and restores it + * on the next visit. + */ +@Directive({ + selector: '[gfPersistSort]' +}) +export class GfPersistSortDirective implements OnInit { + public readonly gfPersistSort = input.required(); + + private readonly destroyRef = inject(DestroyRef); + private readonly matSort = inject(MatSort, { self: true }); + + public ngOnInit() { + const storageKey = `${STORAGE_KEY_PREFIX}${this.gfPersistSort()}`; + const storedSort = this.readStoredSort(storageKey); + + if (storedSort) { + this.matSort.active = storedSort.active; + this.matSort.direction = storedSort.direction; + } + + this.matSort.sortChange + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe(({ active, direction }) => { + try { + globalThis.localStorage?.setItem( + storageKey, + JSON.stringify({ active, direction }) + ); + } catch {} + }); + } + + private readStoredSort(aStorageKey: string) { + try { + const item = globalThis.localStorage?.getItem(aStorageKey); + const { active, direction } = JSON.parse(item ?? 'null') ?? {}; + + if ( + typeof active === 'string' && + active.length > 0 && + (direction === 'asc' || direction === 'desc') + ) { + return { active, direction: direction as SortDirection }; + } + } catch {} + + return undefined; + } +}