-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseState.ts
More file actions
354 lines (328 loc) · 10.6 KB
/
Copy pathuseState.ts
File metadata and controls
354 lines (328 loc) · 10.6 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
import { computed, ref, type Ref, watch } from "vue";
import { useRouter } from "vue-router";
import { cloneDeep } from "lodash";
import * as d3 from "d3";
import {
type Configuration,
getConfiguration,
getCurrentUser,
type OtherUserAnnotations,
type Recording,
type SpectrogramAnnotation,
type SpectrogramSequenceAnnotation,
getComputedPulseContour,
type ComputedPulseContour,
getVettingDetailsForUser,
} from "../api/api";
import {
interpolateCividis,
interpolateViridis,
interpolateInferno,
interpolateMagma,
interpolatePlasma,
interpolateTurbo,
} from "d3-scale-chromatic";
const annotationState: Ref<AnnotationState> = ref("");
const creationType: Ref<"pulse" | "sequence"> = ref("pulse");
type LayersVis = "time" | "freq" | "species" | "grid" | "sequence" | "duration";
const layerVisibility: Ref<LayersVis[]> = ref([
"sequence",
"species",
"duration",
"freq",
]);
const colorScale: Ref<d3.ScaleOrdinal<string, string, never> | undefined> =
ref();
const colorSchemes = [
{ value: "inferno", title: "Inferno", scheme: interpolateInferno },
{ value: "cividis", title: "Cividis", scheme: interpolateCividis },
{ value: "viridis", title: "Viridis", scheme: interpolateViridis },
{ value: "magma", title: "Magma", scheme: interpolateMagma },
{ value: "plasma", title: "Plasma", scheme: interpolatePlasma },
{ value: "turbo", title: "Turbo", scheme: interpolateTurbo },
];
const colorScheme: Ref<{
value: string;
title: string;
scheme: (input: number) => string;
}> = ref(colorSchemes[0]);
const backgroundColor = ref("rgb(0, 0, 0)");
const selectedUsers: Ref<string[]> = ref([]);
const currentUser: Ref<string> = ref("");
const currentUserId: Ref<number | undefined> = ref(undefined);
const selectedId: Ref<number | null> = ref(null);
const selectedType: Ref<"pulse" | "sequence"> = ref("pulse");
const annotations: Ref<SpectrogramAnnotation[]> = ref([]);
const sequenceAnnotations: Ref<SpectrogramSequenceAnnotation[]> = ref([]);
const otherUserAnnotations: Ref<OtherUserAnnotations> = ref({});
const sharedList: Ref<Recording[]> = ref([]);
const recordingList: Ref<Recording[]> = ref([]);
const currentRecordingId: Ref<number | undefined> = ref(undefined);
const recordingTagList: Ref<string[]> = ref([]);
const nextShared: Ref<Recording | false> = ref(false);
const scaledVals: Ref<{ x: number; y: number }> = ref({ x: 1, y: 1 });
const viewCompressedOverlay = ref(false);
const sideTab: Ref<"annotations" | "recordings"> = ref("annotations");
const configuration: Ref<Configuration> = ref({
display_pulse_annotations: true,
display_sequence_annotations: true,
spectrogram_view: "compressed",
spectrogram_x_stretch: 2.5,
run_inference_on_upload: true,
create_pulse_annotations_from_batbot: false,
default_color_scheme: "inferno",
default_spectrogram_background_color: "rgb(0, 0, 0)",
is_admin: false,
mark_annotations_completed_enabled: false,
non_admin_upload_enabled: true,
});
const scaledWidth = ref(0);
const scaledHeight = ref(0);
const measuring: Ref<boolean> = ref(false);
const frequencyRulerY: Ref<number> = ref(0);
const toggleMeasureMode = () => {
measuring.value = !measuring.value;
};
const drawingBoundingBox = ref(false);
const boundingBoxError = ref("");
const toggleDrawingBoundingBox = () => {
drawingBoundingBox.value = !drawingBoundingBox.value;
};
const fixedAxes = ref(true);
const toggleFixedAxes = () => {
fixedAxes.value = !fixedAxes.value;
};
const computedPulseContours: Ref<ComputedPulseContour[]> = ref([]);
// Initial contour state is off; not persisted or loaded from localStorage.
const contoursEnabled = ref(false);
const imageOpacity = ref(1.0);
const contourOpacity = ref(1.0);
const contoursLoading = ref(false);
// Default mask overlay to visible at 50% opacity.
const viewMaskOverlay = ref(true);
const maskOverlayOpacity = ref(0.5);
// Waveplot below compressed spectrogram; on by default when available.
const viewWaveplot = ref(true);
const setContoursEnabled = (value: boolean) => {
contoursEnabled.value = value;
};
async function loadContours(recordingId: number) {
contoursLoading.value = true;
computedPulseContours.value = await getComputedPulseContour(recordingId);
contoursLoading.value = false;
}
function clearContours() {
computedPulseContours.value = [];
}
const reviewerMaterials = ref("");
const transparencyThreshold = ref(0); // 0-100 percentage
const FILTER_TAG_STORAGE_KEY = "bataiFilterTags";
const SHARED_FILTER_TAG_STORAGE_KEY = "bataiSharedFilterTags";
const MAP_FILTER_BOUNDS_STORAGE_KEY = "bataiMapFilterBounds";
const filterTags: Ref<string[]> = ref([]);
const sharedFilterTags: Ref<string[]> = ref([]);
const showSubmittedRecordings = ref(false);
/** Current spectrogram recording filename (for app bar display). Set by Spectrogram view, cleared when not on spectrogram. */
const spectrogramFilename: Ref<string> = ref("");
type AnnotationState = "" | "editing" | "creating" | "disabled";
export default function useState() {
const setAnnotationState = (state: AnnotationState) => {
annotationState.value = state;
};
function toggleLayerVisibility(value: LayersVis) {
const index = layerVisibility.value.indexOf(value);
const clone = cloneDeep(layerVisibility.value);
if (index === -1) {
// If the value is not present, add it
clone.push(value);
} else {
// If the value is present, remove it
clone.splice(index, 1);
}
if (value === "time" && clone.includes("duration")) {
const durationInd = layerVisibility.value.indexOf("duration");
clone.splice(durationInd, 1);
}
if (value === "duration" && clone.includes("time")) {
const timeInd = layerVisibility.value.indexOf("time");
clone.splice(timeInd, 1);
}
layerVisibility.value = clone;
}
const setSelectedUsers = (newUsers: string[]) => {
selectedUsers.value = newUsers;
};
function createColorScale(userEmails: string[]) {
colorScale.value = d3
.scaleOrdinal<string>()
.domain(userEmails)
.range(
d3.schemeCategory10.filter(
(color) => color !== "red" && color !== "cyan" && color !== "yellow",
),
);
}
function setSelectedId(
id: number | null,
annotationType?: "pulse" | "sequence",
) {
selectedId.value = id;
if (annotationType) {
selectedType.value = annotationType;
}
}
watch(sharedList, () => {
const filtered = sharedList.value.filter(
(item) => !item.userMadeAnnotations,
);
if (filtered.length > 0) {
nextShared.value = filtered[0];
} else {
nextShared.value = false;
}
});
async function loadConfiguration() {
configuration.value = (await getConfiguration()).data;
}
async function loadCurrentUser() {
const userInfo = (await getCurrentUser()).data;
currentUser.value = userInfo.name;
currentUserId.value = userInfo.id;
}
/**
* Function used to determine whether or not we are currently looking
* at an NABat-specific view.
*
* returns `true` if looking at an NABat view, `false` otherwise
*/
function isNaBat(): boolean {
const router = useRouter();
return router.currentRoute.value.fullPath.includes("nabat");
}
// Server filters by exclude_submitted when "Show submitted" is unchecked; we refetch on toggle.
const myRecordingsDisplay = computed(() => recordingList.value);
const sharedRecordingsDisplay = computed(() => sharedList.value);
async function loadReviewerMaterials() {
// Only make this request if vetting is enabled and a user is logged in
if (!configuration.value.mark_annotations_completed_enabled) return;
if (currentUserId.value === undefined) return;
const vettingDetails = await getVettingDetailsForUser(currentUserId.value);
if (vettingDetails) {
reviewerMaterials.value = vettingDetails.reference_materials;
}
}
function saveFilterTags() {
localStorage.setItem(
FILTER_TAG_STORAGE_KEY,
JSON.stringify(filterTags.value),
);
localStorage.setItem(
SHARED_FILTER_TAG_STORAGE_KEY,
JSON.stringify(sharedFilterTags.value),
);
}
function loadFilterTags() {
filterTags.value = JSON.parse(
localStorage.getItem(FILTER_TAG_STORAGE_KEY) || "[]",
);
sharedFilterTags.value = JSON.parse(
localStorage.getItem(SHARED_FILTER_TAG_STORAGE_KEY) || "[]",
);
}
function saveMapFilterBounds(bounds: [number, number, number, number]) {
if (
!bounds ||
bounds.length !== 4 ||
bounds.some((n) => !Number.isFinite(n))
) {
return;
}
localStorage.setItem(MAP_FILTER_BOUNDS_STORAGE_KEY, JSON.stringify(bounds));
}
function clearMapFilterBounds() {
localStorage.removeItem(MAP_FILTER_BOUNDS_STORAGE_KEY);
}
function loadMapFilterBounds(): [number, number, number, number] | null {
try {
const raw = localStorage.getItem(MAP_FILTER_BOUNDS_STORAGE_KEY);
if (!raw) return null;
const parsed: unknown = JSON.parse(raw);
if (!Array.isArray(parsed) || parsed.length !== 4) return null;
const nums = parsed.map((v) => (typeof v === "number" ? v : Number(v)));
if (nums.some((n) => !Number.isFinite(n))) return null;
return nums as [number, number, number, number];
} catch {
return null;
}
}
return {
annotationState,
creationType,
setAnnotationState,
toggleLayerVisibility,
layerVisibility,
createColorScale,
colorScale,
measuring,
toggleMeasureMode,
frequencyRulerY,
drawingBoundingBox,
boundingBoxError,
toggleDrawingBoundingBox,
colorSchemes,
colorScheme,
backgroundColor,
setSelectedUsers,
selectedUsers,
currentUser,
currentUserId,
setSelectedId,
loadConfiguration,
loadCurrentUser,
isNaBat,
// State Passing Elements
annotations,
configuration,
sequenceAnnotations,
otherUserAnnotations,
selectedId,
selectedType,
sharedList,
recordingList,
recordingTagList,
nextShared,
scaledVals,
viewCompressedOverlay,
sideTab,
scaledWidth,
scaledHeight,
fixedAxes,
toggleFixedAxes,
transparencyThreshold,
contoursEnabled,
imageOpacity,
contourOpacity,
contoursLoading,
setContoursEnabled,
loadContours,
clearContours,
computedPulseContours,
showSubmittedRecordings,
myRecordingsDisplay,
sharedRecordingsDisplay,
currentRecordingId,
reviewerMaterials,
loadReviewerMaterials,
viewMaskOverlay,
maskOverlayOpacity,
viewWaveplot,
filterTags,
sharedFilterTags,
saveFilterTags,
loadFilterTags,
saveMapFilterBounds,
clearMapFilterBounds,
loadMapFilterBounds,
spectrogramFilename,
};
}