-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.svelte
More file actions
220 lines (197 loc) · 6.46 KB
/
App.svelte
File metadata and controls
220 lines (197 loc) · 6.46 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
<script lang="ts">
import Sidebar from '$lib/components/Sidebar.svelte';
import StatusBar from '$lib/components/StatusBar.svelte';
import ProgressPanel from '$lib/components/ProgressPanel.svelte';
import LogPanel from '$lib/components/LogPanel.svelte';
import SetupWizard from '$lib/components/SetupWizard.svelte';
import LicenseViewer from '$lib/components/LicenseViewer.svelte';
import AnalysisPage from './pages/AnalysisPage.svelte';
import DetectionsPage from './pages/DetectionsPage.svelte';
import MapPage from './pages/MapPage.svelte';
import SettingsPage from './pages/SettingsPage.svelte';
import { appState } from '$lib/stores/app.svelte';
import {
analysisState,
handleAnalysisEvent,
resetAnalysis,
type BirdaEventEnvelope,
} from '$lib/stores/analysis.svelte';
import { addLog, type LogEntry } from '$lib/stores/log.svelte';
import {
getCatalogStats,
getSettings,
startAnalysis,
cancelAnalysis,
onAnalysisProgress,
offAnalysisProgress,
onLog,
offLog,
onSetupWizard,
offSetupWizard,
onShowLicenses,
offShowLicenses,
} from '$lib/utils/ipc';
import { setupMenuListeners } from '$lib/utils/shortcuts';
import { onMount, onDestroy } from 'svelte';
let cleanupMenu: (() => void) | null = null;
let showWizard = $state<boolean | null>(null); // null = loading, true/false = resolved
let showLicenses = $state(false);
async function handleWizardComplete() {
try {
const settings = await getSettings();
appState.theme = settings.theme;
appState.analysisConfidence = settings.default_confidence;
if (settings.default_model) {
appState.selectedModel = settings.default_model;
}
} catch {
// proceed with existing state
}
try {
appState.catalogStats = await getCatalogStats();
} catch {
// DB may not be ready
}
showWizard = false;
}
async function handleStop() {
try {
await cancelAnalysis();
} catch {
// Ensure UI recovers even if cancel IPC fails
}
appState.isAnalysisRunning = false;
}
async function handleStartAnalysis(opts: {
locationName: string;
latitude: number;
longitude: number;
month?: number | undefined;
day?: number | undefined;
}) {
if (!appState.sourcePath) return;
resetAnalysis();
appState.isAnalysisRunning = true;
onAnalysisProgress((envelope) => {
handleAnalysisEvent(envelope as BirdaEventEnvelope);
});
try {
const result = await startAnalysis({
source_path: appState.sourcePath,
model: appState.selectedModel,
min_confidence: appState.analysisConfidence,
latitude: opts.latitude || undefined,
longitude: opts.longitude || undefined,
location_name: opts.locationName || undefined,
month: opts.month,
day: opts.day,
});
analysisState.status = 'completed';
appState.lastRunId = result.runId;
appState.lastSourceFile = appState.sourcePath;
appState.selectedRunId = result.runId;
appState.activeTab = 'detections';
appState.catalogStats = await getCatalogStats();
} catch (err) {
analysisState.status = 'failed';
analysisState.error = (err as Error).message;
} finally {
appState.isAnalysisRunning = false;
offAnalysisProgress();
}
}
let systemPrefersDark = $state(false);
onMount(() => {
// Initial theme setup
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
systemPrefersDark = mediaQuery.matches;
const handler = (e: MediaQueryListEvent) => {
systemPrefersDark = e.matches;
};
mediaQuery.addEventListener('change', handler);
// Async init (no cleanup needed from these)
void (async () => {
try {
const settings = await getSettings();
appState.theme = settings.theme;
appState.analysisConfidence = settings.default_confidence;
if (settings.default_model) {
appState.selectedModel = settings.default_model;
}
showWizard = !settings.setup_completed;
} catch {
// Failed to load settings — show wizard as fallback
showWizard = true;
}
try {
appState.catalogStats = await getCatalogStats();
} catch {
// DB not ready yet
}
})();
onSetupWizard(() => {
showWizard = true;
});
onShowLicenses(() => {
showLicenses = true;
});
cleanupMenu = setupMenuListeners({
onOpenFile: (path: string) => {
appState.sourcePath = path;
},
onFocusSearch: () => {
const searchInput = document.querySelector<HTMLInputElement>('input[placeholder*="species"]');
searchInput?.focus();
},
});
onLog((entry) => {
const { level, source, message } = entry as { level: LogEntry['level']; source: string; message: string };
addLog(level, source, message);
});
return () => {
mediaQuery.removeEventListener('change', handler);
};
});
$effect(() => {
// Determine effective theme and apply daisyUI data-theme attribute
const isDark = appState.theme === 'dark' || (appState.theme === 'system' && systemPrefersDark);
document.documentElement.setAttribute('data-theme', isDark ? 'birda-dark' : 'birda-light');
document.documentElement.style.colorScheme = isDark ? 'dark' : 'light';
});
onDestroy(() => {
offAnalysisProgress();
offLog();
offSetupWizard();
offShowLicenses();
cleanupMenu?.();
});
</script>
{#if showWizard === null}
<!-- Loading settings, show nothing -->
<main class="bg-base-100 flex h-screen select-none"></main>
{:else if showWizard}
<main class="bg-base-100 text-base-content h-screen select-none">
<SetupWizard oncomplete={handleWizardComplete} />
</main>
{:else}
<main class="bg-base-100 text-base-content flex h-screen select-none">
<Sidebar />
<div class="flex flex-1 flex-col overflow-hidden">
<div class="flex flex-1 flex-col overflow-hidden">
{#if appState.activeTab === 'analysis'}
<AnalysisPage onstart={handleStartAnalysis} onstop={handleStop} />
{:else if appState.activeTab === 'detections'}
<DetectionsPage />
{:else if appState.activeTab === 'map'}
<MapPage />
{:else if appState.activeTab === 'settings'}
<SettingsPage />
{/if}
</div>
<ProgressPanel />
<LogPanel />
<StatusBar />
</div>
</main>
{/if}
<LicenseViewer bind:open={showLicenses} />