Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@kentcdodds/pie-night

Pie night sign-ups: hosts start an event, guests add the pies they are bringing and watch the list fill in live. Installable, with optional push notifications.

FollowForkOpen app

Files

settings.ts

34 lines · 1.5 KB · TypeScript
/**
 * Owner settings for this install.
 *
 * Home page redirect: send visitors of the bare home page (/) straight to one
 * event, for example while an old shared link still points at /.
 *
 *   import { setHomeRedirect } from 'kody:@you/pie-night/settings'
 *   export default () => setHomeRedirect({ code: 'smith-family-2026' })   // turn on
 *   export default () => setHomeRedirect({ code: null })                    // turn off
 *
 * The redirect is a temporary 302, so turning it off takes effect right away.
 * Add ?home to the URL to see the real home page while it is on.
 */
import { packageStorage } from 'kody:runtime'
import { HOME_REDIRECT_KEY, slugify, validSlug } from './pies.ts'
import { getEvent } from './store.ts'

export async function setHomeRedirect(params: { code: string | null }) {
	if (params.code == null || params.code === '') {
		await packageStorage().set(HOME_REDIRECT_KEY, '')
		return { ok: true, homeRedirect: null, message: 'The home page shows the Pie Night home again.' }
	}
	const slug = slugify(params.code)
	if (!validSlug(slug) || !(await getEvent(slug))) return { ok: false, homeRedirect: null, message: `No event with the code "${params.code}".` }
	await packageStorage().set(HOME_REDIRECT_KEY, slug)
	return { ok: true, homeRedirect: slug, message: `/ now sends visitors to /e/${slug} (302).` }
}

export async function getHomeRedirect() {
	const raw = await packageStorage().get(HOME_REDIRECT_KEY)
	return { homeRedirect: typeof raw === 'string' && raw ? raw : null }
}

export default setHomeRedirect