All files / src/entities/settings/model settings.service.tsx

100% Statements 32/32
100% Branches 14/14
100% Functions 6/6
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98              14x     14x   14x         9x             2x 1x 1x             2x 2x             3x                                 10x   9x 9x   9x 9x   8x   7x 7x 2x 1x   2x       5x     5x     5x 1x     5x   9x 9x       9x      
import { setActiveProjectId } from '@/entities/project/model/slice';
import { activeProjectRepository } from '@/entities/settings/api';
import { projectRepository } from '@/entities/project/api';
import { loadProjectData } from '@/features/editor';
import type { AppDispatch } from '@/store';
 
// Prevent parallel restore calls
let restorePromise: Promise<string | null> | null = null;
 
// Indicates when the restore process is running
let restoring = false;
 
export const activeProjectService = {
	/**
	 * Returns the current active project ID from storage.
	 */
	async getActiveProjectId() {
		return activeProjectRepository.get();
	},
 
	/**
	 * Sets the active project both in Dexie and Redux.
	 */
	async setActiveProject(dispatch: AppDispatch, projectId: string) {
		if (!projectId) return;
		await activeProjectRepository.set(projectId);
		dispatch(setActiveProjectId(projectId));
	},
 
	/**
	 * Clears the active project.
	 */
	async clearActiveProject(dispatch: AppDispatch) {
		await activeProjectRepository.clear();
		dispatch(setActiveProjectId(null));
	},
 
	/**
	 * True if a restore operation is currently running.
	 */
	isRestoring() {
		return restoring;
	},
 
	/**
	 * Restores a project into the editor:
	 * - projectId from URL has priority
	 * - validates project exists in Dexie
	 * - loads layers, viewport, history
	 * - persists project as active if restored via URL
	 *
	 * Prevents parallel restore calls.
	 */
	async restoreIntoEditor(
		dispatch: AppDispatch,
		projectIdFromUrl?: string,
	): Promise<string | null> {
		// If restore is already in progress, return same promise
		if (restorePromise) return restorePromise;
 
		restorePromise = (async () => {
			restoring = true;
 
			try {
				const projectId = projectIdFromUrl || (await this.getActiveProjectId());
 
				if (!projectId) return null;
 
				const exists = await projectRepository.exists(projectId);
				if (!exists) {
					if (!projectIdFromUrl) {
						await this.clearActiveProject(dispatch);
					}
					return null;
				}
 
				// Set active project in Redux state
				dispatch(setActiveProjectId(projectId));
 
				// Load layers, history, viewport, etc.
				await dispatch(loadProjectData(projectId)).unwrap();
 
				// If restore was triggered by URL — persist this project as active
				if (projectIdFromUrl) {
					await activeProjectRepository.set(projectId);
				}
 
				return projectId;
			} finally {
				restorePromise = null;
				restoring = false;
			}
		})();
 
		return restorePromise;
	},
};