Files
AgentHub/client/src/components/FieldHelp.tsx
T
nntrivi2001 c7dbaf2d9f refactor: finish the AgentHub rename and drop the MCP server
Two leftovers from the rebrand, done together because they touch the same
docs and manifests.

**Data directory.** `agent-dashboard` could not simply be renamed in the
source: it names a real directory holding the live SQLite database. Startup
now moves it once, via `migrateLegacyDataDir()` in `server/lib/claude-home.js`
— a same-filesystem rename, so it is instant even at multiple gigabytes and
cannot leave two divergent copies. `getDataDir()` stays PURE and resolves to
the legacy path until the move happens; the first version of this put the
rename inside the getter, which meant `npm run test:server` relocated the
developer's real database as a side effect. The call sits behind
`require.main === module` and ahead of the router requires, because those pull
in db.js, which opens SQLite at module load. A failed rename is silent and the
old directory keeps being used: an unfashionable directory name beats a blank
database beside an unreachable one. Same treatment for the port-discovery file
(`.agent-dashboard.json` → `.agenthub.json`, old one dropped on write).

Not renamed on purpose: the Sagi localStorage keys (renaming silently resets
everyone's prefs) and the PWA manifest `id` (changing it orphans an installed
app).

**MCP server.** 4 tool calls in 46 days across 577,132 events, against 4,556
lines of TypeScript, a 2 MB committed build artifact, and a pre-commit guard
that had already blocked commits twice. Deleted: `mcp/`, the maintainer skills
and `mcp-reviewer` agent, the build check, `plugins/agenthub/.mcp.json`,
`docs/MCP.md`, 12 npm scripts, 5 Makefile targets, and every doc section
describing it.

Kept, because they are unrelated things sharing the name: the Config
Explorer's MCP tab and its API, per-lane Playwright MCP, the agenthub-config
plugin's MCP audit, and the historical `mcp__plugin_agenthub_*` events that
analytics must keep attributing.

Verified: server 1432/1432, client 459/459, tsc clean, prettier clean. Both
directions of the migration proven — a sandbox-HOME test run leaves the legacy
directory untouched, and a real boot moves it with its contents intact.
2026-09-08 09:48:40 +07:00

177 lines
7.1 KiB
TypeScript

