From b28c204f23605d5704bfb361e2f17be658f38e5b Mon Sep 17 00:00:00 2001 From: Lorenz Hilpert Date: Tue, 1 Jul 2025 12:00:12 +0200 Subject: [PATCH] refactor(tabs): add metadata and navigation properties to Tab model --- libs/core/tabs/src/lib/tab.service.ts | 167 +++++++++++++------------- 1 file changed, 86 insertions(+), 81 deletions(-) diff --git a/libs/core/tabs/src/lib/tab.service.ts b/libs/core/tabs/src/lib/tab.service.ts index eb37fa960..85c0d8ec3 100644 --- a/libs/core/tabs/src/lib/tab.service.ts +++ b/libs/core/tabs/src/lib/tab.service.ts @@ -1,81 +1,86 @@ -import { - patchState, - signalStore, - withComputed, - withHooks, - withMethods, - withState, -} from '@ngrx/signals'; -import { - addEntities, - addEntity, - removeEntity, - updateEntity, - withEntities, -} from '@ngrx/signals/entities'; -import { Tab } from './tab'; -import { z } from 'zod'; -import { AddTabSchema, PatchTabSchema } from './tab.schemas'; -import { computed, effect } from '@angular/core'; - -export const TabService = signalStore( - { providedIn: 'root' }, - withState<{ activatedTabId: number | null }>({ - activatedTabId: null, - }), - withEntities(), - withComputed((store) => ({ - nextId: computed( - () => Math.max(0, ...store.entities().map((e) => e.id)) + 1, - ), - activatedTab: computed(() => { - const activeTabId = store.activatedTabId(); - if (activeTabId === null) { - return null; - } - return store.entities().find((e) => e.id === activeTabId) ?? null; - }), - })), - withMethods((store) => ({ - addTab(add: z.infer) { - const parsed = AddTabSchema.parse(add); - const tab: Tab = { - name: parsed.name, - id: store.nextId(), - createdAt: Date.now(), - tags: parsed.tags, - }; - patchState(store, addEntity(tab)); - return tab; - }, - activateTab(id: number) { - patchState(store, { - ...updateEntity({ id, changes: { activatedAt: Date.now() } }), - activatedTabId: id, - }); - }, - patchTab(id: number, changes: z.infer) { - patchState( - store, - updateEntity({ id, changes: PatchTabSchema.parse(changes) }), - ); - }, - removeTab(id: number) { - patchState(store, removeEntity(id)); - }, - })), - withHooks((store) => ({ - onInit() { - const entitiesStr = localStorage.getItem('TabEntities'); - if (entitiesStr) { - const entities = JSON.parse(entitiesStr); - patchState(store, addEntities(entities)); - } - - effect(() => { - const state = store.entities(); - localStorage.setItem('TabEntities', JSON.stringify(state)); - }); - }, - })), -); +import { + patchState, + signalStore, + withComputed, + withHooks, + withMethods, + withState, +} from '@ngrx/signals'; +import { + addEntities, + addEntity, + removeEntity, + updateEntity, + withEntities, +} from '@ngrx/signals/entities'; +import { Tab } from './tab'; +import { z } from 'zod'; +import { AddTabSchema, PatchTabSchema } from './tab.schemas'; +import { computed, effect } from '@angular/core'; + +export const TabService = signalStore( + { providedIn: 'root' }, + withState<{ activatedTabId: number | null }>({ + activatedTabId: null, + }), + withEntities(), + withComputed((store) => ({ + nextId: computed( + () => Math.max(0, ...store.entities().map((e) => e.id)) + 1, + ), + activatedTab: computed(() => { + const activeTabId = store.activatedTabId(); + if (activeTabId === null) { + return null; + } + return store.entities().find((e) => e.id === activeTabId) ?? null; + }), + })), + withMethods((store) => ({ + addTab(add: z.infer) { + const parsed = AddTabSchema.parse(add); + const tab: Tab = { + name: parsed.name, + id: store.nextId(), + createdAt: Date.now(), + tags: parsed.tags, + metadata: {}, + navigation: { + current: 0, + locations: [], + }, + }; + patchState(store, addEntity(tab)); + return tab; + }, + activateTab(id: number) { + patchState(store, { + ...updateEntity({ id, changes: { activatedAt: Date.now() } }), + activatedTabId: id, + }); + }, + patchTab(id: number, changes: z.infer) { + patchState( + store, + updateEntity({ id, changes: PatchTabSchema.parse(changes) }), + ); + }, + removeTab(id: number) { + patchState(store, removeEntity(id)); + }, + })), + withHooks((store) => ({ + onInit() { + const entitiesStr = localStorage.getItem('TabEntities'); + if (entitiesStr) { + const entities = JSON.parse(entitiesStr); + patchState(store, addEntities(entities)); + } + + effect(() => { + const state = store.entities(); + localStorage.setItem('TabEntities', JSON.stringify(state)); + }); + }, + })), +);