import { useEncryptionKey } from '@/contexts/encryption-key-context';
import { cn } from '@/lib/utils';
import { LockKeyhole, LockKeyholeOpen } from 'lucide-react';
import { useEffect, useState } from 'react';
import { Button } from './ui/button';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from './ui/dialog';
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from './ui/tooltip';
import UnlockMessageDialog from './unlock-message-dialog';
export function EncryptionKeyButton() {
const {
isKeySet,
encryptedMessageData,
fetchEncryptedMessage,
clearEncryptionKey,
} = useEncryptionKey();
const [showUnlockDialog, setShowUnlockDialog] = useState(false);
const [showClearDialog, setShowClearDialog] = useState(false);
useEffect(() => {
if (!encryptedMessageData) {
fetchEncryptedMessage();
}
}, [encryptedMessageData, fetchEncryptedMessage]);
function handleClick() {
if (isKeySet) {
setShowClearDialog(true);
} else {
if (!encryptedMessageData) {
fetchEncryptedMessage();
}
setShowUnlockDialog(true);
}
}
function handleClearKey() {
clearEncryptionKey();
setShowClearDialog(false);
}
function handleUnlock() {
setShowUnlockDialog(false);
}
return (
<>
{isKeySet
? 'Click to lock encryption key'
: 'Click to unlock encryption key'}
{encryptedMessageData && (
)}
>
);
}