Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

refactor: use Base UI Popover for folder-popover (#177)

Generf906903f3ebc
diff --git a/src/web/src/components/sidebar/folder-popover.tsx b/src/web/src/components/sidebar/folder-popover.tsx
index d85b3af60..464c710e0 100644
--- a/src/web/src/components/sidebar/folder-popover.tsx
+++ b/src/web/src/components/sidebar/folder-popover.tsx
@@ -1,6 +1,5 @@
"use client";
-import { useState, useRef, useEffect, useCallback } from "react";
import type { Agent } from "@alook/shared";
import type { AgentFolder } from "@/hooks/use-agent-folders";
import { AgentSidebarButton } from "./agent-sidebar-button";
@@ -22,6 +21,7 @@ import {
arrayMove,
} from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
+import { Popover } from "@base-ui/react/popover";
function SortablePopoverAgent({
id,
@@ -76,48 +76,6 @@ export function FolderPopover({
onReorder: (orderedAgentIds: string[]) => void;
onClose: () => void;
}) {
- const popoverRef = useRef<HTMLDivElement>(null);
- const [position, setPosition] = useState<{ left: number; top: number } | null>(null);
-
- const updatePosition = useCallback(() => {
- if (!anchorRef) return;
- const rect = anchorRef.getBoundingClientRect();
- setPosition({ left: rect.right + 8, top: rect.top });
- }, [anchorRef]);
-
- useEffect(() => {
- updatePosition();
- }, [updatePosition]);
-
- useEffect(() => {
- function handleClickOutside(e: MouseEvent) {
- const target = e.target as Node;
- if (
- popoverRef.current &&
- !popoverRef.current.contains(target) &&
- anchorRef &&
- !anchorRef.contains(target) &&
- !(target as HTMLElement).closest?.('[data-slot="context-menu-content"]')
- ) {
- onClose();
- }
- }
- function handleEscape(e: KeyboardEvent) {
- if (e.key === "Escape") onClose();
- }
- const scrollParent = anchorRef?.closest("[class*='overflow']") ?? window;
- document.addEventListener("mousedown", handleClickOutside);
- document.addEventListener("keydown", handleEscape);
- window.addEventListener("resize", updatePosition);
- scrollParent.addEventListener("scroll", updatePosition, { passive: true });
- return () => {
- document.removeEventListener("mousedown", handleClickOutside);
- document.removeEventListener("keydown", handleEscape);
- window.removeEventListener("resize", updatePosition);
- scrollParent.removeEventListener("scroll", updatePosition);
- };
- }, [anchorRef, onClose, updatePosition]);
-
const sensors = useSensors(
useSensor(PointerSensor, { activationConstraint: { distance: 5 } })
);
@@ -134,60 +92,64 @@ export function FolderPopover({
onReorder(arrayMove(folder.agentIds, oldIndex, newIndex));
}
- if (!anchorRef || !position) return null;
+ if (!anchorRef) return null;
return (
- <div
- ref={popoverRef}
- className="fixed z-50 bg-popover border rounded-xl shadow-md p-1.5 animate-in fade-in-0 zoom-in-95"
- style={{
- left: position.left,
- top: position.top,
- }}
- >
- <DndContext
- sensors={sensors}
- collisionDetection={closestCenter}
- modifiers={[restrictToVerticalAxis]}
- onDragEnd={handleDragEnd}
- >
- <SortableContext
- items={folder.agentIds}
- strategy={verticalListSortingStrategy}
+ <Popover.Root open onOpenChange={(open) => { if (!open) onClose(); }}>
+ <Popover.Portal>
+ <Popover.Positioner
+ anchor={anchorRef}
+ side="inline-end"
+ sideOffset={8}
+ align="start"
>
- <div className="flex flex-col gap-1">
- {folderAgents.map((agent) => (
- <SortablePopoverAgent key={agent.id} id={agent.id}>
- <AgentSidebarButton
- agent={agent}
- isActive={activeAgentId === agent.id}
- isPinned={false}
- isOnline={isOnline}
- taskCount={taskCounts[agent.id] ?? 0}
- hidePin
- onClick={() => {
- onAgentClick(agent.id);
- onClose();
- }}
- onPin={() => {
- onPinAgent(agent.id);
- onClose();
- }}
- onUnpin={() => {}}
- extraContextMenuItems={
- <ContextMenuItem
- onClick={() => onRemoveFromFolder(agent.id)}
- >
- <PinOffIcon className="size-3.5 mr-1.5" />
- Remove from group
- </ContextMenuItem>
- }
- />
- </SortablePopoverAgent>
- ))}
- </div>
- </SortableContext>
- </DndContext>
- </div>
+ <Popover.Popup className="z-50 bg-popover border rounded-xl shadow-md p-1.5 animate-in fade-in-0 zoom-in-95 outline-none">
+ <DndContext
+ sensors={sensors}
+ collisionDetection={closestCenter}
+ modifiers={[restrictToVerticalAxis]}
+ onDragEnd={handleDragEnd}
+ >
+ <SortableContext
+ items={folder.agentIds}
+ strategy={verticalListSortingStrategy}
+ >
+ <div className="flex flex-col gap-1">
+ {folderAgents.map((agent) => (
+ <SortablePopoverAgent key={agent.id} id={agent.id}>
+ <AgentSidebarButton
+ agent={agent}
+ isActive={activeAgentId === agent.id}
+ isPinned={false}
+ isOnline={isOnline}
+ taskCount={taskCounts[agent.id] ?? 0}
+ hidePin
+ onClick={() => {
+ onAgentClick(agent.id);
+ onClose();
+ }}
+ onPin={() => {
+ onPinAgent(agent.id);
+ onClose();
+ }}
+ onUnpin={() => {}}
+ extraContextMenuItems={
+ <ContextMenuItem
+ onClick={() => onRemoveFromFolder(agent.id)}
+ >
+ <PinOffIcon className="size-3.5 mr-1.5" />
+ Remove from group
+ </ContextMenuItem>
+ }
+ />
+ </SortablePopoverAgent>
+ ))}
+ </div>
+ </SortableContext>
+ </DndContext>
+ </Popover.Popup>
+ </Popover.Positioner>
+ </Popover.Portal>
+ </Popover.Root>
);
}