0 directories, 46 files

util

Home / tinai / util
/**
 * Custom confirmation and alert modal dialogs, built by the openDialog utility.
 */

import { openDialog } from './dialog.js';

/**
 * Displays an asynchronous confirmation modal.
 * @param {string} title - Dialog title.
 * @param {string} text - Dialog description / message.
 * @param {string} [yes_caption='Yes'] - Confirm button text.
 * @param {string} [no_caption='No'] - Cancel button text.
 * @param {string} [note=''] - Optional smaller-text note below the message.
 * @returns {Promise<boolean>} Resolves to true if confirmed, false otherwise.
 */
export function customConfirm(title, text, yes_caption = 'Yes', no_caption = 'No', note = '') {
	return new Promise((resolve) => {
		const content = document.createElement('div');
		const text_el = document.createElement('p');
		text_el.style.whiteSpace = 'pre-line';
		text_el.textContent = text || '';
		content.appendChild(text_el);
		if (note) {
			const note_el = document.createElement('p');
			note_el.className = 'confirm-note';
			note_el.textContent = note;
			content.appendChild(note_el);
		}

		let result = false;
		openDialog({
			title,
			content,
			buttons: [
				{ caption: yes_caption, onclick: (close) => { result = true; close(); } },
				{ caption: no_caption, onclick: (close) => close() }
			],
			// Escape dismisses as "No", like the original handler
			on_escape: () => {
				result = false;
				return true;
			},
			on_close: () => resolve(result)
		});
	});
}

/**
 * Builds the delete-dialog note about shared copies: set when any of the
 * items has a shared copy, since deleting them also deletes those copies
 * and their public links.
 * @param {Object} callbacks - App callbacks; provides count_shared_copies.
 * @param {string[]} guids - The conversation guids about to be deleted.
 * @param {string} label - Singular noun for the items ('conversation', 'scratchpad', 'tool').
 * @returns {Promise<string>} The note text, or '' when nothing is shared.
 */
export async function shared_copies_note(callbacks, guids, label) {
	if (!callbacks?.count_shared_copies) return '';
	const shared = await callbacks.count_shared_copies(guids);
	if (shared === 0) return '';
	if (guids.length === 1) {
		return `This ${label} has a shared copy. Deleting it also deletes the shared copy, and the share link stops working.`;
	}
	const verb = shared === 1 ? 'has' : 'have';
	const pronoun = shared === 1 ? 'it' : 'them';
	return `${shared} of the selected ${label}s ${verb} a shared copy. Deleting also removes ${pronoun}, and the share links stop working.`;
}

/**
 * Displays an asynchronous alert modal dialog.
 * @param {string} title - Dialog title.
 * @param {string} text - Dialog message.
 * @param {string} [ok_caption='OK'] - Dismiss button text.
 * @returns {Promise<void>} Resolves when dismissed.
 */
export function customAlert(title, text, ok_caption = 'OK') {
	return new Promise((resolve) => {
		const content = document.createElement('div');
		const text_el = document.createElement('p');
		text_el.style.whiteSpace = 'pre-line';
		text_el.textContent = text || '';
		content.appendChild(text_el);

		openDialog({
			title,
			content,
			buttons: [{ caption: ok_caption, onclick: (close) => close() }],
			on_close: () => resolve()
		});
	});
}
🌐
confirm-dialog.js ×
Type: Web, text/x-java
3.17 Kilobytes
Last Modified 2026-11-04 09:48:28
⬇ Download File