Skip to content

Commit ec2103b

Browse files
author
Aleksander Grygier
committed
feat: Use IndexedDB for storing the recordings data
1 parent 3e3ae2c commit ec2103b

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

apps/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"eslint-config-prettier": "^9.1.0",
2828
"eslint-plugin-svelte": "^2.36.0",
2929
"globals": "^15.0.0",
30+
"idb": "^8.0.0",
3031
"openai": "^4.67.3",
3132
"prettier": "^3.1.1",
3233
"prettier-plugin-svelte": "^3.1.2",

apps/web/src/lib/components/RecordingTile.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282

8383
<div bind:this={waveformContainer}></div>
8484

85-
{#if transcription}
85+
{#if transcription?.text && transcription.text !== `Empty ${id}`}
8686
<div class="transcription">
8787
<div class="inner">
8888
{transcription.text}
@@ -142,7 +142,7 @@
142142

143143
<Button kind="secondary" download={name} href={recordingUrl} label="Download"></Button>
144144

145-
<Button kind="danger" onclick={async () => await deleteRecording()} label="Delete" />
145+
<Button kind="danger" onclick={() => deleteRecording()} label="Delete" />
146146
</div>
147147
</div>
148148

apps/web/src/lib/methods/create-recording.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default async function createRecording(recordingUrl: string): Promise<{
55
id: string;
66
name: string;
77
data: string;
8-
transcription: Transcription | undefined;
8+
transcription: Transcription;
99
}> {
1010
const response = await fetch(recordingUrl);
1111

@@ -16,6 +16,11 @@ export default async function createRecording(recordingUrl: string): Promise<{
1616
id: recordingId,
1717
name: `${new Date().toISOString()}-${recordingId}.webm`,
1818
data: await blobToBase64(recordingBlob),
19-
transcription: undefined
19+
transcription: {
20+
text: `Empty ${recordingId}`,
21+
vtt: '',
22+
word_count: 0,
23+
words: []
24+
}
2025
};
2126
}

apps/web/src/lib/methods/transcribe-recording.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default async function transcribeRecording(
44
try {
55
const formData = new FormData();
66
const newBlob = new Blob([audioBlob], { type: 'audio/webm' });
7+
78
formData.append('audio', newBlob, 'audio.webm');
89

910
const response = await fetch('/api/transcribe', {

apps/web/src/routes/+page.svelte

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import base64ToBlob from '$lib/utils/base64-to-blob';
66
import type { Transcription } from '$lib/types';
77
import createRecording from '$lib/methods/create-recording';
8+
import { openDB } from 'idb';
9+
10+
let db: any;
811
912
let recordingUrl = $state('');
1013
@@ -15,20 +18,37 @@
1518
transcription?: Transcription;
1619
}[] = $state.raw([]);
1720
18-
onMount(() => {
19-
if (localStorage.getItem('savedRecordings')) {
20-
savedRecordings = JSON.parse(`${localStorage.getItem('savedRecordings')}`);
21-
}
21+
async function deleteRecordingFromDB(id: string) {
22+
const tx = db.transaction('recordings', 'readwrite');
23+
const store = tx.objectStore('recordings');
24+
25+
await store.delete(id);
26+
}
27+
28+
onMount(async () => {
29+
db = await openDB('MemvoDB', 1, {
30+
upgrade(db) {
31+
if (!db.objectStoreNames.contains('recordings')) {
32+
db.createObjectStore('recordings', {
33+
keyPath: 'id'
34+
});
35+
}
36+
}
37+
});
38+
39+
const allRecordings = await db.getAll('recordings');
40+
41+
savedRecordings = allRecordings || [];
2242
});
2343
2444
$effect(() => {
2545
if (savedRecordings) {
26-
console.log(
27-
'savedRecordings',
28-
savedRecordings.map((x) => x.transcription)
29-
);
46+
const tx = db?.transaction('recordings', 'readwrite');
47+
const store = tx?.objectStore('recordings');
3048
31-
localStorage.setItem('savedRecordings', JSON.stringify(savedRecordings));
49+
for (const recording of savedRecordings) {
50+
store.put(recording);
51+
}
3252
}
3353
});
3454
</script>
@@ -76,9 +96,11 @@
7696
<li>
7797
<RecordingTile
7898
blob={base64ToBlob(recording.data)}
79-
deleteRecording={() => {
99+
deleteRecording={async () => {
80100
if (confirm('Are you sure you want to delete this recording?')) {
81101
savedRecordings = savedRecordings.filter((r) => r.id !== recording.id);
102+
103+
await deleteRecordingFromDB(recording.id);
82104
}
83105
}}
84106
id={recording.id}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)