Files
supabase/apps/studio/components/ui/CopyButton.tsx
Bobbie Soedirgo 90d355b9cb feat: show upgrade warning if user objects exist in internal schemas (#36654)
* feat: show upgrade warning if user objects exist in internal schemas

* Clean up warnings into a separate file

* Update API types + fix TS issues

* Update apps/studio/components/interfaces/Settings/Infrastructure/InfrastructureInfo.tsx

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-06-27 08:26:03 +00:00

81 lines
1.8 KiB
TypeScript

import { Check, Clipboard } from 'lucide-react'
import { ComponentProps, forwardRef, useEffect, useState } from 'react'
import { Button, cn, copyToClipboard } from 'ui'
type CopyButtonBaseProps = {
iconOnly?: boolean
copyLabel?: string
copiedLabel?: string
}
type CopyButtonWithText = CopyButtonBaseProps & {
text: string
asyncText?: never
}
type CopyButtonWithAsyncText = CopyButtonBaseProps & {
text?: never
asyncText: () => Promise<string> | string
}
export type CopyButtonProps = (CopyButtonWithText | CopyButtonWithAsyncText) &
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)
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