-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusePulseMetadata.ts
More file actions
148 lines (133 loc) · 5.03 KB
/
usePulseMetadata.ts
File metadata and controls
148 lines (133 loc) · 5.03 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
import { ref, Ref, watch } from "vue";
import { getPulseMetadata, PulseMetadata } from "../api/api";
const STORAGE_KEY = "pulseMetadata";
/** Single mode for pulse metadata labels: None, Always, Hover (inline labels), or Tooltip. */
export type PulseMetadataLabelsMode = "None" | "Always" | "Hover" | "Tooltip";
export const PULSE_METADATA_LABELS_OPTIONS: { title: string; value: PulseMetadataLabelsMode }[] = [
{ title: "None", value: "None" },
{ title: "Always", value: "Always" },
{ title: "Hover (labels)", value: "Hover" },
{ title: "Tooltip", value: "Tooltip" },
];
interface PulseMetadataStorage {
viewPulseMetadataLayer?: boolean;
pulseMetadataLineColor?: string;
pulseMetadataLineSize?: number;
pulseMetadataHeelColor?: string;
pulseMetadataCharFreqColor?: string;
pulseMetadataKneeColor?: string;
pulseMetadataLabelColor?: string;
pulseMetadataLabelFontSize?: number;
pulseMetadataPointSize?: number;
/** Replaces legacy pulseMetadataShowLabels, pulseMetadataShowLabelsOnHover, pulseMetadataHoverMode */
pulseMetadataLabels?: PulseMetadataLabelsMode;
pulseMetadataDurationFreqLineColor?: string;
}
function loadFromStorage(): Partial<PulseMetadataStorage> {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw) {
return JSON.parse(raw) as PulseMetadataStorage;
}
} catch {
// ignore parse errors
}
return {};
}
function saveToStorage(data: PulseMetadataStorage) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
} catch {
// ignore quota errors
}
}
const stored = loadFromStorage();
/** Migrate legacy storage keys to pulseMetadataLabels. */
function getInitialLabelsMode(): PulseMetadataLabelsMode {
const storedLabels = stored.pulseMetadataLabels;
if (storedLabels && ["None", "Always", "Hover", "Tooltip"].includes(storedLabels)) {
return storedLabels as PulseMetadataLabelsMode;
}
return "Tooltip"; // default to Tooltip if no valid stored value
}
const pulseMetadataList: Ref<PulseMetadata[]> = ref([]);
const pulseMetadataLoading = ref(false);
// Default to showing pulse metadata unless user explicitly turned it off.
const viewPulseMetadataLayer = ref(stored.viewPulseMetadataLayer ?? true);
async function loadPulseMetadata(recordingId: number) {
pulseMetadataLoading.value = true;
try {
pulseMetadataList.value = await getPulseMetadata(recordingId);
} finally {
pulseMetadataLoading.value = false;
}
}
function clearPulseMetadata() {
pulseMetadataList.value = [];
}
const toggleViewPulseMetadataLayer = () => {
viewPulseMetadataLayer.value = !viewPulseMetadataLayer.value;
};
// Pulse metadata display style (curve line + heel, char_freq, knee colors and sizes)
const pulseMetadataLineColor = ref(stored.pulseMetadataLineColor ?? "#00FFFF");
const pulseMetadataLineSize = ref(stored.pulseMetadataLineSize ?? 2);
const pulseMetadataHeelColor = ref(stored.pulseMetadataHeelColor ?? "#FF0088");
const pulseMetadataCharFreqColor = ref(stored.pulseMetadataCharFreqColor ?? "#00FF00");
const pulseMetadataKneeColor = ref(stored.pulseMetadataKneeColor ?? "#FF8800");
const pulseMetadataLabelColor = ref(stored.pulseMetadataLabelColor ?? "#FFFFFF");
const pulseMetadataLabelFontSize = ref(stored.pulseMetadataLabelFontSize ?? 12);
const pulseMetadataPointSize = ref(stored.pulseMetadataPointSize ?? 5);
const pulseMetadataLabels = ref<PulseMetadataLabelsMode>(getInitialLabelsMode());
const pulseMetadataDurationFreqLineColor = ref(
stored.pulseMetadataDurationFreqLineColor ?? "#FFFF00",
);
watch(
[
viewPulseMetadataLayer,
pulseMetadataLineColor,
pulseMetadataLineSize,
pulseMetadataHeelColor,
pulseMetadataCharFreqColor,
pulseMetadataKneeColor,
pulseMetadataLabelColor,
pulseMetadataLabelFontSize,
pulseMetadataPointSize,
pulseMetadataLabels,
pulseMetadataDurationFreqLineColor,
],
() => {
saveToStorage({
viewPulseMetadataLayer: viewPulseMetadataLayer.value,
pulseMetadataLineColor: pulseMetadataLineColor.value,
pulseMetadataLineSize: pulseMetadataLineSize.value,
pulseMetadataHeelColor: pulseMetadataHeelColor.value,
pulseMetadataCharFreqColor: pulseMetadataCharFreqColor.value,
pulseMetadataKneeColor: pulseMetadataKneeColor.value,
pulseMetadataLabelColor: pulseMetadataLabelColor.value,
pulseMetadataLabelFontSize: pulseMetadataLabelFontSize.value,
pulseMetadataPointSize: pulseMetadataPointSize.value,
pulseMetadataLabels: pulseMetadataLabels.value,
pulseMetadataDurationFreqLineColor: pulseMetadataDurationFreqLineColor.value,
});
},
);
export default function usePulseMetadata() {
return {
pulseMetadataList,
pulseMetadataLoading,
loadPulseMetadata,
clearPulseMetadata,
viewPulseMetadataLayer,
toggleViewPulseMetadataLayer,
pulseMetadataLineColor,
pulseMetadataLineSize,
pulseMetadataHeelColor,
pulseMetadataCharFreqColor,
pulseMetadataKneeColor,
pulseMetadataLabelColor,
pulseMetadataLabelFontSize,
pulseMetadataPointSize,
pulseMetadataLabels,
pulseMetadataDurationFreqLineColor,
};
}