@devries-cameron20 / composio Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
tsx 416 lines 15.5 KB
Raw
1 'use client';
2
3 import { Radio, RefreshCw, Webhook, Check } from 'lucide-react';
4 import { useCallback, useEffect, useRef, useState } from 'react';
5
6 const LOGO_CDN = 'https://logos.composio.dev/api';
7
8 const SOURCES: { slug: string; name: string; kind: 'realtime' | 'polling' }[] = [
9 { slug: 'github', name: 'GitHub', kind: 'realtime' },
10 { slug: 'slack', name: 'Slack', kind: 'realtime' },
11 { slug: 'gmail', name: 'Gmail', kind: 'polling' },
12 ];
13
14 const RT_POOL = 5; // concurrent packets per realtime source
15 const POLL_POOL = 6; // packets that pile up in the polling box per batch
16 const OUT_POOL = 6; // packets streaming out to your webhook URL
17
18 // Polling cadence (seconds): packets fill in, wait, then release together.
19 const FILL_GAP = 0.45;
20 const T_RELEASE = 3.0;
21 const MOVE_DUR = 0.6;
22 const CYCLE = 3.9;
23
24 type Pt = { x: number; y: number };
25 type InGeom = { spawn: Pt; edge: Pt; end: Pt };
26 type Geom = { ins: InGeom[]; out: { spawn: Pt; end: Pt } } | null;
27 type Line = { x1: number; y1: number; x2: number; y2: number; dashed: boolean };
28
29 type RtSlot = { active: boolean; p: number; dur: number; wait: number };
30 type RtState = { slots: RtSlot[] };
31 type PollState = { t: number; offsets: Pt[] };
32
33 const rand = (a: number, b: number) => a + Math.random() * (b - a);
34 const easeOut = (p: number) => 1 - (1 - p) * (1 - p);
35
36 function makeRt(pool: number): RtState {
37 return {
38 slots: Array.from({ length: pool }, () => ({
39 active: false,
40 p: 0,
41 dur: rand(0.55, 1.0),
42 wait: rand(0, 1.3),
43 })),
44 };
45 }
46
47 function setCircle(el: SVGCircleElement | null, x: number, y: number, op: number, r: number) {
48 if (!el) return;
49 el.setAttribute('cx', String(x));
50 el.setAttribute('cy', String(y));
51 el.setAttribute('r', String(r));
52 el.style.opacity = String(op);
53 }
54
55 function stepRealtime(state: RtState, pool: (SVGCircleElement | null)[], from: Pt, to: Pt, dt: number) {
56 state.slots.forEach((s, k) => {
57 if (!s.active) {
58 s.wait -= dt;
59 if (s.wait <= 0) {
60 s.active = true;
61 s.p = 0;
62 s.dur = rand(0.5, 0.95);
63 } else {
64 setCircle(pool[k], 0, 0, 0, 0);
65 return;
66 }
67 }
68 s.p += dt / s.dur;
69 if (s.p >= 1) {
70 s.active = false;
71 s.wait = rand(0.12, 0.85); // jitter between emissions
72 setCircle(pool[k], 0, 0, 0, 0);
73 return;
74 }
75 const e = easeOut(s.p);
76 const op = Math.min(1, s.p * 7) * Math.min(1, (1 - s.p) * 7);
77 setCircle(pool[k], from.x + (to.x - from.x) * e, from.y + (to.y - from.y) * e, op, 2);
78 });
79 }
80
81 function stepPolling(state: PollState, pool: (SVGCircleElement | null)[], spawn: Pt, end: Pt, dt: number) {
82 state.t += dt;
83 if (state.t > CYCLE) state.t -= CYCLE;
84 for (let k = 0; k < pool.length; k++) {
85 const off = state.offsets[k] ?? { x: 0, y: 0 };
86 const sx = spawn.x + off.x;
87 const sy = spawn.y + off.y;
88 const fillAt = k * FILL_GAP;
89 if (state.t < fillAt) {
90 setCircle(pool[k], 0, 0, 0, 0); // hasn't arrived in the box yet
91 } else if (state.t < T_RELEASE) {
92 setCircle(pool[k], sx, sy, 0.9, 2); // waiting inside the box
93 } else {
94 const mp = (state.t - T_RELEASE) / MOVE_DUR;
95 if (mp >= 1) {
96 setCircle(pool[k], 0, 0, 0, 0);
97 } else {
98 const e = easeOut(mp);
99 setCircle(pool[k], sx + (end.x - sx) * e, sy + (end.y - sy) * e, 1, 2);
100 }
101 }
102 }
103 }
104
105 /**
106 * TriggersFlow — branded flow diagram for the Triggers concept page.
107 *
108 * Connected apps fan into the bare Composio logo (the hub), which fans out to
109 * the single webhook URL you configure. The animation models the actual
110 * behavior: realtime sources emit a packet that leaves almost as soon as it
111 * appears (with jitter), while the polling source accumulates packets inside
112 * its box and releases the whole batch on each poll tick. The point: however an
113 * event reaches Composio, there is only one destination you configure.
114 *
115 * Client component: geometry is measured from live layout; packets are driven
116 * by requestAnimationFrame. Honors prefers-reduced-motion (rails only).
117 */
118 export function TriggersFlow() {
119 const rootRef = useRef<HTMLDivElement>(null);
120 const appRefs = useRef<(HTMLLIElement | null)[]>([]);
121 const hubRef = useRef<HTMLSpanElement>(null);
122 const targetRef = useRef<HTMLDivElement>(null);
123
124 const geomRef = useRef<Geom>(null);
125 const inCircles = useRef<(SVGCircleElement | null)[][]>([[], [], []]);
126 const outCircles = useRef<(SVGCircleElement | null)[]>([]);
127 const simRef = useRef<{ ins: (RtState | PollState)[]; out: RtState } | null>(null);
128
129 const [rails, setRails] = useState<{ ins: Line[]; out: Line | null }>({ ins: [], out: null });
130 const [animate, setAnimate] = useState(true);
131 const [pollFiring, setPollFiring] = useState(false);
132 const firingRef = useRef(false);
133
134 const poolSize = (kind: 'realtime' | 'polling') => (kind === 'polling' ? POLL_POOL : RT_POOL);
135
136 const calc = useCallback(() => {
137 const root = rootRef.current;
138 const hub = hubRef.current;
139 const target = targetRef.current;
140 if (!root || !hub || !target) return;
141 const cr = root.getBoundingClientRect();
142 const hubR = hub.getBoundingClientRect();
143 const tgtR = target.getBoundingClientRect();
144 const hubLeft: Pt = { x: hubR.left - cr.left, y: hubR.top - cr.top + hubR.height / 2 };
145 const hubRight: Pt = { x: hubR.right - cr.left, y: hubLeft.y };
146 const tgtLeft: Pt = { x: tgtR.left - cr.left, y: tgtR.top - cr.top + tgtR.height / 2 };
147
148 const ins: InGeom[] = [];
149 const inLines: Line[] = [];
150 appRefs.current.forEach((el, i) => {
151 if (!el) return;
152 const r = el.getBoundingClientRect();
153 const y = r.top - cr.top + r.height / 2;
154 const edge: Pt = { x: r.right - cr.left, y };
155 // Spawn inside the card, in the clear right-hand lane (the extra pr on each
156 // row), so packets read as coming from the app without covering its label.
157 const spawn: Pt = { x: edge.x - 13, y };
158 // Fan the convergence points slightly so the lines don't knot at the logo.
159 const end: Pt = { x: hubLeft.x - 2, y: hubLeft.y + (i - 1) * 5 };
160 ins[i] = { spawn, edge, end };
161 inLines[i] = { x1: edge.x, y1: edge.y, x2: end.x, y2: end.y, dashed: SOURCES[i]?.kind === 'polling' };
162 });
163
164 geomRef.current = { ins, out: { spawn: hubRight, end: tgtLeft } };
165 setRails({ ins: inLines, out: { x1: hubRight.x, y1: hubRight.y, x2: tgtLeft.x, y2: tgtLeft.y, dashed: false } });
166 }, []);
167
168 useEffect(() => {
169 simRef.current = {
170 ins: SOURCES.map((s) =>
171 s.kind === 'realtime'
172 ? makeRt(RT_POOL)
173 : { t: 0, offsets: Array.from({ length: POLL_POOL }, () => ({ x: rand(-6, 3), y: rand(-7, 7) })) }
174 ),
175 out: makeRt(OUT_POOL),
176 };
177
178 calc();
179 const t = setTimeout(calc, 120);
180 window.addEventListener('resize', calc);
181 const root = rootRef.current;
182 const ro = root ? new ResizeObserver(() => calc()) : null;
183 if (root && ro) ro.observe(root);
184
185 const mq = window.matchMedia('(prefers-reduced-motion: reduce)');
186 setAnimate(!mq.matches);
187 const onMq = () => setAnimate(!mq.matches);
188 mq.addEventListener('change', onMq);
189
190 let raf = 0;
191 let last: number | null = null;
192 const loop = (ts: number) => {
193 if (last === null) last = ts;
194 const dt = Math.min(0.05, (ts - last) / 1000);
195 last = ts;
196 const geom = geomRef.current;
197 const sim = simRef.current;
198 if (geom && sim && !mq.matches) {
199 SOURCES.forEach((src, i) => {
200 const g = geom.ins[i];
201 const pool = inCircles.current[i];
202 if (!g || !pool) return;
203 if (src.kind === 'realtime') stepRealtime(sim.ins[i] as RtState, pool, g.spawn, g.end, dt);
204 else stepPolling(sim.ins[i] as PollState, pool, g.spawn, g.end, dt);
205 });
206 stepRealtime(sim.out, outCircles.current, geom.out.spawn, geom.out.end, dt);
207
208 // Light the polling tag while the batch is firing, with a buffer either side.
209 const pollIdx = SOURCES.findIndex((s) => s.kind === 'polling');
210 if (pollIdx >= 0) {
211 const ps = sim.ins[pollIdx] as PollState;
212 const BUF = 0.3;
213 const firing = ps.t >= T_RELEASE - BUF && ps.t <= T_RELEASE + MOVE_DUR + BUF;
214 if (firing !== firingRef.current) {
215 firingRef.current = firing;
216 setPollFiring(firing);
217 }
218 }
219 }
220 raf = requestAnimationFrame(loop);
221 };
222 raf = requestAnimationFrame(loop);
223
224 return () => {
225 clearTimeout(t);
226 cancelAnimationFrame(raf);
227 window.removeEventListener('resize', calc);
228 ro?.disconnect();
229 mq.removeEventListener('change', onMq);
230 };
231 }, [calc]);
232
233 return (
234 <div className="not-prose my-6 overflow-hidden rounded-sm border border-fd-border bg-fd-background">
235 {/* header strip */}
236 <div className="flex items-center justify-between border-b border-fd-border px-3 py-1.5 font-mono text-[10px] uppercase tracking-[0.06em] text-fd-foreground/45">
237 <span>triggers.delivery</span>
238 <span className="inline-flex items-center gap-1.5 text-fd-foreground/55">
239 <span className="relative inline-flex">
240 <span className="size-1.5 rounded-full bg-[var(--composio-brand)]" />
241 <span className="absolute inset-0 animate-ping rounded-full bg-[var(--composio-brand)] opacity-60" />
242 </span>
243 one webhook URL
244 </span>
245 </div>
246
247 <div ref={rootRef} className="relative flex flex-col gap-5 p-4 md:block md:h-[300px] md:p-6">
248 {/* ── connector + packet overlay (md and up) ── */}
249 <svg
250 aria-hidden="true"
251 className="pointer-events-none absolute inset-0 z-20 hidden size-full overflow-visible md:block"
252 fill="none"
253 style={{ color: 'var(--composio-brand)' }}
254 >
255 {rails.ins.map((l, i) => (
256 <line
257 key={SOURCES[i]?.slug ?? i}
258 opacity={0.18}
259 stroke="currentColor"
260 strokeDasharray={l.dashed ? '3 4' : undefined}
261 strokeWidth={1.25}
262 x1={l.x1}
263 x2={l.x2}
264 y1={l.y1}
265 y2={l.y2}
266 />
267 ))}
268 {rails.out && (
269 <line opacity={0.18} stroke="currentColor" strokeWidth={1.25} x1={rails.out.x1} x2={rails.out.x2} y1={rails.out.y1} y2={rails.out.y2} />
270 )}
271 {animate &&
272 SOURCES.map((src, i) => (
273 <g key={src.slug} fill="currentColor">
274 {Array.from({ length: poolSize(src.kind) }).map((_, k) => (
275 <circle
276 key={k}
277 cx={0}
278 cy={0}
279 r={2}
280 ref={(el) => {
281 inCircles.current[i][k] = el;
282 }}
283 style={{ opacity: 0 }}
284 />
285 ))}
286 </g>
287 ))}
288 {animate && (
289 <g fill="currentColor">
290 {Array.from({ length: OUT_POOL }).map((_, k) => (
291 <circle
292 key={k}
293 cx={0}
294 cy={0}
295 r={2}
296 ref={(el) => {
297 outCircles.current[k] = el;
298 }}
299 style={{ opacity: 0 }}
300 />
301 ))}
302 </g>
303 )}
304 </svg>
305
306 {/* ── Connected apps ── */}
307 <div className="relative z-10 md:absolute md:top-1/2 md:left-0 md:w-[248px] md:-translate-y-1/2">
308 <p className="mb-2 font-mono text-[10px] font-medium uppercase tracking-[0.07em] text-fd-foreground/55">
309 Connected apps
310 </p>
311 <ul className="overflow-hidden rounded-sm border border-fd-border bg-fd-card">
312 {SOURCES.map((app, i) => (
313 <li
314 key={app.slug}
315 ref={(el) => {
316 appRefs.current[i] = el;
317 }}
318 className={
319 'flex items-center gap-2 py-2.5 pl-2.5 pr-6' +
320 (i < SOURCES.length - 1 ? ' border-b border-fd-border' : '')
321 }
322 >
323 <img
324 alt=""
325 aria-hidden="true"
326 className="size-4 object-contain"
327 draggable={false}
328 src={`${LOGO_CDN}/${app.slug}`}
329 />
330 <span className="text-[12px] text-fd-foreground/80">{app.name}</span>
331 <KindTag kind={app.kind} active={app.kind === 'polling' && pollFiring} />
332 </li>
333 ))}
334 </ul>
335 </div>
336
337 {/* ── Composio (bare logo hub, no card) ── */}
338 <div className="relative z-10 flex justify-center md:absolute md:top-1/2 md:left-1/2 md:block md:-translate-x-1/2 md:-translate-y-1/2 md:text-center">
339 <span ref={hubRef} className="inline-flex flex-col items-center">
340 <img
341 alt="Composio"
342 className="h-7 w-auto object-contain dark:hidden"
343 draggable={false}
344 src="/Composio Logo.svg"
345 />
346 <img
347 alt="Composio"
348 className="hidden h-7 w-auto object-contain dark:block"
349 draggable={false}
350 src="/Composio Logo Dark.svg"
351 />
352 </span>
353 </div>
354
355 {/* ── Your application ── */}
356 <div className="relative z-10 md:absolute md:top-1/2 md:right-0 md:w-[248px] md:-translate-y-1/2">
357 <p className="mb-2 font-mono text-[10px] font-medium uppercase tracking-[0.07em] text-fd-foreground/55">
358 Your application
359 </p>
360 <div ref={targetRef} className="border border-fd-border bg-fd-card p-3">
361 <div className="flex items-center gap-2">
362 <span className="flex size-7 items-center justify-center rounded-sm bg-[var(--composio-brand)]/10 text-[var(--composio-brand)]">
363 <Webhook className="size-3.5" aria-hidden="true" />
364 </span>
365 <div className="min-w-0">
366 <div className="truncate text-[12px] font-medium leading-tight text-fd-foreground">
367 Your webhook URL
368 </div>
369 <div className="truncate font-mono text-[10px] text-fd-foreground/45">
370 POST /webhooks/composio
371 </div>
372 </div>
373 </div>
374 <p className="mt-2.5 inline-flex items-center gap-1.5 text-[11px] leading-snug text-fd-foreground/55">
375 <Check className="size-3 text-[var(--composio-brand)]" aria-hidden="true" />
376 One URL receives every event.
377 </p>
378 </div>
379 </div>
380 </div>
381
382 {/* footer caption */}
383 <div className="border-t border-fd-border px-3 py-2 text-center font-mono text-[10px] text-fd-foreground/45">
384 app event <Arrow /> Composio <Arrow /> your webhook URL
385 </div>
386 </div>
387 );
388 }
389
390 function KindTag({ kind, active = false }: { kind: 'realtime' | 'polling'; active?: boolean }) {
391 const realtime = kind === 'realtime';
392 const lit = realtime || active;
393 return (
394 <span
395 className={
396 'ml-auto inline-flex items-center gap-1 font-mono text-[9px] uppercase tracking-[0.05em] transition-colors duration-300 ' +
397 (lit ? 'text-[var(--composio-brand)]' : 'text-fd-foreground/40')
398 }
399 >
400 {realtime ? (
401 <Radio className="size-2.5" aria-hidden="true" />
402 ) : (
403 <RefreshCw className={'size-2.5' + (active ? ' animate-spin' : '')} aria-hidden="true" />
404 )}
405 {kind}
406 </span>
407 );
408 }
409
410 function Arrow() {
411 return (
412 <span aria-hidden="true" className="text-[var(--composio-brand)]">
413 {'→'}
414 </span>
415 );
416 }