- 设置内容(同步桥接/悬浮按钮/自建站点/历史入口)从抽屉移出,直接铺在首页 - 删除首页「谢谢支持」分享横幅与 GitHub/分享文案 - 顶栏移除「设置」按钮,设置抽屉组件一并删除
265 lines
9.0 KiB
TypeScript
265 lines
9.0 KiB
TypeScript
import { useState, useEffect, useRef } from 'react'
|
|
import { Plug, PlugZap, Plus, Trash2, ChevronRight } from 'lucide-react'
|
|
import { cn } from '@/lib/utils'
|
|
import { trackFeatureDiscovery } from '../../lib/analytics'
|
|
|
|
interface McpStatus {
|
|
enabled: boolean
|
|
connected: boolean
|
|
token?: string
|
|
serverUrl?: string
|
|
}
|
|
|
|
interface CMSAccount {
|
|
id: string
|
|
name: string
|
|
type: string
|
|
url: string
|
|
}
|
|
|
|
/**
|
|
* 设置面板(内联版)—— 从原设置抽屉移出,直接展示在首页上。
|
|
* 同步桥接锁定为始终连接,不可关闭。
|
|
*/
|
|
export function SettingsPanel() {
|
|
const [mcpStatus, setMcpStatus] = useState<McpStatus>({ enabled: true, connected: false })
|
|
const [cmsAccounts, setCmsAccounts] = useState<CMSAccount[]>([])
|
|
const [loading, setLoading] = useState(false)
|
|
const [floatingButtonEnabled, setFloatingButtonEnabled] = useState(false)
|
|
const [serverUrlInput, setServerUrlInput] = useState('')
|
|
const serverUrlTimer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
|
|
// 获取状态(面板常驻,挂载即加载)
|
|
useEffect(() => {
|
|
// MCP 状态
|
|
chrome.runtime.sendMessage({ type: 'MCP_STATUS' }, (response) => {
|
|
if (response && !response.error) {
|
|
setMcpStatus({
|
|
enabled: response.enabled ?? false,
|
|
connected: response.connected ?? false,
|
|
token: response.token,
|
|
serverUrl: response.serverUrl,
|
|
})
|
|
setServerUrlInput(response.serverUrl || '')
|
|
}
|
|
})
|
|
|
|
// CMS 账户
|
|
chrome.storage.local.get('cmsAccounts', (result) => {
|
|
setCmsAccounts(result.cmsAccounts || [])
|
|
})
|
|
|
|
// 悬浮按钮设置
|
|
chrome.storage.local.get('floatingButtonEnabled', (result) => {
|
|
setFloatingButtonEnabled(result.floatingButtonEnabled ?? false)
|
|
})
|
|
}, [])
|
|
|
|
// MCP 状态轮询 + 通知 background 加速重连
|
|
useEffect(() => {
|
|
// 通知 background:用户正在关注,加速重连
|
|
chrome.runtime.sendMessage({ type: 'MCP_WATCH_START' })
|
|
|
|
const interval = setInterval(() => {
|
|
chrome.runtime.sendMessage({ type: 'MCP_STATUS' }, (response) => {
|
|
if (response && !response.error) {
|
|
setMcpStatus(prev => ({ ...prev, connected: response.connected ?? false }))
|
|
}
|
|
})
|
|
}, 3000)
|
|
|
|
return () => {
|
|
clearInterval(interval)
|
|
// 面板卸载,恢复正常重连策略
|
|
chrome.runtime.sendMessage({ type: 'MCP_WATCH_STOP' })
|
|
}
|
|
}, [])
|
|
|
|
// 桥接始终连接:点击只确保开启,不会关闭
|
|
const toggleMcp = async () => {
|
|
if (loading) return
|
|
setLoading(true)
|
|
try {
|
|
chrome.runtime.sendMessage({ type: 'MCP_ENABLE' }, () => {
|
|
setMcpStatus(prev => ({ ...prev, enabled: true }))
|
|
})
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
// 服务器地址输入(防抖保存)
|
|
const handleServerUrlChange = (value: string) => {
|
|
setServerUrlInput(value)
|
|
trackFeatureDiscovery('mcp_server_url_input', 'home_panel')
|
|
if (serverUrlTimer.current) clearTimeout(serverUrlTimer.current)
|
|
serverUrlTimer.current = setTimeout(() => {
|
|
chrome.storage.local.set({ mcpServerUrl: value }, () => {
|
|
chrome.runtime.sendMessage({ type: 'MCP_SET_SERVER_URL', serverUrl: value })
|
|
})
|
|
}, 600)
|
|
}
|
|
|
|
// 悬浮按钮开关
|
|
const toggleFloatingButton = () => {
|
|
const next = !floatingButtonEnabled
|
|
setFloatingButtonEnabled(next)
|
|
chrome.storage.local.set({ floatingButtonEnabled: next })
|
|
}
|
|
|
|
// 删除 CMS 账户
|
|
const deleteCmsAccount = (id: string) => {
|
|
const next = cmsAccounts.filter(a => a.id !== id)
|
|
setCmsAccounts(next)
|
|
chrome.storage.local.set({ cmsAccounts: next })
|
|
}
|
|
|
|
return (
|
|
<div className="px-4 pt-3 space-y-4">
|
|
{/* 同步桥接设置 */}
|
|
<div className="space-y-3">
|
|
<h3 className="text-sm font-medium text-muted-foreground">同步桥接</h3>
|
|
|
|
<div className="flex items-center justify-between p-3 bg-muted/50 rounded-lg">
|
|
<div className="flex items-center gap-2">
|
|
{mcpStatus.connected ? (
|
|
<PlugZap className="w-5 h-5 text-green-500" />
|
|
) : (
|
|
<Plug className="w-5 h-5 text-muted-foreground" />
|
|
)}
|
|
<div>
|
|
<p className="text-sm font-medium">CLI / MCP 连接</p>
|
|
<p className="text-xs text-muted-foreground">
|
|
{mcpStatus.enabled
|
|
? mcpStatus.connected
|
|
? '已连接'
|
|
: '等待连接...'
|
|
: '未启用'}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
<button
|
|
onClick={toggleMcp}
|
|
disabled={loading}
|
|
title="始终连接(已锁定,不可关闭)"
|
|
className={cn(
|
|
'relative w-11 h-6 rounded-full transition-colors cursor-default',
|
|
'bg-primary opacity-100',
|
|
loading && 'opacity-50'
|
|
)}
|
|
>
|
|
<span
|
|
className={cn(
|
|
'absolute top-1 w-4 h-4 rounded-full bg-white transition-transform',
|
|
'translate-x-6'
|
|
)}
|
|
/>
|
|
</button>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<p className="text-xs text-muted-foreground">
|
|
供 CLI 和 MCP Server 通过 WebSocket 桥接同步文章 —— 始终连接,已锁定不可关闭
|
|
</p>
|
|
{mcpStatus.token && (
|
|
<div className="p-2 bg-muted/50 rounded text-xs">
|
|
<p className="text-muted-foreground mb-1">Token:</p>
|
|
<code className="block bg-background p-1.5 rounded break-all select-all">
|
|
{mcpStatus.token}
|
|
</code>
|
|
</div>
|
|
)}
|
|
<div className="p-2 bg-muted/50 rounded text-xs">
|
|
<p className="text-muted-foreground mb-1">服务器地址 (留空使用本地默认):</p>
|
|
<input
|
|
type="text"
|
|
value={serverUrlInput}
|
|
onChange={(e) => handleServerUrlChange(e.target.value)}
|
|
placeholder="ws://192.168.68.19:9600"
|
|
className="w-full bg-background p-1.5 rounded border border-border text-xs font-mono focus:outline-none focus:ring-1 focus:ring-primary"
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 悬浮同步按钮 */}
|
|
<div className="space-y-3">
|
|
<h3 className="text-sm font-medium text-muted-foreground">网页功能</h3>
|
|
|
|
<div className="flex items-center justify-between p-3 bg-muted/50 rounded-lg">
|
|
<div>
|
|
<p className="text-sm font-medium">悬浮同步按钮</p>
|
|
<p className="text-xs text-muted-foreground">在网页右下角显示快捷同步按钮</p>
|
|
</div>
|
|
<button
|
|
onClick={toggleFloatingButton}
|
|
className={cn(
|
|
'relative w-11 h-6 rounded-full transition-colors',
|
|
floatingButtonEnabled ? 'bg-primary' : 'bg-muted-foreground/30',
|
|
)}
|
|
>
|
|
<span
|
|
className={cn(
|
|
'absolute top-1 w-4 h-4 rounded-full bg-white transition-transform',
|
|
floatingButtonEnabled ? 'translate-x-6' : 'translate-x-1'
|
|
)}
|
|
/>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* CMS 账户 */}
|
|
<div className="space-y-3">
|
|
<div className="flex items-center justify-between">
|
|
<h3 className="text-sm font-medium text-muted-foreground">自建站点</h3>
|
|
<button
|
|
onClick={() => { window.location.hash = '/add-cms' }}
|
|
className="flex items-center gap-1 text-xs text-primary hover:underline"
|
|
>
|
|
<Plus className="w-3 h-3" />
|
|
添加
|
|
</button>
|
|
</div>
|
|
|
|
{cmsAccounts.length === 0 ? (
|
|
<p className="text-xs text-muted-foreground text-center py-4">
|
|
暂无自建站点
|
|
</p>
|
|
) : (
|
|
<div className="space-y-2">
|
|
{cmsAccounts.map(account => (
|
|
<div
|
|
key={account.id}
|
|
className="flex items-center justify-between p-2 bg-muted/50 rounded-lg"
|
|
>
|
|
<div className="flex-1 min-w-0">
|
|
<p className="text-sm font-medium truncate">{account.name}</p>
|
|
<p className="text-xs text-muted-foreground truncate">{account.url}</p>
|
|
</div>
|
|
<button
|
|
onClick={() => deleteCmsAccount(account.id)}
|
|
className="p-1 text-muted-foreground hover:text-destructive"
|
|
>
|
|
<Trash2 className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{/* 历史记录 */}
|
|
<div className="space-y-3">
|
|
<button
|
|
onClick={() => { window.location.hash = '/history' }}
|
|
className="flex items-center justify-between w-full p-3 bg-muted/50 rounded-lg hover:bg-muted"
|
|
>
|
|
<span className="text-sm font-medium">查看全部历史</span>
|
|
<ChevronRight className="w-4 h-4 text-muted-foreground" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|