/**
* @file FieldHelp.tsx
* @description Contextual "(?)" help trigger for dense settings forms. Opens a
* rich popover with title, description, optional example chips, and an optional
* footnote — all portaled to `<body>` and repositioned on scroll/resize so the
* panel never clips inside scrolling cards.
*
* ## Interaction model
* Opens on hover, focus, or click (toggle). Escape dismisses. The popover uses
* `pointer-events-none` so moving the pointer toward it does not accidentally
* close the trigger's hover state mid-read.
*
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
/* =============================================================================
* MODULE_GUIDE — extended in-file reference (comments only; safe to read, never executed)
* =============================================================================
* **Purpose:** Dashboard module consumed by the React client, MCP tools, or desktop shell depending on deployment mode.
*
* ## Design constraints
* - Local-first: no telemetry leaves the machine unless the user configures webhooks.
* - Fail-safe hooks path on the server must never block Claude Code; UI mirrors that
* philosophy by degrading gracefully (empty states, stale badges, reconnect loops).
* - Destructive flows stay behind explicit confirmation modals and server-side gates.
* - Internationalization: user-visible strings belong in i18n JSON, not literals here.
*
* ## Remote data & SSH
* Remote Data Sources let operators aggregate multiple machines. SSH entries describe
* how to reach a peer dashboard; the global data scope (`dataScope.ts`) narrows every
* scoped GET via `?sources=`. Health checks and import history surface in Settings.
*
* ## Public surface
* - `FieldHelpProps` — exported API; see TSDoc on the symbol for behavior.
* - `FieldHelp` — exported API; see TSDoc on the symbol for behavior.
*
* ## Testing pointers
* - Prefer colocated `__tests__` with Vitest + Testing Library for UI.
* - Server contract changes require `npm run test:server` and OpenAPI sync.
*
* ## Related docs
* - `ARCHITECTURE.md` — hooks → API → SQLite → WebSocket → UI pipeline.
* - `docs/API.md` — REST reference.
* - `skills/file-headers/` — mandatory `@author` header policy.
* ============================================================================= */
/* -----------------------------------------------------------------------------
* EXPORT CATALOG — quick index of symbols defined below (documentation only).
* -----------------------------------------------------------------------------
* **FieldHelpProps**
* Part of this module's public contract. Downstream imports should treat
* the signature and return type as stable unless release notes say otherwise.
* When behavior changes, update the `@file` overview and relevant tests.
*
* **FieldHelp**
* Part of this module's public contract. Downstream imports should treat
* the signature and return type as stable unless release notes say otherwise.
* When behavior changes, update the `@file` overview and relevant tests.
*
* ----------------------------------------------------------------------------- */
import { useCallback, useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { useTranslation } from "react-i18next";
import { HelpCircle } from "lucide-react";
/** Props for {@link FieldHelp}. */
export interface FieldHelpProps {
/** Optional bold heading inside the popover. */
title?: string;
/** Main explanatory copy. */
description: string;
/** Short example values rendered as monospace chips. */
examples?: string[];
/** Secondary note shown below examples. */
note?: string;
}
/**
* Inline help control for form fields.
* @param props See {@link FieldHelpProps}.
*/
export function FieldHelp({ title, description, examples, note }: FieldHelpProps) {
const { t } = useTranslation("common");
const [open, setOpen] = useState(false);
const btnRef = useRef<HTMLButtonElement>(null);
const popRef = useRef<HTMLDivElement>(null);
const [pos, setPos] = useState<{ left: number; top: number }>({ left: 0, top: 0 });
const place = useCallback(() => {
const btn = btnRef.current;
const pop = popRef.current;
if (!btn) return;
const r = btn.getBoundingClientRect();
const w = pop?.offsetWidth ?? 300;
const h = pop?.offsetHeight ?? 120;
const pad = 10;
let left = r.left + r.width / 2 - w / 2;
if (left < pad) left = pad;
if (left + w > window.innerWidth - pad) left = window.innerWidth - w - pad;
let top = r.bottom + 8;
if (top + h > window.innerHeight - pad) top = r.top - h - 8; // flip up
setPos({ left, top });
}, []);
useEffect(() => {
if (!open) return;
place();
const onScroll = () => place();
window.addEventListener("scroll", onScroll, true);
window.addEventListener("resize", onScroll);
const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);
document.addEventListener("keydown", onKey);
return () => {
window.removeEventListener("scroll", onScroll, true);
window.removeEventListener("resize", onScroll);
document.removeEventListener("keydown", onKey);
};
}, [open, place]);
return (
<span className="relative inline-flex">
<button
ref={btnRef}
type="button"
aria-label={title || t("help")}
onMouseEnter={() => setOpen(true)}
onMouseLeave={() => setOpen(false)}
onFocus={() => setOpen(true)}
onBlur={() => setOpen(false)}
onClick={(e) => {
e.preventDefault();
setOpen((v) => !v);
}}
className="text-fg-muted hover:text-fg-secondary transition-colors"
>
<HelpCircle className="w-3.5 h-3.5" />
</button>
{open &&
createPortal(
<div
ref={popRef}
role="tooltip"
style={{
position: "fixed",
left: pos.left,
top: pos.top,
zIndex: 99999,
maxWidth: 320,
}}
className="rounded-lg border border-border bg-surface-1 shadow-xl shadow-black/40 p-3 w-[300px] pointer-events-none"
>
{title && <p className="text-xs font-semibold text-fg-secondary mb-1">{title}</p>}
<p className="text-[11px] leading-relaxed text-fg-secondary">{description}</p>
{examples && examples.length > 0 && (
<div className="mt-2">
<p className="text-[10px] uppercase tracking-wider text-fg-muted mb-1">
{t("examples")}
</p>
<div className="flex flex-wrap gap-1">
{examples.map((ex) => (
<code
key={ex}
className="text-[10px] font-mono text-accent bg-accent/10 border border-accent/20 rounded px-1.5 py-0.5"
>
{ex}
</code>
))}
</div>
</div>
)}
{note && <p className="text-[10px] text-fg-muted mt-2 leading-relaxed">{note}</p>}
</div>,
document.body
)}
</span>
);
}