- drizzle/0008_add_default_monitoring_mode.sql - src/db/schema/platform-settings.ts - src/db/repositories/platform-settings-repository.ts - src/types/index.ts - src/server/routes/platform-settings.ts - src/frontend/src/components/PlatformSettingsForm.tsx - src/frontend/src/api/hooks/usePlatformSettings.ts - src/__tests__/platform-settings-api.test.ts
59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { apiClient } from '../client';
|
|
import type { PlatformSettings } from '@shared/types/index';
|
|
|
|
// ── Query Keys ──
|
|
|
|
export const platformSettingsKeys = {
|
|
all: ['platformSettings'] as const,
|
|
detail: (platform: string) => ['platformSettings', platform] as const,
|
|
};
|
|
|
|
// ── Queries ──
|
|
|
|
/** Fetch all platform settings. */
|
|
export function usePlatformSettings() {
|
|
return useQuery({
|
|
queryKey: platformSettingsKeys.all,
|
|
queryFn: () => apiClient.get<PlatformSettings[]>('/api/v1/platform-settings'),
|
|
});
|
|
}
|
|
|
|
/** Fetch platform settings for a single platform. */
|
|
export function usePlatformSetting(platform: string | null) {
|
|
return useQuery({
|
|
queryKey: platformSettingsKeys.detail(platform ?? ''),
|
|
queryFn: () => apiClient.get<PlatformSettings>(`/api/v1/platform-settings/${platform}`),
|
|
enabled: !!platform,
|
|
});
|
|
}
|
|
|
|
// ── Mutation Payload ──
|
|
|
|
export interface UpdatePlatformSettingsInput {
|
|
platform: string;
|
|
defaultFormatProfileId?: number | null;
|
|
checkInterval?: number;
|
|
concurrencyLimit?: number;
|
|
subtitleLanguages?: string | null;
|
|
grabAllEnabled?: boolean;
|
|
grabAllOrder?: 'newest' | 'oldest';
|
|
scanLimit?: number;
|
|
rateLimitDelay?: number;
|
|
defaultMonitoringMode?: 'all' | 'future' | 'existing' | 'none';
|
|
}
|
|
|
|
// ── Mutations ──
|
|
|
|
/** Upsert platform settings. Invalidates list on success. */
|
|
export function useUpdatePlatformSettings() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ platform, ...input }: UpdatePlatformSettingsInput) =>
|
|
apiClient.put<PlatformSettings>(`/api/v1/platform-settings/${platform}`, input),
|
|
onSuccess: (_data, variables) => {
|
|
queryClient.invalidateQueries({ queryKey: platformSettingsKeys.all });
|
|
queryClient.invalidateQueries({ queryKey: platformSettingsKeys.detail(variables.platform) });
|
|
},
|
|
});
|
|
}
|