import { decryptDataWithStoredPassword, encryptDataWithStoredPassword, getEncryptionPassword, MakeNewDate, } from "../helpers.ts"; import { Income } from "../services/financhalServices.ts"; import { useEffect, useState } from "preact/hooks"; import { PopAddIncomeDialog } from "./dialogs/PopAddIncomeDialog.tsx"; interface Props { incomes: Income[]; userId?: string; openModal?: boolean; } export default function IncomeList( { incomes, userId, openModal = false }: Props, ) { // Theme classes are defined in styles.css and respond to theme mode + media. const [_popAddIncome, popupAddIncomeDialog] = PopAddIncomeDialog({ userId, defaultOpen: openModal, }); const [processedIncomes, setProcessedIncomes] = useState(incomes); const [_isProcessing, setIsProcessing] = useState(false); const [isOpeningModal, setIsOpeningModal] = useState(openModal); // Process incomes on component mount and when incomes or userId change useEffect(() => { setTimeout(() => { const lockRoute = localStorage.getItem("lock-route"); const url = new URL(globalThis.location.href); const currentRoute = url.pathname + url.search; if (lockRoute && lockRoute === currentRoute) { localStorage.removeItem("lock-route"); } }, 100); if (!userId) { setProcessedIncomes(incomes); return; } const processIncomes = async () => { setIsProcessing(true); const encryptionPassword = getEncryptionPassword(userId); if (!encryptionPassword) { // No encryption key available, use incomes as-is setProcessedIncomes(incomes); setIsProcessing(false); return; } const updatedIncomes: Income[] = []; for (const income of incomes) { const processedIncome = { ...income }; if (income.encrypted_data) { // Try to decrypt existing encrypted data try { const decryptedData = await decryptDataWithStoredPassword( userId, income.encrypted_data, ); if (decryptedData) { const parsed = JSON.parse(decryptedData); if (parsed.amount !== undefined) { processedIncome.amount = parsed.amount; } if (parsed.source !== undefined) { processedIncome.source = parsed.source; } } } catch (error) { console.error("Failed to decrypt income data:", error); } } else if (income.amount !== null || income.source !== null) { // No encrypted data but has plaintext data - encrypt it and clear plaintext try { const dataToEncrypt = { amount: income.amount, source: income.source, }; const encryptedData = await encryptDataWithStoredPassword( userId, JSON.stringify(dataToEncrypt), ); if (encryptedData) { // Update the income in the database const response = await fetch("/api/update/income", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ income_id: income.id, encrypted_data: encryptedData, source: null, // Clear plaintext source amount: null, // Clear plaintext amount }), }); if (response.ok) { // Update local state processedIncome.encrypted_data = encryptedData; processedIncome.source = income.source; // Keep for display processedIncome.amount = income.amount; // Keep for display } } } catch (error) { console.error("Failed to encrypt income data:", error); } } updatedIncomes.push(processedIncome); } setProcessedIncomes(updatedIncomes); setIsProcessing(false); }; processIncomes(); }, []); return ( <> {popupAddIncomeDialog}

Your Incomes

Back to Financial Calendar
); }