diff --git a/resources/js/pages/dashboard.tsx b/resources/js/pages/dashboard.tsx index 2b7dfbca..2ed65b5c 100644 --- a/resources/js/pages/dashboard.tsx +++ b/resources/js/pages/dashboard.tsx @@ -1,16 +1,7 @@ import { Head } from '@inertiajs/react'; -import axios from 'axios'; -import { useEffect, useState } from 'react'; -import { Button } from '@/components/ui/button'; -import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { PlaceholderPattern } from '@/components/ui/placeholder-pattern'; -import { Skeleton } from '@/components/ui/skeleton'; -import UnlockMessageDialog from '@/components/unlock-message-dialog'; -import { useEncryptionKey } from '@/contexts/encryption-key-context'; import AppLayout from '@/layouts/app-layout'; -import { decrypt, importKey } from '@/lib/crypto'; -import { getStoredKey } from '@/lib/key-storage'; import { dashboard } from '@/routes'; import { type BreadcrumbItem } from '@/types'; @@ -21,159 +12,15 @@ const breadcrumbs: BreadcrumbItem[] = [ }, ]; -interface EncryptedMessageData { - encrypted_content: string; - iv: string; - salt: string; -} - export default function Dashboard() { - const { clearEncryptionKey, isKeySet } = useEncryptionKey(); - const [messageData, setMessageData] = useState( - null, - ); - const [decryptedMessage, setDecryptedMessage] = useState( - null, - ); - const [showUnlockDialog, setShowUnlockDialog] = useState(false); - const [loading, setLoading] = useState(true); - const [error, setError] = useState(null); - - useEffect(() => { - fetchAndDecryptMessage(); - }, []); - - useEffect(() => { - if (!isKeySet && decryptedMessage) { - setDecryptedMessage(null); - } else if (isKeySet && !decryptedMessage && messageData) { - attemptAutoDecrypt(); - } - }, [isKeySet]); - - async function fetchAndDecryptMessage() { - setLoading(true); - setError(null); - - try { - const response = await axios.get( - '/api/encryption/message', - ); - const data = response.data; - setMessageData(data); - - const storedKey = getStoredKey(); - - if (storedKey) { - try { - const aesKey = await importKey(storedKey); - const decrypted = await decrypt( - data.encrypted_content, - aesKey, - data.iv, - ); - setDecryptedMessage(decrypted); - } catch (err) { - console.error('Auto-decrypt failed:', err); - clearEncryptionKey(); - } - } - } catch (err: any) { - if (err.response?.status === 404) { - setError( - 'No encrypted message found. Please set up encryption first.', - ); - } else { - console.error('Fetch error:', err); - setError('Failed to load encrypted message.'); - } - } finally { - setLoading(false); - } - } - - async function attemptAutoDecrypt() { - if (!messageData) return; - - const storedKey = getStoredKey(); - if (!storedKey) return; - - try { - const aesKey = await importKey(storedKey); - const decrypted = await decrypt( - messageData.encrypted_content, - aesKey, - messageData.iv, - ); - setDecryptedMessage(decrypted); - } catch (err) { - console.error('Auto-decrypt failed:', err); - clearEncryptionKey(); - } - } - - function handleUnlock(message: string) { - setDecryptedMessage(message); - } - - function handleLock() { - clearEncryptionKey(); - setDecryptedMessage(null); - } - return (
- - - Encrypted Message - - - {loading ? ( -
- - -
- ) : error ? ( -
- {error} -
- ) : decryptedMessage ? ( -
-

- {decryptedMessage} -

- -
- ) : messageData ? ( -
-

- {messageData.encrypted_content.substring( - 0, - 100, - )} - ... -

- -
- ) : null} -
-
- +
+ +
@@ -185,17 +32,6 @@ export default function Dashboard() {
- - {messageData && ( - - )}
); }