-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
450 lines (399 loc) · 18.1 KB
/
Copy pathapp.js
File metadata and controls
450 lines (399 loc) · 18.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
function App() {
const [activeTab, setActiveTab] = React.useState('predictions');
const [logs, setLogs] = useLocalStorage('fishingLogs', []);
const [syncStatus, setSyncStatus] = React.useState({
syncing: false,
lastSynced: null,
error: null
});
const { location, error: locationError, loading: locationLoading } = useGeolocation();
const {
weatherData,
tideData,
prediction,
selectedDate,
forecastDays,
setSelectedDate,
loading: dataLoading,
error: dataError
} = useFishingData(location);
// Setup offline/online status tracking
const [isOnline, setIsOnline] = React.useState(navigator.onLine);
const [showFeatureSuggestions, setShowFeatureSuggestions] = React.useState(false);
React.useEffect(() => {
const handleOnline = () => {
setIsOnline(true);
// Attempt to sync when coming back online
LogManager.syncWhenOnline();
};
const handleOffline = () => setIsOnline(false);
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, []);
// Set theme class on body
React.useEffect(() => {
// Initialize theme from localStorage
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {
document.documentElement.classList.add('dark');
} else if (savedTheme === 'light') {
document.documentElement.classList.remove('dark');
} else if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
// If no saved preference, use system preference
document.documentElement.classList.add('dark');
localStorage.setItem('theme', 'dark');
}
}, []);
const handleLogSubmit = React.useCallback(async (logData) => {
try {
LogManager.validateLog(logData);
const conditions = {
weather: weatherData?.current ? {
temperature: weatherData.current.temperature,
windSpeed: weatherData.current.windSpeed,
pressure: weatherData.current.pressure,
cloudCover: weatherData.current.cloudCover,
rainMm: weatherData.current.rainMm
} : null,
tide: tideData?.current ? {
height: tideData.current.height,
type: tideData.current.type
} : null,
prediction: prediction?.percentage
};
const formattedLog = LogManager.formatLog(logData, conditions);
// Show sync status
setSyncStatus(prev => ({...prev, syncing: true, error: null}));
// Save the log
await LogManager.saveLog(formattedLog);
// Update state with latest logs
setLogs(LogManager.loadLogs());
// Update sync status
setSyncStatus({
syncing: false,
lastSynced: new Date(),
error: null
});
// Show success notification if permitted
if ('Notification' in window && Notification.permission === 'granted') {
new Notification('Log Saved', {
body: 'Your fishing log has been saved successfully!'
});
}
} catch (error) {
console.error('Log submission error:', error);
reportError(error);
// Update sync status with error
setSyncStatus({
syncing: false,
lastSynced: null,
error: 'Failed to save log: ' + error.message
});
// Alert user of error
alert('Failed to save log. Please check all required fields.');
}
}, [weatherData, tideData, prediction, setLogs]);
const handleLogDelete = React.useCallback(async (logId) => {
try {
// Show sync status
setSyncStatus(prev => ({...prev, syncing: true, error: null}));
// Delete the log
await LogManager.deleteLog(logId);
// Update state with remaining logs
setLogs(LogManager.loadLogs());
// Update sync status
setSyncStatus({
syncing: false,
lastSynced: new Date(),
error: null
});
} catch (error) {
console.error('Log deletion error:', error);
reportError(error);
// Update sync status with error
setSyncStatus({
syncing: false,
lastSynced: null,
error: 'Failed to delete log: ' + error.message
});
alert('Failed to delete log. Please try again.');
}
}, [setLogs]);
const handleLogEdit = React.useCallback(async (logId, updatedData) => {
try {
LogManager.validateLog(updatedData);
// Find existing log
const existingLog = logs.find(log => log.id === logId);
if (!existingLog) {
throw new Error('Log not found');
}
// Show sync status
setSyncStatus(prev => ({...prev, syncing: true, error: null}));
// Create updated log with preserved conditions
const updatedLog = {
...existingLog,
...updatedData,
updatedAt: new Date().toISOString()
};
// Save the updated log
await LogManager.saveLog(updatedLog);
// Update state with all logs
setLogs(LogManager.loadLogs());
// Update sync status
setSyncStatus({
syncing: false,
lastSynced: new Date(),
error: null
});
} catch (error) {
console.error('Log edit error:', error);
reportError(error);
// Update sync status with error
setSyncStatus({
syncing: false,
lastSynced: null,
error: 'Failed to update log: ' + error.message
});
alert('Failed to update log. Please check all required fields.');
}
}, [logs, setLogs]);
// Force a sync with the server
const handleForceSync = React.useCallback(async () => {
if (!isOnline) {
alert('You are offline. Please connect to the internet to sync.');
return;
}
try {
setSyncStatus(prev => ({...prev, syncing: true, error: null}));
await LogManager.saveLogs(logs);
setSyncStatus({
syncing: false,
lastSynced: new Date(),
error: null
});
// Show success message
alert('Logs synchronized successfully!');
} catch (error) {
console.error('Force sync error:', error);
reportError(error);
setSyncStatus({
syncing: false,
lastSynced: null,
error: 'Failed to sync: ' + error.message
});
alert('Failed to synchronize logs. Please try again later.');
}
}, [logs, isOnline]);
// Request notification permission
const requestNotificationPermission = React.useCallback(async () => {
if (!('Notification' in window)) {
return;
}
if (Notification.permission !== 'granted' && Notification.permission !== 'denied') {
const permission = await Notification.requestPermission();
if (permission === 'granted') {
new Notification('Notifications Enabled', {
body: 'You will now receive notifications about your fishing logs.'
});
}
}
}, []);
// Request notification permission on first load
React.useEffect(() => {
requestNotificationPermission();
}, [requestNotificationPermission]);
// Toggle feature suggestions
const toggleFeatureSuggestions = () => {
setShowFeatureSuggestions(!showFeatureSuggestions);
};
if (locationLoading || dataLoading) {
return (
<div className="min-h-screen bg-gray-100 dark:bg-gray-900 flex items-center justify-center p-4">
<div className="text-center">
<LoadingSpinner size="large" color="blue" />
<p className="mt-4 text-gray-600 dark:text-gray-400">
{locationLoading ? 'Getting your location...' : 'Loading fishing conditions...'}
</p>
</div>
</div>
);
}
if (locationError || dataError) {
return (
<div className="min-h-screen bg-gray-100 dark:bg-gray-900 flex items-center justify-center p-4">
<div className="text-center p-6 bg-white dark:bg-gray-800 rounded-lg shadow-md max-w-md w-full">
<i className="fas fa-exclamation-circle text-3xl text-red-600 mb-4"></i>
<p className="text-gray-700 dark:text-gray-300 mb-4">
{locationError || dataError}
</p>
<button
onClick={() => window.location.reload()}
className="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700"
>
Retry
</button>
</div>
</div>
);
}
const renderSyncStatus = () => {
if (syncStatus.syncing) {
return (
<div className="flex items-center text-blue-600 dark:text-blue-400">
<LoadingSpinner size="small" color="blue" />
<span className="ml-2 text-sm">Syncing...</span>
</div>
);
}
if (syncStatus.error) {
return (
<div className="flex items-center text-red-600 dark:text-red-400">
<i className="fas fa-exclamation-circle mr-2"></i>
<span className="text-sm">{syncStatus.error}</span>
</div>
);
}
if (syncStatus.lastSynced) {
return (
<div className="flex items-center text-green-600 dark:text-green-400">
<i className="fas fa-check-circle mr-2"></i>
<span className="text-sm">
Last synced: {syncStatus.lastSynced.toLocaleTimeString()}
</span>
</div>
);
}
return null;
};
return (
<div className="min-h-screen bg-gray-100 dark:bg-gray-900" data-name="app">
<Header activeTab={activeTab} onTabChange={setActiveTab} />
<main className="container mx-auto px-4 py-6">
{/* Sync status indicator */}
<div className="mb-4 flex flex-wrap items-center justify-between gap-2">
{renderSyncStatus()}
{activeTab === 'logs' && (
<button
onClick={handleForceSync}
disabled={!isOnline || syncStatus.syncing}
className={`px-3 py-1 text-sm bg-blue-600 text-white rounded-lg hover:bg-blue-700 flex items-center ${
!isOnline || syncStatus.syncing ? 'opacity-50 cursor-not-allowed' : ''
}`}
>
<i className="fas fa-sync-alt mr-2"></i>
Force Sync
</button>
)}
</div>
{activeTab === 'predictions' ? (
<>
<DaySelector
selectedDate={selectedDate}
onDateChange={setSelectedDate}
forecastDays={forecastDays}
/>
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
<div className="lg:col-span-2 space-y-6">
<ActivityAlerts
weatherData={weatherData}
tideData={tideData}
prediction={prediction}
location={location}
/>
<WeatherInfo weatherData={weatherData} />
<WeatherTrends weatherData={weatherData} />
<TideInfo tideData={tideData} />
<FishingPrediction prediction={prediction} />
<AdvancedPredictions
weatherData={weatherData}
tideData={tideData}
prediction={prediction}
/>
<ActivityTrends
solunarData={prediction?.solunarData}
weatherData={weatherData}
/>
{prediction?.solunarData && (
<HourlyDetails
solunarData={prediction.solunarData}
weatherData={weatherData}
tideData={tideData}
/>
)}
<FishingAnalytics logs={logs} />
<button
onClick={toggleFeatureSuggestions}
className="w-full py-2 text-blue-600 dark:text-blue-400 hover:text-blue-800 dark:hover:text-blue-300 text-center font-medium"
>
<i className="fas fa-lightbulb mr-2"></i>
{showFeatureSuggestions ? 'Hide Feature Suggestions' : 'View Feature Suggestions'}
</button>
{showFeatureSuggestions && <NewFeaturesSuggestion />}
</div>
<div className="lg:col-span-1">
<div className="sticky top-4">
<LogForm onSubmit={handleLogSubmit} />
</div>
</div>
</div>
</>
) : activeTab === 'species' ? (
<FishingSpeciesTab
weatherData={weatherData}
tideData={tideData}
location={location}
/>
) : activeTab === 'hotspots' ? (
<HotspotsPage
weatherData={weatherData}
tideData={tideData}
location={location}
/>
) : (
<div className="space-y-6">
<div className="bg-white dark:bg-gray-800 p-4 md:p-6 rounded-lg shadow-md">
<div className="flex flex-wrap items-center justify-between gap-2 mb-4">
<h2 className="text-xl font-semibold dark:text-white">Fishing Logs</h2>
<div className="flex items-center space-x-2">
<span className={`inline-flex items-center px-2 py-1 rounded-lg text-xs ${
isOnline
? 'bg-green-100 dark:bg-green-800 text-green-800 dark:text-green-100'
: 'bg-red-100 dark:bg-red-800 text-red-800 dark:text-red-100'
}`}>
<i className={`fas fa-${isOnline ? 'wifi' : 'wifi-slash'} mr-1`}></i>
{isOnline ? 'Online' : 'Offline'}
</span>
<button
onClick={handleForceSync}
disabled={!isOnline || syncStatus.syncing}
className={`px-2 py-1 text-xs bg-blue-600 text-white rounded-lg hover:bg-blue-700 flex items-center ${
!isOnline || syncStatus.syncing ? 'opacity-50 cursor-not-allowed' : ''
}`}
>
<i className="fas fa-sync-alt mr-1"></i>
Sync
</button>
</div>
</div>
<div className="mb-6">
<LogForm onSubmit={handleLogSubmit} />
</div>
<FishingLog
logs={logs}
onDelete={handleLogDelete}
onEdit={handleLogEdit}
/>
</div>
</div>
)}
</main>
<OfflineIndicator />
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);