# Groceries checklist

A shopping list the user ticks off in the store, with a bar that fills as they go.

**Sizes:** small (the bar), medium (the first four items), large (six items and the bar)
**Interaction:** checklist (works with no agent running); a listener keeps the bar in step
**Gotcha:** only the paired user's phone sees and ticks it. For a family list, the user relays changes to you.

## Widget

```json
{
  "slug": "groceries",
  "title": "Groceries",
  "symbol": "cart.fill",
  "accent": "#30D158",
  "blocks": [
    { "type": "checklist", "title": "This week", "items": [
      { "title": "Chicken breast", "done": true },
      { "title": "Broccoli x2", "done": true },
      { "title": "Brown rice", "done": false },
      { "title": "Greek yoghurt", "done": false },
      { "title": "Eggs x12", "done": false },
      { "title": "Olive oil", "done": false }
    ] },
    { "type": "progress", "label": "In the cart", "value": 0.33 }
  ],
  "sizes": { "small": [1], "medium": [0], "large": [0, 1] },
  "lock": { "circular": 1, "inline": "4 left to buy" }
}
```

A checklist holds 8 items but shows 6 on large and 4 on medium, so keep the list short or split a long one into two widgets (for example by aisle). When you write a fresh list, put the items in the order the user shops.

## Handler: keep the bar in step

```javascript
// groceries-handler.mjs, run by listen.mjs
import { readFileSync } from "node:fs";

const API = "https://ujtuxanqlzmhnrzijfoo.supabase.co/functions/v1/agent-api";
const headers = { authorization: `Bearer ${process.env.DROPLET_TOKEN}`, "content-type": "application/json", "x-region": "us-east-1" };
const droplet = async (action, body = {}) => (await fetch(API, { method: "POST", headers, body: JSON.stringify({ action, ...body }) })).json();

const event = JSON.parse(readFileSync(0, "utf8"));
if (event.kind !== "check") process.exit(0);

const { widgets } = await droplet("list_widgets");
const w = widgets.find((x) => x.slug === "groceries");
const list = w.blocks[0];
const done = list.items.filter((i) => i.done).length;
const left = list.items.length - done;

console.log(JSON.stringify({
  blocks: [list, { type: "progress", label: left ? "In the cart" : "All done", value: done / list.items.length }],
  lock: { circular: 1, inline: left ? `${left} left to buy` : "All done" },
}));
```

## Adding an item

Read the widget, add to the end (ticks are stored by position, so never reorder), send it back:

```javascript
const { widgets } = await droplet_list_widgets();
const w = widgets.find((x) => x.slug === "groceries");
const list = w.blocks[0];
if (list.items.length < 8) list.items.push({ title: "Honey", done: false });
await droplet_set_widget({ slug: "groceries", title: "Groceries", blocks: [list, w.blocks[1]] });
```
