2026-03-19 01:12:17 +08:00
|
|
|
|
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
2026-03-20 15:15:35 +08:00
|
|
|
|
import { router } from 'expo-router';
|
|
|
|
|
|
|
|
|
|
|
|
import { tokenManager } from '@/core/auth/token-manager';
|
|
|
|
|
|
import { authKeys } from '@/features/auth/hooks';
|
2026-03-19 01:12:17 +08:00
|
|
|
|
|
|
|
|
|
|
import { profileApi } from './api';
|
|
|
|
|
|
import type {
|
|
|
|
|
|
LegalDocType,
|
2026-03-20 15:15:35 +08:00
|
|
|
|
PurgeUserDataRequest,
|
2026-03-19 01:12:17 +08:00
|
|
|
|
SubmitFeedbackRequest,
|
|
|
|
|
|
UpdateProfileRequest,
|
|
|
|
|
|
} from './types';
|
|
|
|
|
|
|
|
|
|
|
|
const profileKeys = {
|
|
|
|
|
|
all: ['profile'] as const,
|
|
|
|
|
|
detail: () => [...profileKeys.all, 'detail'] as const,
|
|
|
|
|
|
plans: () => [...profileKeys.all, 'plans'] as const,
|
|
|
|
|
|
currentPlan: () => [...profileKeys.all, 'current-plan'] as const,
|
|
|
|
|
|
quota: () => [...profileKeys.all, 'quota'] as const,
|
|
|
|
|
|
faqs: () => [...profileKeys.all, 'faqs'] as const,
|
|
|
|
|
|
legal: (type: LegalDocType) => [...profileKeys.all, 'legal', type] as const,
|
|
|
|
|
|
} as const;
|
|
|
|
|
|
|
|
|
|
|
|
// ─── Profile ───
|
|
|
|
|
|
|
|
|
|
|
|
export function useProfile() {
|
|
|
|
|
|
return useQuery({
|
|
|
|
|
|
queryKey: profileKeys.detail(),
|
|
|
|
|
|
queryFn: () => profileApi.fetchProfile(),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function useUpdateProfile() {
|
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
|
|
mutationFn: (body: UpdateProfileRequest) => profileApi.updateProfile(body),
|
|
|
|
|
|
onSuccess: (data) => {
|
|
|
|
|
|
queryClient.setQueryData(profileKeys.detail(), data);
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─── Plans ───
|
|
|
|
|
|
|
|
|
|
|
|
export function usePlans() {
|
|
|
|
|
|
return useQuery({
|
|
|
|
|
|
queryKey: profileKeys.plans(),
|
|
|
|
|
|
queryFn: () => profileApi.fetchPlans(),
|
|
|
|
|
|
staleTime: 10 * 60 * 1000,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function useCurrentPlan() {
|
|
|
|
|
|
return useQuery({
|
|
|
|
|
|
queryKey: profileKeys.currentPlan(),
|
|
|
|
|
|
queryFn: () => profileApi.fetchCurrentPlan(),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─── Quota ───
|
|
|
|
|
|
|
|
|
|
|
|
export function useQuota() {
|
|
|
|
|
|
return useQuery({
|
|
|
|
|
|
queryKey: profileKeys.quota(),
|
|
|
|
|
|
queryFn: () => profileApi.checkQuota(),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─── FAQ ───
|
|
|
|
|
|
|
|
|
|
|
|
export function useFaqs() {
|
|
|
|
|
|
return useQuery({
|
|
|
|
|
|
queryKey: profileKeys.faqs(),
|
|
|
|
|
|
queryFn: () => profileApi.fetchFaqs(),
|
|
|
|
|
|
staleTime: 30 * 60 * 1000,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ─── Feedback ───
|
|
|
|
|
|
|
|
|
|
|
|
export function useSubmitFeedback() {
|
|
|
|
|
|
return useMutation({
|
|
|
|
|
|
mutationFn: (body: SubmitFeedbackRequest) =>
|
|
|
|
|
|
profileApi.submitFeedback(body),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-20 15:15:35 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 永久清空服务端业务数据;成功后服务端会吊销所有 refresh token,
|
|
|
|
|
|
* 因此仅清本地会话并跳转登录(不再调用 logout 接口)。
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function usePurgeUserData() {
|
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
|
|
return useMutation({
|
|
|
|
|
|
mutationFn: (body: PurgeUserDataRequest) => profileApi.purgeUserData(body),
|
|
|
|
|
|
onSuccess: async () => {
|
|
|
|
|
|
await tokenManager.clearTokens();
|
|
|
|
|
|
queryClient.clear();
|
|
|
|
|
|
queryClient.setQueryData(authKeys.tokenCheck, false);
|
|
|
|
|
|
router.replace('/(auth)/login');
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-19 01:12:17 +08:00
|
|
|
|
// ─── Legal ───
|
|
|
|
|
|
|
2026-03-22 16:45:57 +08:00
|
|
|
|
export function useLegalDoc(
|
|
|
|
|
|
type: LegalDocType,
|
|
|
|
|
|
options?: { enabled?: boolean },
|
|
|
|
|
|
) {
|
2026-03-19 01:12:17 +08:00
|
|
|
|
return useQuery({
|
|
|
|
|
|
queryKey: profileKeys.legal(type),
|
|
|
|
|
|
queryFn: () => profileApi.fetchLegalDoc(type),
|
|
|
|
|
|
staleTime: Infinity,
|
2026-03-22 16:45:57 +08:00
|
|
|
|
enabled: options?.enabled ?? true,
|
2026-03-19 01:12:17 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|