|
3 | 3 | import RecordingTile from '$lib/components/RecordingTile.svelte'; |
4 | 4 | import { onMount } from 'svelte'; |
5 | 5 | import base64ToBlob from '$lib/utils/base64-to-blob'; |
6 | | - import blobToBase64 from '$lib/utils/blob-to-base64'; |
| 6 | + import type { Transcription } from '$lib/types'; |
| 7 | + import createRecording from '$lib/methods/create-recording'; |
7 | 8 |
|
8 | | - let recordingBlob: Blob | MediaSource | undefined = $state(); |
9 | | - let recordingFileName = $state(''); |
10 | 9 | let recordingUrl = $state(''); |
11 | 10 |
|
12 | 11 | let savedRecordings: { |
| 12 | + data: string; |
13 | 13 | id: string; |
14 | 14 | name: string; |
15 | | - url: string; |
16 | | - transcription?: string; |
17 | | - data: string; |
| 15 | + transcription?: Transcription; |
18 | 16 | }[] = $state.raw([]); |
19 | 17 |
|
20 | | - async function discardRecording() { |
21 | | - if (!recordingUrl) return alert('No recording to delete'); |
22 | | -
|
23 | | - URL.revokeObjectURL(recordingUrl); |
24 | | - recordingUrl = ''; |
25 | | - } |
26 | | -
|
27 | | - async function saveRecording() { |
28 | | - if (!recordingUrl) return alert('No recording to save'); |
29 | | -
|
30 | | - const response = await fetch(recordingUrl); |
31 | | - recordingBlob = await response.blob(); |
32 | | - recordingFileName = `${new Date().toISOString()}-${crypto.randomUUID()}.webm`; |
33 | | - recordingUrl = ''; |
34 | | -
|
35 | | - const base64Data = await blobToBase64(recordingBlob); |
36 | | -
|
37 | | - savedRecordings = [ |
38 | | - { |
39 | | - id: recordingFileName, |
40 | | - name: recordingFileName, |
41 | | - data: base64Data, |
42 | | - transcription: '', |
43 | | - url: URL.createObjectURL(recordingBlob) |
44 | | - }, |
45 | | - ...savedRecordings |
46 | | - ]; |
47 | | - } |
48 | | -
|
49 | | - function deleteRecording(id: string) { |
50 | | - if (confirm('Are you sure you want to delete this recording?')) { |
51 | | - savedRecordings = savedRecordings.filter((rec) => rec.id !== id); |
52 | | - } |
53 | | - } |
54 | | -
|
55 | 18 | onMount(() => { |
56 | 19 | if (localStorage.getItem('savedRecordings')) { |
57 | | - savedRecordings = JSON.parse(`${localStorage.getItem('savedRecordings')}`).map( |
58 | | - (rec: { id: string; name: string; data: string; transcription?: string }) => { |
59 | | - const blob = base64ToBlob(rec.data); |
60 | | - const url = URL.createObjectURL(blob); |
61 | | - return { |
62 | | - id: rec.id, |
63 | | - name: rec.name, |
64 | | - data: rec.data, |
65 | | - transcription: rec.transcription, |
66 | | - url |
67 | | - }; |
68 | | - } |
69 | | - ); |
| 20 | + savedRecordings = JSON.parse(`${localStorage.getItem('savedRecordings')}`); |
70 | 21 | } |
71 | 22 | }); |
72 | 23 |
|
73 | 24 | $effect(() => { |
74 | 25 | if (savedRecordings) { |
75 | | - console.log('savedRecordings:', savedRecordings); |
76 | | -
|
77 | | - localStorage.setItem( |
| 26 | + console.log( |
78 | 27 | 'savedRecordings', |
79 | | - JSON.stringify( |
80 | | - savedRecordings.map(({ id, name, data, transcription }) => ({ |
81 | | - id, |
82 | | - name, |
83 | | - data, |
84 | | - transcription |
85 | | - })) |
86 | | - ) |
| 28 | + savedRecordings.map((x) => x.transcription) |
87 | 29 | ); |
| 30 | +
|
| 31 | + localStorage.setItem('savedRecordings', JSON.stringify(savedRecordings)); |
88 | 32 | } |
89 | 33 | }); |
90 | 34 | </script> |
|
106 | 50 | <br /> |
107 | 51 | <strong>It's that simple.</strong> |
108 | 52 | </h1> |
109 | | - |
110 | | - <!-- <p> |
111 | | - The easiest way to instantly get your ideas, interviews and conversations to Google Docs. |
112 | | - </p> --> |
113 | 53 | </div> |
114 | 54 |
|
115 | | - <Recorder {discardRecording} bind:recordingUrl {saveRecording} /> |
| 55 | + <Recorder |
| 56 | + discardRecording={() => { |
| 57 | + URL.revokeObjectURL(recordingUrl); |
| 58 | + recordingUrl = ''; |
| 59 | + }} |
| 60 | + saveRecording={async () => { |
| 61 | + if (!recordingUrl) return alert('No recording to save'); |
| 62 | + |
| 63 | + const newRecording = await createRecording(recordingUrl); |
| 64 | + |
| 65 | + recordingUrl = ''; |
| 66 | + |
| 67 | + savedRecordings = [newRecording, ...savedRecordings]; |
| 68 | + }} |
| 69 | + bind:recordingUrl |
| 70 | + /> |
116 | 71 |
|
117 | 72 | {#if savedRecordings.length > 0} |
118 | 73 | <section class="recordings"> |
119 | 74 | <ul> |
120 | | - {#each savedRecordings as recording (recording.id)} |
| 75 | + {#each savedRecordings as recording ((recording.id, recording.transcription))} |
121 | 76 | <li> |
122 | 77 | <RecordingTile |
123 | 78 | blob={base64ToBlob(recording.data)} |
| 79 | + deleteRecording={() => { |
| 80 | + if (confirm('Are you sure you want to delete this recording?')) { |
| 81 | + savedRecordings = savedRecordings.filter((r) => r.id !== recording.id); |
| 82 | + } |
| 83 | + }} |
124 | 84 | id={recording.id} |
125 | 85 | name={recording.name} |
126 | | - url={recording.url} |
127 | 86 | transcription={recording.transcription} |
128 | | - deleteRecording={() => deleteRecording(recording.id)} |
129 | 87 | bind:savedRecordings |
130 | 88 | /> |
131 | 89 | </li> |
|
161 | 119 | } |
162 | 120 | } |
163 | 121 |
|
164 | | - .hero p { |
165 | | - font-size: 1.125rem; |
166 | | - line-height: 1.5; |
167 | | - } |
168 | | -
|
169 | 122 | h1 { |
170 | 123 | font-size: 1.875rem; |
171 | 124 | line-height: 1.5; |
|
0 commit comments