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