|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect, useCallback } from "react"; |
| 4 | +import { |
| 5 | + Dialog, |
| 6 | + DialogContent, |
| 7 | + DialogHeader, |
| 8 | + DialogTitle, |
| 9 | + DialogDescription, |
| 10 | +} from "@/components/ui/dialog"; |
| 11 | +import { Button } from "@/components/ui/button"; |
| 12 | +import { useTranslations } from "@/hooks/use-translations"; |
| 13 | +import { useAuth } from "@/contexts/auth-context"; |
| 14 | +import { getChatProviderConfigs } from "@/lib/admin-api"; |
| 15 | +import { |
| 16 | + Copy, |
| 17 | + Check, |
| 18 | + ExternalLink, |
| 19 | + MessageSquare, |
| 20 | + Server, |
| 21 | + CheckCircle, |
| 22 | + XCircle, |
| 23 | + Loader2, |
| 24 | +} from "lucide-react"; |
| 25 | +import Link from "next/link"; |
| 26 | + |
| 27 | +interface IntegrationsDialogProps { |
| 28 | + open: boolean; |
| 29 | + onOpenChange: (open: boolean) => void; |
| 30 | +} |
| 31 | + |
| 32 | +export function IntegrationsDialog({ open, onOpenChange }: IntegrationsDialogProps) { |
| 33 | + const t = useTranslations(); |
| 34 | + const { user } = useAuth(); |
| 35 | + const isAdmin = user?.roles?.includes("Admin") ?? false; |
| 36 | + |
| 37 | + const [slackLoading, setSlackLoading] = useState(false); |
| 38 | + const [slackConnected, setSlackConnected] = useState(false); |
| 39 | + const [slackError, setSlackError] = useState(false); |
| 40 | + const [mcpUrlCopied, setMcpUrlCopied] = useState(false); |
| 41 | + const [configCopied, setConfigCopied] = useState(false); |
| 42 | + |
| 43 | + const mcpUrl = typeof window !== "undefined" ? `${window.location.origin}/api/mcp` : "/api/mcp"; |
| 44 | + |
| 45 | + const claudeDesktopConfig = JSON.stringify( |
| 46 | + { |
| 47 | + mcpServers: { |
| 48 | + deepwiki: { |
| 49 | + url: mcpUrl, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + null, |
| 54 | + 2 |
| 55 | + ); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + if (!open) return; |
| 59 | + |
| 60 | + setSlackLoading(true); |
| 61 | + setSlackError(false); |
| 62 | + setSlackConnected(false); |
| 63 | + |
| 64 | + getChatProviderConfigs() |
| 65 | + .then((providers) => { |
| 66 | + const slack = providers.find((p) => p.platform === "slack"); |
| 67 | + setSlackConnected(slack ? slack.isEnabled && slack.isRegistered : false); |
| 68 | + }) |
| 69 | + .catch(() => { |
| 70 | + setSlackError(true); |
| 71 | + }) |
| 72 | + .finally(() => { |
| 73 | + setSlackLoading(false); |
| 74 | + }); |
| 75 | + }, [open]); |
| 76 | + |
| 77 | + const copyToClipboard = useCallback(async (text: string, type: "url" | "config") => { |
| 78 | + try { |
| 79 | + await navigator.clipboard.writeText(text); |
| 80 | + if (type === "url") { |
| 81 | + setMcpUrlCopied(true); |
| 82 | + setTimeout(() => setMcpUrlCopied(false), 2000); |
| 83 | + } else { |
| 84 | + setConfigCopied(true); |
| 85 | + setTimeout(() => setConfigCopied(false), 2000); |
| 86 | + } |
| 87 | + } catch { |
| 88 | + // Clipboard API not available |
| 89 | + } |
| 90 | + }, []); |
| 91 | + |
| 92 | + return ( |
| 93 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 94 | + <DialogContent className="sm:max-w-lg max-h-[90vh] overflow-y-auto"> |
| 95 | + <DialogHeader> |
| 96 | + <DialogTitle>{t("home.integrations.title")}</DialogTitle> |
| 97 | + <DialogDescription>{t("home.integrations.description")}</DialogDescription> |
| 98 | + </DialogHeader> |
| 99 | + |
| 100 | + <div className="space-y-4 pt-2"> |
| 101 | + {/* Slack Integration Section */} |
| 102 | + <div className="rounded-lg border p-4 space-y-3"> |
| 103 | + <div className="flex items-center justify-between"> |
| 104 | + <div className="flex items-center gap-2"> |
| 105 | + <MessageSquare className="h-5 w-5 text-muted-foreground" /> |
| 106 | + <h3 className="font-medium">{t("home.integrations.slack.title")}</h3> |
| 107 | + </div> |
| 108 | + <div className="flex items-center gap-1.5"> |
| 109 | + {slackLoading ? ( |
| 110 | + <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" /> |
| 111 | + ) : slackConnected ? ( |
| 112 | + <> |
| 113 | + <CheckCircle className="h-4 w-4 text-green-500" /> |
| 114 | + <span className="text-sm text-green-500">{t("home.integrations.slack.connected")}</span> |
| 115 | + </> |
| 116 | + ) : ( |
| 117 | + <> |
| 118 | + <XCircle className="h-4 w-4 text-muted-foreground" /> |
| 119 | + <span className="text-sm text-muted-foreground">{t("home.integrations.slack.notConnected")}</span> |
| 120 | + </> |
| 121 | + )} |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + <div className="text-sm text-muted-foreground"> |
| 125 | + {isAdmin ? ( |
| 126 | + <Link |
| 127 | + href="/admin/chat-providers" |
| 128 | + className="inline-flex items-center gap-1 text-primary hover:underline" |
| 129 | + onClick={() => onOpenChange(false)} |
| 130 | + > |
| 131 | + {t("home.integrations.slack.configure")} |
| 132 | + <ExternalLink className="h-3 w-3" /> |
| 133 | + </Link> |
| 134 | + ) : ( |
| 135 | + <p>{t("home.integrations.slack.contactAdmin")}</p> |
| 136 | + )} |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + |
| 140 | + {/* MCP Server Section */} |
| 141 | + <div className="rounded-lg border p-4 space-y-3"> |
| 142 | + <div className="flex items-center gap-2"> |
| 143 | + <Server className="h-5 w-5 text-muted-foreground" /> |
| 144 | + <h3 className="font-medium">{t("home.integrations.mcp.title")}</h3> |
| 145 | + </div> |
| 146 | + <p className="text-sm text-muted-foreground">{t("home.integrations.mcp.description")}</p> |
| 147 | + |
| 148 | + {/* MCP URL */} |
| 149 | + <div className="space-y-1.5"> |
| 150 | + <label className="text-xs font-medium text-muted-foreground uppercase tracking-wide"> |
| 151 | + {t("home.integrations.mcp.serverUrl")} |
| 152 | + </label> |
| 153 | + <div className="flex items-center gap-2"> |
| 154 | + <code className="flex-1 rounded-md bg-muted px-3 py-2 text-sm font-mono break-all"> |
| 155 | + {mcpUrl} |
| 156 | + </code> |
| 157 | + <Button |
| 158 | + variant="outline" |
| 159 | + size="sm" |
| 160 | + className="shrink-0 gap-1.5" |
| 161 | + onClick={() => copyToClipboard(mcpUrl, "url")} |
| 162 | + > |
| 163 | + {mcpUrlCopied ? ( |
| 164 | + <> |
| 165 | + <Check className="h-3.5 w-3.5" /> |
| 166 | + {t("home.integrations.mcp.copied")} |
| 167 | + </> |
| 168 | + ) : ( |
| 169 | + <> |
| 170 | + <Copy className="h-3.5 w-3.5" /> |
| 171 | + {t("home.integrations.mcp.copyUrl")} |
| 172 | + </> |
| 173 | + )} |
| 174 | + </Button> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + |
| 178 | + {/* Claude Desktop Config */} |
| 179 | + <div className="space-y-1.5"> |
| 180 | + <p className="text-sm text-muted-foreground"> |
| 181 | + {t("home.integrations.mcp.claudeDesktopHint")} |
| 182 | + </p> |
| 183 | + <div className="relative"> |
| 184 | + <pre className="rounded-md bg-muted px-3 py-2 text-xs font-mono overflow-x-auto"> |
| 185 | + {claudeDesktopConfig} |
| 186 | + </pre> |
| 187 | + <Button |
| 188 | + variant="outline" |
| 189 | + size="sm" |
| 190 | + className="absolute top-1.5 right-1.5 h-7 gap-1 text-xs" |
| 191 | + onClick={() => copyToClipboard(claudeDesktopConfig, "config")} |
| 192 | + > |
| 193 | + {configCopied ? ( |
| 194 | + <> |
| 195 | + <Check className="h-3 w-3" /> |
| 196 | + {t("home.integrations.mcp.copied")} |
| 197 | + </> |
| 198 | + ) : ( |
| 199 | + <> |
| 200 | + <Copy className="h-3 w-3" /> |
| 201 | + {t("home.integrations.mcp.copyConfig")} |
| 202 | + </> |
| 203 | + )} |
| 204 | + </Button> |
| 205 | + </div> |
| 206 | + </div> |
| 207 | + |
| 208 | + {/* Claude.ai Hint */} |
| 209 | + <p className="text-sm text-muted-foreground"> |
| 210 | + {t("home.integrations.mcp.claudeAiHint")} |
| 211 | + </p> |
| 212 | + </div> |
| 213 | + </div> |
| 214 | + </DialogContent> |
| 215 | + </Dialog> |
| 216 | + ); |
| 217 | +} |
0 commit comments