0 directories, 46 files

util

Home / tinai / util
/**
 * Mobile back-action support: the browser/gesture back closes the topmost
 * dialog, or in single-column mode steps back toward the conversations list,
 * before a "Press back again to close." confirmation is shown; a second back
 * press within a short window leaves the page.
 */

import { customConfirm } from './confirm-dialog.js';
import { step_panels_back_single_column } from './swipe-navigation.js';

/** Milliseconds during which a second back press is treated as "close". */
const EXIT_PROMPT_WINDOW_MS = 2500;

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

let app = null;
let exit_prompted_at = 0;
let exit_prompt_visible = false;

/**
 * Initializes history-based back navigation: pushes a sentinel entry so the
 * first back action reaches this handler instead of leaving the app.
 * @param {App} app_instance - The main application instance.
 */
export function init_history_navigation(app_instance) {
	app = app_instance;
	history.pushState({ tinai_sentinel: true }, '', location.href);
	window.addEventListener('popstate', on_popstate);
}

/**
 * Handles a back navigation: closes the topmost dialog, or in single-column
 * mode steps back one panel toward panel 1, or shows the exit confirmation;
 * a second press leaves the app.
 */
function on_popstate() {
	if (app.dismiss_topmost_overlay()) {
		arm_sentinel();
		return;
	}
	if (step_panels_back_single_column()) {
		arm_sentinel();
		return;
	}
	const now = Date.now();
	if (exit_prompt_visible || now - exit_prompted_at < EXIT_PROMPT_WINDOW_MS) {
		exit_prompt_visible = false;
		history.back();
		return;
	}
	exit_prompted_at = now;
	exit_prompt_visible = true;
	arm_sentinel();
	void customConfirm('Close TinAI?', 'Press back again to close.').then(() => {
		exit_prompt_visible = false;
	});
}

/**
 * Re-arms the intercepted history entry so the next back action fires
 * popstate again instead of leaving the app.
 */
function arm_sentinel() {
	history.pushState({ tinai_sentinel: true }, '', location.href);
}
🌐
history-navigation.js ×
Type: Web, text/x-java
1.95 Kilobytes
Last Modified 2026-11-04 09:48:28
⬇ Download File