-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
220 lines (194 loc) · 8.14 KB
/
App.tsx
File metadata and controls
220 lines (194 loc) · 8.14 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
import React, { lazy, Suspense, useCallback, useEffect, useState } from "react";
import { Maximize2, Minimize2, Trash2 } from "lucide-react";
import { parseJSONData } from "./utils/dataParser";
import { AccountData, PositionData } from "./types";
import SummaryCard from "./components/SummaryCard";
import FileDropZone from "./components/FileDropZone";
const PortfolioChart = lazy(() => import("./components/PortfolioChart"));
const PositionsTable = lazy(() => import("./components/PositionsTable"));
const CategoricalAnalysis = lazy(
() => import("./components/CategoricalAnalysis"),
);
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`;
interface ParsedPortfolioData {
account: AccountData;
positions: PositionData[];
}
const parsePortfolioData = (jsonContent: string): ParsedPortfolioData => {
const { account, positions } = parseJSONData(jsonContent);
if (!account) {
throw new Error(
"Failed to parse account data. Please check the file format.",
);
}
if (positions.length === 0) {
throw new Error(
"Failed to parse positions data or file is empty. Please check the file format.",
);
}
return { account, positions };
};
const DashboardFallback = () => (
<div className="rounded-xl border border-slate-700 bg-slate-800 p-8 text-center text-sm text-slate-400">
Loading dashboard sections...
</div>
);
const LoadingScreen = () => (
<div className="flex min-h-screen items-center justify-center bg-slate-900">
<div className="animate-pulse text-xl font-semibold text-blue-500">
Loading portfolio data...
</div>
</div>
);
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(false);
useEffect(() => {
try {
const storedJSON = localStorage.getItem(STORAGE_KEY);
if (storedJSON) {
try {
const parsed = parsePortfolioData(storedJSON);
setAccount(parsed.account);
setPositions(parsed.positions);
} catch (error) {
console.error("Stored portfolio data is invalid", error);
localStorage.removeItem(STORAGE_KEY);
}
}
const fullWidthPreference = localStorage.getItem(
FULL_WIDTH_STORAGE_KEY,
);
if (fullWidthPreference !== null) {
setIsFullWidth(fullWidthPreference === "true");
}
} catch (error) {
console.error("Failed to load data from localStorage", error);
localStorage.removeItem(STORAGE_KEY);
} finally {
setIsLoadingFromStorage(false);
}
}, []);
const handleFileLoaded = useCallback((jsonContent: string) => {
try {
setParseError(null);
const parsed = parsePortfolioData(jsonContent);
localStorage.setItem(STORAGE_KEY, jsonContent);
setAccount(parsed.account);
setPositions(parsed.positions);
} catch (error) {
const errorMessage =
error instanceof Error
? error.message
: "Failed to parse data. Please check your JSON file.";
setParseError(errorMessage);
console.error("Failed to parse data", error);
}
}, []);
const handleReset = useCallback(() => {
setAccount(null);
setPositions([]);
setParseError(null);
localStorage.removeItem(STORAGE_KEY);
}, []);
const handleToggleFullWidth = useCallback(() => {
setIsFullWidth((current) => {
const next = !current;
localStorage.setItem(FULL_WIDTH_STORAGE_KEY, String(next));
return next;
});
}, []);
if (isLoadingFromStorage) {
return <LoadingScreen />;
}
if (!account) {
return (
<FileDropZone
onFileLoaded={handleFileLoaded}
onError={setParseError}
parseError={parseError}
/>
);
}
const widthClass = isFullWidth ? "max-w-full" : "max-w-7xl";
return (
<div className="min-h-screen bg-slate-900 pb-12">
<nav className="sticky top-0 z-50 border-b border-slate-700 bg-slate-800">
<div
className={`${widthClass} mx-auto px-4 sm:px-6 lg:px-8`}
>
<div className="flex h-16 items-center justify-between">
<div className="flex items-center gap-2">
<img
src={FAVICON_PATH}
alt="Alloc logo"
className="h-7 w-7 rounded-sm"
/>
<span className="text-xl font-bold tracking-tight text-white">
<span className="text-blue-500">Alloc</span>{" "}
Dashboard
</span>
</div>
<div className="flex items-center gap-4 text-sm">
<button
type="button"
onClick={handleToggleFullWidth}
className="flex items-center gap-2 rounded-lg bg-slate-700/50 px-3 py-1.5 text-sm text-slate-400 transition-colors hover:bg-slate-700 hover:text-white"
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
type="button"
onClick={handleReset}
className="flex items-center gap-2 rounded-lg bg-slate-700/50 px-3 py-1.5 text-sm text-slate-400 transition-colors hover:bg-slate-700 hover:text-white"
title="Clear Data"
>
<Trash2 className="h-4 w-4" />
<span className="hidden sm:inline">
Clear Data
</span>
</button>
</div>
</div>
</div>
</nav>
<main
className={`${widthClass} mx-auto px-4 pt-8 sm:px-6 lg:px-8`}
>
<section className="mb-2">
<SummaryCard account={account} />
</section>
<Suspense fallback={<DashboardFallback />}>
<section className="mb-8">
<PortfolioChart positions={positions} />
</section>
<section className="mb-8">
<PositionsTable positions={positions} />
</section>
<section>
<CategoricalAnalysis positions={positions} />
</section>
</Suspense>
</main>
</div>
);
};
export default App;