Skip to content

Commit a037765

Browse files
committed
added copy button
1 parent f2aa8e1 commit a037765

File tree

2 files changed

+66
-40
lines changed

2 files changed

+66
-40
lines changed

components/PriceFeedTable.tsx

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react'
1+
import { useState, useEffect } from "react";
22
import {
33
Table,
44
TableHead,
@@ -8,46 +8,56 @@ import {
88
TextField,
99
Select,
1010
MenuItem,
11-
Box
12-
} from '@mui/material'
13-
import { HermesClient } from '@pythnetwork/hermes-client'
11+
Box,
12+
Snackbar,
13+
} from "@mui/material";
14+
import { HermesClient } from "@pythnetwork/hermes-client";
15+
import copy from "copy-to-clipboard";
1416

1517
interface PriceFeed {
16-
id: string
17-
name: string
18-
assetType: string
18+
id: string;
19+
name: string;
20+
assetType: string;
1921
}
2022

2123
export function PriceFeedTable() {
22-
const [priceFeeds, setPriceFeeds] = useState<PriceFeed[]>([])
23-
const [searchTerm, setSearchTerm] = useState("")
24-
const [selectedAssetType, setSelectedAssetType] = useState("All")
24+
const [priceFeeds, setPriceFeeds] = useState<PriceFeed[]>([]);
25+
const [searchTerm, setSearchTerm] = useState("");
26+
const [selectedAssetType, setSelectedAssetType] = useState("All");
27+
const [openSnackbar, setOpenSnackbar] = useState(false);
2528

2629
useEffect(() => {
2730
const fetchPriceFeeds = async () => {
28-
const hermesClient = new HermesClient("https://hermes.pyth.network")
29-
const feeds = await hermesClient.getPriceFeeds()
30-
const transformedFeeds = feeds.map(feed => ({
31+
const hermesClient = new HermesClient("https://hermes.pyth.network");
32+
const feeds = await hermesClient.getPriceFeeds();
33+
const transformedFeeds = feeds.map((feed) => ({
3134
id: feed.id,
32-
name: feed.attributes.display_symbol || '',
33-
assetType: feed.attributes.asset_type || ''
34-
}))
35-
setPriceFeeds(transformedFeeds)
36-
}
37-
38-
fetchPriceFeeds()
39-
}, [])
35+
name: feed.attributes.display_symbol || "",
36+
assetType: feed.attributes.asset_type || "",
37+
}));
38+
setPriceFeeds(transformedFeeds);
39+
};
4040

41-
const filteredData = priceFeeds.filter(feed => {
42-
const matchesSearch = feed.id.toLowerCase().includes(searchTerm.toLowerCase()) ||
43-
feed.name.toLowerCase().includes(searchTerm.toLowerCase())
44-
const matchesAssetType = selectedAssetType === "All" || feed.assetType === selectedAssetType
45-
return matchesSearch && matchesAssetType
46-
})
41+
fetchPriceFeeds();
42+
}, []);
43+
44+
const filteredData = priceFeeds.filter((feed) => {
45+
const matchesSearch =
46+
feed.id.toLowerCase().includes(searchTerm.toLowerCase()) ||
47+
feed.name.toLowerCase().includes(searchTerm.toLowerCase());
48+
const matchesAssetType =
49+
selectedAssetType === "All" || feed.assetType === selectedAssetType;
50+
return matchesSearch && matchesAssetType;
51+
});
52+
53+
const handleCopy = (id: string) => {
54+
copy(id);
55+
setOpenSnackbar(true);
56+
};
4757

4858
return (
4959
<Box>
50-
<Box sx={{ mb: 3, display: 'flex', gap: 2 }}>
60+
<Box sx={{ mb: 3, display: "flex", gap: 2 }}>
5161
<TextField
5262
label="Search price feeds"
5363
variant="outlined"
@@ -63,11 +73,13 @@ export function PriceFeedTable() {
6373
sx={{ width: 200 }}
6474
>
6575
<MenuItem value="All">All Asset Types</MenuItem>
66-
{Array.from(new Set(priceFeeds.map(feed => feed.assetType))).map(type => (
67-
<MenuItem key={type} value={type}>
68-
{type || 'Uncategorized'}
69-
</MenuItem>
70-
))}
76+
{Array.from(new Set(priceFeeds.map((feed) => feed.assetType))).map(
77+
(type) => (
78+
<MenuItem key={type} value={type}>
79+
{type || "Uncategorized"}
80+
</MenuItem>
81+
)
82+
)}
7183
</Select>
7284
</Box>
7385

@@ -84,11 +96,28 @@ export function PriceFeedTable() {
8496
<TableRow key={feed.id}>
8597
<TableCell>{feed.name}</TableCell>
8698
<TableCell>{feed.assetType}</TableCell>
87-
<TableCell>{feed.id}</TableCell>
99+
<TableCell
100+
onClick={() => handleCopy(feed.id)}
101+
sx={{
102+
cursor: "pointer",
103+
"&:hover": {
104+
backgroundColor: "#BB86FC",
105+
},
106+
}}
107+
>
108+
{feed.id}
109+
</TableCell>
88110
</TableRow>
89111
))}
90112
</TableBody>
91113
</Table>
114+
115+
<Snackbar
116+
open={openSnackbar}
117+
autoHideDuration={2000}
118+
onClose={() => setOpenSnackbar(false)}
119+
message="Feed ID copied to clipboard"
120+
/>
92121
</Box>
93-
)
94-
}
122+
);
123+
}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Price Feed IDs
22

3-
import { PriceFeedTable } from '@/components/PriceFeedTable'
3+
import { PriceFeedTable } from "@/components/PriceFeedTable";
44

55
<PriceFeedTable />
6-
7-
8-

0 commit comments

Comments
 (0)