GeezerVison is showing github.com — feat(web): add metadata-led task navigation and relink controls (#43) · tomcorke/t3code@b4d6358
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/tasks/TaskDocuments.test.ts +59Lines changed: 59 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -253,6 +253,65 @@ describe("TaskDocuments", () => { | 253 | 253 | }).pipe(Effect.scoped, Effect.provide(layer)), | 254 | 254 | ); | 255 | 255 |
| | 256 | + it.effect("reconciles a changed file ID only when explicitly requested", () => | | 257 | + Effect.gen(function* () { | | 258 | + const { fs, path, directory, target, service } = yield* setup; | | 259 | + const original = yield* service.register({ path: target }); | | 260 | + const duplicateId = "b8cf0bb8-531f-4d13-9aa3-3219ee85e623"; | | 261 | + const replacementId = "c9df1cc9-642f-4e24-8bb4-432a0ff96f34"; | | 262 | + const duplicate = path.join(directory, "other.md"); | | 263 | + const replacement = path.join(directory, "replacement.md"); | | 264 | + yield* fs.writeFileString(duplicate, fixture.replace(fileId, duplicateId)); | | 265 | + yield* service.register({ path: duplicate }); | | 266 | + yield* fs.writeFileString(replacement, fixture.replace(fileId, replacementId)); | | 267 | + | | 268 | + expect( | | 269 | + yield* service.relink({ ...original.summary, path: replacement }).pipe(Effect.flip), | | 270 | + ).toMatchObject({ reason: "identity-changed" }); | | 271 | + yield* fs.writeFileString(replacement, fixture.replace(fileId, duplicateId)); | | 272 | + expect( | | 273 | + yield* service | | 274 | + .relink({ ...original.summary, path: replacement, reconcileIdentity: true }) | | 275 | + .pipe(Effect.flip), | | 276 | + ).toMatchObject({ reason: "duplicate-id" }); | | 277 | + | | 278 | + yield* fs.writeFileString(replacement, fixture.replace(fileId, replacementId)); | | 279 | + const relinked = yield* service.relink({ | | 280 | + ...original.summary, | | 281 | + path: replacement, | | 282 | + reconcileIdentity: true, | | 283 | + }); | | 284 | + expect(relinked.summary).toMatchObject({ | | 285 | + taskDocumentId: original.summary.taskDocumentId, | | 286 | + canonicalPath: replacement, | | 287 | + locatorVersion: original.summary.locatorVersion + 1, | | 288 | + observedFileId: replacementId, | | 289 | + availability: "available", | | 290 | + }); | | 291 | + }).pipe(Effect.scoped, Effect.provide(layer)), | | 292 | + ); | | 293 | + | | 294 | + it.effect("reads task metadata from bare-CR frontmatter closed with ...", () => | | 295 | + Effect.gen(function* () { | | 296 | + const { fs, path, directory, service } = yield* setup; | | 297 | + const id = "d0ae6d1e-5375-4e9a-817d-721cb4fdb91d"; | | 298 | + const target = path.join(directory, "bare-cr.md"); | | 299 | + yield* fs.writeFileString( | | 300 | + target, | | 301 | + `---\rid: ${id}\rtitle: "CR task"\rstate: "Ready"\rpinned: true\r...\r# Body\r`, | | 302 | + ); | | 303 | + | | 304 | + const opened = yield* service.register({ path: target }); | | 305 | + expect(opened.summary).toMatchObject({ | | 306 | + observedFileId: id, | | 307 | + title: "CR task", | | 308 | + state: "Ready", | | 309 | + pinned: true, | | 310 | + availability: "available", | | 311 | + }); | | 312 | + }).pipe(Effect.scoped, Effect.provide(layer)), | | 313 | + ); | | 314 | + | 256 | 315 | it.effect( | 257 | 316 | "relinks a missing file without recreating its old locator, and unregisters without deletion", | 258 | 317 | () => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/tasks/TaskDocuments.ts +9-4Lines changed: 9 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -94,8 +94,13 @@ const decodeRecovery = Schema.decodeUnknownEffect(Schema.fromJsonString(TaskDocu | 94 | 94 | const encodeRecovery = Schema.encodeEffect(Schema.fromJsonString(TaskDocumentRecovery)); | 95 | 95 |
| 96 | 96 | function metadata(contents: string) { | 97 | | - const frontmatter = /^\uFEFF?---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/.exec(contents)?.[1]; | 98 | | - const document = parseDocument(frontmatter ?? "", { uniqueKeys: true }); | | 97 | + const frontmatter = | | 98 | + /^\uFEFF?---[ \t]*(?:\r\n|\r|\n)([\s\S]*?)(?:\r\n|\r|\n)(?:---|\.\.\.)[ \t]*(?:\r\n|\r|\n|$)/.exec( | | 99 | + contents, | | 100 | + )?.[1]; | | 101 | + const document = parseDocument((frontmatter ?? "").replace(/\r(?!\n)/g, "\n"), { | | 102 | + uniqueKeys: true, | | 103 | + }); | 99 | 104 | const get = (key: string): unknown => | 100 | 105 | document.errors.length === 0 ? document.get(key) : undefined; | 101 | 106 | const id = decodeUuid(get("id")); | @@ -563,10 +568,10 @@ const make = Effect.gen(function* () { | 563 | 568 | return yield* new TaskDocumentError({ reason: "unsupported" }); | 564 | 569 | const file = yield* read(input.path); | 565 | 570 | yield* unique(file, row.taskDocumentId); | 566 | | - if (file.metadata.fileId !== row.observedFileId) | | 571 | + if (file.metadata.fileId !== row.observedFileId && input.reconcileIdentity !== true) | 567 | 572 | return yield* new TaskDocumentError({ reason: "identity-changed" }); | 568 | 573 | yield* mapIo( | 569 | | - sql`UPDATE task_documents SET canonical_path = ${file.canonicalPath}, locator_version = locator_version + 1 WHERE task_document_id = ${row.taskDocumentId}`, | | 574 | + sql`UPDATE task_documents SET canonical_path = ${file.canonicalPath}, file_id = ${file.metadata.fileId}, locator_version = locator_version + 1 WHERE task_document_id = ${row.taskDocumentId}`, | 570 | 575 | ); | 571 | 576 | yield* notify(row.taskDocumentId); | 572 | 577 | yield* syncWatches(); | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/tasks/TaskDocumentPane.tsx +91-8Lines changed: 91 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,7 +12,7 @@ import { | 12 | 12 | } from "@t3tools/contracts"; | 13 | 13 | import { maskMarkdownFrontmatter } from "@t3tools/shared/markdownFrontmatter"; | 14 | 14 | import { Link, useNavigate } from "@tanstack/react-router"; | 15 | | -import { CodeIcon, EyeIcon, MaximizeIcon, PlusIcon, SaveIcon } from "lucide-react"; | | 15 | +import { CodeIcon, EyeIcon, MaximizeIcon, PencilIcon, PlusIcon, SaveIcon } from "lucide-react"; | 16 | 16 | import { useLayoutEffect, useRef, useState, type ReactNode } from "react"; | 17 | 17 |
| 18 | 18 | import ChatMarkdown from "~/components/ChatMarkdown"; | @@ -37,6 +37,8 @@ import { useProjects } from "~/state/entities"; | 37 | 37 | import { readTaskUpdateInstructionsPath, taskDocumentEnvironment } from "~/state/taskDocuments"; | 38 | 38 | import { useAtomCommand } from "~/state/use-atom-command"; | 39 | 39 |
| | 40 | +import { TaskPropertiesDialog } from "./TaskPropertiesDialog"; | | 41 | +import { TaskRelinkDialog } from "./TaskRelinkDialog"; | 40 | 42 | import { type TaskSession, useTaskCatalogue, useTaskDocument } from "./useTaskDocuments"; | 41 | 43 |
| 42 | 44 | const splitDocumentPath = (path: string) => { | @@ -273,14 +275,18 @@ export default function TaskDocumentPane(props: { | 273 | 275 | environmentId, | 274 | 276 | taskDocumentId, | 275 | 277 | ); | 276 | | - const { tasks } = useTaskCatalogue(environmentId); | | 278 | + const { tasks, canRelink } = useTaskCatalogue(environmentId); | 277 | 279 | const navigate = useNavigate(); | 278 | 280 | const projects = useProjects(); | 279 | 281 | const { openScratchProject } = useScratchProject(); | 280 | 282 | const handleNewThread = useNewThreadHandler(); | 281 | 283 | const [showDisk, setShowDisk] = useState(false); | | 284 | + const [showProperties, setShowProperties] = useState(false); | | 285 | + const [showRelink, setShowRelink] = useState(false); | 282 | 286 | const { base, draft, disk, recovery, error, saving, view } = state; | 283 | 287 | const summary = base?.summary ?? null; | | 288 | + const catalogueTask = tasks.find((candidate) => candidate.taskDocumentId === taskDocumentId); | | 289 | + const relinkTask = summary ?? catalogueTask; | 284 | 290 | const { directory, name } = splitDocumentPath(summary?.canonicalPath ?? ""); | 285 | 291 | const dirty = base !== null && draft !== base.contents; | 286 | 292 |
| @@ -328,10 +334,42 @@ export default function TaskDocumentPane(props: { | 328 | 334 | ); | 329 | 335 | } | 330 | 336 | if (base === null) { | | 337 | + const canRelinkMissing = | | 338 | + relinkTask != null && | | 339 | + (relinkTask.availability === "missing" || relinkTask.availability === "identity-changed"); | 331 | 340 | return error ? ( | 332 | | - <p role="alert" className="m-auto p-6 text-center text-muted-foreground text-sm"> | 333 | | - {error} | 334 | | - </p> | | 341 | + <section className="m-auto flex max-w-xl flex-col items-center gap-3 p-6 text-center"> | | 342 | + <h2 className="font-medium text-base">{relinkTask?.title ?? "Task file unavailable"}</h2> | | 343 | + <p role="alert" className="text-muted-foreground text-sm"> | | 344 | + {relinkTask?.availability === "missing" | | 345 | + ? "This task file is missing from its saved path." | | 346 | + : error} | | 347 | + </p> | | 348 | + {relinkTask ? ( | | 349 | + <code className="max-w-full break-all text-muted-foreground text-xs"> | | 350 | + {relinkTask.canonicalPath} | | 351 | + </code> | | 352 | + ) : null} | | 353 | + {canRelinkMissing ? ( | | 354 | + canRelink ? ( | | 355 | + <Button size="sm" variant="outline" onClick={() => setShowRelink(true)}> | | 356 | + Relink task file | | 357 | + </Button> | | 358 | + ) : ( | | 359 | + <p className="text-muted-foreground text-xs"> | | 360 | + You do not have permission to relink task files on this environment. | | 361 | + </p> | | 362 | + ) | | 363 | + ) : null} | | 364 | + {showRelink && relinkTask ? ( | | 365 | + <TaskRelinkDialog | | 366 | + environmentId={environmentId} | | 367 | + task={relinkTask} | | 368 | + onRelink={(document) => session.acceptRead(session.beginRead(), document)} | | 369 | + onClose={() => setShowRelink(false)} | | 370 | + /> | | 371 | + ) : null} | | 372 | + </section> | 335 | 373 | ) : ( | 336 | 374 | <div className="m-auto p-6"> | 337 | 375 | <Spinner /> | @@ -346,7 +384,7 @@ export default function TaskDocumentPane(props: { | 346 | 384 | onKeyDown={(event) => { | 347 | 385 | if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "s") { | 348 | 386 | event.preventDefault(); | 349 | | - void save(); | | 387 | + if (summary?.availability === "available") void save(); | 350 | 388 | } | 351 | 389 | }} | 352 | 390 | > | @@ -378,11 +416,23 @@ export default function TaskDocumentPane(props: { | 378 | 416 | variant="ghost" | 379 | 417 | aria-label="Save task document" | 380 | 418 | title="Save task document" | 381 | | - disabled={!canSave || !dirty || disk !== null || saving} | | 419 | + disabled={ | | 420 | + !canSave || summary?.availability !== "available" || !dirty || disk !== null || saving | | 421 | + } | 382 | 422 | onClick={() => void save()} | 383 | 423 | > | 384 | 424 | {saving ? <Spinner /> : <SaveIcon />} | 385 | 425 | </Button> | | 426 | + <Button | | 427 | + size="icon-xs" | | 428 | + variant="ghost" | | 429 | + aria-label="Edit task properties" | | 430 | + title="Edit task properties" | | 431 | + disabled={!canSave || summary?.availability !== "available" || disk !== null} | | 432 | + onClick={() => setShowProperties(true)} | | 433 | + > | | 434 | + <PencilIcon /> | | 435 | + </Button> | 386 | 436 | <Button | 387 | 437 | size="icon-xs" | 388 | 438 | variant="ghost" | @@ -416,6 +466,21 @@ export default function TaskDocumentPane(props: { | 416 | 466 | thread={thread} | 417 | 467 | /> | 418 | 468 | ) : null} | | 469 | + {summary?.availability === "identity-changed" ? ( | | 470 | + <Notice | | 471 | + actions={ | | 472 | + canRelink ? ( | | 473 | + <Button size="xs" variant="outline" onClick={() => setShowRelink(true)}> | | 474 | + Relink task file | | 475 | + </Button> | | 476 | + ) : null | | 477 | + } | | 478 | + > | | 479 | + This file has a different task ID. Relink it to keep this task's threads attached before | | 480 | + editing. | | 481 | + {!canRelink ? " You do not have permission to relink task files here." : ""} | | 482 | + </Notice> | | 483 | + ) : null} | 419 | 484 | {error ? <Notice>{error} Your draft is kept.</Notice> : null} | 420 | 485 | {!canSave ? <Notice>You can read this task but not edit it here.</Notice> : null} | 421 | 486 | {disk !== null ? ( | @@ -477,11 +542,29 @@ export default function TaskDocumentPane(props: { | 477 | 542 | taskDocumentId={taskDocumentId} | 478 | 543 | name={name} | 479 | 544 | contents={draft} | 480 | | - editable={canSave && disk === null} | | 545 | + editable={canSave && summary?.availability === "available" && disk === null} | 481 | 546 | session={session} | 482 | 547 | /> | 483 | 548 | </DiffWorkerPoolProvider> | 484 | 549 | )} | | 550 | + {showProperties && summary ? ( | | 551 | + <TaskPropertiesDialog | | 552 | + contents={draft} | | 553 | + title={summary.title} | | 554 | + state={summary.state} | | 555 | + pinned={summary.pinned} | | 556 | + onApply={(contents) => session.edit(contents)} | | 557 | + onClose={() => setShowProperties(false)} | | 558 | + /> | | 559 | + ) : null} | | 560 | + {showRelink && relinkTask ? ( | | 561 | + <TaskRelinkDialog | | 562 | + environmentId={environmentId} | | 563 | + task={relinkTask} | | 564 | + onRelink={(document) => session.acceptRead(session.beginRead(), document)} | | 565 | + onClose={() => setShowRelink(false)} | | 566 | + /> | | 567 | + ) : null} | 485 | 568 | </section> | 486 | 569 | ); | 487 | 570 | } |
Collapse file Copy file name to clipboard +103Lines changed: 103 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { useState } from "react"; | | 2 | + | | 3 | +import { Button } from "~/components/ui/button"; | | 4 | +import { Checkbox } from "~/components/ui/checkbox"; | | 5 | +import { | | 6 | + Dialog, | | 7 | + DialogDescription, | | 8 | + DialogFooter, | | 9 | + DialogHeader, | | 10 | + DialogPanel, | | 11 | + DialogPopup, | | 12 | + DialogTitle, | | 13 | +} from "~/components/ui/dialog"; | | 14 | +import { Input } from "~/components/ui/input"; | | 15 | +import { patchTaskFrontmatter, readTaskFrontmatter } from "@t3tools/shared/markdownFrontmatter"; | | 16 | + | | 17 | +export function TaskPropertiesDialog(props: { | | 18 | + readonly contents: string; | | 19 | + readonly title: string; | | 20 | + readonly state: string | null; | | 21 | + readonly pinned: boolean; | | 22 | + readonly onApply: (contents: string) => void; | | 23 | + readonly onClose: () => void; | | 24 | +}) { | | 25 | + const metadata = readTaskFrontmatter(props.contents); | | 26 | + const [title, setTitle] = useState(metadata?.title ?? props.title); | | 27 | + const [state, setState] = useState(metadata?.state ?? props.state ?? ""); | | 28 | + const [pinned, setPinned] = useState(metadata?.pinned ?? props.pinned); | | 29 | + const [error, setError] = useState(false); | | 30 | + | | 31 | + const apply = () => { | | 32 | + const contents = patchTaskFrontmatter(props.contents, { title, state, pinned }); | | 33 | + if (contents === null) { | | 34 | + setError(true); | | 35 | + return; | | 36 | + } | | 37 | + props.onApply(contents); | | 38 | + props.onClose(); | | 39 | + }; | | 40 | + | | 41 | + return ( | | 42 | + <Dialog open onOpenChange={(open) => !open && props.onClose()}> | | 43 | + <DialogPopup className="max-w-lg"> | | 44 | + <form | | 45 | + className="flex min-h-0 flex-col" | | 46 | + onSubmit={(event) => { | | 47 | + event.preventDefault(); | | 48 | + apply(); | | 49 | + }} | | 50 | + > | | 51 | + <DialogHeader> | | 52 | + <DialogTitle>Task properties</DialogTitle> | | 53 | + <DialogDescription> | | 54 | + Changes stay in your draft until you save the task file. | | 55 | + </DialogDescription> | | 56 | + </DialogHeader> | | 57 | + <DialogPanel> | | 58 | + <div className="flex flex-col gap-3"> | | 59 | + <label className="flex flex-col gap-1 text-sm"> | | 60 | + Title | | 61 | + <Input | | 62 | + autoFocus | | 63 | + maxLength={512} | | 64 | + value={title} | | 65 | + onChange={(event) => setTitle(event.target.value)} | | 66 | + /> | | 67 | + </label> | | 68 | + <label className="flex flex-col gap-1 text-sm"> | | 69 | + State | | 70 | + <Input | | 71 | + maxLength={512} | | 72 | + placeholder="No state" | | 73 | + value={state} | | 74 | + onChange={(event) => setState(event.target.value)} | | 75 | + /> | | 76 | + </label> | | 77 | + <label className="flex items-center gap-2 text-sm"> | | 78 | + <Checkbox | | 79 | + checked={pinned} | | 80 | + onCheckedChange={(checked) => setPinned(checked === true)} | | 81 | + /> | | 82 | + Pin this task | | 83 | + </label> | | 84 | + {error ? ( | | 85 | + <p role="alert" className="text-destructive-foreground text-sm"> | | 86 | + The frontmatter could not be patched safely. Edit it in the source and try again. | | 87 | + </p> | | 88 | + ) : null} | | 89 | + </div> | | 90 | + </DialogPanel> | | 91 | + <DialogFooter> | | 92 | + <Button type="button" variant="ghost" onClick={props.onClose}> | | 93 | + Cancel | | 94 | + </Button> | | 95 | + <Button type="submit" disabled={title.trim() === ""}> | | 96 | + Apply changes | | 97 | + </Button> | | 98 | + </DialogFooter> | | 99 | + </form> | | 100 | + </DialogPopup> | | 101 | + </Dialog> | | 102 | + ); | | 103 | +} |
|