feat(documents): open the doc-scoped Coach in the side drawer, not a full page

"Ask the Coach" on a document used to navigate to /coach?doc=<id> — a full-page
jump away from the vault. It now opens the real, doc-scoped conversation in the
shared side drawer, and the drawer's maximize control expands it to the full
/coach page while preserving scope.

- Thread an optional documentId through the launch context: askCoach gains a
  fourth argument, the provider tracks it alongside prefill/scope and clears it
  on close, and LayoutCoachMount forwards it to the drawer as initialDocumentId.
  The open-state precedence moves into a pure resolveLaunchState helper.
- The vault detail sheet closes itself and calls askCoach(..., doc.id) instead
  of routing away, so the drawer owns the surface.
- Maximize preserves continuity: the drawer reads the live conversation id via
  a new imperative getter out of the conversation surface (parallel to the
  reset registration) and hands off to /coach?c=<id> once a thread exists,
  falling back to /coach?doc=<id> for a chat maximized before its first turn.

The send path is untouched: every document turn still routes through the
hardened fenced endpoint keyed on documentId — only where the conversation
renders (drawer vs page) changed. The documents-chat e2e now asserts the
drawer opens in place, doc-scoped, rather than a URL change.
This commit is contained in:
Marc-André Bombeck committed 2026-07-16 22:34:17 +02:00
1 parent 7c96abc8ec
commit d72f078764
9 files changed
+390 -51

No files matched your search

@@ -0,0 +1,66 @@
import { describe, expect, it } from "vitest";
import { readFileSync } from "node:fs";
import { join } from "node:path";
/**
* v1.28.52 (Documents R3) — "Ask the Coach" opens the REAL fenced coach
* conversation in the shared SIDE DRAWER, scoped to the document, instead of a
* full-page nav to `/coach?doc=<id>` (the v1.28.51 behaviour Marc corrected).
*
* The wiring is source-structural (the send-path fence itself is covered by the
* coach-send-target unit tests — untouched here). These guards pin:
* 1. the detail sheet drives the drawer via `askCoach(..., doc.id)`, closing
* the sheet first, and no longer navigates to `/coach?doc=`;
* 2. the launch context → mount → drawer → conversation `documentId` thread
* stays connected end-to-end.
*/
const ROOT = join(__dirname, "../../../..");
const DETAIL_SHEET = join(
ROOT,
"src/components/documents/document-detail-sheet.tsx",
);
const MOUNT = join(ROOT, "src/components/insights/layout-coach-mount.tsx");
const DRAWER = join(
ROOT,
"src/components/insights/coach-panel/coach-drawer.tsx",
);
function load(path: string): string {
return readFileSync(path, "utf8");
}
describe("document detail sheet — Ask the Coach opens the drawer", () => {
const src = load(DETAIL_SHEET);
it("launches the doc-scoped coach drawer via askCoach(..., doc.id)", () => {
expect(src).toContain("useCoachLaunch");
expect(src).toMatch(/askCoach\(null,\s*undefined,\s*false,\s*doc\.id\)/);
});
it("closes the detail sheet when handing off to the drawer", () => {
// The Ask-the-Coach onClick closes the sheet before opening the drawer so
// the drawer owns the surface (no stacked dialogs).
expect(src).toMatch(
/onClick=\{\(\)\s*=>\s*\{[\s\S]*?onOpenChange\(false\);[\s\S]*?askCoach\(null,\s*undefined,\s*false,\s*doc\.id\)/,
);
});
it("no longer navigates to the full-page /coach?doc= route", () => {
expect(src).not.toContain("/coach?doc=");
expect(src).not.toContain('from "next/navigation"');
});
});
describe("documentId threads launch context → mount → drawer", () => {
it("the mount forwards launch.documentId to the drawer", () => {
expect(load(MOUNT)).toContain("documentId={launch.documentId}");
});
it("the drawer seeds CoachConversation with initialDocumentId + a conversation-id getter", () => {
const src = load(DRAWER);
expect(src).toContain("initialDocumentId={documentId}");
expect(src).toContain(
"registerConversationIdGetter={registerConversationIdGetter}",
);
});
});
@@ -28,7 +28,6 @@ import {
X,
} from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useRef, useState } from "react";
import { toast } from "sonner";
@@ -59,6 +58,7 @@ import {
apiPost,
} from "@/lib/api/api-fetch";
import { useFormatters, useTranslations } from "@/lib/i18n/context";
import { useCoachLaunch } from "@/lib/insights/coach-launch-context";
import { invalidateKeys, queryKeys } from "@/lib/query-keys";
import { cn } from "@/lib/utils";
import {
@@ -216,7 +216,11 @@ export function DocumentDetailSheet({
const { t, locale } = useTranslations();
const format = useFormatters();
const queryClient = useQueryClient();
const router = useRouter();
// v1.28.52 (Documents R3) — the vault "Ask the Coach" action opens the REAL
// fenced coach conversation in the shared side drawer (scoped to this
// document) instead of a full-page nav; `askCoach(..., doc.id)` carries the
// document scope through the launch context.
const launch = useCoachLaunch();
const detail = useQuery({
queryKey: queryKeys.inboundDocument(documentId ?? "none"),
@@ -472,14 +476,19 @@ export function DocumentDetailSheet({
</Button>
<div className="flex items-center gap-2">
{aiEnabled ? (
// v1.28.51 (Documents R3, Design A) — "Ask the Coach" opens
// the REAL coach view scoped to this document
// (`/coach?doc=<id>`), replacing the bespoke doc-chat drawer.
// Every doc turn there still runs on the hardened fenced
// document endpoint (never the coach tool route).
// v1.28.52 (Documents R3) — "Ask the Coach" opens the REAL
// coach conversation scoped to this document in the SIDE
// DRAWER (the maximize control there expands it to the full
// `/coach` page). Every doc turn still runs on the hardened
// fenced document endpoint (never the coach tool route) — only
// WHERE the conversation renders changed, not the send path.
// Close the detail sheet so the drawer owns the surface.
<Button
variant="outline"
onClick={() => router.push(`/coach?doc=${doc.id}`)}
onClick={() => {
onOpenChange(false);
launch?.askCoach(null, undefined, false, doc.id);
}}
data-slot="document-chat-open"
aria-label={t("documents.chat.openAria")}
>