import { useEffect, useState } from 'react'; import { Head } from '@inertiajs/react'; import axios from 'axios'; import UnlockMessageDialog from '@/components/unlock-message-dialog'; 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 AppLayout from '@/layouts/app-layout'; import { base64ToBuffer, decrypt, importKey } from '@/lib/crypto'; import { clearKey, getStoredKey } from '@/lib/key-storage'; import { dashboard } from '@/routes'; import { type BreadcrumbItem } from '@/types'; const breadcrumbs: BreadcrumbItem[] = [ { title: 'Dashboard', href: dashboard().url, }, ]; interface EncryptedMessageData { encrypted_content: string; iv: string; salt: string; } export default function Dashboard() { 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(); }, []); 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); clearKey(); } } } 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); } } function handleUnlock(message: string) { setDecryptedMessage(message); } function handleLock() { clearKey(); setDecryptedMessage(null); } return (
Encrypted Message {loading ? (
) : error ? (
{error}
) : decryptedMessage ? (

{decryptedMessage}

) : messageData ? (

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

) : null}
{messageData && ( )}
); }