Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor([issue-710]): reuse useClickOutside in ProvenanceChip (/simplify)

[._.]/ Adam Eivyca8254b942c3
diff --git a/client/src/components/ui/ProvenanceChip.jsx b/client/src/components/ui/ProvenanceChip.jsx
index 6b28769ba4..8fb431c196 100644
--- a/client/src/components/ui/ProvenanceChip.jsx
+++ b/client/src/components/ui/ProvenanceChip.jsx
@@ -1,6 +1,7 @@
-import { useEffect, useId, useRef, useState } from 'react';
+import { useCallback, useEffect, useId, useRef, useState } from 'react';
import { BadgeCheck, FlaskConical, HelpCircle, Sigma, Telescope } from 'lucide-react';
import { getProvenanceLevel } from '../../lib/healthProvenance.js';
+import useClickOutside from '../../hooks/useClickOutside';
// Source-style provenance chip for health/longevity insights. Tap (or click) to
// reveal how the insight was derived plus a "what would change this?" explainer.
@@ -41,22 +42,18 @@ export default function ProvenanceChip({
const [open, setOpen] = useState(false);
const wrapRef = useRef(null);
const popId = useId();
+ const close = useCallback(() => setOpen(false), []);
+
+ useClickOutside(wrapRef, open, close);
useEffect(() => {
if (!open) return undefined;
- const onPointerDown = (e) => {
- if (wrapRef.current && !wrapRef.current.contains(e.target)) setOpen(false);
- };
const onKey = (e) => {
- if (e.key === 'Escape') setOpen(false);
+ if (e.key === 'Escape') close();
};
- document.addEventListener('mousedown', onPointerDown);
document.addEventListener('keydown', onKey);
- return () => {
- document.removeEventListener('mousedown', onPointerDown);
- document.removeEventListener('keydown', onKey);
- };
- }, [open]);
+ return () => document.removeEventListener('keydown', onKey);
+ }, [open, close]);
const Icon = LEVEL_ICONS[meta.id] ?? Sigma;
const chipTone = TONE_CHIP[meta.tone] ?? TONE_CHIP.muted;
diff --git a/client/src/components/ui/ProvenanceChip.test.jsx b/client/src/components/ui/ProvenanceChip.test.jsx
index 6bc08dda2c..581c840eff 100644
--- a/client/src/components/ui/ProvenanceChip.test.jsx
+++ b/client/src/components/ui/ProvenanceChip.test.jsx
@@ -52,6 +52,19 @@ describe('ProvenanceChip', () => {
expect(btn.className).toContain('text-port-warning');
});
+ it('closes the popover on a click outside', () => {
+ render(
+ <div>
+ <ProvenanceChip level="inferred" />
+ <button type="button">elsewhere</button>
+ </div>,
+ );
+ fireEvent.click(screen.getByRole('button', { name: /inferred/i }));
+ expect(screen.getByRole('dialog')).toBeTruthy();
+ fireEvent.mouseDown(screen.getByRole('button', { name: 'elsewhere' }));
+ expect(screen.queryByRole('dialog')).toBeNull();
+ });
+
it('closes the popover on Escape', () => {
render(<ProvenanceChip level="inferred" />);
const btn = screen.getByRole('button');