"use client" import * as React from "react" import { useState, useEffect } from "react" import { motion } from "motion/react" import { toast } from "sonner" import { ErrorDisplay } from "@/components/layout/error" import { EmptyState } from "@/components/layout/empty" import { LoadingPage } from "@/components/layout/loading" import { MerchantSelector } from "@/components/common/merchant/merchant-selector" import { MerchantInfo } from "@/components/common/merchant/merchant-info" import { MerchantData } from "@/components/common/merchant/merchant-data" import { MerchantDialog } from "@/components/common/merchant/merchant-dialog" import { type MerchantAPIKey } from "@/lib/services" import { useMerchant } from "@/contexts/merchant-context" /** * 集市中心主页面组件 * 负责组装集市中心的各个子组件 */ export function MerchantMain() { const { apiKeys, loading, error, loadAPIKeys, createAPIKey, updateAPIKey, deleteAPIKey } = useMerchant() const [selectedKeyId, setSelectedKeyId] = useState(null) const selectedKey = apiKeys.find(key => key.id === selectedKeyId) || null /* 加载 API Keys */ useEffect(() => { loadAPIKeys() }, [loadAPIKeys]) /* 选择默认 API Key */ useEffect(() => { if (apiKeys.length > 0 && !selectedKeyId) { setSelectedKeyId(apiKeys[0].id) } }, [apiKeys, selectedKeyId]) /* 创建成功回调 */ const handleCreateSuccess = (newKey: MerchantAPIKey) => { setSelectedKeyId(newKey.id) } /* 删除成功回调 */ const handleDelete = async (id: string) => { try { await deleteAPIKey(id) toast.success('删除成功') if (selectedKeyId === id) { const remainingKeys = apiKeys.filter(key => key && key.id !== id) setSelectedKeyId(remainingKeys.length > 0 ? remainingKeys[0].id : null) } } catch (error) { toast.error('删除失败', { description: (error as Error).message || '无法删除应用' }) } } /* 正在加载中 */ if (loading) { return } return (

集市中心

{apiKeys.length > 0 && ( )}
{error ? ( ) : apiKeys.length === 0 ? ( ) : ( {selectedKey && (
)}
)}
) }