/ magland / rip-sound-analyzer
Sign in
magland / rip-sound-analyzer
Add a button to clear all samples from the browser
Jeremy Magland <jmagland@flatironinstitute.org> committed commit 135ff3ac94f1 parent 09371b0 Browse files
3 changed files+34−1
src/App.tsxmodified+19−1View file
@@ -7,12 +7,13 @@ import { readTheme } from "./components/CanvasPlot.tsx";
77 import { DEFAULT_SETTINGS, detectSegment, energyBand } from "./analysis/analyze.ts";
88 import type { CaptureInfo } from "./audio/recorder.ts";
99 import { encodeWavFloat32 } from "./audio/wav.ts";
10-import { deleteRecording, listRecordings, newId, saveRecording, updateMeta, type RecordingMeta } from "./storage/db.ts";
10+import { clearAll, deleteRecording, listRecordings, newId, saveRecording, updateMeta, type RecordingMeta } from "./storage/db.ts";
1111 import {
1212 download,
1313 durationColor,
1414 fmtDate,
1515 fmtSec,
16+ forgetAllAudio,
1617 forgetAudio,
1718 getAudio,
1819 loadSettings,
@@ -118,6 +119,16 @@ export function App() {
118119 setSelected(rest.length ? rest[Math.min(samples.indexOf(m), rest.length - 1)].id : NEW);
119120 };
120121
122+ const clearEverything = async () => {
123+ if (!samples.length) return;
124+ if (!confirm(`Delete all ${samples.length} sample${samples.length > 1 ? "s" : ""} from this browser? This cannot be undone. Download WAVs first if you want to keep any.`)) return;
125+ await clearAll();
126+ forgetAllAudio();
127+ setRecs([]);
128+ setRerecording(null);
129+ setSelected(NEW);
130+ };
131+
121132 const current = recs.find((r) => r.id === selected) ?? null;
122133 const th = readTheme(document.documentElement);
123134 const dur = current ? tearDuration(current) : null;
@@ -186,6 +197,13 @@ export function App() {
186197 })}
187198 {loaded && !samples.length && <p className="muted pad small">No samples yet.</p>}
188199 <p className="muted pad small">Recordings are stored only in this browser.</p>
200+ {samples.length > 0 && (
201+ <p className="pad small">
202+ <button className="danger" onClick={clearEverything}>
203+ Clear all data…
204+ </button>
205+ </p>
206+ )}
189207 </aside>
190208
191209 <main className="main">
src/model.tsmodified+4−0View file
@@ -70,6 +70,10 @@ export function forgetAudio(id: string) {
7070 audioCache.delete(id);
7171 }
7272
73+export function forgetAllAudio() {
74+ audioCache.clear();
75+}
76+
7377 const analysisCache = new Map<string, Analysis>();
7478
7579 function key(m: RecordingMeta, s: AnalysisSettings) {
src/storage/db.tsmodified+11−0View file
@@ -83,6 +83,17 @@ export async function deleteRecording(id: string): Promise<void> {
8383 });
8484 }
8585
86+export async function clearAll(): Promise<void> {
87+ const d = await db();
88+ const tx = d.transaction(["recordings", "audio"], "readwrite");
89+ tx.objectStore("recordings").clear();
90+ tx.objectStore("audio").clear();
91+ await new Promise<void>((resolve, reject) => {
92+ tx.oncomplete = () => resolve();
93+ tx.onerror = () => reject(tx.error);
94+ });
95+}
96+
8697 export function newId(): string {
8798 return crypto.randomUUID();
8899 }
↑↓ move↵openescclose