mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-10-02 16:45:15 -07:00
- Replaced direct Tauri API calls with a unified platform context across multiple components, enhancing code maintainability and readability. - Removed the deprecated tauri.ts file, consolidating platform-related logic into the new PlatformContext. - Updated components such as App, AudioPlayer, and ServerSettings to utilize the new platform context for lifecycle management and server interactions. - Improved platform detection and handling for audio playback and system audio capture functionalities. - Ensured consistent error handling and user feedback across the application when interacting with platform-specific features.
94 lines
2.4 KiB
TypeScript
94 lines
2.4 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { apiClient } from '@/lib/api/client';
|
|
import type { HistoryQuery } from '@/lib/api/types';
|
|
import { usePlatform } from '@/platform/PlatformContext';
|
|
|
|
export function useHistory(query?: HistoryQuery) {
|
|
return useQuery({
|
|
queryKey: ['history', query],
|
|
queryFn: () => apiClient.listHistory(query),
|
|
});
|
|
}
|
|
|
|
export function useGenerationDetail(generationId: string) {
|
|
return useQuery({
|
|
queryKey: ['history', generationId],
|
|
queryFn: () => apiClient.getGeneration(generationId),
|
|
enabled: !!generationId,
|
|
});
|
|
}
|
|
|
|
export function useDeleteGeneration() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (generationId: string) => apiClient.deleteGeneration(generationId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['history'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useExportGeneration() {
|
|
const platform = usePlatform();
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ generationId, text }: { generationId: string; text: string }) => {
|
|
const blob = await apiClient.exportGeneration(generationId);
|
|
|
|
// Create safe filename from text
|
|
const safeText = text
|
|
.substring(0, 30)
|
|
.replace(/[^a-z0-9]/gi, '-')
|
|
.toLowerCase();
|
|
const filename = `generation-${safeText}.voicebox.zip`;
|
|
|
|
await platform.filesystem.saveFile(filename, blob, [
|
|
{
|
|
name: 'Voicebox Generation',
|
|
extensions: ['zip'],
|
|
},
|
|
]);
|
|
|
|
return blob;
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useExportGenerationAudio() {
|
|
const platform = usePlatform();
|
|
|
|
return useMutation({
|
|
mutationFn: async ({ generationId, text }: { generationId: string; text: string }) => {
|
|
const blob = await apiClient.exportGenerationAudio(generationId);
|
|
|
|
// Create safe filename from text
|
|
const safeText = text
|
|
.substring(0, 30)
|
|
.replace(/[^a-z0-9]/gi, '-')
|
|
.toLowerCase();
|
|
const filename = `${safeText}.wav`;
|
|
|
|
await platform.filesystem.saveFile(filename, blob, [
|
|
{
|
|
name: 'Audio File',
|
|
extensions: ['wav'],
|
|
},
|
|
]);
|
|
|
|
return blob;
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useImportGeneration() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (file: File) => apiClient.importGeneration(file),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['history'] });
|
|
},
|
|
});
|
|
}
|