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' ;
33import { Button } from '@/components/ui/button' ;
4+ import {
5+ Dialog ,
6+ DialogContent ,
7+ DialogDescription ,
8+ DialogFooter ,
9+ DialogHeader ,
10+ DialogTitle ,
11+ } from '@/components/ui/dialog' ;
412import {
513 DropdownMenu ,
614 DropdownMenuContent ,
@@ -9,7 +17,13 @@ import {
917} from '@/components/ui/dropdown-menu' ;
1018import { Textarea } from '@/components/ui/textarea' ;
1119import { 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' ;
1327import { cn } from '@/lib/utils/cn' ;
1428import { formatDate , formatDuration } from '@/lib/utils/format' ;
1529import { 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 - z 0 - 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}
0 commit comments