feat: personal/work area toggle
Global lens in the home header: every list, Today, Projects and Review show only the active area; new rows are born into it. Tasks and projects gain an area column (personal default); the task form's Area chip re-files across areas. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SW5iVXkZ2JBHgyweMCRymY
This commit is contained in:
@@ -11,12 +11,13 @@ await sql.unsafe(await Bun.file(new URL("schema.sql", import.meta.url)).text());
|
||||
|
||||
const TASK_COLS = `id, title, notes, status, project_id, context, waiting_for,
|
||||
due_date::text as due_date, defer_date::text as defer_date,
|
||||
priority, estimate_min, repeat, completed_at, sort_order, created_at, updated_at`;
|
||||
const PROJECT_COLS = `id, name, status, notes, sort_order, created_at, updated_at`;
|
||||
priority, estimate_min, repeat, completed_at, sort_order, area, created_at, updated_at`;
|
||||
const PROJECT_COLS = `id, name, status, notes, sort_order, area, created_at, updated_at`;
|
||||
|
||||
const TASK_STATUSES = ["inbox", "next", "waiting", "scheduled", "someday", "done", "trashed"];
|
||||
const REPEATS = ["daily", "weekdays", "weekly", "monthly", "yearly"];
|
||||
const PROJECT_STATUSES = ["active", "someday", "completed", "dropped"];
|
||||
const AREAS = ["personal", "work"];
|
||||
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
||||
|
||||
const CORS = {
|
||||
@@ -33,8 +34,8 @@ function err(message: string, status: number) {
|
||||
}
|
||||
|
||||
// Fields a client may set. completed_at is derived from status server-side.
|
||||
const TASK_FIELDS = ["title", "notes", "status", "project_id", "context", "waiting_for", "due_date", "defer_date", "priority", "estimate_min", "repeat", "sort_order"];
|
||||
const PROJECT_FIELDS = ["name", "status", "notes", "sort_order"];
|
||||
const TASK_FIELDS = ["title", "notes", "status", "project_id", "context", "waiting_for", "due_date", "defer_date", "priority", "estimate_min", "repeat", "sort_order", "area"];
|
||||
const PROJECT_FIELDS = ["name", "status", "notes", "sort_order", "area"];
|
||||
|
||||
// Only these may be cleared to NULL; notes/title are NOT NULL and keep "".
|
||||
const NULLABLE = new Set(["project_id", "context", "waiting_for", "due_date", "defer_date", "estimate_min", "repeat"]);
|
||||
@@ -62,12 +63,15 @@ async function handle(req: Request): Promise<Response> {
|
||||
if (path === "/v1/tasks" && method === "GET") {
|
||||
const status = url.searchParams.get("status");
|
||||
const projectId = url.searchParams.get("project_id");
|
||||
const area = url.searchParams.get("area");
|
||||
if (status && !TASK_STATUSES.includes(status)) return err("bad status", 400);
|
||||
if (area && !AREAS.includes(area)) return err("bad area", 400);
|
||||
const where: string[] = [];
|
||||
const params: unknown[] = [];
|
||||
if (status) { params.push(status); where.push(`status = $${params.length}`); }
|
||||
else where.push(`status != 'trashed'`);
|
||||
if (projectId) { params.push(projectId); where.push(`project_id = $${params.length}`); }
|
||||
if (area) { params.push(area); where.push(`area = $${params.length}`); }
|
||||
const rows = await sql.unsafe(
|
||||
`select ${TASK_COLS} from tasks where ${where.join(" and ")}
|
||||
order by sort_order, created_at desc`, params);
|
||||
@@ -79,16 +83,17 @@ async function handle(req: Request): Promise<Response> {
|
||||
if (typeof t.title !== "string" || !t.title.trim()) return err("title required", 400);
|
||||
if (t.status && !TASK_STATUSES.includes(t.status as string)) return err("bad status", 400);
|
||||
if (t.repeat && !REPEATS.includes(t.repeat as string)) return err("bad repeat", 400);
|
||||
if (t.area && !AREAS.includes(t.area as string)) return err("bad area", 400);
|
||||
const id = typeof body.id === "string" && UUID_RE.test(body.id) ? body.id : null;
|
||||
// Client-supplied id + on-conflict no-op make offline-queue retries idempotent.
|
||||
const rows = await sql.unsafe(
|
||||
`insert into tasks (id, title, notes, status, project_id, context, waiting_for, due_date, defer_date, priority, estimate_min, repeat, sort_order)
|
||||
values (coalesce($1::uuid, gen_random_uuid()), $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
||||
`insert into tasks (id, title, notes, status, project_id, context, waiting_for, due_date, defer_date, priority, estimate_min, repeat, sort_order, area)
|
||||
values (coalesce($1::uuid, gen_random_uuid()), $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
||||
on conflict (id) do nothing
|
||||
returning ${TASK_COLS}`,
|
||||
[id, t.title, t.notes ?? "", t.status ?? "next", t.project_id ?? null, t.context ?? null,
|
||||
t.waiting_for ?? null, t.due_date ?? null, t.defer_date ?? null, t.priority ?? 4,
|
||||
t.estimate_min ?? null, t.repeat ?? null, t.sort_order ?? 0]);
|
||||
t.estimate_min ?? null, t.repeat ?? null, t.sort_order ?? 0, t.area ?? "personal"]);
|
||||
if (rows.length) return json(rows[0], 201);
|
||||
const existing = await sql.unsafe(`select ${TASK_COLS} from tasks where id = $1`, [id]);
|
||||
return json(existing[0], 200);
|
||||
@@ -104,6 +109,7 @@ async function handle(req: Request): Promise<Response> {
|
||||
const patch = method === "DELETE" ? { status: "trashed" } : pick(body, TASK_FIELDS);
|
||||
if (patch.status && !TASK_STATUSES.includes(patch.status as string)) return err("bad status", 400);
|
||||
if (patch.repeat && !REPEATS.includes(patch.repeat as string)) return err("bad repeat", 400);
|
||||
if (patch.area && !AREAS.includes(patch.area as string)) return err("bad area", 400);
|
||||
const existing = await sql.unsafe(`select ${TASK_COLS} from tasks where id = $1`, [id]);
|
||||
if (!existing.length) return err("not found", 404);
|
||||
const cur = { ...existing[0], ...patch };
|
||||
@@ -111,10 +117,10 @@ async function handle(req: Request): Promise<Response> {
|
||||
const rows = await sql.unsafe(
|
||||
`update tasks set title=$2, notes=$3, status=$4, project_id=$5, context=$6, waiting_for=$7,
|
||||
due_date=$8, defer_date=$9, priority=$10, estimate_min=$11, repeat=$12, completed_at=$13,
|
||||
sort_order=$14, updated_at=now()
|
||||
sort_order=$14, area=$15, updated_at=now()
|
||||
where id=$1 returning ${TASK_COLS}`,
|
||||
[id, cur.title, cur.notes, cur.status, cur.project_id, cur.context, cur.waiting_for,
|
||||
cur.due_date, cur.defer_date, cur.priority, cur.estimate_min, cur.repeat, completedAt, cur.sort_order]);
|
||||
cur.due_date, cur.defer_date, cur.priority, cur.estimate_min, cur.repeat, completedAt, cur.sort_order, cur.area]);
|
||||
return json(rows[0]);
|
||||
}
|
||||
|
||||
@@ -133,13 +139,14 @@ async function handle(req: Request): Promise<Response> {
|
||||
const p = pick(body, PROJECT_FIELDS);
|
||||
if (typeof p.name !== "string" || !p.name.trim()) return err("name required", 400);
|
||||
if (p.status && !PROJECT_STATUSES.includes(p.status as string)) return err("bad status", 400);
|
||||
if (p.area && !AREAS.includes(p.area as string)) return err("bad area", 400);
|
||||
const id = typeof body.id === "string" && UUID_RE.test(body.id) ? body.id : null;
|
||||
const rows = await sql.unsafe(
|
||||
`insert into projects (id, name, status, notes, sort_order)
|
||||
values (coalesce($1::uuid, gen_random_uuid()), $2, $3, $4, $5)
|
||||
`insert into projects (id, name, status, notes, sort_order, area)
|
||||
values (coalesce($1::uuid, gen_random_uuid()), $2, $3, $4, $5, $6)
|
||||
on conflict (id) do nothing
|
||||
returning ${PROJECT_COLS}`,
|
||||
[id, p.name, p.status ?? "active", p.notes ?? "", p.sort_order ?? 0]);
|
||||
[id, p.name, p.status ?? "active", p.notes ?? "", p.sort_order ?? 0, p.area ?? "personal"]);
|
||||
if (rows.length) return json(rows[0], 201);
|
||||
const existing = await sql.unsafe(`select ${PROJECT_COLS} from projects where id = $1`, [id]);
|
||||
return json(existing[0], 200);
|
||||
@@ -150,13 +157,14 @@ async function handle(req: Request): Promise<Response> {
|
||||
const id = m[1];
|
||||
const patch = method === "DELETE" ? { status: "dropped" } : pick(body, PROJECT_FIELDS);
|
||||
if (patch.status && !PROJECT_STATUSES.includes(patch.status as string)) return err("bad status", 400);
|
||||
if (patch.area && !AREAS.includes(patch.area as string)) return err("bad area", 400);
|
||||
const existing = await sql.unsafe(`select ${PROJECT_COLS} from projects where id = $1`, [id]);
|
||||
if (!existing.length) return err("not found", 404);
|
||||
const cur = { ...existing[0], ...patch };
|
||||
const rows = await sql.unsafe(
|
||||
`update projects set name=$2, status=$3, notes=$4, sort_order=$5, updated_at=now()
|
||||
`update projects set name=$2, status=$3, notes=$4, sort_order=$5, area=$6, updated_at=now()
|
||||
where id=$1 returning ${PROJECT_COLS}`,
|
||||
[id, cur.name, cur.status, cur.notes, cur.sort_order]);
|
||||
[id, cur.name, cur.status, cur.notes, cur.sort_order, cur.area]);
|
||||
return json(rows[0]);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,3 +38,10 @@ create index if not exists tasks_project_idx on tasks (project_id);
|
||||
-- Columns added after the initial deploy (create-if-not-exists won't alter).
|
||||
alter table tasks add column if not exists repeat text
|
||||
check (repeat in ('daily', 'weekdays', 'weekly', 'monthly', 'yearly'));
|
||||
|
||||
-- Personal vs work: the app's top-level toggle filters every list by area.
|
||||
alter table tasks add column if not exists area text not null default 'personal'
|
||||
check (area in ('personal', 'work'));
|
||||
alter table projects add column if not exists area text not null default 'personal'
|
||||
check (area in ('personal', 'work'));
|
||||
create index if not exists tasks_area_idx on tasks (area);
|
||||
|
||||
Reference in New Issue
Block a user