Skip to content
← Public packages

@kody/mailroom

Offline-first PWA for your Kody mailbox: sent and received threads, copy-prompt for agents, no send.

src/deep-link.ts

30 lines · 1.1 KB · TypeScript
export const DEFAULT_HOSTED_APP_URL = 'https://kody.kody.run/packages/mailroom'

export function hostedAppUrl(hostedUrl?: string | null): string {
	const raw = (hostedUrl ?? DEFAULT_HOSTED_APP_URL).trim()
	return raw.replace(/\/+$/, '') || DEFAULT_HOSTED_APP_URL
}

export function parseDeepLink(input: { search?: string; hash?: string }): {
	threadId: string | null
	messageId: string | null
} {
	const decodeValue = (value: string | null | undefined): string | null => {
		if (!value) return null
		try {
			const next = decodeURIComponent(value).trim()
			return next || null
		} catch {
			return String(value).trim() || null
		}
	}
	const hash = String(input.hash ?? '')
	const threadFromHash = hash.match(/^#thread\/([^/?#]+)/i)
	const mailFromHash = hash.match(/^#mail\/([^/?#]+)/i)
	const search = String(input.search ?? '')
	const params = new URLSearchParams(search.startsWith('?') ? search.slice(1) : search)
	return {
		threadId: decodeValue(threadFromHash?.[1]) || decodeValue(params.get('thread')),
		messageId: decodeValue(mailFromHash?.[1]) || decodeValue(params.get('mail')),
	}
}