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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
refactor: dismiss the three portaled popovers via useClickOutside's array form (#3845)
[._.]/ Adam Eivy3c195b333a0f
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 25534ae0e9..d785cd78e1 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -29,3 +29,4 @@- Notification panel placement now goes through the shared popover-positioning hook that the theme switcher beside it already used, so it clamps into the viewport and flips above/below on its own instead of relying on hand-written positioning.- The Code Review Defaults panel now names every CLI reviewer in its help text, generated from the reviewer roster itself — the sentence used to spell them out by hand and fell behind twice as new reviewers shipped, telling users their CLI reviewers ran a different way than they do.- The remaining trigger-anchored dropdowns — "Run on App" on a scheduled task, and the Share and Sync buttons on series and universe cards — now use the same shared popover-positioning hook as the notification panel. "Run on App" opened a panel pinned to its button's left edge that merely got narrower on a phone rather than moving, so it ran off the right edge; the Share and Sync menus always opened downward even with no room below. All three now clamp into the viewport, flip when there is no room, and stay attached to their button as the page scrolls.+- The theme switcher, the media collection picker, and the Shell photo composer now dismiss through the shared click-outside hook instead of three private copies of the same two-ref check, so the rule that a portaled panel counts as "inside" its own trigger lives in one place. Escape still closes each of them.diff --git a/client/src/components/ThemeSwitcher.jsx b/client/src/components/ThemeSwitcher.jsxindex e4dd7427b1..73e580008f 100644--- a/client/src/components/ThemeSwitcher.jsx+++ b/client/src/components/ThemeSwitcher.jsx@@ -1,9 +1,11 @@-import { useState, useRef, useEffect } from 'react';+import { useState, useRef } from 'react';import { createPortal } from 'react-dom';import { Check, Palette } from 'lucide-react';import { useThemeContext } from './ThemeContext';import { getFamilyIcon } from '../themes/familyIcons';import usePopoverPosition, { VIEWPORT_PADDING } from '../hooks/usePopoverPosition.js';+import useClickOutside from '../hooks/useClickOutside.js';+import useEscapeKey from '../hooks/useEscapeKey.js';const MENU_WIDTH = 288;@@ -18,28 +20,11 @@ export default function ThemeSwitcher({ position = 'above', className = '' }) {} = usePopoverPosition({ open, width: MENU_WIDTH, minWidth: 180, gap: 8, position });// Close on outside-click / Escape — the popover-position hook owns placement- // and reflow; this component still owns its own dismiss semantics.- useEffect(() => {- if (!open) return undefined;-- const onMouseDown = (e) => {- const clickedTrigger = containerRef.current?.contains(e.target);- const clickedMenu = menuRef.current?.contains(e.target);- if (!clickedTrigger && !clickedMenu) setOpen(false);- };-- const onKeyDown = (e) => {- if (e.key === 'Escape') setOpen(false);- };-- document.addEventListener('mousedown', onMouseDown);- document.addEventListener('keydown', onKeyDown);-- return () => {- document.removeEventListener('mousedown', onMouseDown);- document.removeEventListener('keydown', onKeyDown);- };- }, [open, menuRef]);+ // and reflow; this component still owns its own dismiss semantics. The menu is+ // portaled to <body>, so both the trigger container and the panel have to count+ // as "inside" — that's what the array form of useClickOutside is for.+ useClickOutside([containerRef, menuRef], open, () => setOpen(false));+ useEscapeKey(open, () => setOpen(false));return (<div ref={containerRef} className={`relative ${className}`}>diff --git a/client/src/components/media/CollectionPickerShell.jsx b/client/src/components/media/CollectionPickerShell.jsxindex a07e22069e..3b318b73b5 100644--- a/client/src/components/media/CollectionPickerShell.jsx+++ b/client/src/components/media/CollectionPickerShell.jsx@@ -1,10 +1,12 @@-import { useCallback, useEffect, useMemo, useRef, useState } from 'react';+import { useCallback, useEffect, useMemo, useState } from 'react';import { createPortal } from 'react-dom';import { Plus, Search } from 'lucide-react';import BrailleSpinner from '../BrailleSpinner';import toast from '../ui/Toast';import { listMediaCollections, createMediaCollection } from '../../services/api';import usePopoverPosition, { VIEWPORT_PADDING } from '../../hooks/usePopoverPosition.js';+import useClickOutside from '../../hooks/useClickOutside.js';+import useEscapeKey from '../../hooks/useEscapeKey.js';import { applyCollectionView } from '../../lib/mediaCollectionList.js';// Shared popover shell for the list-pick-or-create pickers:@@ -96,6 +98,7 @@ export default function CollectionPickerShell({const [creating, setCreating] = useState(false);const [newName, setNewName] = useState('');const {+ triggerRef,popoverRef: menuRef,style,} = usePopoverPosition({@@ -112,10 +115,14 @@ export default function CollectionPickerShell({contentDeps: [collectionsState, query, excludeId],});- // Parents may pass inline arrow handlers — read through a ref so the- // event-listener effect doesn't tear down on every parent render.- const onCloseRef = useRef(onClose);- useEffect(() => { onCloseRef.current = onClose; });+ // Close on outside-click / Escape — placement and scroll/resize reflow are+ // owned by usePopoverPosition. The menu is portaled to <body>, so the trigger+ // and the panel both have to count as "inside": that's the array form of+ // useClickOutside. `triggerRef` mirrors the caller's `anchorRef` when one was+ // passed, so this works in either mode. Both hooks read the handler through a+ // ref, so the inline arrow doesn't resubscribe on every parent render.+ useClickOutside([triggerRef, menuRef], open, () => onClose?.());+ useEscapeKey(open, () => onClose?.());// When the parent owns `collections`, mirror it into local state on each// change so the same render path works for both modes.@@ -151,25 +158,6 @@ export default function CollectionPickerShell({return orderItems(base, query);}, [collectionsState, query, excludeId, orderItems]);- // Close on outside-click / Escape — placement and scroll/resize reflow are- // owned by usePopoverPosition; this effect only handles dismissal.- useEffect(() => {- if (!open) return undefined;- const close = () => onCloseRef.current?.();- const onKey = (e) => { if (e.key === 'Escape') close(); };- const onAway = (e) => {- const onTrigger = anchorRef?.current?.contains(e.target);- const onMenu = menuRef.current?.contains(e.target);- if (!onTrigger && !onMenu) close();- };- document.addEventListener('keydown', onKey);- document.addEventListener('mousedown', onAway);- return () => {- document.removeEventListener('keydown', onKey);- document.removeEventListener('mousedown', onAway);- };- }, [open, anchorRef, menuRef]);-const updateCollections = useCallback((updater) => {setCollectionsState((prev) => {const next = typeof updater === 'function' ? updater(prev) : updater;diff --git a/client/src/components/shell/ShellImageDrop.jsx b/client/src/components/shell/ShellImageDrop.jsxindex 097d09f016..8c850d1632 100644--- a/client/src/components/shell/ShellImageDrop.jsx+++ b/client/src/components/shell/ShellImageDrop.jsx@@ -5,6 +5,7 @@ import FilePickerButton from '../ui/FilePickerButton';import FormField from '../ui/FormField';import usePopoverPosition, { VIEWPORT_PADDING } from '../../hooks/usePopoverPosition.js';import useEscapeKey from '../../hooks/useEscapeKey';+import useClickOutside from '../../hooks/useClickOutside.js';import { IMAGE_ACCEPT, validateImageFile } from '../../utils/fileUpload';import { formatBytes } from '../../utils/formatters';import toast from '../ui/Toast';@@ -123,22 +124,11 @@ export default function ShellImageDrop({ onSend, placement = 'below' }) {useEscapeKey(open, close);- // Click-outside is hand-rolled rather than `useClickOutside` (and NOT an- // oversight): that hook decides "outside" from a SINGLE ref, and this panel is- // portaled to <body>, so it isn't a descendant of the trigger container — the- // hook would treat every click on the textarea as outside and close the- // composer mid-typing. Two refs have to be consulted. Same shape as the other- // portaled popover, ThemeSwitcher.jsx.- useEffect(() => {- if (!open) return undefined;- const onMouseDown = (e) => {- const inTrigger = containerRef.current?.contains(e.target);- const inPanel = popoverRef.current?.contains(e.target);- if (!inTrigger && !inPanel) close();- };- document.addEventListener('mousedown', onMouseDown);- return () => document.removeEventListener('mousedown', onMouseDown);- }, [open, close, popoverRef]);+ // This panel is portaled to <body>, so it isn't a descendant of the trigger+ // container — a single-ref containment check would read every click on the+ // textarea as outside and close the composer mid-typing. Both refs have to+ // count as "inside", which is what the array form of useClickOutside does.+ useClickOutside([containerRef, popoverRef], open, close);return (<div className="relative shrink-0" ref={containerRef}>