|
1 | 1 | import { useState } from "react";
|
2 | 2 | import { Copy } from "lucide-react";
|
| 3 | +import { ServerNotification } from "mcp-typescript/types.js"; |
3 | 4 |
|
4 |
| -const History = ({ |
| 5 | +const HistoryAndNotifications = ({ |
5 | 6 | requestHistory,
|
| 7 | + serverNotifications, |
6 | 8 | }: {
|
7 | 9 | requestHistory: Array<{ request: string; response: string | null }>;
|
| 10 | + serverNotifications: ServerNotification[]; |
8 | 11 | }) => {
|
9 | 12 | const [expandedRequests, setExpandedRequests] = useState<{
|
10 | 13 | [key: number]: boolean;
|
11 | 14 | }>({});
|
| 15 | + const [expandedNotifications, setExpandedNotifications] = useState<{ |
| 16 | + [key: number]: boolean; |
| 17 | + }>({}); |
12 | 18 |
|
13 | 19 | const toggleRequestExpansion = (index: number) => {
|
14 | 20 | setExpandedRequests((prev) => ({ ...prev, [index]: !prev[index] }));
|
15 | 21 | };
|
16 | 22 |
|
| 23 | + const toggleNotificationExpansion = (index: number) => { |
| 24 | + setExpandedNotifications((prev) => ({ ...prev, [index]: !prev[index] })); |
| 25 | + }; |
| 26 | + |
17 | 27 | const copyToClipboard = (text: string) => {
|
18 | 28 | navigator.clipboard.writeText(text);
|
19 | 29 | };
|
20 | 30 |
|
21 | 31 | return (
|
22 |
| - <div className="w-64 bg-white shadow-md p-4 overflow-y-auto"> |
23 |
| - <h2 className="text-lg font-semibold mb-4">History</h2> |
24 |
| - <ul className="space-y-3"> |
25 |
| - {requestHistory |
26 |
| - .slice() |
27 |
| - .reverse() |
28 |
| - .map((request, index) => ( |
29 |
| - <li |
30 |
| - key={index} |
31 |
| - className="text-sm text-gray-600 bg-gray-100 p-2 rounded" |
32 |
| - > |
33 |
| - <div |
34 |
| - className="flex justify-between items-center cursor-pointer" |
35 |
| - onClick={() => |
36 |
| - toggleRequestExpansion(requestHistory.length - 1 - index) |
37 |
| - } |
38 |
| - > |
39 |
| - <span className="font-mono"> |
40 |
| - {requestHistory.length - index}.{" "} |
41 |
| - {JSON.parse(request.request).method} |
42 |
| - </span> |
43 |
| - <span> |
44 |
| - {expandedRequests[requestHistory.length - 1 - index] |
45 |
| - ? "▼" |
46 |
| - : "▶"} |
47 |
| - </span> |
48 |
| - </div> |
49 |
| - {expandedRequests[requestHistory.length - 1 - index] && ( |
50 |
| - <> |
51 |
| - <div className="mt-2"> |
52 |
| - <div className="flex justify-between items-center mb-1"> |
53 |
| - <span className="font-semibold text-blue-600"> |
54 |
| - Request: |
55 |
| - </span> |
56 |
| - <button |
57 |
| - onClick={() => copyToClipboard(request.request)} |
58 |
| - className="text-blue-500 hover:text-blue-700" |
59 |
| - > |
60 |
| - <Copy size={16} /> |
61 |
| - </button> |
62 |
| - </div> |
63 |
| - <pre className="whitespace-pre-wrap break-words bg-blue-50 p-2 rounded"> |
64 |
| - {JSON.stringify(JSON.parse(request.request), null, 2)} |
65 |
| - </pre> |
| 32 | + <div className="w-64 bg-white shadow-md p-4 overflow-hidden flex flex-col h-full"> |
| 33 | + <div className="flex-1 overflow-y-auto mb-4 border-b pb-4"> |
| 34 | + <h2 className="text-lg font-semibold mb-4">History</h2> |
| 35 | + {requestHistory.length === 0 ? ( |
| 36 | + <p className="text-sm text-gray-500 italic">No history yet</p> |
| 37 | + ) : ( |
| 38 | + <ul className="space-y-3"> |
| 39 | + {requestHistory |
| 40 | + .slice() |
| 41 | + .reverse() |
| 42 | + .map((request, index) => ( |
| 43 | + <li |
| 44 | + key={index} |
| 45 | + className="text-sm text-gray-600 bg-gray-100 p-2 rounded" |
| 46 | + > |
| 47 | + <div |
| 48 | + className="flex justify-between items-center cursor-pointer" |
| 49 | + onClick={() => |
| 50 | + toggleRequestExpansion(requestHistory.length - 1 - index) |
| 51 | + } |
| 52 | + > |
| 53 | + <span className="font-mono"> |
| 54 | + {requestHistory.length - index}.{" "} |
| 55 | + {JSON.parse(request.request).method} |
| 56 | + </span> |
| 57 | + <span> |
| 58 | + {expandedRequests[requestHistory.length - 1 - index] |
| 59 | + ? "▼" |
| 60 | + : "▶"} |
| 61 | + </span> |
| 62 | + </div> |
| 63 | + {expandedRequests[requestHistory.length - 1 - index] && ( |
| 64 | + <> |
| 65 | + <div className="mt-2"> |
| 66 | + <div className="flex justify-between items-center mb-1"> |
| 67 | + <span className="font-semibold text-blue-600"> |
| 68 | + Request: |
| 69 | + </span> |
| 70 | + <button |
| 71 | + onClick={() => copyToClipboard(request.request)} |
| 72 | + className="text-blue-500 hover:text-blue-700" |
| 73 | + > |
| 74 | + <Copy size={16} /> |
| 75 | + </button> |
| 76 | + </div> |
| 77 | + <pre className="whitespace-pre-wrap break-words bg-blue-50 p-2 rounded"> |
| 78 | + {JSON.stringify(JSON.parse(request.request), null, 2)} |
| 79 | + </pre> |
| 80 | + </div> |
| 81 | + {request.response && ( |
| 82 | + <div className="mt-2"> |
| 83 | + <div className="flex justify-between items-center mb-1"> |
| 84 | + <span className="font-semibold text-green-600"> |
| 85 | + Response: |
| 86 | + </span> |
| 87 | + <button |
| 88 | + onClick={() => copyToClipboard(request.response!)} |
| 89 | + className="text-blue-500 hover:text-blue-700" |
| 90 | + > |
| 91 | + <Copy size={16} /> |
| 92 | + </button> |
| 93 | + </div> |
| 94 | + <pre className="whitespace-pre-wrap break-words bg-green-50 p-2 rounded"> |
| 95 | + {JSON.stringify( |
| 96 | + JSON.parse(request.response), |
| 97 | + null, |
| 98 | + 2, |
| 99 | + )} |
| 100 | + </pre> |
| 101 | + </div> |
| 102 | + )} |
| 103 | + </> |
| 104 | + )} |
| 105 | + </li> |
| 106 | + ))} |
| 107 | + </ul> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + <div className="flex-1 overflow-y-auto"> |
| 111 | + <h2 className="text-lg font-semibold mb-4">Server Notifications</h2> |
| 112 | + {serverNotifications.length === 0 ? ( |
| 113 | + <p className="text-sm text-gray-500 italic">No notifications yet</p> |
| 114 | + ) : ( |
| 115 | + <ul className="space-y-3"> |
| 116 | + {serverNotifications |
| 117 | + .slice() |
| 118 | + .reverse() |
| 119 | + .map((notification, index) => ( |
| 120 | + <li |
| 121 | + key={index} |
| 122 | + className="text-sm text-gray-600 bg-gray-100 p-2 rounded" |
| 123 | + > |
| 124 | + <div |
| 125 | + className="flex justify-between items-center cursor-pointer" |
| 126 | + onClick={() => toggleNotificationExpansion(index)} |
| 127 | + > |
| 128 | + <span className="font-mono"> |
| 129 | + {serverNotifications.length - index}.{" "} |
| 130 | + {notification.method} |
| 131 | + </span> |
| 132 | + <span>{expandedNotifications[index] ? "▼" : "▶"}</span> |
66 | 133 | </div>
|
67 |
| - {request.response && ( |
| 134 | + {expandedNotifications[index] && ( |
68 | 135 | <div className="mt-2">
|
69 | 136 | <div className="flex justify-between items-center mb-1">
|
70 |
| - <span className="font-semibold text-green-600"> |
71 |
| - Response: |
| 137 | + <span className="font-semibold text-purple-600"> |
| 138 | + Details: |
72 | 139 | </span>
|
73 | 140 | <button
|
74 |
| - onClick={() => copyToClipboard(request.response!)} |
| 141 | + onClick={() => |
| 142 | + copyToClipboard(JSON.stringify(notification)) |
| 143 | + } |
75 | 144 | className="text-blue-500 hover:text-blue-700"
|
76 | 145 | >
|
77 | 146 | <Copy size={16} />
|
78 | 147 | </button>
|
79 | 148 | </div>
|
80 |
| - <pre className="whitespace-pre-wrap break-words bg-green-50 p-2 rounded"> |
81 |
| - {JSON.stringify(JSON.parse(request.response), null, 2)} |
| 149 | + <pre className="whitespace-pre-wrap break-words bg-purple-50 p-2 rounded"> |
| 150 | + {JSON.stringify(notification, null, 2)} |
82 | 151 | </pre>
|
83 | 152 | </div>
|
84 | 153 | )}
|
85 |
| - </> |
86 |
| - )} |
87 |
| - </li> |
88 |
| - ))} |
89 |
| - </ul> |
| 154 | + </li> |
| 155 | + ))} |
| 156 | + </ul> |
| 157 | + )} |
| 158 | + </div> |
90 | 159 | </div>
|
91 | 160 | );
|
92 | 161 | };
|
93 | 162 |
|
94 |
| -export default History; |
| 163 | +export default HistoryAndNotifications; |
0 commit comments