diff --git a/docs/chat.md b/docs/chat.md index 58880d3..28941e6 100644 --- a/docs/chat.md +++ b/docs/chat.md @@ -1322,6 +1322,16 @@ moves between a task and any of its chats (`web/src/work/`). from `GET /api/todos/sheet` already filed: its own filing, else the sheet's default folder (`docs/task-sheet.md`). Deleting a folder moves its tasks up to its project, deleting a project makes them Personal, as with its chats. +- **Adding.** The "Add a task…" line at the top ends in a folder button: any project or + folder (a new one can be typed into the picker), or Personal, whether or not it has open + tasks and so a group of its own. The line remembers the last one chosen on this device + (`totem.work.addFolder`; a folder deleted since reads as Personal). A group's + opens the + same line starting on that group's folder, without changing the remembered one. + `tomorrow` and `p1` in the title show as chips under the line, with × to keep the word + as text (the Tasks page's parser, `web/src/todos/parse.ts`). A typed `#folder` shows on + the button instead, tinted, and wins for that task; choosing on the button after typing + it keeps the word as text (`web/src/work/AddTask.tsx`). A new task's folder group + unfolds, and a toast says so only when the filter hides the task. - **The task card** (`web/src/work/TaskCard.tsx`, `card.ts`), T3 Code's thread card for a task: the check circle and title, then one row per setting with a dropdown (Folder, Due, Priority, Repeat, Snooze), the description, the chats working on it with New chat, Open diff --git a/web/src/work/AddTask.tsx b/web/src/work/AddTask.tsx new file mode 100644 index 0000000..e308b1b --- /dev/null +++ b/web/src/work/AddTask.tsx @@ -0,0 +1,180 @@ +import React, { useLayoutEffect, useMemo, useRef, useState } from 'react' +import { useChat } from '../chat/store' +import { TI } from '../chat/ui' +import { IconCalendar, IconChevronDown, IconFlag, IconPlus, IconX } from '../chat/icons' +import { dueText } from '../todos/format' +import { folderFor, folderLabel, folderNames, PERSONAL } from '../todos/folders' +import { hintKey, parseQuickAdd, type Hint } from '../todos/parse' +import { priorityLabel } from '../todos/types' +import { FolderIcon } from './FolderField' +import { FolderPicker } from './pickers' +import type { WorkTask } from './groups' +import { createTask } from './state' + +// The chats panel's "Add a task…" line. The folder button at its end is where the +// task goes: any chat project or folder, a new one typed into the picker, or +// Personal, whether or not that folder has open tasks (and so a group) to add from. +// A "#folder" typed in the title is shown on the button (tinted) and wins for that +// task, unless the button is used after it, which keeps the word as text; "tomorrow" +// and "p1" show as chips under the line, each with × to keep the word as text, as on +// the Tasks page. + +interface Props { + /** The folder the button starts on: the one last used (the top line) or the group's. */ + folderId: string | null + /** The button was used: the top line remembers it for the next task. */ + onFolder?: (folderId: string | null) => void + placeholder?: string + autoFocus?: boolean + /** The task was added (with where it went). */ + onAdded?: (task: WorkTask) => void + /** Escape, or focus left an empty line: the group's line closes. */ + onDone?: () => void +} + +function chipText(hint: Hint, now: Date) { + if (hint.kind === 'due' && hint.dueDate) return dueText(hint.dueDate, now) + if (hint.kind === 'priority' && hint.priority) return priorityLabel(hint.priority) + return hint.text +} + +export default function AddTask({ folderId: start, onFolder, placeholder = 'Add a task…', autoFocus = false, onAdded, onDone }: Props) { + const projects = useChat((s) => s.projects) + const names = useMemo(() => folderNames(projects), [projects]) + const [text, setText] = useState('') + const [ignore, setIgnore] = useState([]) + const [saving, setSaving] = useState(false) + const [picking, setPicking] = useState(false) + // The button's folder. A #folder typed in the title wins over it, unless the button is used after. + const [chosen, setChosen] = useState(start) + const input = useRef(null) + const button = useRef(null) + const line = useRef(null) + const current = useRef(text) + current.current = text + + const now = new Date() + const parsed = parseQuickAdd(text, { now, folders: names, ignore }) + // A folder deleted since it was chosen reads as Personal. + const base = folderFor(chosen, projects)?.id ?? null + const folderId = parsed.folderId ?? base + const typed = !!parsed.folderId && parsed.folderId !== base + const project = folderFor(folderId, projects) + // A #folder shows on the button rather than as a chip; choosing on the button keeps the word as text. + const chips = parsed.hints.filter((hint) => hint.kind !== 'folder') + // Grows with a long title (field-sizing is Chromium-only; the desktop app is WebKit). CSS caps it at five lines. + // Measured again when the folder button's width changes too. + useLayoutEffect(() => { + const el = input.current + if (!el) return + el.style.height = 'auto' + el.style.height = `${el.scrollHeight}px` + }, [text, folderId, projects]) + + async function add() { + const title = parsed.title.trim() + if (saving) return + if (!title) { if (!text.trim()) onDone?.(); return } + setSaving(true) + const submitted = text + const task = await createTask({ title, dueDate: parsed.dueDate, priority: parsed.priority }, folderId) + setSaving(false) + if (!task) return + // Words typed while the bridge answered are the next task's. + setText(current.current.startsWith(submitted) ? current.current.slice(submitted.length).trimStart() : current.current) + setIgnore([]) + onAdded?.(task) + input.current?.focus() + } + + return ( +
{ + // Only the line's own blank space; the picker's events bubble here through its portal. + if (!line.current?.contains(e.target as Node) || (e.target as HTMLElement).closest('button, textarea')) return + e.preventDefault() + input.current?.focus() + }} + onBlur={(e) => { + // Focus moving to the folder button, its picker or a chip keeps the group's line open. + if (picking || (e.relatedTarget && line.current?.contains(e.relatedTarget as Node))) return + if (!current.current.trim()) onDone?.() + }} + > +
+ + {/* A textarea so a long title wraps in the narrow panel rather than scrolling out of sight. */} +