diff --git a/src/pages/admin/bots/[bot_id]/(partials)/bot-question-ref-chip.tsx b/src/pages/admin/bots/[bot_id]/(partials)/bot-question-ref-chip.tsx index 3eaef715..87edb250 100644 --- a/src/pages/admin/bots/[bot_id]/(partials)/bot-question-ref-chip.tsx +++ b/src/pages/admin/bots/[bot_id]/(partials)/bot-question-ref-chip.tsx @@ -1,6 +1,5 @@ import { useState } from "react"; import { twMerge } from "tailwind-merge"; -import Modal from "@moduletrace/twui/elements/Modal"; import Button from "@moduletrace/twui/layout/Button"; import H2 from "@moduletrace/twui/layout/H2"; import Span from "@moduletrace/twui/layout/Span"; @@ -8,6 +7,7 @@ import Stack from "@moduletrace/twui/layout/Stack"; import { MessageCircleQuestion } from "lucide-react"; import useBotQuestion from "../(hooks)/use-bot-question"; import BotQuestionCard from "./bot-question-card"; +import RefChipModal from "./ref-chip-modal"; type Props = { question_id: number; @@ -42,14 +42,15 @@ export default function BotQuestionRefChip({ question_id }: Props) { const pending = view.status == "pending"; return ( - setOpen(true)} className={twMerge( "inline-flex h-6 max-w-full gap-1.5! flex-nowrap align-middle px-2! text-[13px] font-bold whitespace-nowrap", !pending && "text-ink", @@ -60,9 +61,8 @@ export default function BotQuestionRefChip({ question_id }: Props) { {`${name} · ${status}`} } - targetWrapperProps={{ className: "inline-flex max-w-full align-middle" }} open={open} - setOpen={setOpen} + onClose={() => setOpen(false)} className="max-w-xl bg-paper p-6" > @@ -79,6 +79,6 @@ export default function BotQuestionRefChip({ question_id }: Props) { setOpen(false)} /> - + ); } diff --git a/src/pages/admin/bots/[bot_id]/(partials)/hook-proposal-ref-chip.tsx b/src/pages/admin/bots/[bot_id]/(partials)/hook-proposal-ref-chip.tsx index d976b34f..12741695 100644 --- a/src/pages/admin/bots/[bot_id]/(partials)/hook-proposal-ref-chip.tsx +++ b/src/pages/admin/bots/[bot_id]/(partials)/hook-proposal-ref-chip.tsx @@ -1,6 +1,5 @@ import { useState } from "react"; import { twMerge } from "tailwind-merge"; -import Modal from "@moduletrace/twui/elements/Modal"; import Button from "@moduletrace/twui/layout/Button"; import H2 from "@moduletrace/twui/layout/H2"; import Span from "@moduletrace/twui/layout/Span"; @@ -10,6 +9,7 @@ import useHookProposal from "../(hooks)/use-hook-proposal"; import hookProposalTitle from "../(utils)/hook-proposal-title"; import proposalStatusLabel from "../(utils)/proposal-status-label"; import HookProposalCard from "./hook-proposal-card"; +import RefChipModal from "./ref-chip-modal"; type Props = { proposal_id: number; @@ -36,14 +36,15 @@ export default function HookProposalRefChip({ proposal_id }: Props) { const status = proposalStatusLabel({ view }); return ( - setOpen(true)} className={twMerge( "inline-flex h-6 max-w-full gap-1.5! flex-nowrap align-middle px-2! text-[13px] font-bold text-ink whitespace-nowrap", "[&_.twui-button-content-wrapper]:min-w-0", @@ -53,9 +54,8 @@ export default function HookProposalRefChip({ proposal_id }: Props) { {`${title} · ${status}`} } - targetWrapperProps={{ className: "inline-flex max-w-full align-middle" }} open={open} - setOpen={setOpen} + onClose={() => setOpen(false)} className="max-w-xl bg-paper p-6" > @@ -68,6 +68,6 @@ export default function HookProposalRefChip({ proposal_id }: Props) { setOpen(false)} /> - + ); } diff --git a/src/pages/admin/bots/[bot_id]/(partials)/proposal-ref-chip.tsx b/src/pages/admin/bots/[bot_id]/(partials)/proposal-ref-chip.tsx index 362bee40..c9f8c9d3 100644 --- a/src/pages/admin/bots/[bot_id]/(partials)/proposal-ref-chip.tsx +++ b/src/pages/admin/bots/[bot_id]/(partials)/proposal-ref-chip.tsx @@ -1,6 +1,5 @@ import { useState } from "react"; import { twMerge } from "tailwind-merge"; -import Modal from "@moduletrace/twui/elements/Modal"; import Button from "@moduletrace/twui/layout/Button"; import H2 from "@moduletrace/twui/layout/H2"; import Span from "@moduletrace/twui/layout/Span"; @@ -11,6 +10,7 @@ import useExtensionProposal from "../(hooks)/use-extension-proposal"; import proposalStatusLabel from "../(utils)/proposal-status-label"; import proposalTitle from "../(utils)/proposal-title"; import ProposalCard from "./proposal-card"; +import RefChipModal from "./ref-chip-modal"; type Props = { proposal_id: number; @@ -40,14 +40,15 @@ export default function ProposalRefChip({ proposal_id }: Props) { const status = proposalStatusLabel({ view }); return ( - setOpen(true)} className={twMerge( "inline-flex h-6 max-w-full gap-1.5! flex-nowrap align-middle px-2! text-[13px] font-bold text-ink whitespace-nowrap", "[&_.twui-button-content-wrapper]:min-w-0", @@ -57,9 +58,8 @@ export default function ProposalRefChip({ proposal_id }: Props) { {`${title} · ${status}`} } - targetWrapperProps={{ className: "inline-flex max-w-full align-middle" }} open={open} - setOpen={setOpen} + onClose={() => setOpen(false)} className="max-w-xl bg-paper p-6" > @@ -72,6 +72,6 @@ export default function ProposalRefChip({ proposal_id }: Props) { - + ); } diff --git a/src/pages/admin/bots/[bot_id]/(partials)/ref-chip-modal.render.test.tsx b/src/pages/admin/bots/[bot_id]/(partials)/ref-chip-modal.render.test.tsx new file mode 100644 index 00000000..1c851991 --- /dev/null +++ b/src/pages/admin/bots/[bot_id]/(partials)/ref-chip-modal.render.test.tsx @@ -0,0 +1,45 @@ +import { describe, expect, test } from "bun:test"; +import { renderToString } from "react-dom/server"; +import Button from "@moduletrace/twui/layout/Button"; +import P from "@moduletrace/twui/layout/P"; +import Span from "@moduletrace/twui/layout/Span"; +import RefChipModal from "./ref-chip-modal"; + +/** + * A ref chip sits in a markdown paragraph, which takes phrasing content + * only. twui's own `Modal` target wrapper is a div, so the chip used to + * put a div in the paragraph and React logged "p cannot contain a nested + * div" for every question or proposal chip on the page. + */ +describe("RefChipModal", () => { + test("wraps its trigger in a span, with no block element beside it", () => { + const html = renderToString( +

+ Review} open={false} onClose={() => {}}> + Card + +

, + ); + + // twui's own Button fills itself with a div, valid inside a button + // (React only warns about a p in button scope), so the paragraph + // is broken only by a div before the button: twui's Modal used to + // wrap its target in one. + const first_button = html.indexOf("'); + expect(html).toContain("Review"); + expect(first_button).toBeGreaterThan(-1); + expect(html.slice(0, first_button)).not.toContain(" { + const html = renderToString( + Review} open={false} onClose={() => {}}> + Card + , + ); + + expect(html).not.toContain("Card"); + }); +}); \ No newline at end of file diff --git a/src/pages/admin/bots/[bot_id]/(partials)/ref-chip-modal.tsx b/src/pages/admin/bots/[bot_id]/(partials)/ref-chip-modal.tsx new file mode 100644 index 00000000..b2d00cea --- /dev/null +++ b/src/pages/admin/bots/[bot_id]/(partials)/ref-chip-modal.tsx @@ -0,0 +1,38 @@ +import type { ReactNode } from "react"; +import Modal from "@moduletrace/twui/elements/Modal"; +import Span from "@moduletrace/twui/layout/Span"; + +type Props = { + trigger: ReactNode; + open: boolean; + onClose: () => void; + className?: string; + children: ReactNode; +}; + +/** + * A ref chip's trigger and the card it opens (`[question:ID]`, + * `[proposal:ID]`, ...). The chip sits in a markdown paragraph, which takes + * phrasing content only: the trigger is a span around the caller's button, + * and the card is mounted only while open, since twui's own `target` + * wrapper is a div and a div inside the paragraph is invalid (React says + * so in the console). + */ +export default function RefChipModal({ trigger, open, onClose, className, children }: Props) { + return ( + <> + {trigger} + {open ? ( + { + if (!next_open) onClose(); + }} + className={className} + > + {children} + + ) : null} + + ); +} \ No newline at end of file diff --git a/src/pages/admin/bots/[bot_id]/(partials)/ref-chips-in-paragraph.render.test.tsx b/src/pages/admin/bots/[bot_id]/(partials)/ref-chips-in-paragraph.render.test.tsx new file mode 100644 index 00000000..f7e177e5 --- /dev/null +++ b/src/pages/admin/bots/[bot_id]/(partials)/ref-chips-in-paragraph.render.test.tsx @@ -0,0 +1,170 @@ +import { afterAll, describe, expect, mock, test } from "bun:test"; +import { renderToString } from "react-dom/server"; +import P from "@moduletrace/twui/layout/P"; +import type { + BotQuestionView, + ExtensionProposalView, + HookProposalView, + SecretRefView, + SecretRequestView, +} from "@/src/types"; + +const question: BotQuestionView = { + id: 1, + title: "Deploy now?", + questions: [{ question: "When?", kind: "single", options: ["Now", "Later"], allow_other: false }], + answers: null, + status: "pending", + bot_id: 2, + bot_name: "Ada", +}; + +const secret_request: SecretRequestView = { + id: 3, + key: "GITHUB_TOKEN", + description: "A token to push.", + reason: "To push the branch.", + status: "pending", + bot_id: 2, + bot_name: "Ada", + env_id: null, +}; + +const proposal: ExtensionProposalView = { + id: 4, + kind: "mcp", + extension_id: null, + name: "GitHub", + status: "pending", + expired: false, + expires_at: 0, + outcome: "", + result_id: null, + host: null, + envs: [], + details: [], + research: "", + sources: [], +}; + +const hook_proposal: HookProposalView = { + id: 5, + action: "create", + hook_id: null, + title: "GitHub pushes", + bot_id: 2, + bot_name: "Ada", + status: "pending", + expired: false, + expires_at: 0, + outcome: "", + result_id: null, + makes_url: true, + details: [], + reason: "So it hears about pushes.", +}; + +const secret: SecretRefView = { + id: 6, + key: "NPM_TOKEN", + description: "A token to publish.", + can_reveal: true, +}; + +mock.module("../(hooks)/use-bot-question", () => ({ + default: () => ({ view: question, loading: false, setView: () => {} }), +})); +mock.module("../(hooks)/use-secret-request", () => ({ + default: () => ({ view: secret_request, loading: false, setView: () => {} }), +})); +mock.module("../(hooks)/use-extension-proposal", () => ({ + default: () => ({ view: proposal, loading: false, setView: () => {} }), +})); +mock.module("../(hooks)/use-hook-proposal", () => ({ + default: () => ({ view: hook_proposal, loading: false, setView: () => {} }), +})); +mock.module("../(hooks)/use-secret-ref", () => ({ + default: () => ({ view: secret, loading: false }), +})); + +const { default: BotQuestionRefChip } = await import("./bot-question-ref-chip"); +const { default: SecretRequestRefChip } = await import("./secret-request-ref-chip"); +const { default: ProposalRefChip } = await import("./proposal-ref-chip"); +const { default: HookProposalRefChip } = await import("./hook-proposal-ref-chip"); +const { default: SecretRefChip } = await import("./secret-ref-chip"); + +/** + * twui's Button fills itself with a div, valid inside a button (React only + * warns about a p in button scope), so what a paragraph may not hold is a + * div outside one: twui's Modal used to wrap each chip's trigger in one. + */ +function outsideButtons(html: string) { + return html.replaceAll(//g, ""); +} + +afterAll(() => { + mock.restore(); +}); + +/** + * Message markdown renders bot text in real paragraphs, so every ref chip + * that lands in it must be phrasing content: a block element beside its + * text was React's "p cannot contain a nested div" warning, once per chip + * on the page. + */ +describe("ref chips in a markdown paragraph", () => { + test("a question chip", () => { + const html = renderToString( +

+ +

, + ); + + expect(html).toContain("Deploy now?"); + expect(outsideButtons(html)).not.toContain(" { + const html = renderToString( +

+ +

, + ); + + expect(html).toContain("GITHUB_TOKEN"); + expect(outsideButtons(html)).not.toContain(" { + const html = renderToString( +

+ +

, + ); + + expect(html).toContain("Add MCP server GitHub"); + expect(outsideButtons(html)).not.toContain(" { + const html = renderToString( +

+ +

, + ); + + expect(html).toContain("Add hook GitHub pushes"); + expect(outsideButtons(html)).not.toContain(" { + const html = renderToString( +

+ +

, + ); + + expect(html).toContain("NPM_TOKEN"); + expect(outsideButtons(html)).not.toContain(" @@ -76,6 +75,6 @@ export default function SecretRefChip({ env_id }: Props) { ) : null} {error ? {error} : null} - + ); } diff --git a/src/pages/admin/bots/[bot_id]/(partials)/secret-request-ref-chip.tsx b/src/pages/admin/bots/[bot_id]/(partials)/secret-request-ref-chip.tsx index de34dcc2..2eebc1d5 100644 --- a/src/pages/admin/bots/[bot_id]/(partials)/secret-request-ref-chip.tsx +++ b/src/pages/admin/bots/[bot_id]/(partials)/secret-request-ref-chip.tsx @@ -1,6 +1,5 @@ import { useState } from "react"; import { twMerge } from "tailwind-merge"; -import Modal from "@moduletrace/twui/elements/Modal"; import Button from "@moduletrace/twui/layout/Button"; import H2 from "@moduletrace/twui/layout/H2"; import Span from "@moduletrace/twui/layout/Span"; @@ -8,6 +7,7 @@ import Stack from "@moduletrace/twui/layout/Stack"; import { KeyRound } from "lucide-react"; import useSecretRequest from "../(hooks)/use-secret-request"; import SecretRequestCard from "./secret-request-card"; +import RefChipModal from "./ref-chip-modal"; type Props = { request_id: number; @@ -39,14 +39,15 @@ export default function SecretRequestRefChip({ request_id }: Props) { const status = StatusLabels[view.status]; return ( - setOpen(true)} className={twMerge( "inline-flex h-6 max-w-full gap-1.5! flex-nowrap align-middle px-2! text-[13px] font-bold text-ink whitespace-nowrap", "[&_.twui-button-content-wrapper]:min-w-0", @@ -56,9 +57,8 @@ export default function SecretRequestRefChip({ request_id }: Props) { {`${view.key} · ${status}`} } - targetWrapperProps={{ className: "inline-flex max-w-full align-middle" }} open={open} - setOpen={setOpen} + onClose={() => setOpen(false)} className="max-w-xl bg-paper p-6" > @@ -71,6 +71,6 @@ export default function SecretRequestRefChip({ request_id }: Props) { setOpen(false)} /> - + ); }