refactor(types): replace most usages of 'any' with 'unknown' and improve type safety across services and components (#2050)

* refactor(types): replace most usages of 'any' with 'unknown' and improve type safety across services and components

- Update method signatures, variables, and interfaces to use 'unknown' instead of 'any'
- Refine event and observable typings for better type inference
- Add or adjust explicit types for function parameters and return values
- Update Chart.js tooltip and context typings for chart services
- Improve type safety in Book, BookState, and related models
- Adjust template bindings and method calls to match stricter typings
- Add SecureSrcDirective for secure image loading with fallback and error handling

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* chore(angular): remove proxy configuration from development build settings

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* refactor(book-browser): streamline entity and book state management with reactive streams

- Refactor entity type and entity fetching logic to use observables and combine route params
- Simplify book state handling by consolidating filtering and fetching into a single reactive pipeline
- Remove redundant applySortOption logic and related subscriptions
- Fix book card index binding in template for improved rendering performance
- Ensure proper cleanup of epub rendition in epub-reader component

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* fix(book-browser, book-uploader): improve permission checks and enhance file upload handling

- Use optional chaining for user permission checks in book-browser.component.html to prevent runtime errors when user data is undefined
- Refactor book-uploader.component.ts to pass destination and IDs explicitly to uploadBatch, reconstruct files for strict type handling, and ensure proper upload flow and UI feedback

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* refactor(charts, services): improve type safety for book state validation and update form field checks

- Refactor isValidBookState methods across chart components and services to use type predicates and stricter type checks for BookState
- Update metadata-picker.component to simplify array field checks for authors, categories, moods, and tags
- Adjust magic-shelf-utils to remove unnecessary fallback in object spread
- Update notification-event.service to use correct timeout type for highlightTimeout

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* fix(theme): ensure safe merging of theme properties and improve type definition for themesMap

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* refactor(epub-reader): streamline book and rendition initialization logic

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2026-01-02 18:22:54 -07:00
committed by GitHub
parent 998b07d243
commit ab96eee5bc
86 changed files with 853 additions and 638 deletions
@@ -6,6 +6,7 @@ import {catchError, filter, first, takeUntil} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {BookService} from '../../../book/service/book.service';
import {Book} from '../../../book/model/book.model';
import {BookState} from '../../../book/model/state/book-state.model';
interface RatingStats {
ratingRange: string;
@@ -212,8 +213,16 @@ export class PersonalRatingChartComponent implements OnInit, OnDestroy {
return this.processPersonalRatingStats(currentState.books!);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private processPersonalRatingStats(books: Book[]): RatingStats[] {
@@ -3,9 +3,10 @@ import {CommonModule} from '@angular/common';
import {BaseChartDirective} from 'ng2-charts';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {catchError, filter, first, takeUntil} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {ChartConfiguration, ChartData, Chart, TooltipItem} from 'chart.js';
import {BookService} from '../../../book/service/book.service';
import {Book, ReadStatus} from '../../../book/model/book.model';
import {BookState} from '../../../book/model/state/book-state.model';
interface ReadingStatusStats {
status: string;
@@ -154,8 +155,16 @@ export class ReadStatusChartComponent implements OnInit, OnDestroy {
return this.processReadingStatusStats(currentState.books!);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private processReadingStatusStats(books: Book[]): ReadingStatusStats[] {
@@ -209,7 +218,7 @@ export class ReadStatusChartComponent implements OnInit, OnDestroy {
return STATUS_MAPPING[status] ?? 'No Status';
}
private generateLegendLabels(chart: any) {
private generateLegendLabels(chart: Chart) {
const data = chart.data;
if (!data.labels?.length || !data.datasets?.[0]?.data?.length) {
return [];
@@ -218,13 +227,13 @@ export class ReadStatusChartComponent implements OnInit, OnDestroy {
const dataset = data.datasets[0];
const dataValues = dataset.data as number[];
return data.labels.map((label: string, index: number) => {
return data.labels.map((label: unknown, index: number) => {
const isVisible = typeof chart.getDataVisibility === 'function'
? chart.getDataVisibility(index)
: !((chart.getDatasetMeta && chart.getDatasetMeta(0)?.data?.[index]?.hidden) || false);
: !((chart.getDatasetMeta && (chart.getDatasetMeta(0)?.data?.[index] as any)?.hidden) || false);
return {
text: `${label} (${dataValues[index]})`,
text: `${String(label)} (${dataValues[index]})`,
fillStyle: (dataset.backgroundColor as string[])[index],
strokeStyle: '#ffffff',
lineWidth: 1,
@@ -235,7 +244,7 @@ export class ReadStatusChartComponent implements OnInit, OnDestroy {
});
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<any>): string {
const dataIndex = context.dataIndex;
const dataset = context.dataset;
const value = dataset.data[dataIndex] as number;
@@ -6,6 +6,7 @@ import {catchError, filter, first, takeUntil} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {BookService} from '../../../book/service/book.service';
import {Book, ReadStatus} from '../../../book/model/book.model';
import {BookState} from '../../../book/model/state/book-state.model';
interface ReadingDNAProfile {
adventurous: number;
@@ -238,8 +239,16 @@ export class ReadingDNAChartComponent implements OnInit, OnDestroy {
return this.analyzeReadingDNA(currentState.books!);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private analyzeReadingDNA(books: Book[]): ReadingDNAProfile {
@@ -6,6 +6,7 @@ import {catchError, filter, first, takeUntil} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {BookService} from '../../../book/service/book.service';
import {Book, ReadStatus} from '../../../book/model/book.model';
import {BookState} from '../../../book/model/state/book-state.model';
interface ReadingHabitsProfile {
consistency: number;
@@ -238,8 +239,16 @@ export class ReadingHabitsChartComponent implements OnInit, OnDestroy {
return this.analyzeReadingHabits(currentState.books!);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private analyzeReadingHabits(books: Book[]): ReadingHabitsProfile {
@@ -6,6 +6,7 @@ import {catchError, filter, first, takeUntil} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {BookService} from '../../../book/service/book.service';
import {Book} from '../../../book/model/book.model';
import {BookState} from '../../../book/model/state/book-state.model';
interface MatrixDataPoint {
x: number; // month (0-11)
@@ -204,8 +205,16 @@ export class ReadingHeatmapChartComponent implements OnInit, OnDestroy {
return this.processHeatmapData(currentState.books!);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private processHeatmapData(books: Book[]): YearMonthData[] {
@@ -5,6 +5,7 @@ import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {catchError, filter, first, takeUntil} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {BookService} from '../../../book/service/book.service';
import {BookState} from '../../../book/model/state/book-state.model';
import {Book} from '../../../book/model/book.model';
interface ReadingProgressStats {
@@ -195,8 +196,16 @@ export class ReadingProgressChartComponent implements OnInit, OnDestroy {
return this.processReadingProgressStats(currentState.books!);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private processReadingProgressStats(books: Book[]): ReadingProgressStats[] {
@@ -4,7 +4,8 @@ import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operato
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book, ReadStatus} from '../../book/model/book.model';
import {ChartConfiguration, ChartData, ChartType} from 'chart.js';
import {BookState} from '../../book/model/state/book-state.model';
import {ChartConfiguration, ChartData, ChartType, TooltipItem} from 'chart.js';
interface AuthorStats {
author: string;
@@ -218,8 +219,16 @@ export class AuthorPopularityChartService implements OnDestroy {
return this.processAuthorStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | number | null): Book[] {
@@ -290,7 +299,7 @@ export class AuthorPopularityChartService implements OnDestroy {
})).filter(stat => stat.bookCount > 0);
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<any>): string {
const dataIndex = context.dataIndex;
const stats = this.getLastCalculatedStats();
@@ -1,11 +1,12 @@
import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {ChartConfiguration, ChartData, Chart, TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface QualityScoreStats {
category: string;
@@ -159,8 +160,16 @@ export class BookQualityScoreChartService implements OnDestroy {
return this.processQualityScoreStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | null): Book[] {
@@ -242,7 +251,7 @@ export class BookQualityScoreChartService implements OnDestroy {
.sort((a, b) => b.averageScore - a.averageScore);
}
private generateLegendLabels(chart: any) {
private generateLegendLabels(chart: Chart) {
const data = chart.data;
if (!data.labels?.length || !data.datasets?.[0]?.data?.length) {
return [];
@@ -250,13 +259,13 @@ export class BookQualityScoreChartService implements OnDestroy {
const dataset = data.datasets[0];
return data.labels.map((label: string, index: number) => {
return data.labels.map((label: unknown, index: number) => {
const isVisible = typeof chart.getDataVisibility === 'function'
? chart.getDataVisibility(index)
: !((chart.getDatasetMeta && chart.getDatasetMeta(0)?.data?.[index]?.hidden) || false);
: !((chart.getDatasetMeta && (chart.getDatasetMeta(0)?.data?.[index] as any)?.hidden) || false);
return {
text: label,
text: String(label),
fillStyle: (dataset.backgroundColor as string[])[index],
strokeStyle: '#ffffff',
lineWidth: 1,
@@ -267,7 +276,7 @@ export class BookQualityScoreChartService implements OnDestroy {
});
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<any>): string {
const dataIndex = context.dataIndex;
const qualityStats = this.getLastCalculatedStats();
@@ -276,8 +285,10 @@ export class BookQualityScoreChartService implements OnDestroy {
}
const stats = qualityStats[dataIndex];
const total = context.chart.data.datasets[0].data.reduce((a: number, b: number) => a + b, 0);
const percentage = ((stats.count / total) * 100).toFixed(1);
// Defensive: filter to numbers only, avoid division by zero
const dataArr = (context.chart.data.datasets[0].data as (number | null | undefined)[]).filter((v): v is number => typeof v === 'number');
const total = dataArr.reduce((a, b) => a + b, 0);
const percentage = total > 0 ? ((stats.count / total) * 100).toFixed(1) : '0.0';
return `${stats.count} books (${percentage}%) | Average Score: ${stats.averageScore}/10 (${stats.scoreRange})`;
}
@@ -4,6 +4,7 @@ import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operato
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
import {ChartConfiguration, ChartData, ChartType} from 'chart.js';
interface RatingStats {
@@ -190,8 +191,16 @@ export class BookRatingChartService implements OnDestroy {
return this.processRatingStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | number | null): Book[] {
@@ -2,10 +2,12 @@ import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface BookSizeStats {
title: string;
@@ -195,8 +197,16 @@ export class BookSizeChartService implements OnDestroy {
return this.processBookSizeStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | null): Book[] {
@@ -224,7 +234,7 @@ export class BookSizeChartService implements OnDestroy {
return booksWithSize;
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<any>): string {
const dataIndex = context.dataIndex;
const stats = this.getLastCalculatedStats();
@@ -1,11 +1,12 @@
import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData, ChartType} from 'chart.js';
import {ChartConfiguration, ChartData, ChartType, Chart, TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface BookTypeStats {
bookType: string;
@@ -154,8 +155,16 @@ export class BookTypeChartService implements OnDestroy {
this.updateChartData(stats);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | null): Book[] {
@@ -205,7 +214,7 @@ export class BookTypeChartService implements OnDestroy {
return TYPE_MAPPING[type] || type;
}
private generateLegendLabels(chart: any) {
private generateLegendLabels(chart: Chart) {
const data = chart.data;
if (!data.labels?.length || !data.datasets?.[0]?.data?.length) {
return [];
@@ -214,8 +223,8 @@ export class BookTypeChartService implements OnDestroy {
const dataset = data.datasets[0];
const dataValues = dataset.data as number[];
return data.labels.map((label: string, index: number) => ({
text: `${label} (${dataValues[index]})`,
return data.labels.map((label: unknown, index: number) => ({
text: `${String(label)} (${dataValues[index]})`,
fillStyle: (dataset.backgroundColor as string[])[index],
strokeStyle: '#ffffff',
lineWidth: 1,
@@ -225,7 +234,7 @@ export class BookTypeChartService implements OnDestroy {
}));
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<any>): string {
const dataIndex = context.dataIndex;
const dataset = context.dataset;
const value = dataset.data[dataIndex] as number;
@@ -1,11 +1,12 @@
import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {ChartConfiguration, ChartData, Chart, TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface LanguageStats {
language: string;
@@ -154,8 +155,16 @@ export class LanguageDistributionChartService implements OnDestroy {
return this.processLanguageStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | null): Book[] {
@@ -246,7 +255,7 @@ export class LanguageDistributionChartService implements OnDestroy {
.sort((a, b) => b.count - a.count);
}
private generateLegendLabels(chart: any) {
private generateLegendLabels(chart: Chart) {
const data = chart.data;
if (!data.labels?.length || !data.datasets?.[0]?.data?.length) {
return [];
@@ -255,13 +264,13 @@ export class LanguageDistributionChartService implements OnDestroy {
const dataset = data.datasets[0];
const dataValues = dataset.data as number[];
return data.labels.map((label: string, index: number) => {
return data.labels.map((label: unknown, index: number) => {
const isVisible = typeof chart.getDataVisibility === 'function'
? chart.getDataVisibility(index)
: !((chart.getDatasetMeta && chart.getDatasetMeta(0)?.data?.[index]?.hidden) || false);
: !((chart.getDatasetMeta && (chart.getDatasetMeta(0)?.data?.[index] as any)?.hidden) || false);
return {
text: `${label} (${dataValues[index]})`,
text: `${String(label)} (${dataValues[index]})`,
fillStyle: (dataset.backgroundColor as string[])[index],
strokeStyle: '#ffffff',
lineWidth: 1,
@@ -272,7 +281,7 @@ export class LanguageDistributionChartService implements OnDestroy {
});
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<any>): string {
const dataIndex = context.dataIndex;
const dataset = context.dataset;
const value = dataset.data[dataIndex] as number;
@@ -2,10 +2,12 @@ import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book, ReadStatus} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface MonthlyPattern {
month: string;
@@ -280,8 +282,16 @@ export class MonthlyReadingPatternsChartService implements OnDestroy {
return this.processMonthlyPatternsStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | null): Book[] {
@@ -456,7 +466,7 @@ export class MonthlyReadingPatternsChartService implements OnDestroy {
return `${monthNames[parseInt(month) - 1]} ${year}`;
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<any>): string {
const datasetLabel = context.dataset.label;
const value = context.parsed.y;
const dataIndex = context.dataIndex;
@@ -6,6 +6,7 @@ import {ChartConfiguration, ChartData, ChartType} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface PageCountStats {
category: string;
@@ -179,8 +180,16 @@ export class PageCountChartService implements OnDestroy {
return this.processPageCountStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | number | null): Book[] {
@@ -1,11 +1,12 @@
import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData, ChartType} from 'chart.js';
import {ChartConfiguration, ChartData, ChartType, TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface PublicationYearStats {
year: string;
@@ -205,8 +206,16 @@ export class PublicationYearChartService implements OnDestroy {
this.updateChartData(stats);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | number | null): Book[] {
@@ -244,7 +253,7 @@ export class PublicationYearChartService implements OnDestroy {
return yearMatch ? parseInt(yearMatch[1]) : null;
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<any>): string {
const value = context.parsed.y;
return `${value} book${value === 1 ? '' : 's'} published`;
}
@@ -4,7 +4,8 @@ import {catchError, filter, first, map, switchMap, takeUntil} from 'rxjs/operato
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book, ReadStatus} from '../../book/model/book.model';
import {ChartConfiguration, ChartData} from 'chart.js';
import {BookState} from '../../book/model/state/book-state.model';
import {ChartConfiguration, ChartData, TooltipItem} from 'chart.js';
interface CompletionStats {
category: string;
@@ -216,8 +217,16 @@ export class ReadingCompletionChartService implements OnDestroy {
return this.processCompletionStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | number | null): Book[] {
@@ -269,7 +278,7 @@ export class ReadingCompletionChartService implements OnDestroy {
).join(' ');
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<'bar'>): string {
const dataIndex = context.dataIndex;
const stats = this.getLastCalculatedStats();
@@ -1,11 +1,12 @@
import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {ChartConfiguration, ChartData, TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book, ReadStatus} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface VelocityTimelineData {
month: string;
@@ -261,8 +262,16 @@ export class ReadingVelocityTimelineChartService implements OnDestroy {
return this.processVelocityTimelineStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | null): Book[] {
@@ -353,7 +362,7 @@ export class ReadingVelocityTimelineChartService implements OnDestroy {
return `${monthNames[parseInt(month) - 1]} ${year}`;
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<'line'>): string {
const datasetLabel = context.dataset.label;
const value = context.parsed.y;
const dataIndex = context.dataIndex;
@@ -371,7 +380,7 @@ export class ReadingVelocityTimelineChartService implements OnDestroy {
case 'Avg Pages/Day':
return `${value} pages/day | ${monthStats.averagePages} avg pages/book`;
case 'Avg Rating (×2)':
const actualRating = value / 2;
const actualRating = (value ?? 0) / 2;
return `${actualRating.toFixed(1)}/5 avg rating | ${monthStats.booksCompleted} books rated`;
case 'Reading Velocity':
return `${value} books/month | ${monthStats.avgPagesPerDay} pages/day velocity`;
@@ -6,6 +6,7 @@ import {ChartConfiguration, ChartData, ChartType} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface CategoryStats {
category: string;
@@ -192,8 +193,16 @@ export class TopCategoriesChartService implements OnDestroy {
this.updateChartData(stats);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | number | null): Book[] {
@@ -1,11 +1,12 @@
import {inject, Injectable, OnDestroy} from '@angular/core';
import {BehaviorSubject, EMPTY, Observable, Subject} from 'rxjs';
import {map, takeUntil, catchError, filter, first, switchMap} from 'rxjs/operators';
import {ChartConfiguration, ChartData} from 'chart.js';
import {ChartConfiguration, ChartData, TooltipItem} from 'chart.js';
import {LibraryFilterService} from './library-filter.service';
import {BookService} from '../../book/service/book.service';
import {Book} from '../../book/model/book.model';
import {BookState} from '../../book/model/state/book-state.model';
interface SeriesStats {
seriesName: string;
@@ -190,8 +191,16 @@ export class TopSeriesChartService implements OnDestroy {
return this.processTopSeriesStats(filteredBooks);
}
private isValidBookState(state: any): boolean {
return state?.loaded && state?.books && Array.isArray(state.books) && state.books.length > 0;
private isValidBookState(state: unknown): state is BookState {
return (
typeof state === 'object' &&
state !== null &&
'loaded' in state &&
typeof (state as {loaded: boolean}).loaded === 'boolean' &&
'books' in state &&
Array.isArray((state as {books: unknown}).books) &&
(state as {books: Book[]}).books.length > 0
);
}
private filterBooksByLibrary(books: Book[], selectedLibraryId: string | null): Book[] {
@@ -241,7 +250,7 @@ export class TopSeriesChartService implements OnDestroy {
return colors.slice(0, dataLength);
}
private formatTooltipLabel(context: any): string {
private formatTooltipLabel(context: TooltipItem<'bar'>): string {
const dataIndex = context.dataIndex;
const stats = this.getLastCalculatedStats();
@@ -16,7 +16,7 @@ import {ReadStatusChartComponent} from '../component/read-status-chart/read-stat
export interface UserChartConfig {
id: string;
title: string;
component: any;
component: unknown;
enabled: boolean;
sizeClass: string;
order: number;
@@ -99,7 +99,7 @@ export class UserChartConfigService {
const config = JSON.parse(savedConfig);
const charts = JSON.parse(JSON.stringify(this.defaultCharts));
config.forEach((saved: any) => {
config.forEach((saved: Partial<UserChartConfig>) => {
const chart = charts.find((c: UserChartConfig) => c.id === saved.id);
if (chart) {
chart.enabled = saved.enabled;