Skip to content

Commit 551abc9

Browse files
committed
Enhance history management with export and import functionalities
- Added endpoints for exporting generations as ZIP archives and audio files. - Implemented import functionality for generations from ZIP archives with validation. - Updated HistoryTable component to support new export and import features. - Improved error handling and user notifications for export/import processes. - Refactored related hooks and API client methods to accommodate new functionalities.
1 parent 333cb26 commit 551abc9

14 files changed

Lines changed: 653 additions & 45 deletions

File tree

‎app/src/components/History/HistoryTable.tsx‎

Lines changed: 152 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
import { AudioWaveform, Download, MoreHorizontal, Play, Trash2 } from 'lucide-react';
2-
import { useState, useRef, useEffect } from 'react';
1+
import { AudioWaveform, Download, FileArchive, MoreHorizontal, Play, Trash2 } from 'lucide-react';
2+
import { useEffect, useRef, useState } from 'react';
33
import { Button } from '@/components/ui/button';
4+
import {
5+
Dialog,
6+
DialogContent,
7+
DialogDescription,
8+
DialogFooter,
9+
DialogHeader,
10+
DialogTitle,
11+
} from '@/components/ui/dialog';
412
import {
513
DropdownMenu,
614
DropdownMenuContent,
@@ -9,7 +17,13 @@ import {
917
} from '@/components/ui/dropdown-menu';
1018
import { Textarea } from '@/components/ui/textarea';
1119
import { apiClient } from '@/lib/api/client';
12-
import { useDeleteGeneration, useHistory } from '@/lib/hooks/useHistory';
20+
import {
21+
useDeleteGeneration,
22+
useExportGeneration,
23+
useExportGenerationAudio,
24+
useHistory,
25+
useImportGeneration,
26+
} from '@/lib/hooks/useHistory';
1327
import { cn } from '@/lib/utils/cn';
1428
import { formatDate, formatDuration } from '@/lib/utils/format';
1529
import { usePlayerStore } from '@/stores/playerStore';
@@ -22,6 +36,9 @@ export function HistoryTable() {
2236
const [page, setPage] = useState(0);
2337
const [isScrolled, setIsScrolled] = useState(false);
2438
const scrollRef = useRef<HTMLDivElement>(null);
39+
const fileInputRef = useRef<HTMLInputElement>(null);
40+
const [importDialogOpen, setImportDialogOpen] = useState(false);
41+
const [selectedFile, setSelectedFile] = useState<File | null>(null);
2542
const limit = 20;
2643

2744
const { data: historyData, isLoading } = useHistory({
@@ -30,6 +47,9 @@ export function HistoryTable() {
3047
});
3148

3249
const deleteGeneration = useDeleteGeneration();
50+
const exportGeneration = useExportGeneration();
51+
const exportGenerationAudio = useExportGenerationAudio();
52+
const importGeneration = useImportGeneration();
3353
const setAudio = usePlayerStore((state) => state.setAudio);
3454
const restartCurrentAudio = usePlayerStore((state) => state.restartCurrentAudio);
3555
const currentAudioId = usePlayerStore((state) => state.audioId);
@@ -60,23 +80,65 @@ export function HistoryTable() {
6080
}
6181
};
6282

63-
const handleDownload = (audioId: string, text: string) => {
64-
const audioUrl = apiClient.getAudioUrl(audioId);
65-
const filename = `${text.substring(0, 30).replace(/[^a-z0-9]/gi, '_')}.wav`;
66-
const link = document.createElement('a');
67-
link.href = audioUrl;
68-
link.download = filename;
69-
document.body.appendChild(link);
70-
link.click();
71-
document.body.removeChild(link);
83+
const handleDownloadAudio = (generationId: string, text: string) => {
84+
exportGenerationAudio.mutate(
85+
{ generationId, text },
86+
{
87+
onError: (error) => {
88+
alert(`Failed to download audio: ${error.message}`);
89+
},
90+
},
91+
);
7292
};
7393

74-
if (isLoading) {
75-
return (
76-
<div className="flex items-center justify-center p-8">
77-
<div className="text-muted-foreground">Loading history...</div>
78-
</div>
94+
const handleExportPackage = (generationId: string, text: string) => {
95+
exportGeneration.mutate(
96+
{ generationId, text },
97+
{
98+
onError: (error) => {
99+
alert(`Failed to export generation: ${error.message}`);
100+
},
101+
},
79102
);
103+
};
104+
105+
const _handleImportClick = () => {
106+
file_handleImportClickk.click();
107+
};
108+
109+
const _handleFileChange = (_e: React.ChangeEvent<HTMLInputElement>) => {
110+
cons_handleFileChangeet.files?.[0];
111+
if (file) {
112+
// Validate file extension
113+
if (!file.name.endsWith('.voicebox.zip')) {
114+
alert('Please select a valid .voicebox.zip file');
115+
return;
116+
}
117+
setSelectedFile(file);
118+
setImportDialogOpen(true);
119+
}
120+
};
121+
122+
const handleImportConfirm = () => {
123+
if (selectedFile) {
124+
importGeneration.mutate(selectedFile, {
125+
onSuccess: (data) => {
126+
setImportDialogOpen(false);
127+
setSelectedFile(null);
128+
if (fileInputRef.current) {
129+
fileInputRef.current.value = '';
130+
}
131+
alert(data.message || 'Generation imported successfully');
132+
},
133+
onError: (error) => {
134+
alert(`Failed to import generation: ${error.message}`);
135+
},
136+
});
137+
}
138+
};
139+
140+
if (isLoading) {
141+
return null;
80142
}
81143

82144
const history = historyData?.items || [];
@@ -85,8 +147,25 @@ export function HistoryTable() {
85147

86148
return (
87149
<div className="flex flex-col h-full min-h-0 relative">
150+
{/* <div className="flex justify-between items-center mb-4 shrink-0">
151+
<h2 className="text-2xl font-bold">History</h2>
152+
<div className="flex gap-2">
153+
<Button variant="outline" onClick={handleImportClick}>
154+
<Upload className="mr-2 h-4 w-4" />
155+
Import Generation
156+
</Button>
157+
<input
158+
ref={fileInputRef}
159+
type="file"
160+
accept=".voicebox.zip"
161+
onChange={handleFileChange}
162+
className="hidden"
163+
/>
164+
</div>
165+
</div> */}
166+
88167
{history.length === 0 ? (
89-
<div className="text-center py-12 text-muted-foreground flex-1 flex items-center justify-center">
168+
<div className="text-center py-12 px-5 border-2 border-dashed border-gray-200 rounded-md text-muted-foreground flex-1 flex items-center justify-center">
90169
No generation history yet. Generate your first audio to see it here.
91170
</div>
92171
) : (
@@ -104,15 +183,20 @@ export function HistoryTable() {
104183
{history.map((gen) => {
105184
const isCurrentlyPlaying = currentAudioId === gen.id && isPlaying;
106185
return (
107-
<button
186+
<div
108187
key={gen.id}
109-
type="button"
110188
className={cn(
111-
'flex items-stretch gap-4 h-24 border rounded-md p-3 bg-card hover:bg-muted/70 transition-colors cursor-pointer text-left w-full',
189+
'flex items-stretch gap-4 h-26 border rounded-md p-3 bg-card hover:bg-muted/70 transition-colors text-left w-full',
112190
isCurrentlyPlaying && 'bg-muted/70',
113191
)}
114-
onClick={() => handlePlay(gen.id, gen.text)}
115-
aria-label={`Play audio: ${gen.text.substring(0, 50)}`}
192+
onMouseDown={(e) => {
193+
// Don't trigger play if clicking on textarea or if text is selected
194+
const target = e.target as HTMLElement;
195+
if (target.closest('textarea') || window.getSelection()?.toString()) {
196+
return;
197+
}
198+
handlePlay(gen.id, gen.text);
199+
}}
116200
>
117201
{/* Waveform icon */}
118202
<div className="flex items-center shrink-0">
@@ -138,10 +222,8 @@ export function HistoryTable() {
138222
{/* Right side - Transcript textarea */}
139223
<div className="flex-1 min-w-0 flex">
140224
<Textarea
141-
readOnly
142225
value={gen.text}
143-
className="flex-1 resize-none text-sm text-muted-foreground"
144-
onClick={(e) => e.stopPropagation()}
226+
className="flex-1 resize-none text-sm text-muted-foreground select-text"
145227
/>
146228
</div>
147229

@@ -164,9 +246,19 @@ export function HistoryTable() {
164246
<Play className="mr-2 h-4 w-4" />
165247
Play
166248
</DropdownMenuItem>
167-
<DropdownMenuItem onClick={() => handleDownload(gen.id, gen.text)}>
249+
<DropdownMenuItem
250+
onClick={() => handleDownloadAudio(gen.id, gen.text)}
251+
disabled={exportGenerationAudio.isPending}
252+
>
168253
<Download className="mr-2 h-4 w-4" />
169-
Download
254+
Export Audio
255+
</DropdownMenuItem>
256+
<DropdownMenuItem
257+
onClick={() => handleExportPackage(gen.id, gen.text)}
258+
disabled={exportGeneration.isPending}
259+
>
260+
<FileArchive className="mr-2 h-4 w-4" />
261+
Export Package
170262
</DropdownMenuItem>
171263
<DropdownMenuItem
172264
onClick={() => deleteGeneration.mutate(gen.id)}
@@ -179,7 +271,7 @@ export function HistoryTable() {
179271
</DropdownMenuContent>
180272
</DropdownMenu>
181273
</div>
182-
</button>
274+
</div>
183275
);
184276
})}
185277
</div>
@@ -203,6 +295,37 @@ export function HistoryTable() {
203295
)}
204296
</>
205297
)}
298+
299+
<Dialog open={importDialogOpen} onOpenChange={setImportDialogOpen}>
300+
<DialogContent>
301+
<DialogHeader>
302+
<DialogTitle>Import Generation</DialogTitle>
303+
<DialogDescription>
304+
Import the generation from "{selectedFile?.name}". This will add it to your history.
305+
</DialogDescription>
306+
</DialogHeader>
307+
<DialogFooter>
308+
<Button
309+
variant="outline"
310+
onClick={() => {
311+
setImportDialogOpen(false);
312+
setSelectedFile(null);
313+
if (fileInputRef.current) {
314+
fileInputRef.current.value = '';
315+
}
316+
}}
317+
>
318+
Cancel
319+
</Button>
320+
<Button
321+
onClick={handleImportConfirm}
322+
disabled={importGeneration.isPending || !selectedFile}
323+
>
324+
{importGeneration.isPending ? 'Importing...' : 'Import'}
325+
</Button>
326+
</DialogFooter>
327+
</DialogContent>
328+
</Dialog>
206329
</div>
207330
);
208331
}

‎app/src/components/Sidebar.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Volume2, Loader2, Settings } from 'lucide-react';
1+
import { Loader2, Settings, Volume2 } from 'lucide-react';
22
import voiceboxLogo from '@/assets/voicebox-logo.png';
33
import { cn } from '@/lib/utils/cn';
44
import { useGenerationStore } from '@/stores/generationStore';
@@ -21,10 +21,12 @@ export function Sidebar({ activeTab, onTabChange, isMacOS }: SidebarProps) {
2121
const isPlayerVisible = !!audioUrl;
2222

2323
return (
24-
<div className={cn(
25-
"fixed left-0 top-0 h-full w-20 bg-sidebar border-r border-border flex flex-col items-center py-6 gap-6",
26-
isMacOS && "pt-14"
27-
)}>
24+
<div
25+
className={cn(
26+
'fixed left-0 top-0 h-full w-20 bg-sidebar border-r border-border flex flex-col items-center py-6 gap-6',
27+
isMacOS && 'pt-14',
28+
)}
29+
>
2830
{/* Logo */}
2931
<div className="mb-2">
3032
<img src={voiceboxLogo} alt="Voicebox" className="w-12 h-12 object-contain" />

‎app/src/components/VoiceProfiles/ProfileList.tsx‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ export function ProfileList() {
5858
};
5959

6060
if (isLoading) {
61-
return (
62-
<div className="flex items-center justify-center p-8">
63-
<div className="text-muted-foreground">Loading profiles...</div>
64-
</div>
65-
);
61+
return null;
6662
}
6763

6864
if (error) {

‎app/src/lib/api/client.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,54 @@ class ApiClient {
176176
});
177177
}
178178

179+
async exportGeneration(generationId: string): Promise<Blob> {
180+
const url = `${this.getBaseUrl()}/history/${generationId}/export`;
181+
const response = await fetch(url);
182+
183+
if (!response.ok) {
184+
const error = await response.json().catch(() => ({
185+
detail: response.statusText,
186+
}));
187+
throw new Error(error.detail || `HTTP error! status: ${response.status}`);
188+
}
189+
190+
return response.blob();
191+
}
192+
193+
async exportGenerationAudio(generationId: string): Promise<Blob> {
194+
const url = `${this.getBaseUrl()}/history/${generationId}/export-audio`;
195+
const response = await fetch(url);
196+
197+
if (!response.ok) {
198+
const error = await response.json().catch(() => ({
199+
detail: response.statusText,
200+
}));
201+
throw new Error(error.detail || `HTTP error! status: ${response.status}`);
202+
}
203+
204+
return response.blob();
205+
}
206+
207+
async importGeneration(file: File): Promise<{ id: string; profile_id: string; profile_name: string; text: string; message: string }> {
208+
const url = `${this.getBaseUrl()}/history/import`;
209+
const formData = new FormData();
210+
formData.append('file', file);
211+
212+
const response = await fetch(url, {
213+
method: 'POST',
214+
body: formData,
215+
});
216+
217+
if (!response.ok) {
218+
const error = await response.json().catch(() => ({
219+
detail: response.statusText,
220+
}));
221+
throw new Error(error.detail || `HTTP error! status: ${response.status}`);
222+
}
223+
224+
return response.json();
225+
}
226+
179227
// Audio
180228
getAudioUrl(audioId: string): string {
181229
return `${this.getBaseUrl()}/audio/${audioId}`;

0 commit comments

Comments
 (0)