feat: personal/work area toggle
Some checks failed
Build & Release APK / check (push) Has been cancelled
Build & Release APK / build (push) Has been cancelled

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:
marcuspaico
2026-09-08 17:05:50 +10:00
parent 26922af997
commit abc380bb84
13 changed files with 295 additions and 30 deletions

View File

@@ -21,6 +21,7 @@ const store = await import("./store");
beforeEach(async () => {
storage.clear();
await store.hydrate();
store.setArea("personal");
});
const find = (id: string) => store.getState().tasks.find((t) => t.id === id)!;
@@ -61,3 +62,66 @@ describe("completeTask", () => {
expect(store.completeTask("nope")).toBeNull();
});
});
describe("area lens", () => {
test("new rows are born into the active area and selectors filter by it", () => {
const home = store.createTask({ title: "home" });
store.setArea("work");
const office = store.createTask({ title: "office" });
const proj = store.createProject({ name: "Q4" });
expect(home.area).toBe("personal");
expect(office.area).toBe("work");
expect(proj.area).toBe("work");
// The store is module state shared across tests, so assert on membership
// and on every visible row's area rather than on exact lists.
const ids = (s: ReturnType<typeof store.getState>) => store.tasksByStatus(s, "next").map((t) => t.id);
let s = store.getState();
expect(ids(s)).toContain(office.id);
expect(ids(s)).not.toContain(home.id);
expect(store.tasksByStatus(s, "next").every((t) => t.area === "work")).toBe(true);
expect(store.activeProjects(s).map((p) => p.id)).toEqual([proj.id]);
store.setArea("personal");
s = store.getState();
expect(ids(s)).toContain(home.id);
expect(ids(s)).not.toContain(office.id);
expect(store.activeProjects(s).some((p) => p.id === proj.id)).toBe(false);
});
test("area persists across hydrate", async () => {
store.setArea("work");
expect(storage.get("area_v1")).toBe("work");
});
test("rows cached without an area count as personal", () => {
// Simulate a pre-migration cache row.
const legacy = store.createTask({ title: "old" });
delete (legacy as { area?: string }).area;
store.setArea("work");
expect(store.todayTasks(store.getState()).overdue).toEqual([]);
store.setArea("personal");
expect(store.tasksByStatus(store.getState(), "next").some((t) => t.id === legacy.id)).toBe(true);
});
test("today, review and contexts respect the lens", () => {
store.createTask({ title: "p", due_date: "2000-01-01", context: "@home" });
store.setArea("work");
store.createTask({ title: "w", due_date: "2000-01-01", context: "@office" });
const s = store.getState();
expect(store.todayTasks(s).overdue.every((t) => t.area === "work")).toBe(true);
expect(store.todayTasks(s).overdue.map((t) => t.title)).toContain("w");
expect(store.reviewBuckets(s).overdue.map((t) => t.title)).toContain("w");
expect(store.reviewBuckets(s).overdue.map((t) => t.title)).not.toContain("p");
expect(store.knownContexts(s)).toEqual(["@office"]);
});
test("completing a repeating work task spawns into work", () => {
store.setArea("work");
const t = store.createTask({ title: "standup", repeat: "daily", due_date: "2000-01-01" });
store.setArea("personal");
store.completeTask(t.id);
const spawned = store.getState().tasks.find((x) => x.title === "standup" && x.status === "next")!;
expect(spawned.area).toBe("work");
});
});

View File

