0 directories, 46 files

util

Home / tinai / util
import { showOverlay, hideOverlay } from './overlay-utils.js';
import { formatVersionHeader, formatChangelogItem } from './settings-utils.js';

/** @typedef {import('../app.js').default} App */

/**
 * Manages the app version check, the version section in the settings form, and the changelog overlay.
 */
class VersionOverlay {

	#app_version_data = null;

	/**
	 * @param {App} app - The main application instance.
	 */
	constructor(app) {
		this.app = app;
	}

	/**
	 * Checks the latest app version against stored version and shows changelog overlay if newer.
	 * Also populates version information in application settings.
	 * @returns {Promise<void>}
	 */
	async init_version_check() {
		try {
			const versionData = await this.app.api.get_version();
			if (!versionData || versionData.error) return;

			this.#app_version_data = versionData;
			this.update_app_version_settings(versionData);

			const latestDate = versionData['version-date'];
			if (!latestDate) return;

			const storedDate = this.app.storage.get_stored_version_date();

			if (!storedDate || String(latestDate) > String(storedDate)) {
				await this.show_version_overlay();
				this.app.storage.set_stored_version_date(latestDate);
			}
		} catch (error) {
			console.error('Failed to check app version:', error);
		}
	}

	/**
	 * Loads the cached version data or fetches it, updating the settings display.
	 * @returns {Promise<void>}
	 */
	async ensure_version_loaded() {
		if (this.#app_version_data) {
			this.update_app_version_settings(this.#app_version_data);
			return;
		}
		try {
			const versionData = await this.app.api.get_version();
			if (versionData && !versionData.error) {
				this.#app_version_data = versionData;
				this.update_app_version_settings(versionData);
			}
		} catch (error) {
			console.error('Failed to load version for settings:', error);
		}
	}

	/**
	 * Populates the version section in the application settings form.
	 * @param {Object} versionData - Latest version object.
	 */
	update_app_version_settings(versionData) {
		if (!versionData) return;
		if (this.app.div_app_version_title) {
			this.app.div_app_version_title.textContent = formatVersionHeader(versionData);
		}
		if (this.app.div_app_version_description) {
			this.app.div_app_version_description.textContent = versionData['version-description'] || '';
		}
	}

	/**
	 * Shows the full version overlay with current and previous version changelogs.
	 * @returns {Promise<void>}
	 */
	async show_version_overlay() {
		if (!this.app.div_version_overlay) return;

		try {
			const response = await this.app.api.get_version(2);
			const versions = response?.versions || (response ? [response] : []);
			this.render_version_overlay_content(versions);
			showOverlay(this.app.div_version_overlay);
		} catch (error) {
			console.error('Failed to load full version information:', error);
		}
	}

	/**
	 * Hides the full version overlay modal.
	 */
	hide_version_overlay() {
		if (this.app.div_version_overlay) {
			hideOverlay(this.app.div_version_overlay);
		}
	}

	/**
	 * Renders current and previous version details into the version overlay container.
	 * @param {Array<Object>} versions - Array of version data objects.
	 */
	render_version_overlay_content(versions) {
		if (!this.app.div_version_content) return;
		if (!Array.isArray(versions) || versions.length === 0) {
			this.app.div_version_content.innerHTML = '<p>No version information available.</p>';
			return;
		}

		const current = versions[0];
		const previous = versions.length > 1 ? versions[1] : null;

		let html = '';

		if (current) {
			html += `<fieldset class="div-version-section">
				<legend>Current Version</legend>
				<p style="font-weight: bold; margin-bottom: 4px;">${formatVersionHeader(current)}</p>
				<p style="margin-bottom: 8px;">${current['version-description'] || ''}</p>`;
			if (Array.isArray(current.changelog) && current.changelog.length > 0) {
				html += `<ul>`;
				current.changelog.forEach(item => {
					html += `<li>${formatChangelogItem(item)}</li>`;
				});
				html += `</ul>`;
			}
			html += `</fieldset>`;
		}

		if (previous) {
			html += `<fieldset class="div-version-section" style="margin-top: 15px;">
				<legend>Previous Version</legend>
				<p style="font-weight: bold; margin-bottom: 4px;">${formatVersionHeader(previous)}</p>
				<p style="margin-bottom: 8px;">${previous['version-description'] || ''}</p>`;
			if (Array.isArray(previous.changelog) && previous.changelog.length > 0) {
				html += `<ul>`;
				previous.changelog.forEach(item => {
					html += `<li>${formatChangelogItem(item)}</li>`;
				});
				html += `</ul>`;
			}
			html += `</fieldset>`;
		}

		this.app.div_version_content.innerHTML = html;
	}
}

export default VersionOverlay;
🌐
version-overlay.js ×
Type: Web, text/plain
4.63 Kilobytes
Last Modified 2026-11-04 09:48:28
⬇ Download File