From 246a45f89f390a849f792b58b4c7d5b9fe914a59 Mon Sep 17 00:00:00 2001 From: Jerry Combs Date: Mon, 23 Nov 2026 09:12:26 -0600 Subject: [PATCH] Validation: copy a finding, or every shown finding, as text (#197) Each finding gets a copy button (hover or focus) and the panel head Copy shown; the text reads like idm validate's line. Falls back to the copy command where the Clipboard API isn't available, and says 'copy failed' when refused. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 +++ web/src/components/ValidationPanel.tsx | 85 +++++++++++++++++++++++--- web/src/styles.css | 20 ++++++ web/src/validation.test.ts | 11 +++- 4 files changed, 113 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 94ab368..740e91a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html) ## [Unreleased] +### Added + +- **Copy validation findings (#197):** each finding has a copy button (on hover or focus), and the panel head + has **Copy shown** for every finding at the selected severities. The text reads like `idm validate`'s line: + severity, code, path, message, and the detail under it. Where the browser won't let the page use the + Clipboard API (plain http to another host), it falls back to the copy command, and a refusal says + "copy failed". + ## [0.95.0] - 2026-11-20 ### Changed diff --git a/web/src/components/ValidationPanel.tsx b/web/src/components/ValidationPanel.tsx index 7455780..18b7bc5 100644 --- a/web/src/components/ValidationPanel.tsx +++ b/web/src/components/ValidationPanel.tsx @@ -1,4 +1,4 @@ -import { useMemo } from "react"; +import { useMemo, useState } from "react"; import type { Finding, Validation } from "../types"; export const SEVERITIES = ["error", "warning", "info"] as const; @@ -25,6 +25,39 @@ export function moduleOf(path: string): { module: string; rest: string; order: n return { module: parts[0]!, rest: parts.slice(1).join("/"), order: 3 }; } +/** A finding as text for the clipboard (#197), as `idm validate` prints it: severity, code, path, message, then its detail. */ +export function findingText(f: Finding): string { + const head = `${f.severity.toUpperCase()} ${f.code} ${f.path}: ${f.message}`; + return f.detail ? `${head}\n ${f.detail}` : head; +} + +/** + * Puts text on the clipboard: the Clipboard API, or, where the page may not use it (plain http on + * another host has no navigator.clipboard), a selected textarea and the copy command. + */ +async function copyText(text: string): Promise { + try { + if (navigator.clipboard) { + await navigator.clipboard.writeText(text); + return; + } + } catch { + // refused: try the copy command + } + const ta = document.createElement("textarea"); + ta.value = text; + ta.setAttribute("readonly", ""); + ta.style.position = "fixed"; + ta.style.opacity = "0"; + document.body.appendChild(ta); + ta.select(); + const ok = document.execCommand("copy"); + document.body.removeChild(ta); + if (!ok) { + throw new Error("the browser refused the copy"); + } +} + function pillClass(s: Severity, counts: { error: number }): string { return "pill " + (s === "error" ? (counts.error > 0 ? "err" : "ok") : s === "warning" ? "warn" : ""); } @@ -86,6 +119,16 @@ export function ValidationPanel({ validation, show, onToggle, onOpen, onHide }: const counts = validation?.counts ?? { error: 0, warning: 0, info: 0 }; const visible = (f: Finding) => show.has(f.severity.toLowerCase() as Severity); const shown = groups.map((g) => ({ ...g, items: g.items.filter(visible) })).filter((g) => g.items.length > 0); + // the copy button last pressed ("all" or a finding's key) and whether it worked, shown for a moment + const [copied, setCopied] = useState<{ key: string; ok: boolean } | null>(null); + const copy = (key: string, text: string) => { + const show = (ok: boolean) => { + setCopied({ key, ok }); + window.setTimeout(() => setCopied((c) => (c?.key === key ? null : c)), ok ? 1500 : 4000); + }; + copyText(text).then(() => show(true), () => show(false)); + }; + const copyLabel = (key: string, idle: string) => (copied?.key === key ? (copied.ok ? "copied" : "copy failed") : idle); return (
@@ -96,6 +139,16 @@ export function ValidationPanel({ validation, show, onToggle, onOpen, onHide }: ))} + {shown.length > 0 && ( + + )} @@ -118,15 +171,27 @@ export function ValidationPanel({ validation, show, onToggle, onOpen, onHide }:
    - {g.items.map((f, i) => ( -
  • - -
  • - ))} + {g.items.map((f, i) => { + const key = f.path + f.code + i; + return ( +
  • + + +
  • + ); + })}
)) diff --git a/web/src/styles.css b/web/src/styles.css index 3055d3a..fc7c5db 100644 --- a/web/src/styles.css +++ b/web/src/styles.css @@ -521,6 +521,26 @@ th { .finding.warning > button { border-left-color: var(--warn); } +.finding { + position: relative; +} +/* copy one finding (#197): shown on hover or keyboard focus, beside the message */ +.finding > button.fcopy { + display: block; + position: absolute; + top: 6px; + right: 8px; + width: auto; + padding: 0 4px; + border: none; + font-size: 11px; + background: var(--surface); + opacity: 0; +} +.finding:hover > button.fcopy, +.finding:focus-within > button.fcopy { + opacity: 1; +} .finding .code { font-family: ui-monospace, Menlo, monospace; font-size: 11px; diff --git a/web/src/validation.test.ts b/web/src/validation.test.ts index bbd2c2a..343c876 100644 --- a/web/src/validation.test.ts +++ b/web/src/validation.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { moduleOf } from "./components/ValidationPanel"; +import { findingText, moduleOf } from "./components/ValidationPanel"; describe("moduleOf: the module a finding is grouped under", () => { it("names the driver for anything under drivers/", () => { @@ -16,3 +16,12 @@ describe("moduleOf: the module a finding is grouped under", () => { expect([moduleOf("drivers/Z/x"), moduleOf("library/y"), moduleOf("driverset")].map((m) => m.order)).toEqual([2, 1, 0]); }); }); + +describe("findingText: a finding as text for the clipboard (#197)", () => { + it("reads like idm validate's line, with the detail under it", () => { + expect(findingText({ severity: "ERROR", code: "mapping-table-column", path: "drivers/AD/subscriber/maps", message: " names column 'x'" })).toBe( + "ERROR mapping-table-column drivers/AD/subscriber/maps: names column 'x'", + ); + expect(findingText({ severity: "warning", code: "c", path: "library/L", message: "m", detail: "more" })).toBe("WARNING c library/L: m\n more"); + }); +});