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 22, 04:30 UTC
fix(docs): improve PromptBanner robustness
- Await clipboard.writeText and only show success on fulfilled promise - Clean up setTimeout on unmount and debounce rapid clicks via timerRef - Extract pure DOM helpers (detectLang, getTabLabel, extractSteps) outside component - Eliminate duplicate querySelector('code') call in extractSteps loop Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sushmitha Mallesh committed
Mar 7, 2026 at 00:05 UTC
7d366f90e26a5f10b6749cdfd527b46cffdc0e9e
1 file changed
+76
-78
docs/components/quickstart/prompt-banner.tsx
+76
-78
@@ -1,97 +1,91 @@
1
'use client';
2
3
-import { useState, useRef, type ReactNode } from 'react';
3
+import { useState, useRef, useEffect, type ReactNode } from 'react';
4
import { Copy, Check } from 'lucide-react';
5
6
-interface PromptBannerProps {
7
- children: ReactNode;
6
+function detectLang(pre: Element, code: Element | null): string {
7
+ for (const el of [pre, code]) {
8
+ const lang = el?.getAttribute('data-language') ?? el?.getAttribute('data-lang');
9
+ if (lang) return lang;
10
+ }
11
+ const cls = Array.from(code?.classList ?? []).find(c => c.startsWith('language-'));
12
+ if (cls) return cls.replace('language-', '');
13
+ const text = code?.textContent ?? '';
14
+ if (/^(pip|npm|npx|bun|yarn|pnpm) /.test(text.trim())) return 'bash';
15
+ if (text.includes('from dotenv') || text.includes('import asyncio')) return 'python';
16
+ if (text.includes('from "') || text.includes('from "@')) return 'typescript';
17
+ if (text.includes('COMPOSIO_API_KEY=')) return 'bash';
18
+ return '';
19
}
20
10
-export function PromptBanner({ children }: PromptBannerProps) {
11
- const [copied, setCopied] = useState(false);
12
- const contentRef = useRef<HTMLDivElement>(null);
13
-
14
- const detectLang = (pre: Element): string => {
15
- const code = pre.querySelector('code');
16
- // Check data-language attribute (set by Shiki)
17
- for (const el of [pre, code]) {
18
- const lang = el?.getAttribute('data-language') ?? el?.getAttribute('data-lang');
19
- if (lang) return lang;
20
- }
21
- // Check class="language-xxx"
22
- const cls = Array.from(code?.classList ?? []).find(c => c.startsWith('language-'));
23
- if (cls) return cls.replace('language-', '');
24
- // Guess from content
25
- const text = code?.textContent ?? '';
26
- if (/^(pip|npm|npx|bun|yarn|pnpm) /.test(text.trim())) return 'bash';
27
- if (text.includes('from dotenv') || text.includes('import asyncio')) return 'python';
28
- if (text.includes('from "') || text.includes('from "@')) return 'typescript';
29
- if (text.includes('COMPOSIO_API_KEY=')) return 'bash';
30
- return '';
31
- };
32
-
33
- const getTabLabel = (pre: Element, step: Element): string => {
34
- // Find the nearest tab panel that is INSIDE the step (not an ancestor)
35
- let el: Element | null = pre.parentElement;
36
- while (el && el !== step) {
37
- if (el.getAttribute('role') === 'tabpanel') {
38
- // Found a tab panel inside the step — find its trigger button
39
- const tablist = el.parentElement?.querySelector('[role="tablist"]');
40
- if (tablist) {
41
- const tabs = tablist.querySelectorAll('[role="tab"]');
42
- const panels = el.parentElement?.querySelectorAll(':scope > [role="tabpanel"]');
43
- if (panels) {
44
- const idx = Array.from(panels).indexOf(el);
45
- if (idx >= 0 && tabs[idx]) return tabs[idx].textContent?.trim() ?? '';
46
- }
21
+function getTabLabel(pre: Element, step: Element): string {
22
+ let el: Element | null = pre.parentElement;
23
+ while (el && el !== step) {
24
+ if (el.getAttribute('role') === 'tabpanel') {
25
+ const tablist = el.parentElement?.querySelector('[role="tablist"]');
26
+ if (tablist) {
27
+ const tabs = tablist.querySelectorAll('[role="tab"]');
28
+ const panels = el.parentElement?.querySelectorAll(':scope > [role="tabpanel"]');
29
+ if (panels) {
30
+ const idx = Array.from(panels).indexOf(el);
31
+ if (idx >= 0 && tabs[idx]) return tabs[idx].textContent?.trim() ?? '';
32
}
48
- return '';
33
}
50
- el = el.parentElement;
34
+ return '';
35
}
52
- return '';
53
- };
36
+ el = el.parentElement;
37
+ }
38
+ return '';
39
+}
40
55
- const extractSteps = (stepsEl: Element): string => {
56
- const parts: string[] = [];
57
- const steps = stepsEl.querySelectorAll('.fd-step');
58
-
59
- steps.forEach((step) => {
60
- // Step title — try multiple selectors
61
- const title = step.querySelector('h3, h4, h2, [class*="step-title"], [class*="StepTitle"]');
62
- const titleText = title?.textContent?.trim();
63
- if (titleText) parts.push(`### ${titleText}`);
64
-
65
- // Extract code blocks with tab labels
66
- const pres = step.querySelectorAll('pre');
67
- pres.forEach((pre) => {
68
- const code = pre.querySelector('code')?.textContent?.trim() ?? '';
69
- if (!code) return;
70
-
71
- const lang = detectLang(pre);
72
- const tabLabel = getTabLabel(pre, step);
73
-
74
- if (tabLabel) {
75
- parts.push(`**${tabLabel}**\n\n\`\`\`${lang}\n${code}\n\`\`\``);
76
- } else {
77
- parts.push(`\`\`\`${lang}\n${code}\n\`\`\``);
78
- }
79
- });
41
+function extractSteps(stepsEl: Element): string {
42
+ const parts: string[] = [];
43
+ const steps = stepsEl.querySelectorAll('.fd-step');
44
+
45
+ steps.forEach((step) => {
46
+ const title = step.querySelector('h3, h4, h2, [class*="step-title"], [class*="StepTitle"]');
47
+ const titleText = title?.textContent?.trim();
48
+ if (titleText) parts.push(`### ${titleText}`);
49
+
50
+ const pres = step.querySelectorAll('pre');
51
+ pres.forEach((pre) => {
52
+ const codeEl = pre.querySelector('code');
53
+ const code = codeEl?.textContent?.trim() ?? '';
54
+ if (!code) return;
55
+
56
+ const lang = detectLang(pre, codeEl);
57
+ const tabLabel = getTabLabel(pre, step);
58
+
59
+ if (tabLabel) {
60
+ parts.push(`**${tabLabel}**\n\n\`\`\`${lang}\n${code}\n\`\`\``);
61
+ } else {
62
+ parts.push(`\`\`\`${lang}\n${code}\n\`\`\``);
63
+ }
64
});
65
+ });
66
82
- return parts.join('\n\n');
83
- };
67
+ return parts.join('\n\n');
68
+}
69
+
70
+interface PromptBannerProps {
71
+ children: ReactNode;
72
+}
73
85
- const handleCopy = () => {
74
+export function PromptBanner({ children }: PromptBannerProps) {
75
+ const [copied, setCopied] = useState(false);
76
+ const contentRef = useRef<HTMLDivElement>(null);
77
+ const timerRef = useRef<ReturnType<typeof setTimeout>>(undefined);
78
+
79
+ useEffect(() => () => { clearTimeout(timerRef.current); }, []);
80
+
81
+ const handleCopy = async () => {
82
const fullMeta = contentRef.current?.innerText ?? '';
83
88
- // Split prompt into context (before "Key concepts") and rules (from "Key concepts" onward)
84
const splitMarker = 'Key concepts';
85
const splitIdx = fullMeta.indexOf(splitMarker);
86
const context = splitIdx > 0 ? fullMeta.slice(0, splitIdx).trim() : fullMeta;
87
const rules = splitIdx > 0 ? fullMeta.slice(splitIdx).trim() : '';
88
94
- // Walk siblings after the banner to find the .fd-steps element
89
let sibling = contentRef.current?.closest('.not-prose')?.nextElementSibling;
90
let stepsText = '';
91
while (sibling) {
@@ -102,15 +96,19 @@ export function PromptBanner({ children }: PromptBannerProps) {
96
sibling = sibling.nextElementSibling;
97
}
98
105
- // Order: context → code → rules
99
const parts = [context];
100
if (stepsText) parts.push(`## Code\n\n${stepsText}`);
101
if (rules) parts.push(rules);
102
const fullPrompt = parts.join('\n\n');
103
111
- navigator.clipboard.writeText(fullPrompt);
112
- setCopied(true);
113
- setTimeout(() => setCopied(false), 2000);
104
+ try {
105
+ await navigator.clipboard.writeText(fullPrompt);
106
+ clearTimeout(timerRef.current);
107
+ setCopied(true);
108
+ timerRef.current = setTimeout(() => setCopied(false), 2000);
109
+ } catch {
110
+ // Clipboard write failed (e.g. permission denied) — don't show success
111
+ }
112
};
113
114
return (