Spaces:
Running

File size: 3,960 Bytes
6f5f15e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { useState } from 'react';
import { Check, Copy, Terminal, X } from 'lucide-react';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { buildConnectSnippets, type ConnectSnippet } from '@/lib/commands';

export function ConnectPanel({
  mcpUrl,
  defaultTab = 'uvx',
  onClose,
}: {
  mcpUrl: string;
  defaultTab?: ConnectSnippet['id'];
  onClose: () => void;
}) {
  const snippets = buildConnectSnippets(mcpUrl);
  const [activeTab, setActiveTab] = useState<string>(defaultTab);
  const [copiedId, setCopiedId] = useState<string | null>(null);

  const copyText = async (id: string, text: string, label: string) => {
    try {
      await navigator.clipboard.writeText(text);
      setCopiedId(id);
      toast.success(`Copied ${label}`);
      window.setTimeout(() => setCopiedId((cur) => (cur === id ? null : cur)), 1800);
    } catch {
      toast.error('Clipboard copy failed');
    }
  };

  return (
    <section className="border-b border-line bg-panel/90">
      <div className="mx-auto max-w-[1440px] px-4 py-3.5">
        <div className="flex flex-wrap items-center justify-between gap-2 pb-3">
          <div className="flex items-center gap-2">
            <Terminal className="h-3.5 w-3.5 text-push" />
            <span className="text-xs font-semibold uppercase tracking-wider text-muted">
              Connect an MCP Client
            </span>
          </div>

          <div className="flex flex-1 items-center justify-end gap-2 min-w-[240px]">
            <div className="flex max-w-md flex-1 items-center justify-between gap-2 rounded-lg border border-line bg-panel-2/80 px-2.5 py-1">
              <span className="truncate font-mono text-[11px] text-fg">{mcpUrl}</span>
              <Button
                variant="ghost"
                size="sm"
                className="h-6 px-2 text-[11px]"
                onClick={() => void copyText('mcp-url', mcpUrl, 'MCP URL')}
              >
                {copiedId === 'mcp-url' ? (
                  <Check className="h-3 w-3 text-push" />
                ) : (
                  <Copy className="h-3 w-3" />
                )}
                <span>Copy URL</span>
              </Button>
            </div>
            <Button variant="ghost" size="icon-sm" onClick={onClose} aria-label="Close connect panel">
              <X className="h-3.5 w-3.5" />
            </Button>
          </div>
        </div>

        <Tabs value={activeTab} onValueChange={setActiveTab}>
          <TabsList className="mb-2.5 flex w-full flex-wrap justify-start">
            {snippets.map((s) => (
              <TabsTrigger key={s.id} value={s.id}>
                {s.label}
              </TabsTrigger>
            ))}
          </TabsList>

          {snippets.map((s) => (
            <TabsContent key={s.id} value={s.id}>
              <div className="relative rounded-lg border border-line bg-bg/85 p-3">
                <div className="mb-2 flex items-center justify-between gap-2">
                  <p className="text-[11px] text-muted">{s.description}</p>
                  <Button
                    variant="secondary"
                    size="sm"
                    className="h-6.5 shrink-0"
                    onClick={() => void copyText(s.id, s.code, s.label)}
                  >
                    {copiedId === s.id ? (
                      <Check className="h-3 w-3 text-push" />
                    ) : (
                      <Copy className="h-3 w-3" />
                    )}
                    <span>Copy</span>
                  </Button>
                </div>
                <pre className="overflow-x-auto font-mono text-[11.5px] leading-relaxed text-fg">
                  <code>{s.code}</code>
                </pre>
              </div>
            </TabsContent>
          ))}
        </Tabs>
      </div>
    </section>
  );
}