Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
tsx 177 lines 6.64 KB
Raw
1 'use client';
2
3 import { User } from 'lucide-react';
4 import { useCallback, useEffect, useRef, useState } from 'react';
5
6 const LOGO_CDN = 'https://logos.composio.dev/api';
7
8 const USER_IDENTITY = {
9 name: 'Ada Chen',
10 userId: 'usr_9x2kLm7',
11 };
12
13 const ACCOUNTS = [
14 { slug: 'slack', name: 'Slack', account: 'acme-workspace' },
15 { slug: 'gmail', name: 'Gmail', account: 'ada@acme.com' },
16 { slug: 'linear', name: 'Linear', account: 'acme' },
17 ];
18
19 /**
20 * Orthogonal elbow connector — leaves `(sx, sy)` horizontally, steps
21 * vertically at the midpoint, then runs horizontally into `(ex, ey)` with
22 * rounded corners. Adapted from `connection-refresh-visual.tsx#elbowPath`
23 * but taking explicit endpoints so multiple lines out of the same hub can
24 * exit at staggered y-coordinates and not visually merge into a single
25 * thicker line where they share the initial horizontal run.
26 */
27 function elbowPath(sx: number, sy: number, ex: number, ey: number, r = 8): string {
28 const midX = sx + (ex - sx) * 0.5;
29 const dy = Math.sign(ey - sy) || 1;
30 const rr = Math.min(r, Math.abs(ex - sx) / 2, Math.abs(ey - sy) / 2);
31 if (rr < 1) return `M ${sx} ${sy} L ${ex} ${ey}`;
32 return (
33 `M ${sx} ${sy} L ${midX - rr} ${sy} ` +
34 `Q ${midX} ${sy} ${midX} ${sy + dy * rr} ` +
35 `L ${midX} ${ey - dy * rr} ` +
36 `Q ${midX} ${ey} ${midX + rr} ${ey} ` +
37 `L ${ex} ${ey}`
38 );
39 }
40
41 /**
42 * Auth-and-context mock for the docs Welcome features grid.
43 *
44 * One user identity card on the left is wired to three connected-account
45 * cards on the right via neutral-grey elbow connectors. Paths are computed
46 * from live DOM geometry (getBoundingClientRect + ResizeObserver) so the
47 * wires meet the hub and cards precisely at every breakpoint. All surfaces
48 * use bg-fd-card so nothing reads as recessed against the mock's own card,
49 * and logos render inline without a white swatch behind them.
50 *
51 * Widths are proportional (`w-[36%]` / `w-[52%]`, capped) rather than fixed,
52 * which reserves the remaining ~12% as horizontal run for the elbows at every
53 * size. Fixed widths do not work here: this pane is *not* monotonic in the
54 * viewport — it is ~404px at 1280px wide but only ~242px at 640px, where the
55 * feature grid goes two-column. A fixed pair that fits phones overflows at
56 * `sm`, and `ex === sx` collapses every wire into `elbowPath`'s straight-line
57 * fallback (the middle one to a zero-length, invisible path). The account
58 * label hides by container query for the same reason — viewport breakpoints
59 * would gate on the wrong axis.
60 */
61 export function AuthDiagram() {
62 const rootRef = useRef<HTMLDivElement>(null);
63 const hubRef = useRef<HTMLDivElement>(null);
64 const cardRefs = useRef<(HTMLDivElement | null)[]>([]);
65 const [paths, setPaths] = useState<string[]>([]);
66
67 const calc = useCallback(() => {
68 const root = rootRef.current;
69 const hub = hubRef.current;
70 if (!root || !hub) return;
71 const cr = root.getBoundingClientRect();
72 const from = hub.getBoundingClientRect();
73 const hubX = from.right - cr.left;
74 const hubYCenter = from.top + from.height / 2 - cr.top;
75 const targets = cardRefs.current.filter(
76 (el): el is HTMLDivElement => Boolean(el)
77 );
78 // Space the exit points down the hub's right edge (~7px apart for 3
79 // targets) so lines leave the hub as three distinct wires instead of
80 // stacking on the same y and reading as one thick line before the elbow.
81 const STAGGER = 7;
82 const next = targets.map((ref, i) => {
83 const to = ref.getBoundingClientRect();
84 const stagger = (i - (targets.length - 1) / 2) * STAGGER;
85 const sy = hubYCenter + stagger;
86 const ex = to.left - cr.left;
87 const ey = to.top + to.height / 2 - cr.top;
88 return elbowPath(hubX, sy, ex, ey);
89 });
90 setPaths(next);
91 }, []);
92
93 useEffect(() => {
94 calc();
95 // Re-measure once fonts/images settle so the initial paths land on the
96 // final card positions instead of pre-load ones.
97 const t = window.setTimeout(calc, 120);
98 const root = rootRef.current;
99 const ro = root ? new ResizeObserver(() => calc()) : null;
100 if (root && ro) ro.observe(root);
101 // ResizeObserver covers layout changes, but keep the window listener too:
102 // RO delivery is throttled in backgrounded documents, so a tab resized
103 // while hidden can surface with stale paths. Matches
104 // `connection-refresh-visual.tsx`.
105 window.addEventListener('resize', calc);
106 return () => {
107 window.clearTimeout(t);
108 window.removeEventListener('resize', calc);
109 ro?.disconnect();
110 };
111 }, [calc]);
112
113 return (
114 <div
115 ref={rootRef}
116 className="@container relative flex h-full w-full items-center justify-between overflow-hidden"
117 >
118 <svg
119 aria-hidden="true"
120 className="pointer-events-none absolute inset-0 z-0 h-full w-full text-fd-foreground/30"
121 fill="none"
122 >
123 {paths.map((d, i) => (
124 <path
125 d={d}
126 key={ACCOUNTS[i]?.slug ?? i}
127 stroke="currentColor"
128 strokeWidth={1}
129 />
130 ))}
131 </svg>
132
133 <div
134 ref={hubRef}
135 className="relative z-10 flex w-[36%] max-w-36 shrink-0 items-center gap-2 rounded-[6px] border border-fd-border bg-fd-card px-2.5 py-2"
136 >
137 <span className="flex size-6 shrink-0 items-center justify-center rounded-full bg-fd-muted">
138 <User aria-hidden="true" className="size-3.5 text-fd-foreground/60" />
139 </span>
140 <div className="flex min-w-0 flex-col leading-tight">
141 <span className="truncate font-mono text-[11px] text-fd-foreground/85">
142 {USER_IDENTITY.name}
143 </span>
144 <span className="truncate font-mono text-[9.5px] text-fd-foreground/45">
145 {USER_IDENTITY.userId}
146 </span>
147 </div>
148 </div>
149
150 <div className="relative z-10 flex w-[52%] max-w-56 shrink-0 flex-col gap-2">
151 {ACCOUNTS.map((app, i) => (
152 <div
153 className="flex w-full items-center gap-2 rounded-[6px] border border-fd-border bg-fd-card px-2.5 py-2"
154 key={app.slug}
155 ref={el => {
156 cardRefs.current[i] = el;
157 }}
158 >
159 <img
160 alt=""
161 aria-hidden="true"
162 className="size-4 shrink-0 object-contain"
163 draggable={false}
164 src={`${LOGO_CDN}/${app.slug}`}
165 />
166 <span className="truncate font-mono text-[11px] text-fd-foreground/85">
167 {app.name}
168 </span>
169 <span className="ml-auto hidden truncate font-mono text-[9.5px] text-fd-foreground/45 @[340px]:inline">
170 {app.account}
171 </span>
172 </div>
173 ))}
174 </div>
175 </div>
176 );
177 }