mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
Refactor MCP server UI and improve config section scrolling
This commit is contained in:
1 parent
2c9782aa53
commit
af67716cde
2 files changed
+110
-125
No files matched your search
@@ -1,15 +1,13 @@
|
||||
import { useState } from 'react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Dialog, DialogTrigger } from '@/components/ui/dialog'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Plus, Trash2, Globe, Terminal, Loader2, AlertCircle, RefreshCw, Key, XCircle } from 'lucide-react'
|
||||
import { Plus, Loader2, RefreshCw } from 'lucide-react'
|
||||
import { DeleteDialog } from '@/components/ui/delete-dialog'
|
||||
import { AddMcpServerDialog } from './AddMcpServerDialog'
|
||||
import { McpServerCard } from './McpServerCard'
|
||||
import { useMcpServers } from '@/hooks/useMcpServers'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import type { McpStatus } from '@/api/mcp'
|
||||
|
||||
interface McpServerConfig {
|
||||
type: 'local' | 'remote'
|
||||
@@ -29,37 +27,7 @@ interface McpManagerProps {
|
||||
onConfigUpdate?: (configName: string, content: Record<string, unknown>) => Promise<void>
|
||||
}
|
||||
|
||||
function getStatusBadge(status: McpStatus) {
|
||||
switch (status.status) {
|
||||
case 'connected':
|
||||
return <Badge variant="default" className="text-xs bg-green-600">Connected</Badge>
|
||||
case 'disabled':
|
||||
return <Badge variant="secondary" className="text-xs">Disabled</Badge>
|
||||
case 'failed':
|
||||
return (
|
||||
<Badge variant="destructive" className="text-xs flex items-center gap-1">
|
||||
<AlertCircle className="h-3 w-3" />
|
||||
Failed
|
||||
</Badge>
|
||||
)
|
||||
case 'needs_auth':
|
||||
return (
|
||||
<Badge variant="outline" className="text-xs flex items-center gap-1 border-yellow-500 text-yellow-600">
|
||||
<Key className="h-3 w-3" />
|
||||
Auth Required
|
||||
</Badge>
|
||||
)
|
||||
case 'needs_client_registration':
|
||||
return (
|
||||
<Badge variant="outline" className="text-xs flex items-center gap-1 border-orange-500 text-orange-600">
|
||||
<AlertCircle className="h-3 w-3" />
|
||||
Registration Required
|
||||
</Badge>
|
||||
)
|
||||
default:
|
||||
return <Badge variant="outline" className="text-xs">Unknown</Badge>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function McpManager({ config, onUpdate, onConfigUpdate }: McpManagerProps) {
|
||||
const [isAddDialogOpen, setIsAddDialogOpen] = useState(false)
|
||||
@@ -130,30 +98,7 @@ export function McpManager({ config, onUpdate, onConfigUpdate }: McpManagerProps
|
||||
}
|
||||
}
|
||||
|
||||
const getServerDisplayName = (serverId: string): string => {
|
||||
const name = serverId.replace(/[-_]/g, ' ')
|
||||
return name.charAt(0).toUpperCase() + name.slice(1)
|
||||
}
|
||||
|
||||
const getServerDescription = (serverConfig: McpServerConfig): string => {
|
||||
if (serverConfig.type === 'local' && serverConfig.command) {
|
||||
const command = serverConfig.command.join(' ')
|
||||
if (command.includes('filesystem')) return 'File system access'
|
||||
if (command.includes('git')) return 'Git repository operations'
|
||||
if (command.includes('sqlite')) return 'SQLite database access'
|
||||
if (command.includes('postgres')) return 'PostgreSQL database access'
|
||||
if (command.includes('brave-search')) return 'Web search via Brave'
|
||||
if (command.includes('github')) return 'GitHub repository access'
|
||||
if (command.includes('slack')) return 'Slack integration'
|
||||
if (command.includes('puppeteer')) return 'Web automation'
|
||||
if (command.includes('fetch')) return 'HTTP requests'
|
||||
if (command.includes('memory')) return 'Persistent memory'
|
||||
return `Local command: ${command}`
|
||||
} else if (serverConfig.type === 'remote' && serverConfig.url) {
|
||||
return `Remote server: ${serverConfig.url}`
|
||||
}
|
||||
return 'MCP server'
|
||||
}
|
||||
|
||||
|
||||
const getErrorMessage = (serverId: string): string | null => {
|
||||
const status = mcpStatus?.[serverId]
|
||||
@@ -232,62 +177,18 @@ export function McpManager({ config, onUpdate, onConfigUpdate }: McpManagerProps
|
||||
const errorMessage = getErrorMessage(serverId)
|
||||
|
||||
return (
|
||||
<Card key={serverId} className={errorMessage ? 'border-red-500/50' : ''}>
|
||||
<CardHeader className="pb-3">
|
||||
<div className="flex flex-col gap-4">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="flex items-center gap-2">
|
||||
{serverConfig.type === 'local' ? (
|
||||
<Terminal className="h-4 w-4 text-muted-foreground" />
|
||||
) : (
|
||||
<Globe className="h-4 w-4 text-muted-foreground" />
|
||||
)}
|
||||
<CardTitle className="text-base">{getServerDisplayName(serverId)}</CardTitle>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{status ? getStatusBadge(status) : (
|
||||
<Badge variant="outline" className="text-xs">Loading...</Badge>
|
||||
)}
|
||||
<Badge variant="outline" className="text-xs">
|
||||
{serverConfig.type}
|
||||
</Badge>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
checked={isConnected}
|
||||
onCheckedChange={() => handleToggleServer(serverId)}
|
||||
disabled={isAnyOperationPending || togglingServerId === serverId}
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setDeleteConfirmServer({ id: serverId, name: getServerDisplayName(serverId) })}
|
||||
className="text-red-500 hover:text-red-600"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className='p-2'>
|
||||
<div className="text-sm text-muted-foreground space-y-1">
|
||||
<p>{getServerDescription(serverConfig)}</p>
|
||||
{serverConfig.timeout && (
|
||||
<p>Timeout: {serverConfig.timeout}ms</p>
|
||||
)}
|
||||
{serverConfig.environment && Object.keys(serverConfig.environment).length > 0 && (
|
||||
<p>Environment variables: {Object.keys(serverConfig.environment).length} configured</p>
|
||||
)}
|
||||
{errorMessage && (
|
||||
<div className="flex items-start gap-2 mt-2 p-2 bg-red-500/10 rounded text-red-600 text-xs">
|
||||
<XCircle className="h-4 w-4 flex-shrink-0 mt-0.5" />
|
||||
<span className="break-words">{errorMessage}</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<McpServerCard
|
||||
key={serverId}
|
||||
serverId={serverId}
|
||||
serverConfig={serverConfig}
|
||||
status={status}
|
||||
isConnected={isConnected}
|
||||
errorMessage={errorMessage}
|
||||
isAnyOperationPending={isAnyOperationPending}
|
||||
togglingServerId={togglingServerId}
|
||||
onToggleServer={handleToggleServer}
|
||||
onDeleteServer={(id, name) => setDeleteConfirmServer({ id, name })}
|
||||
/>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
import { Loader2, Plus, Trash2, Edit, Star, StarOff, Download, RotateCcw } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
@@ -16,6 +16,35 @@ import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { parseJsonc, hasJsoncComments } from '@/lib/jsonc'
|
||||
import type { OpenCodeConfig } from '@/api/types/settings'
|
||||
|
||||
interface Command {
|
||||
template: string
|
||||
description?: string
|
||||
agent?: string
|
||||
model?: string
|
||||
subtask?: boolean
|
||||
topP?: number
|
||||
}
|
||||
|
||||
interface Agent {
|
||||
prompt?: string
|
||||
description?: string
|
||||
mode?: 'subagent' | 'primary' | 'all'
|
||||
temperature?: number
|
||||
topP?: number
|
||||
model?: {
|
||||
modelID: string
|
||||
providerID: string
|
||||
}
|
||||
tools?: Record<string, boolean>
|
||||
permission?: {
|
||||
edit?: 'ask' | 'allow' | 'deny'
|
||||
bash?: 'ask' | 'allow' | 'deny' | Record<string, 'ask' | 'allow' | 'deny'>
|
||||
webfetch?: 'ask' | 'allow' | 'deny'
|
||||
}
|
||||
disable?: boolean
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export function OpenCodeConfigManager() {
|
||||
const queryClient = useQueryClient()
|
||||
const [configs, setConfigs] = useState<OpenCodeConfig[]>([])
|
||||
@@ -32,6 +61,31 @@ export function OpenCodeConfigManager() {
|
||||
const [isEditDialogOpen, setIsEditDialogOpen] = useState(false)
|
||||
const [deleteConfirmConfig, setDeleteConfirmConfig] = useState<OpenCodeConfig | null>(null)
|
||||
|
||||
const commandsRef = useRef<HTMLButtonElement>(null)
|
||||
const agentsRef = useRef<HTMLButtonElement>(null)
|
||||
const mcpRef = useRef<HTMLButtonElement>(null)
|
||||
|
||||
const scrollToSection = (ref: React.RefObject<HTMLButtonElement | null>) => {
|
||||
if (ref.current) {
|
||||
const rect = ref.current.getBoundingClientRect()
|
||||
|
||||
console.log('Scroll check:', {
|
||||
section: ref.current.textContent?.trim(),
|
||||
rectTop: rect.top,
|
||||
rectBottom: rect.bottom,
|
||||
viewportHeight: window.innerHeight
|
||||
})
|
||||
|
||||
// Always scroll to ensure the section is at the top when expanded
|
||||
console.log('Scrolling to section:', ref.current.textContent?.trim())
|
||||
ref.current.scrollIntoView({
|
||||
behavior: 'smooth',
|
||||
block: 'start',
|
||||
inline: 'nearest'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const restartServerMutation = useMutation({
|
||||
mutationFn: async () => {
|
||||
return await settingsApi.restartOpenCodeServer()
|
||||
@@ -343,13 +397,23 @@ export function OpenCodeConfigManager() {
|
||||
<>
|
||||
<div className="bg-card border border-border rounded-lg overflow-hidden min-w-0">
|
||||
<button
|
||||
ref={commandsRef}
|
||||
className="w-full px-4 py-3 flex items-center justify-between hover:bg-muted/50 transition-colors min-w-0"
|
||||
onClick={() => setExpandedSections(prev => ({ ...prev, commands: !prev.commands }))}
|
||||
onClick={() => {
|
||||
const isExpanding = !expandedSections.commands
|
||||
console.log('Commands clicked, expanding:', isExpanding)
|
||||
setExpandedSections(prev => ({ ...prev, commands: isExpanding }))
|
||||
|
||||
// Only scroll when expanding, not collapsing
|
||||
if (isExpanding) {
|
||||
setTimeout(() => scrollToSection(commandsRef), 100)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
<h4 className="text-sm font-medium truncate">Commands</h4>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{Object.keys(selectedConfig.content.command as Record<string, any> || {}).length} configured
|
||||
{Object.keys(selectedConfig.content.command as Record<string, Command> || {}).length} configured
|
||||
</span>
|
||||
</div>
|
||||
<Edit className={`h-4 w-4 transition-transform ${expandedSections.commands ? 'rotate-90' : ''}`} />
|
||||
@@ -357,7 +421,7 @@ export function OpenCodeConfigManager() {
|
||||
<div className={`${expandedSections.commands ? 'block' : 'hidden'} border-t border-border`}>
|
||||
<div className="p-1 sm:p-4 max-h-[50vh] overflow-y-auto">
|
||||
<CommandsEditor
|
||||
commands={(selectedConfig.content.command as Record<string, any>) || {}}
|
||||
commands={(selectedConfig.content.command as Record<string, Command>) || {}}
|
||||
onChange={(commands) => {
|
||||
const updatedContent = {
|
||||
...selectedConfig.content,
|
||||
@@ -372,13 +436,23 @@ export function OpenCodeConfigManager() {
|
||||
|
||||
<div className="bg-card border border-border rounded-lg overflow-hidden min-w-0">
|
||||
<button
|
||||
ref={agentsRef}
|
||||
className="w-full px-4 py-3 flex items-center justify-between hover:bg-muted/50 transition-colors min-w-0"
|
||||
onClick={() => setExpandedSections(prev => ({ ...prev, agents: !prev.agents }))}
|
||||
onClick={() => {
|
||||
const isExpanding = !expandedSections.agents
|
||||
console.log('Agents clicked, expanding:', isExpanding)
|
||||
setExpandedSections(prev => ({ ...prev, agents: isExpanding }))
|
||||
|
||||
// Only scroll when expanding, not collapsing
|
||||
if (isExpanding) {
|
||||
setTimeout(() => scrollToSection(agentsRef), 100)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
<h4 className="text-sm font-medium truncate">Agents</h4>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{Object.keys(selectedConfig.content.agent as Record<string, any> || {}).length} configured
|
||||
{Object.keys(selectedConfig.content.agent as Record<string, Agent> || {}).length} configured
|
||||
</span>
|
||||
</div>
|
||||
<Edit className={`h-4 w-4 transition-transform ${expandedSections.agents ? 'rotate-90' : ''}`} />
|
||||
@@ -386,7 +460,7 @@ export function OpenCodeConfigManager() {
|
||||
<div className={`${expandedSections.agents ? 'block' : 'hidden'} border-t border-border`}>
|
||||
<div className="p-4 max-h-[50vh] overflow-y-auto">
|
||||
<AgentsEditor
|
||||
agents={(selectedConfig.content.agent as Record<string, any>) || {}}
|
||||
agents={(selectedConfig.content.agent as Record<string, Agent>) || {}}
|
||||
onChange={(agents) => {
|
||||
const updatedContent = {
|
||||
...selectedConfig.content,
|
||||
@@ -401,13 +475,23 @@ export function OpenCodeConfigManager() {
|
||||
|
||||
<div className="bg-card border border-border rounded-lg overflow-hidden min-w-0">
|
||||
<button
|
||||
ref={mcpRef}
|
||||
className="w-full px-4 py-3 flex items-center justify-between hover:bg-muted/50 transition-colors min-w-0"
|
||||
onClick={() => setExpandedSections(prev => ({ ...prev, mcp: !prev.mcp }))}
|
||||
onClick={() => {
|
||||
const isExpanding = !expandedSections.mcp
|
||||
console.log('MCP clicked, expanding:', isExpanding)
|
||||
setExpandedSections(prev => ({ ...prev, mcp: isExpanding }))
|
||||
|
||||
// Only scroll when expanding, not collapsing
|
||||
if (isExpanding) {
|
||||
setTimeout(() => scrollToSection(mcpRef), 100)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
<h4 className="text-sm font-medium truncate">MCP Servers</h4>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{Object.keys((selectedConfig.content.mcp as Record<string, any>) || {}).length} configured
|
||||
{Object.keys((selectedConfig.content.mcp as Record<string, unknown>) || {}).length} configured
|
||||
</span>
|
||||
</div>
|
||||
<Edit className={`h-4 w-4 transition-transform ${expandedSections.mcp ? 'rotate-90' : ''}`} />
|
||||
|
||||
Reference in new issue
Block a user