import { useQuery, useZero } from "@rocicorp/zero/react"; import { queries, type Mutators, type Schema } from "@money/shared"; import { use, useEffect, useState } from "react"; import { RouterContext } from ".."; import { View, Text, Linking } from "react-native"; import { Button } from "../../components/Button"; import * as Table from "../../components/Table"; import * as Dialog from "../../components/Dialog"; const COLUMNS: Table.Column[] = [ { name: "name", label: "Name" }, { name: "createdAt", label: "Added At", render: (n) => new Date(n).toLocaleString(), }, ]; export function Accounts() { const { auth } = use(RouterContext); const [items] = useQuery(queries.getItems(auth)); const [deleting, setDeleting] = useState([]); const [isAddOpen, setIsAddOpen] = useState(false); const z = useZero(); const onDelete = () => { if (!deleting) return; const accountIds = deleting.map((account) => account.id); z.mutate.link.deleteAccounts({ accountIds }); setDeleting([]); }; const addAccount = () => { setIsAddOpen(true); }; return ( <> 0} close={() => setDeleting([])} > Delete Account You are about to delete the following accounts: {deleting.map((account) => ( - {account.name} ))} setIsAddOpen(false)}> Add Account { if (key.name == "d") { setDeleting(selected); } }} > ); } function AddAccount() { const { auth } = use(RouterContext); const [link, details] = useQuery(queries.getPlaidLink(auth)); const { close } = use(Dialog.Context); const openLink = () => { if (!link) return; Linking.openURL(link.link); }; const z = useZero(); useEffect(() => { console.log(link, details); if (details.type != "complete") return; if (link != undefined) { if (!link.completeAt) { const timer = setInterval(() => { console.log("Checking for link"); z.mutate.link.get({ link_token: link.token }); }, 1000 * 5); return () => clearInterval(timer); } else { if (close) close(); return; } } console.log("Creating new link"); z.mutate.link.create(); }, [link, details]); return ( <> {link ? ( <> Please click the button to complete setup. ) : ( Loading Plaid Link )} ); }