|
| 1 | +import { useState, useEffect, useCallback } from "react"; |
| 2 | +import Markdown from "react-markdown"; |
| 3 | +import remarkGfm from "remark-gfm"; |
| 4 | +import { mcpPost, initMcpSession } from "../lib/mcp.js"; |
| 5 | + |
| 6 | +interface ResourceEntry { |
| 7 | + uri: string; |
| 8 | + name: string; |
| 9 | + description?: string; |
| 10 | + mimeType?: string; |
| 11 | +} |
| 12 | + |
| 13 | +export function ResourcesList() { |
| 14 | + const [open, setOpen] = useState(false); |
| 15 | + const [resources, setResources] = useState<ResourceEntry[]>(); |
| 16 | + const [sessionId, setSessionId] = useState<string>(); |
| 17 | + const [expandedUri, setExpandedUri] = useState<string | null>(null); |
| 18 | + const [contentCache, setContentCache] = useState<Record<string, string>>({}); |
| 19 | + const [loadingUri, setLoadingUri] = useState<string | null>(null); |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + (async () => { |
| 23 | + const sid = await initMcpSession(); |
| 24 | + setSessionId(sid); |
| 25 | + const { data } = await mcpPost( |
| 26 | + { jsonrpc: "2.0", id: 2, method: "resources/list", params: {} }, |
| 27 | + sid, |
| 28 | + ); |
| 29 | + const typed = data as { |
| 30 | + result?: { resources?: ResourceEntry[] }; |
| 31 | + } | null; |
| 32 | + setResources(typed?.result?.resources ?? []); |
| 33 | + })().catch(() => setResources([])); |
| 34 | + }, []); |
| 35 | + |
| 36 | + const handleResourceClick = useCallback( |
| 37 | + async (uri: string) => { |
| 38 | + if (expandedUri === uri) { |
| 39 | + setExpandedUri(null); |
| 40 | + return; |
| 41 | + } |
| 42 | + setExpandedUri(uri); |
| 43 | + if (contentCache[uri]) return; |
| 44 | + setLoadingUri(uri); |
| 45 | + try { |
| 46 | + const { data } = await mcpPost( |
| 47 | + { jsonrpc: "2.0", id: 3, method: "resources/read", params: { uri } }, |
| 48 | + sessionId, |
| 49 | + ); |
| 50 | + const typed = data as { |
| 51 | + result?: { contents?: { text: string }[] }; |
| 52 | + } | null; |
| 53 | + const text = typed?.result?.contents?.[0]?.text ?? ""; |
| 54 | + setContentCache((prev) => ({ ...prev, [uri]: text })); |
| 55 | + } catch { |
| 56 | + setContentCache((prev) => ({ ...prev, [uri]: "Failed to load resource." })); |
| 57 | + } finally { |
| 58 | + setLoadingUri(null); |
| 59 | + } |
| 60 | + }, |
| 61 | + [expandedUri, contentCache, sessionId], |
| 62 | + ); |
| 63 | + |
| 64 | + // Hide the section entirely if there are no resources |
| 65 | + if (resources !== undefined && resources.length === 0) return null; |
| 66 | + |
| 67 | + const loading = resources === undefined; |
| 68 | + |
| 69 | + return ( |
| 70 | + <div className="pg-section"> |
| 71 | + <button className="pg-collapsible-toggle" onClick={() => setOpen(!open)} aria-expanded={open}> |
| 72 | + <span className="pg-tool-chevron" data-expanded={open}> |
| 73 | + ▶ |
| 74 | + </span> |
| 75 | + <span className="pg-heading">Resources</span> |
| 76 | + {!loading && resources && <span className="pg-count-badge">{resources.length}</span>} |
| 77 | + </button> |
| 78 | + |
| 79 | + {open && ( |
| 80 | + <div className="pg-tools-grid"> |
| 81 | + {loading && ( |
| 82 | + <div className="pg-skeleton-list"> |
| 83 | + {[1, 2, 3].map((i) => ( |
| 84 | + <div key={i} className="pg-skeleton" style={{ height: 40 }} /> |
| 85 | + ))} |
| 86 | + </div> |
| 87 | + )} |
| 88 | + {!loading && |
| 89 | + resources?.map((resource) => { |
| 90 | + const isExpanded = expandedUri === resource.uri; |
| 91 | + const isLoading = loadingUri === resource.uri; |
| 92 | + return ( |
| 93 | + <div key={resource.uri} className="pg-tool-card"> |
| 94 | + <button |
| 95 | + className="pg-tool-header pg-resource-header" |
| 96 | + onClick={() => handleResourceClick(resource.uri)} |
| 97 | + aria-expanded={isExpanded} |
| 98 | + > |
| 99 | + <span className="pg-resource-name">{resource.name}</span> |
| 100 | + <span className="pg-tool-chevron" data-expanded={isExpanded}> |
| 101 | + ▶ |
| 102 | + </span> |
| 103 | + </button> |
| 104 | + {isExpanded && ( |
| 105 | + <div className="pg-tool-body"> |
| 106 | + {isLoading && <div className="pg-skeleton" style={{ height: 100 }} />} |
| 107 | + {!isLoading && contentCache[resource.uri] != null && ( |
| 108 | + <div className="pg-markdown"> |
| 109 | + <Markdown remarkPlugins={[remarkGfm]}> |
| 110 | + {contentCache[resource.uri]} |
| 111 | + </Markdown> |
| 112 | + </div> |
| 113 | + )} |
| 114 | + </div> |
| 115 | + )} |
| 116 | + </div> |
| 117 | + ); |
| 118 | + })} |
| 119 | + </div> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + ); |
| 123 | +} |
0 commit comments