Skip to content

src/settings.ts

30 lines · 1.2 KB · TypeScript
Raw
import { parseInput } from './lib/validate.ts'
import { getSettings, setTimeZone, validTimeZone, DEFAULT_TIME_ZONE } from './lib/store.ts'

/**
 * Read or change this todo store's settings. Currently: `timeZone` (IANA name), used to interpret
 * date-only/wall-clock due dates, "today", overdue checks, and the digest. Default is "UTC" until set.
 * Call once after installing, e.g. `{ timeZone: 'America/New_York' }`. Existing items keep their stored due instants.
 *
 * @param input - `{ timeZone?: string }` — omit to just read the current settings
 * @returns `{ timeZone, configured, default, changed }`
 *
 * @example
 * import settings from 'kody:@charlesmatte/todos/settings'
 *
 * await settings({ timeZone: 'Europe/Paris' })
 */
export default async function todoSettings(input: unknown = {}) {
	const p = parseInput<{ timeZone?: string }>(input, { timeZone: { type: 'string', maxLength: 64 } }, 'todos/settings')
	let changed = false
	if (p.timeZone) {
		const tz = validTimeZone(p.timeZone)
		const cur = await getSettings()
		if (!cur.configured || cur.timeZone !== tz) {
			await setTimeZone(tz)
			changed = true
		}
	}
	const s = await getSettings()
	return { ...s, default: DEFAULT_TIME_ZONE, changed }
}