mirror of
https://github.com/jamiepine/voicebox.git
synced 2026-09-18 06:10:43 -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.
182 lines
4.8 KiB
TypeScript
182 lines
4.8 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { apiClient } from '@/lib/api/client';
|
|
import type { VoiceProfileCreate } from '@/lib/api/types';
|
|
import { usePlatform } from '@/platform/PlatformContext';
|
|
|
|
export function useProfiles() {
|
|
return useQuery({
|
|
queryKey: ['profiles'],
|
|
queryFn: () => apiClient.listProfiles(),
|
|
});
|
|
}
|
|
|
|
export function useProfile(profileId: string) {
|
|
return useQuery({
|
|
queryKey: ['profiles', profileId],
|
|
queryFn: () => apiClient.getProfile(profileId),
|
|
enabled: !!profileId,
|
|
});
|
|
}
|
|
|
|
export function useCreateProfile() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (data: VoiceProfileCreate) => apiClient.createProfile(data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['profiles'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateProfile() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({ profileId, data }: { profileId: string; data: VoiceProfileCreate }) =>
|
|
apiClient.updateProfile(profileId, data),
|
|
onSuccess: (_, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['profiles'] });
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['profiles', variables.profileId],
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteProfile() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (profileId: string) => apiClient.deleteProfile(profileId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['profiles'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useProfileSamples(profileId: string) {
|
|
return useQuery({
|
|
queryKey: ['profiles', profileId, 'samples'],
|
|
queryFn: () => apiClient.listProfileSamples(profileId),
|
|
enabled: !!profileId,
|
|
});
|
|
}
|
|
|
|
export function useAddSample() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
profileId,
|
|
file,
|
|
referenceText,
|
|
}: {
|
|
profileId: string;
|
|
file: File;
|
|
referenceText: string;
|
|
}) => apiClient.addProfileSample(profileId, file, referenceText),
|
|
onSuccess: (_, variables) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['profiles', variables.profileId, 'samples'],
|
|
});
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['profiles', variables.profileId],
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteSample() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (sampleId: string) => apiClient.deleteProfileSample(sampleId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['profiles'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateSample() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({ sampleId, referenceText }: { sampleId: string; referenceText: string }) =>
|
|
apiClient.updateProfileSample(sampleId, referenceText),
|
|
onSuccess: (data) => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['profiles', data.profile_id, 'samples'],
|
|
});
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['profiles', data.profile_id],
|
|
});
|
|
queryClient.invalidateQueries({ queryKey: ['profiles'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useExportProfile() {
|
|
const platform = usePlatform();
|
|
|
|
return useMutation({
|
|
mutationFn: async (profileId: string) => {
|
|
const blob = await apiClient.exportProfile(profileId);
|
|
|
|
// Get profile name for filename
|
|
const profile = await apiClient.getProfile(profileId);
|
|
const safeName = profile.name.replace(/[^a-z0-9]/gi, '-').toLowerCase();
|
|
const filename = `profile-${safeName}.voicebox.zip`;
|
|
|
|
await platform.filesystem.saveFile(filename, blob, [
|
|
{
|
|
name: 'Voicebox Profile',
|
|
extensions: ['zip'],
|
|
},
|
|
]);
|
|
|
|
return blob;
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useImportProfile() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (file: File) => apiClient.importProfile(file),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['profiles'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUploadAvatar() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({ profileId, file }: { profileId: string; file: File }) =>
|
|
apiClient.uploadAvatar(profileId, file),
|
|
onSuccess: (_, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: ['profiles'] });
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['profiles', variables.profileId],
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteAvatar() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (profileId: string) => apiClient.deleteAvatar(profileId),
|
|
onSuccess: (_, profileId) => {
|
|
queryClient.invalidateQueries({ queryKey: ['profiles'] });
|
|
queryClient.invalidateQueries({
|
|
queryKey: ['profiles', profileId],
|
|
});
|
|
},
|
|
});
|
|
}
|