import React, { createContext, useContext, useMemo, useState } from "react" import { AnimatePresence, motion } from "framer-motion" import { PlaygroundIodRelevant } from "@/components/Common/Playground/IodRelevant.tsx" import { PlaygroundData } from "@/components/Common/Playground/Data.tsx" import { PlaygroundScene } from "@/components/Common/Playground/Scene.tsx" import { PlaygroundTeam } from "@/components/Common/Playground/Team.tsx" import { Card } from "antd" import { CloseOutlined } from "@ant-design/icons" import { useMessageOption } from "@/hooks/useMessageOption.tsx" import { Message } from "@/types/message.ts" // 定义 Context 类型 interface IodPlaygroundContextType { showPlayground: boolean setShowPlayground: React.Dispatch> detailHeader: React.ReactNode setDetailHeader: React.Dispatch> detailMain: React.ReactNode setDetailMain: React.Dispatch> currentIodMessage: Message | null } // 创建 Context const PlaygroundContext = createContext( undefined ) // 创建自定义 hook 以便子组件使用 export const useIodPlaygroundContext = () => { const context = useContext(PlaygroundContext) if (context === undefined) { throw new Error( "usePlaygroundContext must be used within a PlaygroundProvider" ) } return context } const PlaygroundIodProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { const { messages, iodLoading, currentMessageId, iodSearch } = useMessageOption() const [showPlayground, setShowPlayground] = useState(true) const [detailHeader, setDetailHeader] = useState(<>) const [detailMain, setDetailMain] = useState(<>) const currentIodMessage = useMemo(() => { if (iodLoading) { return null } if (messages.length && iodSearch) { // 如果不存在currentMessageId默认返回最后一个message if (!currentMessageId) { return messages.at(-1) } const currentMessage = messages?.find( (message) => message.id === currentMessageId ) if (currentMessage) { return currentMessage } // 如果当前message不存在最后一个message return messages.at(-1) } return null }, [currentMessageId, messages, iodLoading, iodSearch]) return ( {children} ) } // 子组件使用修改card的默认样式 const classNames = "h-full [&_.ant-card-body]:h-full [&_.ant-card-body]:!p-[20px] overflow-y-hidden !bg-[rgba(240,245,255,0.3)] backdrop-blur-sm border border-white/30 shadow-xl rounded-2xl" // 将原来的返回内容移到这个组件中 const PlaygroundContent = () => { const { showPlayground, detailMain, detailHeader, setShowPlayground } = useIodPlaygroundContext() return ( {showPlayground ? (
) : (
{detailHeader}
setShowPlayground(true)} />
{detailMain}
)}
) } export const PlaygroundIod = () => { return (
) }