"use client"; import React, { useState, useEffect } from "react"; import { useChat } from "@ai-sdk/react"; export const ChatWindowProvider: React.FC<{ children: React.ReactNode }> = ({ children, }) => { return ( <> {children} ); }; const ChatWindow: React.FC = () => { const [isOpen, setIsOpen] = useState(false); const [pageContext, setPageContext] = useState({ url: "" }); useEffect(() => { if (typeof window !== "undefined") { const path = window.location.pathname; let collectionSlug = null; let id = null; // Payload admin URLs are usually /admin/collections/:slug/:id const match = path.match(/\/collections\/([^/]+)(?:\/([^/]+))?/); if (match) { collectionSlug = match[1]; if (match[2] && match[2] !== "create") { id = match[2]; } } setPageContext({ url: window.location.href, title: document.title, collectionSlug, id, }); } }, [isOpen]); // Refresh context when chat is opened const { messages, input, handleInputChange, handleSubmit } = useChat({ api: "/api/mcp-chat", initialMessages: [], body: { pageContext, }, } as any) as any; // Basic implementation to toggle chat window and submit messages return (
{isOpen && (

Payload MCP Chat

{messages.map((m: any) => (
{m.role === "user" ? "G: " : "AI: "} {m.content}
))}
)}
); };