|
1 |
| -import { useState, useCallback, useEffect } from "react"; |
2 |
| -import { Copy, CheckCheck } from "lucide-react"; |
3 |
| -import { Button } from "@/components/ui/button"; |
4 |
| -import { useToast } from "@/lib/hooks/useToast"; |
| 1 | +import { useState, useCallback, useMemo, memo } from "react"; |
| 2 | +import JsonView from "./JsonView"; |
5 | 3 |
|
6 | 4 | interface ResourceLinkViewProps {
|
7 | 5 | uri: string;
|
8 | 6 | name?: string;
|
9 | 7 | description?: string;
|
10 | 8 | mimeType?: string;
|
| 9 | + resourceContent: string; |
| 10 | + onReadResource?: (uri: string) => void; |
11 | 11 | }
|
12 | 12 |
|
13 |
| -const ResourceLinkView = ({ |
14 |
| - uri, |
15 |
| - name, |
16 |
| - description, |
17 |
| - mimeType, |
18 |
| -}: ResourceLinkViewProps) => { |
19 |
| - const { toast } = useToast(); |
20 |
| - const [copied, setCopied] = useState(false); |
| 13 | +const ResourceLinkView = memo( |
| 14 | + ({ |
| 15 | + uri, |
| 16 | + name, |
| 17 | + description, |
| 18 | + mimeType, |
| 19 | + resourceContent, |
| 20 | + onReadResource, |
| 21 | + }: ResourceLinkViewProps) => { |
| 22 | + const [{ expanded, loading }, setState] = useState({ |
| 23 | + expanded: false, |
| 24 | + loading: false, |
| 25 | + }); |
21 | 26 |
|
22 |
| - useEffect(() => { |
23 |
| - let timeoutId: NodeJS.Timeout; |
24 |
| - if (copied) { |
25 |
| - timeoutId = setTimeout(() => { |
26 |
| - setCopied(false); |
27 |
| - }, 500); |
28 |
| - } |
29 |
| - return () => { |
30 |
| - if (timeoutId) { |
31 |
| - clearTimeout(timeoutId); |
32 |
| - } |
33 |
| - }; |
34 |
| - }, [copied]); |
35 |
| - |
36 |
| - const handleCopyUri = useCallback(() => { |
37 |
| - try { |
38 |
| - navigator.clipboard.writeText(uri); |
39 |
| - setCopied(true); |
40 |
| - } catch (error) { |
41 |
| - toast({ |
42 |
| - title: "Error", |
43 |
| - description: `There was an error copying URI to clipboard: ${error instanceof Error ? error.message : String(error)}`, |
44 |
| - variant: "destructive", |
45 |
| - }); |
46 |
| - } |
47 |
| - }, [uri, toast]); |
48 |
| - |
49 |
| - const displayName = name || new URL(uri).pathname.split("/").pop() || uri; |
| 27 | + const expandedContent = useMemo( |
| 28 | + () => |
| 29 | + expanded && resourceContent ? ( |
| 30 | + <div className="mt-2"> |
| 31 | + <div className="flex justify-between items-center mb-1"> |
| 32 | + <span className="font-semibold text-green-600">Resource:</span> |
| 33 | + </div> |
| 34 | + <JsonView data={resourceContent} className="bg-background" /> |
| 35 | + </div> |
| 36 | + ) : null, |
| 37 | + [expanded, resourceContent], |
| 38 | + ); |
50 | 39 |
|
51 |
| - return ( |
52 |
| - <div |
53 |
| - className="p-4 border rounded relative bg-gray-50 dark:bg-gray-800" |
54 |
| - role="article" |
55 |
| - aria-label={`Resource link: ${displayName}`} |
56 |
| - > |
57 |
| - <Button |
58 |
| - size="icon" |
59 |
| - variant="ghost" |
60 |
| - className="absolute top-2 right-2" |
61 |
| - onClick={handleCopyUri} |
62 |
| - > |
63 |
| - {copied ? ( |
64 |
| - <CheckCheck className="size-4 dark:text-green-700 text-green-600" /> |
65 |
| - ) : ( |
66 |
| - <Copy className="size-4 text-foreground" /> |
67 |
| - )} |
68 |
| - </Button> |
| 40 | + const handleClick = useCallback(() => { |
| 41 | + if (!onReadResource) return; |
| 42 | + if (!expanded) { |
| 43 | + setState((prev) => ({ ...prev, expanded: true, loading: true })); |
| 44 | + onReadResource(uri); |
| 45 | + setState((prev) => ({ ...prev, loading: false })); |
| 46 | + } else { |
| 47 | + setState((prev) => ({ ...prev, expanded: false })); |
| 48 | + } |
| 49 | + }, [expanded, onReadResource, uri]); |
69 | 50 |
|
70 |
| - <div className="pr-10"> |
71 |
| - <div className="flex items-start justify-between gap-2 mb-2"> |
72 |
| - <a |
73 |
| - href={uri} |
74 |
| - target="_blank" |
75 |
| - rel="noopener noreferrer" |
76 |
| - className="text-sm text-blue-600 dark:text-blue-400 hover:text-blue-800 dark:hover:text-blue-300 hover:underline focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-1 rounded px-1 py-0.5 break-all font-mono flex-1 min-w-0" |
77 |
| - aria-label={`Open resource: ${uri}`} |
78 |
| - > |
79 |
| - {uri} |
80 |
| - </a> |
81 |
| - {mimeType && ( |
82 |
| - <span className="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200 flex-shrink-0"> |
83 |
| - {mimeType} |
84 |
| - </span> |
85 |
| - )} |
86 |
| - </div> |
| 51 | + const handleKeyDown = useCallback( |
| 52 | + (e: React.KeyboardEvent) => { |
| 53 | + if ((e.key === "Enter" || e.key === " ") && onReadResource) { |
| 54 | + e.preventDefault(); |
| 55 | + handleClick(); |
| 56 | + } |
| 57 | + }, |
| 58 | + [handleClick, onReadResource], |
| 59 | + ); |
87 | 60 |
|
88 |
| - {name && ( |
89 |
| - <div className="font-semibold text-sm text-gray-900 dark:text-gray-100 mb-1"> |
90 |
| - {name} |
| 61 | + return ( |
| 62 | + <div className="text-sm text-foreground bg-secondary py-2 px-3 rounded"> |
| 63 | + <div |
| 64 | + className="flex justify-between items-center cursor-pointer focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-1 rounded" |
| 65 | + onClick={onReadResource ? handleClick : undefined} |
| 66 | + onKeyDown={onReadResource ? handleKeyDown : undefined} |
| 67 | + tabIndex={onReadResource ? 0 : -1} |
| 68 | + role="button" |
| 69 | + aria-expanded={expanded} |
| 70 | + aria-label={`${expanded ? "Collapse" : "Expand"} resource ${uri}`} |
| 71 | + > |
| 72 | + <div className="flex-1 min-w-0"> |
| 73 | + <div className="flex items-start justify-between gap-2 mb-1"> |
| 74 | + <span className="text-sm text-blue-600 dark:text-blue-400 hover:text-blue-800 dark:hover:text-blue-300 hover:underline px-1 py-0.5 break-all font-mono flex-1 min-w-0"> |
| 75 | + {uri} |
| 76 | + </span> |
| 77 | + <div className="flex items-center gap-2 flex-shrink-0"> |
| 78 | + {mimeType && ( |
| 79 | + <span className="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200"> |
| 80 | + {mimeType} |
| 81 | + </span> |
| 82 | + )} |
| 83 | + {onReadResource && ( |
| 84 | + <span className="ml-2 flex-shrink-0" aria-hidden="true"> |
| 85 | + {loading ? ( |
| 86 | + <div className="w-4 h-4 border-2 border-blue-500 border-t-transparent rounded-full animate-spin" /> |
| 87 | + ) : ( |
| 88 | + <span>{expanded ? "▼" : "▶"}</span> |
| 89 | + )} |
| 90 | + </span> |
| 91 | + )} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + {name && ( |
| 95 | + <div className="font-semibold text-sm text-gray-900 dark:text-gray-100 mb-1"> |
| 96 | + {name} |
| 97 | + </div> |
| 98 | + )} |
| 99 | + {description && ( |
| 100 | + <p className="text-sm text-gray-600 dark:text-gray-300 leading-relaxed"> |
| 101 | + {description} |
| 102 | + </p> |
| 103 | + )} |
91 | 104 | </div>
|
92 |
| - )} |
93 |
| - |
94 |
| - {description && ( |
95 |
| - <p className="text-sm text-gray-600 dark:text-gray-300 leading-relaxed"> |
96 |
| - {description} |
97 |
| - </p> |
98 |
| - )} |
| 105 | + </div> |
| 106 | + {expandedContent} |
99 | 107 | </div>
|
100 |
| - </div> |
101 |
| - ); |
102 |
| -}; |
| 108 | + ); |
| 109 | + }, |
| 110 | +); |
103 | 111 |
|
104 | 112 | export default ResourceLinkView;
|
0 commit comments