generated from fahricansecer/boilerplate-fe
Some checks failed
UI Deploy (Next-Auth Support) 🎨 / build-and-deploy (push) Has been cancelled
198 lines
6.9 KiB
TypeScript
198 lines
6.9 KiB
TypeScript
'use client';
|
||
|
||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||
import {
|
||
projectsApi,
|
||
creditsApi,
|
||
templatesApi,
|
||
dashboardApi,
|
||
type Project,
|
||
type CreateProjectPayload,
|
||
type PaginatedResponse,
|
||
} from '@/lib/api/api-service';
|
||
|
||
// ═══════════════════════════════════════════════════════
|
||
// Query Keys — React Query cache yönetimi
|
||
// ═══════════════════════════════════════════════════════
|
||
|
||
export const queryKeys = {
|
||
projects: {
|
||
all: ['projects'] as const,
|
||
list: (params?: Record<string, unknown>) => ['projects', 'list', params] as const,
|
||
detail: (id: string) => ['projects', 'detail', id] as const,
|
||
},
|
||
credits: {
|
||
balance: ['credits', 'balance'] as const,
|
||
history: (params?: Record<string, unknown>) => ['credits', 'history', params] as const,
|
||
},
|
||
templates: {
|
||
all: ['templates'] as const,
|
||
list: (params?: Record<string, unknown>) => ['templates', 'list', params] as const,
|
||
detail: (id: string) => ['templates', 'detail', id] as const,
|
||
},
|
||
dashboard: {
|
||
stats: ['dashboard', 'stats'] as const,
|
||
},
|
||
};
|
||
|
||
// ═══════════════════════════════════════════════════════
|
||
// PROJECTS — Hook'ları
|
||
// ═══════════════════════════════════════════════════════
|
||
|
||
/** Proje listesi — sayfalı, filtreleme destekli */
|
||
export function useProjects(params?: { page?: number; limit?: number; status?: string }) {
|
||
return useQuery({
|
||
queryKey: queryKeys.projects.list(params),
|
||
queryFn: () => projectsApi.list(params),
|
||
staleTime: 30_000,
|
||
});
|
||
}
|
||
|
||
/** Tek proje detayı — sahneler, medya, render job dahil */
|
||
export function useProject(id: string) {
|
||
return useQuery({
|
||
queryKey: queryKeys.projects.detail(id),
|
||
queryFn: () => projectsApi.get(id),
|
||
enabled: !!id,
|
||
staleTime: 10_000,
|
||
});
|
||
}
|
||
|
||
/** Yeni proje oluştur */
|
||
export function useCreateProject() {
|
||
const qc = useQueryClient();
|
||
return useMutation({
|
||
mutationFn: (data: CreateProjectPayload) => projectsApi.create(data),
|
||
onSuccess: () => {
|
||
qc.invalidateQueries({ queryKey: queryKeys.projects.all });
|
||
qc.invalidateQueries({ queryKey: queryKeys.dashboard.stats });
|
||
},
|
||
});
|
||
}
|
||
|
||
/** Proje güncelle */
|
||
export function useUpdateProject() {
|
||
const qc = useQueryClient();
|
||
return useMutation({
|
||
mutationFn: ({ id, data }: { id: string; data: Partial<CreateProjectPayload> }) =>
|
||
projectsApi.update(id, data),
|
||
onSuccess: (_data, variables) => {
|
||
qc.invalidateQueries({ queryKey: queryKeys.projects.detail(variables.id) });
|
||
qc.invalidateQueries({ queryKey: queryKeys.projects.all });
|
||
},
|
||
});
|
||
}
|
||
|
||
/** Proje sil (soft delete) */
|
||
export function useDeleteProject() {
|
||
const qc = useQueryClient();
|
||
return useMutation({
|
||
mutationFn: (id: string) => projectsApi.delete(id),
|
||
onSuccess: () => {
|
||
qc.invalidateQueries({ queryKey: queryKeys.projects.all });
|
||
qc.invalidateQueries({ queryKey: queryKeys.dashboard.stats });
|
||
},
|
||
});
|
||
}
|
||
|
||
/** AI senaryo üret — SEO + Humanizer-enhanced */
|
||
export function useGenerateScript() {
|
||
const qc = useQueryClient();
|
||
return useMutation({
|
||
mutationFn: (projectId: string) => projectsApi.generateScript(projectId),
|
||
onSuccess: (data) => {
|
||
qc.setQueryData(queryKeys.projects.detail(data.id), data);
|
||
qc.invalidateQueries({ queryKey: queryKeys.projects.all });
|
||
},
|
||
});
|
||
}
|
||
|
||
/** Onay ver ve video üretimine gönder (BullMQ kuyruğu) */
|
||
export function useApproveAndQueue() {
|
||
const qc = useQueryClient();
|
||
return useMutation({
|
||
mutationFn: (projectId: string) => projectsApi.approveAndQueue(projectId),
|
||
onSuccess: (_data, projectId) => {
|
||
qc.invalidateQueries({ queryKey: queryKeys.projects.detail(projectId) });
|
||
qc.invalidateQueries({ queryKey: queryKeys.projects.all });
|
||
qc.invalidateQueries({ queryKey: queryKeys.credits.balance });
|
||
qc.invalidateQueries({ queryKey: queryKeys.dashboard.stats });
|
||
},
|
||
});
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════
|
||
// CREDITS — Kredi hook'ları
|
||
// ═══════════════════════════════════════════════════════
|
||
|
||
/** Mevcut kredi bakiyesi */
|
||
export function useCreditBalance() {
|
||
return useQuery({
|
||
queryKey: queryKeys.credits.balance,
|
||
queryFn: () => creditsApi.getBalance(),
|
||
staleTime: 60_000,
|
||
});
|
||
}
|
||
|
||
/** Kredi işlem geçmişi */
|
||
export function useCreditHistory(params?: { page?: number; limit?: number }) {
|
||
return useQuery({
|
||
queryKey: queryKeys.credits.history(params),
|
||
queryFn: () => creditsApi.getHistory(params),
|
||
staleTime: 30_000,
|
||
});
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════
|
||
// TEMPLATES — Şablon hook'ları
|
||
// ═══════════════════════════════════════════════════════
|
||
|
||
/** Şablon galerisi */
|
||
export function useTemplates(params?: {
|
||
category?: string;
|
||
language?: string;
|
||
page?: number;
|
||
limit?: number;
|
||
}) {
|
||
return useQuery({
|
||
queryKey: queryKeys.templates.list(params),
|
||
queryFn: () => templatesApi.list(params),
|
||
staleTime: 120_000,
|
||
});
|
||
}
|
||
|
||
/** Tek şablon detayı */
|
||
export function useTemplate(id: string) {
|
||
return useQuery({
|
||
queryKey: queryKeys.templates.detail(id),
|
||
queryFn: () => templatesApi.get(id),
|
||
enabled: !!id,
|
||
});
|
||
}
|
||
|
||
/** Şablondan proje klonla */
|
||
export function useCloneTemplate() {
|
||
const qc = useQueryClient();
|
||
return useMutation({
|
||
mutationFn: (templateId: string) => templatesApi.clone(templateId),
|
||
onSuccess: () => {
|
||
qc.invalidateQueries({ queryKey: queryKeys.projects.all });
|
||
qc.invalidateQueries({ queryKey: queryKeys.dashboard.stats });
|
||
},
|
||
});
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════
|
||
// DASHBOARD — İstatistik hook'ları
|
||
// ═══════════════════════════════════════════════════════
|
||
|
||
/** Dashboard istatistikleri */
|
||
export function useDashboardStats() {
|
||
return useQuery({
|
||
queryKey: queryKeys.dashboard.stats,
|
||
queryFn: () => dashboardApi.getStats(),
|
||
staleTime: 30_000,
|
||
refetchInterval: 60_000,
|
||
});
|
||
}
|