Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix: relationship UIUX

Gus0ce2b169e05b
diff --git a/src/web/src/app/(app)/w/[slug]/home/page.tsx b/src/web/src/app/(app)/w/[slug]/home/page.tsx
index 9f385c9e8..21bfe02d6 100644
--- a/src/web/src/app/(app)/w/[slug]/home/page.tsx
+++ b/src/web/src/app/(app)/w/[slug]/home/page.tsx
@@ -270,7 +270,6 @@ function AgentCanvas() {
try {
const updated = await updateAgentLink(id, { instruction }, workspaceId);
setLinks((prev) => prev.map((l) => (l.id === id ? updated : l)));
- setSidecarOpen(false);
} catch {
toast.error("Failed to update link");
}
diff --git a/src/web/src/components/canvas/link-sidecar.tsx b/src/web/src/components/canvas/link-sidecar.tsx
index 684350c79..89500be91 100644
--- a/src/web/src/components/canvas/link-sidecar.tsx
+++ b/src/web/src/components/canvas/link-sidecar.tsx
@@ -1,19 +1,21 @@
"use client";
-import { useState, useEffect, useCallback } from "react";
+import { useState, useEffect, useCallback, useRef } from "react";
import type { Agent, AgentLink } from "@alook/shared";
import {
Sheet,
SheetContent,
SheetHeader,
SheetTitle,
- SheetFooter,
} from "@/components/ui/sheet";
import { Button } from "@/components/ui/button";
import { MarkdownEditor } from "@/components/ui/markdown-editor";
import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip";
import { Trash2 } from "lucide-react";
import { AvatarRenderer, parseAvatarUrl } from "@/components/avatar";
+import { ConfirmDialog } from "@/components/ui/confirm-dialog";
+
+const DEBOUNCE_MS = 500;
interface LinkSidecarProps {
open: boolean;
@@ -33,40 +35,70 @@ export function LinkSidecar({
onDelete,
}: LinkSidecarProps) {
const [instruction, setInstruction] = useState("");
+ const [confirmOpen, setConfirmOpen] = useState(false);
- const sourceAgent = agents.find(
- (a) => a.id === link?.source_agent_id,
- );
- const targetAgent = agents.find(
- (a) => a.id === link?.target_agent_id,
- );
+ const instructionRef = useRef(instruction);
+ instructionRef.current = instruction;
+ const savedInstructionRef = useRef("");
+ const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const linkRef = useRef(link);
+ linkRef.current = link;
+
+ const sourceAgent = agents.find((a) => a.id === link?.source_agent_id);
+ const targetAgent = agents.find((a) => a.id === link?.target_agent_id);
useEffect(() => {
- if (link) setInstruction(link.instruction);
+ if (link) {
+ setInstruction(link.instruction);
+ savedInstructionRef.current = link.instruction;
+ }
}, [link]);
- const isDirty = instruction !== (link?.instruction ?? "");
+ const flushSave = useCallback(() => {
+ const current = instructionRef.current;
+ const l = linkRef.current;
+ if (!l || current === savedInstructionRef.current) return;
+ savedInstructionRef.current = current;
+ onSave(l.id, current);
+ }, [onSave]);
- const handleSave = useCallback(() => {
- if (link && isDirty) {
- onSave(link.id, instruction);
- }
- }, [link, isDirty, instruction, onSave]);
+ const scheduleSave = useCallback(() => {
+ if (debounceRef.current) clearTimeout(debounceRef.current);
+ debounceRef.current = setTimeout(flushSave, DEBOUNCE_MS);
+ }, [flushSave]);
- const handleDelete = useCallback(() => {
- if (link) onDelete(link.id);
- }, [link, onDelete]);
+ const handleChange = useCallback(
+ (next: string) => {
+ setInstruction(next);
+ scheduleSave();
+ },
+ [scheduleSave],
+ );
+
+ useEffect(() => {
+ return () => {
+ if (debounceRef.current) clearTimeout(debounceRef.current);
+ flushSave();
+ };
+ }, [flushSave]);
- const handleKeyDown = useCallback(
- (e: React.KeyboardEvent) => {
- if (e.shiftKey && e.key === "Enter") {
- e.preventDefault();
- handleSave();
+ const handleOpenChange = useCallback(
+ (nextOpen: boolean) => {
+ if (!nextOpen) {
+ if (debounceRef.current) clearTimeout(debounceRef.current);
+ flushSave();
}
+ onOpenChange(nextOpen);
},
- [handleSave],
+ [onOpenChange, flushSave],
);
+ const handleDeleteConfirm = useCallback(() => {
+ if (debounceRef.current) clearTimeout(debounceRef.current);
+ if (link) onDelete(link.id);
+ setConfirmOpen(false);
+ }, [link, onDelete]);
+
const renderAvatar = (agent: Agent | undefined) => {
if (!agent) return null;
const avatarConfig = parseAvatarUrl(agent.avatar_url);
@@ -81,25 +113,41 @@ export function LinkSidecar({
};
return (
- <Sheet open={open} onOpenChange={onOpenChange}>
+ <Sheet open={open} onOpenChange={handleOpenChange}>
<SheetContent
side="right"
showCloseButton={false}
className="data-[side=right]:sm:inset-y-2 data-[side=right]:sm:right-2 data-[side=right]:sm:h-auto data-[side=right]:sm:rounded-xl data-[side=right]:sm:border data-[side=right]:sm:max-w-md"
- onKeyDownCapture={handleKeyDown}
>
<SheetTitle className="sr-only">
Edit relationship between {sourceAgent?.name} and {targetAgent?.name}
</SheetTitle>
<SheetHeader className="border-b px-5 py-4">
- <div className="flex items-center gap-2">
- <div className="flex items-center">
- {renderAvatar(sourceAgent)}
- <div className="-ml-2 ring-2 ring-background rounded-xl">
- {renderAvatar(targetAgent)}
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <div className="flex items-center">
+ {renderAvatar(sourceAgent)}
+ <div className="-ml-2 ring-2 ring-background rounded-xl">
+ {renderAvatar(targetAgent)}
+ </div>
</div>
</div>
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <Button
+ variant="ghost"
+ size="icon-sm"
+ className="text-destructive hover:text-destructive hover:bg-destructive/10"
+ onClick={() => setConfirmOpen(true)}
+ />
+ }
+ >
+ <Trash2 className="size-4" />
+ </TooltipTrigger>
+ <TooltipContent>Remove connection</TooltipContent>
+ </Tooltip>
</div>
<p className="text-sm font-medium">
{sourceAgent?.name ?? "Agent"} and {targetAgent?.name ?? "Agent"}
@@ -115,43 +163,20 @@ export function LinkSidecar({
contentType="markdown"
placeholder="Describe how these agents should collaborate. This instruction is shared with both agents when they receive tasks."
value={instruction}
- onChange={setInstruction}
+ onChange={handleChange}
minHeight="12rem"
agents={agents}
/>
</div>
-
- <SheetFooter className="border-t bg-muted/50 px-5 py-3 flex-row items-center justify-between">
- <Tooltip>
- <TooltipTrigger
- render={
- <Button
- variant="ghost"
- size="icon-sm"
- className="text-destructive hover:text-destructive hover:bg-destructive/10"
- onClick={handleDelete}
- />
- }
- >
- <Trash2 className="size-4" />
- </TooltipTrigger>
- <TooltipContent>Remove connection</TooltipContent>
- </Tooltip>
- <div className="flex items-center gap-2">
- <Button variant="ghost" onClick={() => onOpenChange(false)}>
- Cancel
- </Button>
- <Button
- onClick={handleSave}
- disabled={!isDirty}
- className={!isDirty ? "opacity-50 pointer-events-none" : ""}
- >
- Save
- <span className="text-[10px] opacity-50 ml-1">⇧ ⏎</span>
- </Button>
- </div>
- </SheetFooter>
</SheetContent>
+
+ <ConfirmDialog
+ open={confirmOpen}
+ onOpenChange={setConfirmOpen}
+ title="Remove connection"
+ description={`This will remove the connection between "${sourceAgent?.name ?? "Agent"}" and "${targetAgent?.name ?? "Agent"}".`}
+ onConfirm={handleDeleteConfirm}
+ />
</Sheet>
);
}