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

@kody/example-habit-log

A small habit log app built from the app starter: indigo theme, mobile layout, sign-in, and saved entries.

FollowFork

Files

items.ts

31 lines · 897 B · TypeScript
import { packageStorage } from 'kody:runtime'

export type Item = { id: string; text: string; createdAt: string }

function keyFor(userId: string) {
	return `items:${userId}`
}

/** Each signed-in person gets their own list in package storage. */
export async function listItems(userId: string): Promise<Array<Item>> {
	const stored = await packageStorage().get(keyFor(userId))
	return Array.isArray(stored) ? (stored as Array<Item>) : []
}

export async function addItem(userId: string, text: string) {
	const item: Item = {
		id: crypto.randomUUID(),
		text,
		createdAt: new Date().toISOString(),
	}
	await packageStorage().set(keyFor(userId), [item, ...(await listItems(userId))])
	return item
}

export async function deleteItem(userId: string, id: string) {
	const items = await listItems(userId)
	await packageStorage().set(
		keyFor(userId),
		items.filter((item) => item.id !== id),
	)
}