|
| 1 | +import { useState, useCallback } from "react"; |
| 2 | +import { StyledTd } from "./Table"; |
| 3 | + |
| 4 | +interface PriceFeed { |
| 5 | + ids: string; |
| 6 | + assetType: string; |
| 7 | + name: string; |
| 8 | +} |
| 9 | + |
| 10 | +interface TableColumnWidths { |
| 11 | + assetType: string; |
| 12 | + name: string; |
| 13 | + ids: any; |
| 14 | +} |
| 15 | + |
| 16 | +const columnWidths: TableColumnWidths = { |
| 17 | + assetType: "w-3/10", |
| 18 | + name: "w-1/5", |
| 19 | + ids: "w-1/2", |
| 20 | +}; |
| 21 | + |
| 22 | +const PriceFeedTable = ({ priceFeeds }: { priceFeeds: PriceFeed[] }) => { |
| 23 | + const [selectedAssetType, setSelectedAssetType] = useState<string>("All"); |
| 24 | + const [copiedId, setCopiedId] = useState<string | null>(null); |
| 25 | + |
| 26 | + const assetTypes = [ |
| 27 | + "All", |
| 28 | + ...Array.from(new Set(priceFeeds.map((feed) => feed.assetType))), |
| 29 | + ]; |
| 30 | + |
| 31 | + const filteredFeeds = |
| 32 | + selectedAssetType === "All" |
| 33 | + ? priceFeeds |
| 34 | + : priceFeeds.filter((feed) => feed.assetType === selectedAssetType); |
| 35 | + |
| 36 | + const copyToClipboard = useCallback((text: string) => { |
| 37 | + navigator.clipboard |
| 38 | + .writeText(text) |
| 39 | + .then(() => { |
| 40 | + setCopiedId(text); |
| 41 | + setTimeout(() => setCopiedId(null), 2000); // Hide the popup after 2 seconds |
| 42 | + }) |
| 43 | + .catch((err) => { |
| 44 | + console.error("Failed to copy: ", err); |
| 45 | + }); |
| 46 | + }, []); |
| 47 | + |
| 48 | + return ( |
| 49 | + <div> |
| 50 | + <div className="mb-4"> |
| 51 | + <label htmlFor="assetTypeFilter" className="mr-2"> |
| 52 | + Filter by Asset Type: |
| 53 | + </label> |
| 54 | + <select |
| 55 | + id="assetTypeFilter" |
| 56 | + value={selectedAssetType} |
| 57 | + onChange={(e) => setSelectedAssetType(e.target.value)} |
| 58 | + className="p-2 border rounded" |
| 59 | + > |
| 60 | + {assetTypes.map((type) => ( |
| 61 | + <option key={type} value={type}> |
| 62 | + {type} |
| 63 | + </option> |
| 64 | + ))} |
| 65 | + </select> |
| 66 | + </div> |
| 67 | + <table> |
| 68 | + <thead> |
| 69 | + <tr> |
| 70 | + <th className={columnWidths.assetType}>Asset Type</th> |
| 71 | + <th className={columnWidths.name}>Name</th> |
| 72 | + <th className={columnWidths.ids}>Feed ID</th> |
| 73 | + </tr> |
| 74 | + </thead> |
| 75 | + <tbody> |
| 76 | + {filteredFeeds.map((feed, index) => ( |
| 77 | + <tr key={index}> |
| 78 | + <StyledTd>{feed.assetType}</StyledTd> |
| 79 | + <StyledTd>{feed.name}</StyledTd> |
| 80 | + <StyledTd> |
| 81 | + <div className="relative"> |
| 82 | + <button |
| 83 | + onClick={() => copyToClipboard(feed.ids)} |
| 84 | + className="flex items-center space-x-2 px-2 py-1 bg-gray-100 hover:bg-gray-200 dark:bg-darkGray2 dark:hover:bg-darkGray3 rounded transition duration-200" |
| 85 | + > |
| 86 | + <code className="dark:text-darkLinks text-lightLinks dark:bg-darkGray2 bg-gray-100 px-2 py-1 rounded"> |
| 87 | + {feed.ids} |
| 88 | + </code> |
| 89 | + <svg |
| 90 | + xmlns="http://www.w3.org/2000/svg" |
| 91 | + className="h-4 w-4" |
| 92 | + fill="none" |
| 93 | + viewBox="0 0 24 24" |
| 94 | + stroke="currentColor" |
| 95 | + > |
| 96 | + <path |
| 97 | + strokeLinecap="round" |
| 98 | + strokeLinejoin="round" |
| 99 | + strokeWidth={2} |
| 100 | + d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z" |
| 101 | + /> |
| 102 | + </svg> |
| 103 | + </button> |
| 104 | + {copiedId === feed.ids && ( |
| 105 | + <div className="absolute bottom-full left-1/2 transform -translate-x-1/2 mb-2 px-2 py-1 bg-gray-800 text-white text-sm rounded shadow"> |
| 106 | + Copied to clipboard |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + </StyledTd> |
| 111 | + </tr> |
| 112 | + ))} |
| 113 | + </tbody> |
| 114 | + </table> |
| 115 | + </div> |
| 116 | + ); |
| 117 | +}; |
| 118 | + |
| 119 | +export default PriceFeedTable; |
0 commit comments