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