Skip to content
← Public packages

@kentcdodds/quiet-hours

Quiet hours for Kent's bots: 10 PM to 8 AM MT (DST-aware) clock, shared overnight queue, override, and the 8 AM digest of finished work and open decisions.

FollowFork

Files

status-core.ts

40 lines · 1.5 KB · TypeScript
import { clockWindow, QUIET_TIME_ZONE } from './clock.ts'
import { EXCLUDED_BOTS, type QuietOverride, type QuietStatus } from './types.ts'

export function isExcludedBot(bot: unknown): boolean {
	if (typeof bot !== 'string') return false
	const name = bot.trim().toLowerCase()
	return (EXCLUDED_BOTS as ReadonlyArray<string>).includes(name)
}

export function parseNow(value: unknown): Date {
	if (value instanceof Date && Number.isFinite(value.getTime())) return value
	if (typeof value === 'string' || typeof value === 'number') {
		const parsed = new Date(value)
		if (Number.isFinite(parsed.getTime())) return parsed
		throw new Error('now must be an ISO timestamp')
	}
	return new Date()
}

/** Active override at `now`, or null when absent/expired/malformed. */
export function activeOverride(override: QuietOverride | null, now: Date): QuietOverride | null {
	if (!override || (override.mode !== 'on' && override.mode !== 'off')) return null
	const expires = Date.parse(override.expiresAt)
	if (!Number.isFinite(expires) || expires <= now.getTime()) return null
	return override
}

/** Pure status: override wins while active, otherwise the clock decides. */
export function computeStatus(
	now: Date,
	override: QuietOverride | null,
	timeZone = QUIET_TIME_ZONE,
): QuietStatus {
	const active = activeOverride(override, now)
	if (active) {
		return { quiet: active.mode === 'on', until: active.expiresAt, override: active }
	}
	const window = clockWindow(now, timeZone)
	return { quiet: window.quiet, until: window.until, override: null }
}