@@ -12,11 +12,17 @@ import { api } from "./api";
import { getConfig } from "./config";
import { daysFromToday, isDeferred, todayIso } from "./dates";
import { nextOccurrence, shiftDays } from "./repeat";
import type { Project, Task } from "./types";
import type { Area, Project, Task } from "./types";
export interface State {
tasks: Task[];
projects: Project[];
/**
* Which half of life is on screen. Every selector filters by it, so the
* toggle acts as a global lens: work mode shows no personal rows and vice
* versa. New tasks and projects are born into the active area.
*/
area: Area;
hydrated: boolean;
syncing: boolean;
pending: number;
@@ -31,10 +37,12 @@ type Op =
const DATA_KEY = "store_v1";
const QUEUE_KEY = "queue_v1";
const AREA_KEY = "area_v1";
let state: State = {
tasks: [],
projects: [],
area: "personal",
hydrated: false,
syncing: false,
pending: 0,
@@ -68,13 +76,39 @@ function persistQueue() {
export async function hydrate() {
if (state.hydrated) return;
const [data, q] = await Promise.all([AsyncStorage.getItem(DATA_KEY), AsyncStorage.getItem(QUEUE_KEY)]);
const [data, q, area] = await Promise.all([
AsyncStorage.getItem(DATA_KEY),
AsyncStorage.getItem(QUEUE_KEY),
AsyncStorage.getItem(AREA_KEY),
]);
if (q) queue = JSON.parse(q);
const parsed = data ? JSON.parse(data) : { tasks: [], projects: [] };
setState({ tasks: parsed.tasks, projects: parsed.projects, hydrated: true });
setState({
tasks: parsed.tasks,
projects: parsed.projects,
area: area === "work" ? "work" : "personal",
hydrated: true,
});
void sync();
}
/** Flip the personal/work lens. Persists so the app reopens where you left it. */
export function setArea(area: Area) {
if (area === state.area) return;
setState({ area });
void AsyncStorage.setItem(AREA_KEY, area);
}
// Rows cached before the area column existed have no `area`; they are
// personal, matching the server's column default.
function areaOf(row: { area?: Area }): Area {
return row.area ?? "personal";
}
function inArea<T extends { area?: Area }>(s: State, rows: T[]): T[] {
return rows.filter((r) => areaOf(r) === s.area);
}
// ---- mutations (instant; network happens later) ----
function now() {
@@ -83,8 +117,8 @@ function now() {
// Fields the API accepts — everything else (timestamps, completed_at) is local
// bookkeeping until the server's authoritative row arrives via pull().
const TASK_FIELDS = ["title", "notes", "status", "project_id", "context", "waiting_for", "due_date", "defer_date", "priority", "estimate_min", "repeat", "sort_order"] as const;
const PROJECT_FIELDS = ["name", "status", "notes", "sort_order"] as const;
const TASK_FIELDS = ["title", "notes", "status", "project_id", "context", "waiting_for", "due_date", "defer_date", "priority", "estimate_min", "repeat", "sort_order", "area"] as const;
const PROJECT_FIELDS = ["name", "status", "notes", "sort_order", "area"] as const;
function pickFields<T>(data: Partial<T>, fields: readonly (keyof T & string)[]): Partial<T> {
const out: Partial<T> = {};
@@ -122,6 +156,7 @@ export function createTask(data: Partial<Task> & { title: string }): Task {
estimate_min: data.estimate_min ?? null,
repeat: data.repeat ?? null,
completed_at: null,
area: data.area ?? state.area,
// New tasks land at the top of their list.
sort_order:
data.sort_order ??
@@ -202,6 +237,7 @@ function spawnNextOccurrence(t: Task): Task {
priority: t.priority,
estimate_min: t.estimate_min,
repeat: t.repeat,
area: areaOf(t),
});
}
@@ -216,6 +252,7 @@ export function createProject(data: Partial<Project> & { name: string }): Projec
status: data.status ?? "active",
notes: data.notes ?? "",
sort_order: data.sort_order ?? 0,
area: data.area ?? state.area,
created_at: now(),
updated_at: now(),
};
@@ -333,7 +370,7 @@ function byManualOrder(a: Task, b: Task): number {
}
export function tasksByStatus(s: State, status: Task["status"]): Task[] {
return s.tasks.filter((t) => t.status === status).sort(byManualOrder);
return inArea(s, s.tasks).filter((t) => t.status === status).sort(byManualOrder);
}
/**
@@ -362,7 +399,7 @@ export function reorderTask(id: string, status: Task["status"], index: number) {
export function todayTasks(s: State): { overdue: Task[]; today: Task[] } {
const now = new Date();
const todayIso = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
const open = s.tasks.filter((t) => t.status !== "done" && t.status !== "trashed");
const open = inArea(s, s.tasks).filter((t) => t.status !== "done" && t.status !== "trashed");
return {
overdue: open.filter((t) => t.due_date && t.due_date < todayIso).sort(byPriorityDue),
today: open
@@ -372,7 +409,7 @@ export function todayTasks(s: State): { overdue: Task[]; today: Task[] } {
}
export function activeProjects(s: State): Project[] {
return s.projects
return inArea(s, s.projects)
.filter((p) => p.status !== "dropped")
.sort((a, b) => a.sort_order - b.sort_order || b.created_at.localeCompare(a.created_at));
}
@@ -384,7 +421,7 @@ export function openTaskCount(s: State, projectId: string): number {
/** Distinct contexts on open tasks — suggestion chips beat retyping "@homr". */
export function knownContexts(s: State): string[] {
const set = new Set<string>();
for (const t of s.tasks) {
for (const t of inArea(s, s.tasks)) {
if (t.context && t.status !== "done" && t.status !== "trashed") set.add(t.context);
}
return [...set].sort();
@@ -410,7 +447,7 @@ export interface ReviewBuckets {
export function reviewBuckets(s: State): ReviewBuckets {
const today = todayIso();
const open = s.tasks.filter((t) => t.status !== "done" && t.status !== "trashed");
const open = inArea(s, s.tasks).filter((t) => t.status !== "done" && t.status !== "trashed");
const overdue = open.filter((t) => t.due_date && t.due_date < today).sort(byPriorityDue);
const overdueIds = new Set(overdue.map((t) => t.id));
const actionable = new Set(["next", "waiting"]);

View File

@@ -27,6 +27,8 @@ const light = {
accent: "#2F6FED",
/** Tinted accent wash for selected chips and active states. */
accentSoft: "#E8F0FE",
/** Accent for the work lens — the one place the app changes colour. */
work: "#0F766E",
done: "#2FA36B",
danger: "#E5484D",
warn: "#D98324",
@@ -45,6 +47,7 @@ const dark = {
muted: "#7A7A85",
accent: "#4C86F0",
accentSoft: "#1B2740",
work: "#2DD4BF",
done: "#3FBF84",
danger: "#F0605F",
warn: "#E0A93E",

View File

@@ -1,6 +1,11 @@
export type TaskStatus = "inbox" | "next" | "waiting" | "scheduled" | "someday" | "done" | "trashed";
export type ProjectStatus = "active" | "someday" | "completed" | "dropped";
export type Repeat = "daily" | "weekdays" | "weekly" | "monthly" | "yearly";
/** Personal vs work — the app-wide toggle shows one area at a time. */
export type Area = "personal" | "work";
export const AREAS: Area[] = ["personal", "work"];
export const AREA_LABEL: Record<Area, string> = { personal: "Personal", work: "Work" };
export interface Task {
id: string;
@@ -17,6 +22,7 @@ export interface Task {
repeat: Repeat | null;
completed_at: string | null;
sort_order: number;
area: Area;
created_at: string;
updated_at: string;
}
@@ -27,6 +33,7 @@ export interface Project {
status: ProjectStatus;
notes: string;
sort_order: number;
area: Area;
created_at: string;
updated_at: string;
open_tasks?: number;