Files
supabase/apps/studio/components/ui/CopyButton.tsx
T
9318404e61 Feat/api keys rollout prep (#35559)
* first pass

* init

* updated types

* fix up key reveal

* Update QuickKeyCopy.tsx

* remove quick key copy

* api key pill now only allows reveal and copy if you have perm

* Update LegacyAPIKeys.tsx

* fix up layouts

* fix copy

* Fix action menu dropdown position, few small nudges

* Remove unused files.

* Remove the hardcoded and rename the feature flag for basic API keys.

* add support for name and description, some smaller improvements

* Fix the trims for the description.

---------

Co-authored-by: Terry Sutton <saltcod@gmail.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
Co-authored-by: Stojan Dimitrovski <sdimitrovski@gmail.com>
2025-05-27 15:50:42 +02:00

83 lines
1.9 KiB
TypeScript

import { Check, Clipboard } from 'lucide-react'
import React, { forwardRef, useEffect, useState } from 'react'
import { copyToClipboard } from 'lib/helpers'
import { Button, cn } from 'ui'
type CopyButtonBaseProps = {
iconOnly?: boolean
copyLabel?: string
copiedLabel?: string
onCopy?: () => Promise<string> | string
}
type CopyButtonWithText = CopyButtonBaseProps & {
text: string
asyncText?: never
}
type CopyButtonWithAsyncText = CopyButtonBaseProps & {
text?: never
asyncText: () => Promise<string> | string
}
export type CopyButtonProps = (CopyButtonWithText | CopyButtonWithAsyncText) &
React.ComponentProps<typeof Button>
const CopyButton = forwardRef<HTMLButtonElement, CopyButtonProps>(
(
{
text,
asyncText,
iconOnly = false,
children,
onClick,
copyLabel = 'Copy',
copiedLabel = 'Copied',
...props
},
ref
) => {
const [showCopied, setShowCopied] = useState(false)
useEffect(() => {
if (!showCopied) return
const timer = setTimeout(() => setShowCopied(false), 2000)
return () => clearTimeout(timer)
}, [showCopied])
return (
<Button
ref={ref}
onClick={async (e) => {
const textToCopy = asyncText ? await asyncText() : text
setShowCopied(true)
await copyToClipboard(textToCopy!)
onClick?.(e)
}}
{...props}
className={cn(
{
'px-1': iconOnly,
// '!pointer-events-auto': props.disabled,
},
props.className
)}
icon={
showCopied ? (
<Check strokeWidth={2} className="text-brand" />
) : (
props.icon ?? <Clipboard />
)
}
>
{!iconOnly && <>{children ?? (showCopied ? copiedLabel : copyLabel)}</>}
</Button>
)
}
)
CopyButton.displayName = 'CopyButton'
export default CopyButton