import { useOlmContext } from "@/contexts/olm-context"; import { AlertCircle, Info, KeyRound, ShieldCheck } from "lucide-react"; import { useEffect, useState } from "react"; import { Button } from "../ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "../ui/dialog"; import { Input } from "../ui/input"; export default function OlmPasswordDialog({ userId }: { userId: string }) { const [needsPassword, setNeedsPassword] = useState(false); const [password, setPasswordInput] = useState(""); const { setPassword, passwordError, clearPasswordError } = useOlmContext(); useEffect(() => { // The context handles loading & decrypting the password from sessionStorage. // We only need to show the dialog if the context doesn't have a password. // This will be handled by the passwordError effect below. // For initial load, we check if there's encrypted data - if not, show dialog. const hasStoredPassword = sessionStorage.getItem(`olm_password_${userId}`); if (!hasStoredPassword) { setNeedsPassword(true); } // If there IS stored data, the context will decrypt it and load it. // If decryption fails or password is wrong, passwordError will be set. }, [userId]); // Show dialog when there's a password error (wrong password was entered) useEffect(() => { if (passwordError) { setNeedsPassword(true); setPasswordInput(""); // Clear the input for retry } }, [passwordError]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (password.trim()) { setPassword(password); setNeedsPassword(false); } }; return ( e.preventDefault()} onEscapeKeyDown={(e) => e.preventDefault()}>
Encryption Password Required Enter your encryption password to access this conversation. This may be different from your login password.
{ setPasswordInput(e.target.value); if (passwordError) clearPasswordError(); }} /> {passwordError && (

{passwordError}

)}

You'll be asked to re-enter this password each time you start a new browser session.
When continuing, the window will be reloaded, please do not close the window or refresh the page by yourself.

Your password is encrypted before being stored in your browser's session storage using a secure key that cannot be exported.

) }