Skip to content

Commit

Permalink
hide api keys unless hover + implement copy to clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikKaum authored and jakubno committed Jul 15, 2024
1 parent 57895e3 commit 2e71880
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions apps/docs/src/components/Dashboard/General.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const GeneralContent = () => {
const [isDialogOpen, setIsDialogOpen] = useState(false)
const [apiKeys, setApiKeys] = useState(fakeApiKeys)
const [currentKeyId, setCurrentKeyId] = useState<string | null>(null)
const [hoveredKeyId, setHoveredKeyId] = useState<string | null>(null)

const closeDialog = () => setIsDialogOpen(false)
const openDialog = (id: string) => {
Expand All @@ -64,6 +65,21 @@ export const GeneralContent = () => {
}
setApiKeys([...apiKeys, newKey])
}

const copyToClipboard = (text: string) => {
navigator.clipboard.writeText(text)
toast({
title: 'API Key copied to clipboard',
})
}

const maskApiKey = (key: string) => {
const firstFour = key.slice(0, 4)
const lastTwo = key.slice(-2)
const stars = '*'.repeat(key.length - 6) // Use '#' or another fixed-width character
return `${firstFour}${stars}${lastTwo}`
}

return(
<div className="flex flex-col w-full h-full">
<div className="flex flex-col w-fit h-full">
Expand All @@ -75,10 +91,15 @@ export const GeneralContent = () => {
<p className='text-neutral-300 pb-2'>Active keys:</p>

{apiKeys.map((apiKey) => (
<div key={apiKey.id} className='flex w-full justify-between items-center border border-white/5 rounded-lg p-2 mb-4 space-x-4'>
<div>{apiKey.key}</div>
<div
key={apiKey.id}
className='flex w-full justify-between items-center border border-white/5 rounded-lg p-2 mb-4 space-x-4'
onMouseEnter={() => setHoveredKeyId(apiKey.id)}
onMouseLeave={() => setHoveredKeyId(null)}
>
<div className="font-mono text-sm">{hoveredKeyId === apiKey.id ? apiKey.key : maskApiKey(apiKey.key)}</div> {/* Use a monospace font */}
<div className='flex items-center space-x-2'>
<Copy className='hover:cursor-pointer' width={18} height={18} />
<Copy className='hover:cursor-pointer' width={18} height={18} onClick={() => copyToClipboard(apiKey.key)} />
<Delete className='hover:cursor-pointer' color='red' width={20} height={20} onClick={() => openDialog(apiKey.id)} />
</div>
</div>
Expand Down

0 comments on commit 2e71880

Please sign in to comment.