Skip to content

Commit f74e18d

Browse files
committed
feat: add FormatId component for displaying and copying truncated IDs
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent 72326bc commit f74e18d

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/custom/FormatId/FormatId.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Box, Tooltip, Typography } from '@mui/material';
2+
import _ from 'lodash';
3+
import React, { useState } from 'react';
4+
import { CopyIcon } from '../../icons';
5+
import { useTheme } from '../../theme';
6+
7+
interface FormatIdProps {
8+
id: string;
9+
}
10+
11+
export const FormatId: React.FC<FormatIdProps> = ({ id }) => {
12+
const [copied, setCopied] = useState(false);
13+
const theme = useTheme();
14+
15+
// Truncates the id to 15 characters and adds an ellipsis and adds a clipboard copy button
16+
const copyToClipboard = (e: React.MouseEvent<HTMLDivElement>) => {
17+
e.stopPropagation();
18+
navigator.clipboard.writeText(id);
19+
setCopied(true);
20+
21+
// Reset the copied status after a brief delay
22+
setTimeout(() => {
23+
setCopied(false);
24+
}, 2000);
25+
};
26+
27+
const truncatedId = _.truncate(id, { length: 15 });
28+
29+
return (
30+
<Box sx={{ display: 'flex', alignItems: 'center' }}>
31+
<Tooltip title={id} placement="top">
32+
<Typography
33+
variant="body2"
34+
style={{
35+
cursor: 'pointer',
36+
textWrap: 'nowrap',
37+
color: theme.palette.text.primary
38+
}}
39+
>
40+
{truncatedId}
41+
</Typography>
42+
</Tooltip>
43+
<Tooltip title={copied ? 'Copied!' : 'Copy'} placement="top">
44+
<div onClick={copyToClipboard} style={{ padding: '0.25rem' }}>
45+
<CopyIcon width={16} height={16} fill={theme.palette.action.active} />
46+
</div>
47+
</Tooltip>
48+
</Box>
49+
);
50+
};

src/custom/FormatId/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { FormatId } from './FormatId';
2+
3+
export { FormatId };

0 commit comments

Comments
 (0)