- ESLint autofixes: - apply the safe automatic ESLint rewrites without taking on manual typing, accessibility, or Angular inject migrations - normalize `==` and `!=` comparisons in templates, convert `Array<T>` to `T[]`, and tighten a small set of `let` declarations to `const` - remove `autofocus` attributes that ESLint can rewrite mechanically - Cleanup scope: - keep this pass limited to files changed by `ng lint --fix` so the remaining 702 manual ESLint findings stay isolated for later review - Validation: - `env YARN_ENABLE_GLOBAL_CACHE=0 YARN_CACHE_FOLDER=../.yarn/cache corepack yarn lint:eslint --fix` reduced the remaining ESLint count from 721 to 702 before stopping on non-fixable issues
98 lines
2.8 KiB
TypeScript
98 lines
2.8 KiB
TypeScript
import {effect, inject, Injectable, signal} from '@angular/core';
|
|
import {DashboardConfig, DEFAULT_DASHBOARD_CONFIG, ScrollerType} from '../models/dashboard-config.model';
|
|
import {UserService} from '../../settings/user-management/user.service';
|
|
import {MagicShelfService} from '../../magic-shelf/service/magic-shelf.service';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class DashboardConfigService {
|
|
private readonly userService = inject(UserService);
|
|
private readonly magicShelfService = inject(MagicShelfService);
|
|
|
|
private readonly _config = signal<DashboardConfig>(this.cloneConfig(DEFAULT_DASHBOARD_CONFIG));
|
|
readonly config = this._config.asReadonly();
|
|
|
|
constructor() {
|
|
effect(() => {
|
|
const user = this.userService.currentUser();
|
|
if (user) {
|
|
const dashboardConfig = user.userSettings?.dashboardConfig as DashboardConfig;
|
|
this._config.set(this.normalizeConfig(dashboardConfig));
|
|
}
|
|
});
|
|
|
|
effect(() => {
|
|
const shelves = this.magicShelfService.shelves();
|
|
const currentConfig = this._config();
|
|
const syncedConfig = this.syncMagicShelfTitles(currentConfig, shelves);
|
|
|
|
if (syncedConfig) {
|
|
this._config.set(syncedConfig);
|
|
const user = this.userService.getCurrentUser();
|
|
if (user) {
|
|
this.userService.updateUserSetting(user.id, 'dashboardConfig', syncedConfig);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
saveConfig(config: DashboardConfig): void {
|
|
const nextConfig = this.normalizeConfig(config);
|
|
this._config.set(nextConfig);
|
|
|
|
const user = this.userService.getCurrentUser();
|
|
if (user) {
|
|
this.userService.updateUserSetting(user.id, 'dashboardConfig', nextConfig);
|
|
}
|
|
}
|
|
|
|
resetToDefault(): void {
|
|
this.saveConfig(DEFAULT_DASHBOARD_CONFIG);
|
|
}
|
|
|
|
private normalizeConfig(config: DashboardConfig | null | undefined): DashboardConfig {
|
|
if (!config?.scrollers?.length) {
|
|
return this.cloneConfig(DEFAULT_DASHBOARD_CONFIG);
|
|
}
|
|
|
|
return this.cloneConfig(config);
|
|
}
|
|
|
|
private cloneConfig(config: DashboardConfig): DashboardConfig {
|
|
return {
|
|
scrollers: config.scrollers.map(scroller => ({...scroller}))
|
|
};
|
|
}
|
|
|
|
private syncMagicShelfTitles(
|
|
config: DashboardConfig,
|
|
shelves: { id?: number | null; name: string }[]
|
|
): DashboardConfig | null {
|
|
let updated = false;
|
|
|
|
const scrollers = config.scrollers.map(scroller => {
|
|
if (scroller.type !== ScrollerType.MAGIC_SHELF || !scroller.magicShelfId) {
|
|
return scroller;
|
|
}
|
|
|
|
const shelf = shelves.find(currentShelf => currentShelf.id === scroller.magicShelfId);
|
|
if (!shelf || scroller.title === shelf.name) {
|
|
return scroller;
|
|
}
|
|
|
|
updated = true;
|
|
return {
|
|
...scroller,
|
|
title: shelf.name
|
|
};
|
|
});
|
|
|
|
if (!updated) {
|
|
return null;
|
|
}
|
|
|
|
return {scrollers};
|
|
}
|
|
}
|