mirror of
https://github.com/andwati/dokploy.git
synced 2026-11-17 09:25:20 +00:00
feat: add functionality to download
refactor the copy feature to use `copy-to-clipboard`.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { CopyIcon, Fingerprint, QrCode } from "lucide-react";
|
import copy from "copy-to-clipboard";
|
||||||
|
import { CopyIcon, DownloadIcon, Fingerprint, QrCode } from "lucide-react";
|
||||||
import QRCode from "qrcode";
|
import QRCode from "qrcode";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
@@ -184,6 +185,34 @@ export const Enable2FA = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleDownloadBackupCodes = () => {
|
||||||
|
if (!backupCodes || backupCodes.length === 0) {
|
||||||
|
toast.error("No backup codes to download.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const backupCodesText = backupCodes.join("\n");
|
||||||
|
const date = new Date();
|
||||||
|
const year = date.getFullYear();
|
||||||
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
||||||
|
const day = String(date.getDate()).padStart(2, "0");
|
||||||
|
const filename = `dokploy-2fa-backup-codes-${year}${month}${day}.txt`;
|
||||||
|
const blob = new Blob([backupCodesText], { type: "text/plain" });
|
||||||
|
const url = URL.createObjectURL(blob);
|
||||||
|
const a = document.createElement("a");
|
||||||
|
a.href = url;
|
||||||
|
a.download = filename;
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
document.body.removeChild(a);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCopyBackupCodes = () => {
|
||||||
|
copy(backupCodes.join("\n"));
|
||||||
|
toast.success("Backup codes copied to clipboard");
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
|
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
|
||||||
<DialogTrigger asChild>
|
<DialogTrigger asChild>
|
||||||
@@ -289,30 +318,43 @@ export const Enable2FA = () => {
|
|||||||
<div className="w-full space-y-3 border rounded-lg p-4">
|
<div className="w-full space-y-3 border rounded-lg p-4">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<h4 className="font-medium">Backup Codes</h4>
|
<h4 className="font-medium">Backup Codes</h4>
|
||||||
<TooltipProvider>
|
<div className="flex items-center gap-2">
|
||||||
<Tooltip delayDuration={0}>
|
<TooltipProvider>
|
||||||
<TooltipTrigger>
|
<Tooltip delayDuration={0}>
|
||||||
<Button
|
<TooltipTrigger asChild>
|
||||||
type="button"
|
<Button
|
||||||
variant="outline"
|
type="button"
|
||||||
size="icon"
|
variant="outline"
|
||||||
onClick={() => {
|
size="icon"
|
||||||
navigator.clipboard.writeText(
|
onClick={handleCopyBackupCodes}
|
||||||
backupCodes.join("\n"),
|
>
|
||||||
);
|
<CopyIcon className="size-4" />
|
||||||
toast.success(
|
</Button>
|
||||||
"Backup codes copied to clipboard",
|
</TooltipTrigger>
|
||||||
);
|
<TooltipContent>
|
||||||
}}
|
<p>Copy</p>
|
||||||
>
|
</TooltipContent>
|
||||||
<CopyIcon className="size-4" />
|
</Tooltip>
|
||||||
</Button>
|
</TooltipProvider>
|
||||||
</TooltipTrigger>
|
|
||||||
<TooltipContent>
|
<TooltipProvider>
|
||||||
<p>Copy</p>
|
<Tooltip delayDuration={0}>
|
||||||
</TooltipContent>
|
<TooltipTrigger asChild>
|
||||||
</Tooltip>
|
<Button
|
||||||
</TooltipProvider>
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
size="icon"
|
||||||
|
onClick={handleDownloadBackupCodes}
|
||||||
|
>
|
||||||
|
<DownloadIcon className="size-4" />
|
||||||
|
</Button>
|
||||||
|
</TooltipTrigger>
|
||||||
|
<TooltipContent>
|
||||||
|
<p>Download</p>
|
||||||
|
</TooltipContent>
|
||||||
|
</Tooltip>
|
||||||
|
</TooltipProvider>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="grid grid-cols-2 gap-2">
|
<div className="grid grid-cols-2 gap-2">
|
||||||
{backupCodes.map((code, index) => (
|
{backupCodes.map((code, index) => (
|
||||||
|
|||||||
Reference in New Issue
Block a user