Split Reveal/Copy button for API key (#3180)
This commit is contained in:
@@ -1,8 +1,19 @@
|
||||
import { useState } from "react";
|
||||
import { Button } from "./button";
|
||||
import { Input } from "./input";
|
||||
import { CheckIcon, CopyIcon } from "@radix-ui/react-icons";
|
||||
import {
|
||||
CheckIcon,
|
||||
CopyIcon,
|
||||
EyeOpenIcon,
|
||||
EyeClosedIcon,
|
||||
} from "@radix-ui/react-icons";
|
||||
import { copyText } from "@/util/copyText";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipProvider,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
|
||||
type Props = {
|
||||
value: string;
|
||||
@@ -10,36 +21,78 @@ type Props = {
|
||||
|
||||
function HiddenCopyableInput({ value }: Props) {
|
||||
const [hidden, setHidden] = useState(true);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const [revealTooltipOpen, setRevealTooltipOpen] = useState(false);
|
||||
|
||||
const [copied, setCopied] = useState(false);
|
||||
const [copyTooltipOpen, setCopyTooltipOpen] = useState(false);
|
||||
|
||||
const buttonText = hidden ? "Reveal" : copied ? "Copied" : "Copy";
|
||||
const inputValue = hidden ? "**** **** **** ****" : value;
|
||||
|
||||
const handleToggleHidden = () => {
|
||||
setRevealTooltipOpen(false);
|
||||
setHidden((prev) => !prev);
|
||||
setTimeout(() => setRevealTooltipOpen(true), 10);
|
||||
};
|
||||
|
||||
const handleCopy = () => {
|
||||
copyText(value).then(() => {
|
||||
setCopied(true);
|
||||
setCopyTooltipOpen(true);
|
||||
|
||||
setTimeout(() => {
|
||||
setCopyTooltipOpen(false);
|
||||
setTimeout(() => setCopied(false), 200);
|
||||
}, 3000);
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="relative w-full">
|
||||
<Input value={inputValue} className="h-10" readOnly />
|
||||
<div className="absolute inset-y-0 right-1 flex items-center">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
className="cursor-pointer"
|
||||
onClick={() => {
|
||||
if (hidden) {
|
||||
setHidden(false);
|
||||
return;
|
||||
}
|
||||
copyText(value).then(() => {
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 3000);
|
||||
});
|
||||
}}
|
||||
>
|
||||
{!hidden && !copied && <CopyIcon className="mr-2 h-4 w-4" />}
|
||||
{!hidden && copied && <CheckIcon className="mr-2 h-4 w-4" />}
|
||||
{buttonText}
|
||||
</Button>
|
||||
<TooltipProvider delayDuration={200}>
|
||||
<div className="relative w-full">
|
||||
<Input value={inputValue} className="h-10 pr-[7rem]" readOnly />
|
||||
<div className="absolute inset-y-0 right-1 flex items-center gap-1">
|
||||
<Tooltip open={revealTooltipOpen} onOpenChange={setRevealTooltipOpen}>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
onClick={handleToggleHidden}
|
||||
onMouseEnter={() => setRevealTooltipOpen(true)}
|
||||
onMouseLeave={() => setRevealTooltipOpen(false)}
|
||||
>
|
||||
{hidden ? (
|
||||
<EyeOpenIcon className="h-4 w-4" />
|
||||
) : (
|
||||
<EyeClosedIcon className="h-4 w-4" />
|
||||
)}
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="top">
|
||||
{hidden ? "Reveal" : "Hide"}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
<Tooltip
|
||||
open={copyTooltipOpen}
|
||||
onOpenChange={(open) => {
|
||||
if (!copied) setCopyTooltipOpen(open);
|
||||
}}
|
||||
>
|
||||
<TooltipTrigger asChild>
|
||||
<Button size="sm" variant="secondary" onClick={handleCopy}>
|
||||
{copied ? (
|
||||
<CheckIcon className="h-4 w-4" />
|
||||
) : (
|
||||
<CopyIcon className="h-4 w-4" />
|
||||
)}
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="top">
|
||||
{copied ? "Copied!" : "Copy"}
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</TooltipProvider>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user