Files
grimmory/booklore-ui/src/app/features/book/service/book-navigation.service.ts
T
Alex ccb80d7421 refactor(frontend): Migrate state to Tanstack Query + signals (#117)
* refactor(frontend): migrate state to TanStack Query and signals

* fix(frontend): keep book cache in sync after patches
2026-04-22 17:26:31 -04:00

78 lines
2.2 KiB
TypeScript

import {computed, Injectable, signal} from '@angular/core';
export interface BookNavigationState {
bookIds: number[];
currentIndex: number;
}
@Injectable({
providedIn: 'root'
})
export class BookNavigationService {
private readonly _navigationState = signal<BookNavigationState | null>(null);
readonly navigationState = this._navigationState.asReadonly();
private readonly _availableBookIds = signal<number[]>([]);
readonly availableBookIds = this._availableBookIds.asReadonly();
readonly canNavigatePrevious = computed(() => {
const state = this.navigationState();
return state !== null && state.currentIndex > 0;
});
readonly canNavigateNext = computed(() => {
const state = this.navigationState();
return state !== null && state.currentIndex < state.bookIds.length - 1;
});
readonly previousBookId = computed<number | null>(() => {
const state = this.navigationState();
return state && state.currentIndex > 0
? state.bookIds[state.currentIndex - 1]
: null;
});
readonly nextBookId = computed<number | null>(() => {
const state = this.navigationState();
return state && state.currentIndex < state.bookIds.length - 1
? state.bookIds[state.currentIndex + 1]
: null;
});
readonly currentPosition = computed<{ current: number; total: number } | null>(() => {
const state = this.navigationState();
return state
? {
current: state.currentIndex + 1,
total: state.bookIds.length
}
: null;
});
setAvailableBookIds(bookIds: number[]): void {
this._availableBookIds.set(bookIds);
}
setNavigationContext(bookIds: number[], currentBookId: number): void {
const currentIndex = bookIds.indexOf(currentBookId);
if (currentIndex !== -1) {
this._navigationState.set({bookIds, currentIndex});
} else {
this._navigationState.set(null);
}
}
updateCurrentBook(bookId: number): void {
const state = this.navigationState();
if (state) {
const newIndex = state.bookIds.indexOf(bookId);
if (newIndex !== -1) {
this._navigationState.set({
...state,
currentIndex: newIndex
});
}
}
}
}