import { authClient } from '@/lib/auth-client'; import { Pressable, ScrollView, Text, View } from 'react-native'; import { useQuery, useZero } from "@rocicorp/zero/react"; import { queries, type Mutators, type Schema } from '@money/shared'; import { useEffect, useState } from 'react'; export default function HomeScreen() { const { data: session } = authClient.useSession(); const z = useZero(); const [transactions] = useQuery(queries.allTransactions(session)); const [balances] = useQuery(queries.getBalances(session)); const [idx, setIdx] = useState(0); const [accountIdx, setAccountIdx] = useState(0); const account = balances.at(accountIdx)!; const filteredTransactions = transactions .filter(t => t.account_id == account.plaid_id) useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "j") { setIdx((prevIdx) => { if (prevIdx + 1 == filteredTransactions.length) return prevIdx; return prevIdx + 1 }); } else if (event.key === "k") { setIdx((prevIdx) => prevIdx == 0 ? 0 : prevIdx - 1); } else if (event.key == 'g') { setIdx(0); } else if (event.key == "G") { setIdx(transactions.length - 1); } else if (event.key == 'R') { z.mutate.link.updateTransactions(); z.mutate.link.updateBalences(); } else if (event.key == 'h') { setAccountIdx((prevIdx) => prevIdx == 0 ? 0 : prevIdx - 1); } else if (event.key == 'l') { setAccountIdx((prevIdx) => { if (prevIdx + 1 == balances.length) return prevIdx; return prevIdx + 1 }); } }; window.addEventListener("keydown", handleKeyDown); return () => { window.removeEventListener("keydown", handleKeyDown); }; }, [filteredTransactions, balances]); function lpad(n: number): string { const LEN = 9; const nstr = n.toFixed(2).toLocaleString(); return Array.from({ length: LEN - nstr.length }).join(" ") + nstr; } return ( {balances.map((bal, i) => {bal.name}: {bal.current} ({bal.avaliable}) )} {filteredTransactions.map((t, i) => { setIdx(i); }} style={{ backgroundColor: i == idx ? 'black' : undefined, cursor: 'default' as 'auto' }} key={t.id}> {new Date(t.datetime!).toDateString()} 0 ? 'red' : 'green' }}>{lpad(t.amount)} {t.name.substring(0, 50)} )} {JSON.stringify(JSON.parse(filteredTransactions.at(idx)?.json || "null"), null, 4)} ); }