|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { parseJSONData } from "./utils/dataParser"; |
| 3 | +import { AccountData, PositionData } from "./types"; |
| 4 | +import SummaryCard from "./components/SummaryCard"; |
| 5 | +import PositionsTable from "./components/PositionsTable"; |
| 6 | +import PortfolioChart from "./components/PortfolioChart"; |
| 7 | +import FileDropZone from "./components/FileDropZone"; |
| 8 | +import { LayoutDashboard, Trash2, Maximize2, Minimize2 } from "lucide-react"; |
| 9 | + |
| 10 | +const APP_NAME = "Alloc"; |
| 11 | +const STORAGE_KEY = `${APP_NAME}_json_data`; |
| 12 | +const FULL_WIDTH_STORAGE_KEY = `${APP_NAME}_full_width_preference`; |
| 13 | + |
| 14 | +const App: React.FC = () => { |
| 15 | + const [account, setAccount] = useState<AccountData | null>(null); |
| 16 | + const [positions, setPositions] = useState<PositionData[]>([]); |
| 17 | + const [parseError, setParseError] = useState<string | null>(null); |
| 18 | + const [isLoadingFromStorage, setIsLoadingFromStorage] = useState(true); |
| 19 | + const [isFullWidth, setIsFullWidth] = useState<boolean>(false); |
| 20 | + |
| 21 | + // Load data from localStorage on mount |
| 22 | + useEffect(() => { |
| 23 | + const loadFromStorage = () => { |
| 24 | + try { |
| 25 | + const storedJSON = localStorage.getItem(STORAGE_KEY); |
| 26 | + |
| 27 | + if (storedJSON) { |
| 28 | + // Try to parse the stored JSON data |
| 29 | + const { account, positions } = parseJSONData(storedJSON); |
| 30 | + |
| 31 | + if (account && positions && positions.length > 0) { |
| 32 | + setAccount(account); |
| 33 | + setPositions(positions); |
| 34 | + } else { |
| 35 | + // Stored data is invalid, clear it |
| 36 | + localStorage.removeItem(STORAGE_KEY); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Load full-width preference |
| 41 | + const fullWidthPreference = localStorage.getItem( |
| 42 | + FULL_WIDTH_STORAGE_KEY |
| 43 | + ); |
| 44 | + if (fullWidthPreference !== null) { |
| 45 | + setIsFullWidth(fullWidthPreference === "true"); |
| 46 | + } |
| 47 | + } catch (e) { |
| 48 | + console.error("Failed to load data from localStorage", e); |
| 49 | + // Clear corrupted data |
| 50 | + localStorage.removeItem(STORAGE_KEY); |
| 51 | + } finally { |
| 52 | + setIsLoadingFromStorage(false); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + loadFromStorage(); |
| 57 | + }, []); |
| 58 | + |
| 59 | + const handleFileLoaded = (jsonContent: string) => { |
| 60 | + try { |
| 61 | + setParseError(null); |
| 62 | + |
| 63 | + const { account, positions } = parseJSONData(jsonContent); |
| 64 | + |
| 65 | + if (!account) { |
| 66 | + throw new Error( |
| 67 | + "Failed to parse account data. Please check the file format." |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + if (!positions || positions.length === 0) { |
| 72 | + throw new Error( |
| 73 | + "Failed to parse positions data or file is empty. Please check the file format." |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + // Save to localStorage |
| 78 | + localStorage.setItem(STORAGE_KEY, jsonContent); |
| 79 | + |
| 80 | + setAccount(account); |
| 81 | + setPositions(positions); |
| 82 | + } catch (e) { |
| 83 | + const errorMessage = |
| 84 | + e instanceof Error |
| 85 | + ? e.message |
| 86 | + : "Failed to parse data. Please check your JSON file."; |
| 87 | + setParseError(errorMessage); |
| 88 | + console.error("Failed to parse data", e); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + const handleError = (error: string) => { |
| 93 | + setParseError(error); |
| 94 | + }; |
| 95 | + |
| 96 | + const handleReset = () => { |
| 97 | + setAccount(null); |
| 98 | + setPositions([]); |
| 99 | + setParseError(null); |
| 100 | + // Clear localStorage |
| 101 | + localStorage.removeItem(STORAGE_KEY); |
| 102 | + }; |
| 103 | + |
| 104 | + const handleToggleFullWidth = () => { |
| 105 | + const newValue = !isFullWidth; |
| 106 | + setIsFullWidth(newValue); |
| 107 | + localStorage.setItem(FULL_WIDTH_STORAGE_KEY, String(newValue)); |
| 108 | + }; |
| 109 | + |
| 110 | + // Show loading state while checking localStorage |
| 111 | + if (isLoadingFromStorage) { |
| 112 | + return ( |
| 113 | + <div className="min-h-screen bg-slate-900 flex items-center justify-center"> |
| 114 | + <div className="text-blue-500 animate-pulse text-xl font-semibold"> |
| 115 | + Loading Portfolio Data... |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + // Show drag and drop interface if no account data is loaded |
| 122 | + if (!account) { |
| 123 | + return ( |
| 124 | + <FileDropZone |
| 125 | + onFileLoaded={handleFileLoaded} |
| 126 | + onError={handleError} |
| 127 | + parseError={parseError} |
| 128 | + /> |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + // Calculate some aggregate totals that might be missing or useful |
| 133 | + const totalDailyPL = positions.reduce( |
| 134 | + (acc, curr) => acc + curr.today_pl_val, |
| 135 | + 0 |
| 136 | + ); |
| 137 | + const totalUnrealizedPL = positions.reduce( |
| 138 | + (acc, curr) => acc + curr.unrealized_pl, |
| 139 | + 0 |
| 140 | + ); |
| 141 | + |
| 142 | + return ( |
| 143 | + <div className="min-h-screen bg-slate-900 pb-12"> |
| 144 | + {/* Header */} |
| 145 | + <nav className="bg-slate-800 border-b border-slate-700 sticky top-0 z-50"> |
| 146 | + <div |
| 147 | + className={`${ |
| 148 | + isFullWidth ? "max-w-full" : "max-w-7xl" |
| 149 | + } mx-auto px-4 sm:px-6 lg:px-8`} |
| 150 | + > |
| 151 | + <div className="flex justify-between h-16 items-center"> |
| 152 | + <div className="flex items-center gap-2"> |
| 153 | + <LayoutDashboard className="h-6 w-6 text-blue-500" /> |
| 154 | + <span className="font-bold text-xl text-white tracking-tight"> |
| 155 | + <span className="text-blue-500">Alloc</span>{" "} |
| 156 | + Dashboard |
| 157 | + </span> |
| 158 | + </div> |
| 159 | + <div className="flex items-center gap-4 text-sm"> |
| 160 | + <button |
| 161 | + onClick={handleToggleFullWidth} |
| 162 | + className="flex items-center gap-2 px-3 py-1.5 text-sm text-slate-400 hover:text-white bg-slate-700/50 hover:bg-slate-700 rounded-lg transition-colors" |
| 163 | + title={ |
| 164 | + isFullWidth |
| 165 | + ? "Use Constrained Width" |
| 166 | + : "Use Full Width" |
| 167 | + } |
| 168 | + > |
| 169 | + {isFullWidth ? ( |
| 170 | + <Minimize2 className="h-4 w-4" /> |
| 171 | + ) : ( |
| 172 | + <Maximize2 className="h-4 w-4" /> |
| 173 | + )} |
| 174 | + <span className="hidden sm:inline"> |
| 175 | + {isFullWidth ? "Constrained" : "Full Width"} |
| 176 | + </span> |
| 177 | + </button> |
| 178 | + <button |
| 179 | + onClick={handleReset} |
| 180 | + className="flex items-center gap-2 px-3 py-1.5 text-sm text-slate-400 hover:text-white bg-slate-700/50 hover:bg-slate-700 rounded-lg transition-colors" |
| 181 | + title="Clear Data" |
| 182 | + > |
| 183 | + <Trash2 className="h-4 w-4" /> |
| 184 | + <span className="hidden sm:inline"> |
| 185 | + Clear Data |
| 186 | + </span> |
| 187 | + </button> |
| 188 | + </div> |
| 189 | + </div> |
| 190 | + </div> |
| 191 | + </nav> |
| 192 | + |
| 193 | + <main |
| 194 | + className={`${ |
| 195 | + isFullWidth ? "max-w-full" : "max-w-7xl" |
| 196 | + } mx-auto px-4 sm:px-6 lg:px-8 pt-8`} |
| 197 | + > |
| 198 | + {/* Account Summary */} |
| 199 | + <section className="mb-2"> |
| 200 | + {/* <div className="flex justify-between items-end mb-4"> |
| 201 | + <h1 className="text-2xl font-bold text-white"> |
| 202 | + Overview |
| 203 | + </h1> |
| 204 | + <span className="text-xs text-slate-500 bg-slate-800 px-2 py-1 rounded border border-slate-700"> |
| 205 | + Currency: {account.currency} |
| 206 | + </span> |
| 207 | + </div> */} |
| 208 | + <SummaryCard account={account} /> |
| 209 | + </section> |
| 210 | + |
| 211 | + {/* Charts Section */} |
| 212 | + <section className="mb-8"> |
| 213 | + <PortfolioChart positions={positions} /> |
| 214 | + </section> |
| 215 | + |
| 216 | + {/* Positions Table */} |
| 217 | + <section> |
| 218 | + <PositionsTable positions={positions} /> |
| 219 | + </section> |
| 220 | + </main> |
| 221 | + </div> |
| 222 | + ); |
| 223 | +}; |
| 224 | + |
| 225 | +export default App; |
0 commit comments