"use client"; import { useEffect, useState } from "react"; import { AppShell } from "../../components/app-shell"; type ApiResponse = { data: T; meta: { timestamp: string; version: "v1" }; error: null | { message: string; code?: string }; }; type TaxReturn = { id: string; taxYear: number; filingType: "individual" | "business"; jurisdictions: string[]; status: "draft" | "ready" | "exported"; updatedAt: string; }; export default function TaxPage() { const [returns, setReturns] = useState([]); const [status, setStatus] = useState(""); const [useLocal, setUseLocal] = useState(true); const [useSample, setUseSample] = useState(true); const [year, setYear] = useState(new Date().getFullYear()); const [filingType, setFilingType] = useState<"individual" | "business">("individual"); const [jurisdictions, setJurisdictions] = useState(["CA", "NY"]); const sampleProfile = { taxpayer: { name: "John Doe", ssn: "111-22-3333", dob: "1989-04-12", filingStatus: "Single", address: "123 Market St, San Francisco, CA" }, income: { w2: [{ employer: "Northwind Labs", wages: 92000, federalWithheld: 12000 }], interest: [{ payer: "City Bank", amount: 320 }], dividends: [{ payer: "Index Fund", amount: 480 }], selfEmployment: [{ business: "Doe Consulting", income: 28000, expenses: 6400 }] }, deductions: { standard: true, charitable: 600, studentLoanInterest: 900 }, credits: { education: 0, childTax: 0 }, documents: [ "W-2 (Northwind Labs)", "1099-INT (City Bank)", "1099-DIV (Index Fund)", "1099-NEC (Doe Consulting)", "Health Insurance 1095-A", "State withholding statement" ] }; const states = [ { code: "AL", name: "Alabama" }, { code: "AK", name: "Alaska" }, { code: "AZ", name: "Arizona" }, { code: "AR", name: "Arkansas" }, { code: "CA", name: "California" }, { code: "CO", name: "Colorado" }, { code: "CT", name: "Connecticut" }, { code: "DE", name: "Delaware" }, { code: "FL", name: "Florida" }, { code: "GA", name: "Georgia" }, { code: "HI", name: "Hawaii" }, { code: "ID", name: "Idaho" }, { code: "IL", name: "Illinois" }, { code: "IN", name: "Indiana" }, { code: "IA", name: "Iowa" }, { code: "KS", name: "Kansas" }, { code: "KY", name: "Kentucky" }, { code: "LA", name: "Louisiana" }, { code: "ME", name: "Maine" }, { code: "MD", name: "Maryland" }, { code: "MA", name: "Massachusetts" }, { code: "MI", name: "Michigan" }, { code: "MN", name: "Minnesota" }, { code: "MS", name: "Mississippi" }, { code: "MO", name: "Missouri" }, { code: "MT", name: "Montana" }, { code: "NE", name: "Nebraska" }, { code: "NV", name: "Nevada" }, { code: "NH", name: "New Hampshire" }, { code: "NJ", name: "New Jersey" }, { code: "NM", name: "New Mexico" }, { code: "NY", name: "New York" }, { code: "NC", name: "North Carolina" }, { code: "ND", name: "North Dakota" }, { code: "OH", name: "Ohio" }, { code: "OK", name: "Oklahoma" }, { code: "OR", name: "Oregon" }, { code: "PA", name: "Pennsylvania" }, { code: "RI", name: "Rhode Island" }, { code: "SC", name: "South Carolina" }, { code: "SD", name: "South Dakota" }, { code: "TN", name: "Tennessee" }, { code: "TX", name: "Texas" }, { code: "UT", name: "Utah" }, { code: "VT", name: "Vermont" }, { code: "VA", name: "Virginia" }, { code: "WA", name: "Washington" }, { code: "WV", name: "West Virginia" }, { code: "WI", name: "Wisconsin" }, { code: "WY", name: "Wyoming" } ]; const localKey = "ledgerone_tax_returns"; const ensureUserId = () => { if (typeof window === "undefined") { return ""; } let userId = localStorage.getItem("ledgerone_user_id"); if (!userId) { userId = `demo_${crypto.randomUUID()}`; localStorage.setItem("ledgerone_user_id", userId); } return userId; }; const loadReturns = async () => { if (useLocal && typeof window !== "undefined") { const raw = localStorage.getItem(localKey); setReturns(raw ? (JSON.parse(raw) as TaxReturn[]) : []); setStatus("Running in local-only mode."); return; } const userId = ensureUserId(); const query = userId ? `?user_id=${encodeURIComponent(userId)}` : ""; try { const res = await fetch(`/api/tax/returns${query}`); const payload = (await res.json()) as ApiResponse; if (!res.ok || payload.error) { throw new Error(payload.error?.message ?? "Unable to load returns."); } setReturns(payload.data); setUseLocal(false); return; } catch { if (typeof window !== "undefined") { const raw = localStorage.getItem(localKey); setReturns(raw ? (JSON.parse(raw) as TaxReturn[]) : []); setUseLocal(true); setStatus("Running in local-only mode (no backend)."); } } }; useEffect(() => { loadReturns().catch(() => { setStatus("Unable to load returns."); }); }, []); const createReturn = async () => { const userId = ensureUserId(); setStatus("Creating return..."); const payload = { userId, taxYear: year, filingType, jurisdictions }; if (useLocal) { const nextReturn: TaxReturn = { id: `local_${crypto.randomUUID()}`, taxYear: payload.taxYear, filingType: payload.filingType, jurisdictions: payload.jurisdictions, status: "draft", updatedAt: new Date().toISOString() }; const next = [...returns, nextReturn]; localStorage.setItem(localKey, JSON.stringify(next)); setReturns(next); setStatus("Return created locally."); return; } const res = await fetch("/api/tax/returns", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }); const response = (await res.json()) as ApiResponse; if (!res.ok || response.error) { setStatus(response.error?.message ?? "Unable to create return."); return; } setStatus("Return created."); await loadReturns(); }; const exportReturn = async (id: string) => { setStatus("Exporting return..."); if (useLocal) { const ret = returns.find((item) => item.id === id); const payload = { return: ret, documents: useSample ? sampleProfile.documents : [], sampleData: useSample ? sampleProfile : null }; const blob = new Blob([JSON.stringify(payload, null, 2)], { type: "application/json" }); const url = URL.createObjectURL(blob); window.open(url, "_blank", "noopener,noreferrer"); setStatus("Export ready (local)."); return; } const res = await fetch(`/api/tax/returns/${id}/export`, { method: "POST" }); const response = (await res.json()) as ApiResponse<{ return: TaxReturn; documents: unknown[]; }>; if (!res.ok || response.error) { setStatus(response.error?.message ?? "Export failed."); return; } const blob = new Blob([JSON.stringify(response.data, null, 2)], { type: "application/json" }); const url = URL.createObjectURL(blob); window.open(url, "_blank", "noopener,noreferrer"); setStatus("Export ready."); await loadReturns(); }; return (

Create a return

Sample dataset

Use John Doe sample intake data.

{useSample ? (

W-2 wages: $92,000 - 1099-NEC: $28,000

Standard deduction - CA + NY filings

) : null}
setYear(Number(event.target.value))} />

Hold Ctrl/Command to select multiple states.

Required documents

{sampleProfile.documents.map((doc) => (
{doc} Pending
))}
{status ?

{status}

: null}

Your returns

{returns.length ? ( returns.map((ret) => (

{ret.taxYear} - {ret.filingType}

States: {ret.jurisdictions.join(", ")}

{useSample ? (

Sample: John Doe

) : null}
{ret.status}
)) ) : (

No returns yet.

)}
); }