restore()

in src/lib/state/checkpoints.svelte.ts [75:107]


	restore(checkpoint: Checkpoint) {
		const cloned = snapshot(checkpoint);
		const modified = {
			...cloned,
			conversations: cloned.conversations.map(c => ({
				...c,
				projectId: cloned.projectId,
			})),
		};

		const project = projects.all.find(p => p.id == modified.projectId);
		if (!project) return;

		projects.activeId = modified.projectId;

		// conversations.deleteAllFrom(cloned.projectId);
		const prev = conversations.for(modified.projectId);
		modified.conversations.forEach((c, i) => {
			const prevC = prev[i];
			if (prevC) return prevC.update({ ...c });
			conversations.create({
				...c,
				projectId: modified.projectId,
			});
		});

		if (modified.conversations.length < prev.length) {
			prev.forEach((p, i) => {
				if (i < modified.conversations.length) return;
				conversations.delete(p.data);
			});
		}
	}