File size: 1,672 Bytes
9853396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { useMutation, useQuery } from '@tanstack/react-query';
import { useRouter } from '@tanstack/react-router';
import { toast } from 'sonner';
import { systemApi } from '@/lib/api-client';
import i18n from '@/lib/i18n';

export interface SystemStatus {
  isInitialized: boolean;
}

export interface InitializeSystemInput {
  ownerEmail: string;
  ownerPassword: string;
  ownerFirstName: string;
  ownerLastName: string;
  brandName: string;
}

export interface InitializeSystemPayload {
  success: boolean;
  message: string;
}

export function useSystemStatus() {
  return useQuery({
    queryKey: ['systemStatus'],
    queryFn: async (): Promise<SystemStatus> => {
      return await systemApi.getStatus();
    },
    retry: 1, // Allow one retry to handle network hiccups on first load
    retryDelay: 500, // Wait 500ms before retry
    refetchOnWindowFocus: false,
    staleTime: 5 * 60 * 1000, // Consider data fresh for 5 minutes
  });
}

export function useInitializeSystem() {
  const router = useRouter();

  return useMutation({
    mutationFn: async (input: InitializeSystemInput): Promise<InitializeSystemPayload> => {
      return await systemApi.initialize(input);
    },
    onSuccess: (data) => {
      if (data.success) {
        toast.success(data.message);

        // 初始化成功后跳转到登录页面
        router.navigate({ to: '/sign-in' });
      } else {
        toast.error(data.message);
      }
    },
    onError: (error: any) => {
      const errorMessage = error.message || i18n.t('common.errors.initializationFailed');
      toast.error(errorMessage);
    },
  });
}