File size: 3,803 Bytes
01d5a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { create } from 'zustand';
import { getUploadList, type IUploadInfo } from '@/service/upload';

interface UploadState {
  uploads: IUploadInfo[];
  total: number;
  loading: boolean;
  fetchUploadList: (refresh?: boolean) => Promise<void>;
  paginationRef: React.MutableRefObject<{ page_no: number; page_size: number }>;
  addUpload: (upload: IUploadInfo) => void;
  updateUploadStatus: (instance_id: string, status: IUploadInfo) => void;
  removeUpload: (instanceId: string) => void;
  reorderUploads: (instance_id: string | null) => void;
}

export const useUploadStore = create<UploadState>((set, get) => ({
  uploads: [],
  total: 0,
  loading: false,
  fetchUploadList: (refresh = true) => {
    set({ loading: true });

    if (refresh) {
      set({ uploads: [], total: 0 });
      set({ paginationRef: { current: { page_no: 1, page_size: 8 } } });
    } else {
      const pageNo = get().paginationRef.current.page_no;

      set({ paginationRef: { current: { page_no: pageNo + 1, page_size: 8 } } });
    }

    return getUploadList({ ...get().paginationRef.current })
      .then((res) => {
        if (res.data.code === 0) {
          const _data = res.data.data;

          if (refresh) {
            set({
              uploads: _data.items || [],
              total: _data.pagination.total
            });
          } else {
            const _list = get().uploads;
            const _newList = _data.items || [];

            _newList.forEach((item) => {
              const index = _list.findIndex((oldItem) => oldItem.instance_id === item.instance_id);

              if (index !== -1) {
                _list[index] = item;
              } else {
                _list.push(item);
              }
            });

            set({
              uploads: [..._list],
              total: _data.pagination.total
            });
          }
        }
      })
      .catch((err) => {
        console.error('Failed to fetch upload list:', err);
      })
      .finally(() => {
        set({ loading: false });
      });
  },
  paginationRef: { current: { page_no: 1, page_size: 8 } },
  addUpload: (upload) =>
    set((state) => {
      // If an upload with the same upload_name exists, replace it
      const index = state.uploads.findIndex((u) => u.instance_id === upload.instance_id);

      if (index !== -1) {
        const newUploads = [...state.uploads];

        newUploads[index] = upload;

        return { uploads: newUploads };
      }

      // If it doesn't exist, add it to the array
      return { uploads: [...state.uploads, upload] };
    }),
  updateUploadStatus: (instance_id, status) =>
    set((state) => ({
      uploads: state.uploads.map((upload) => (upload.instance_id === instance_id ? status : upload))
    })),
  removeUpload: (instanceId) => {
    set((state) => ({
      uploads: state.uploads.filter((upload) => !(upload.instance_id === instanceId))
    }));
  },
  reorderUploads: (instance_id: string | null) => {
    if (!instance_id) return;

    try {
      set((state) => {
        // Find the current user's upload
        const currentUserUploadIndex = state.uploads.findIndex(
          (upload) => upload.instance_id === instance_id
        );

        // If not found or already at the beginning, return the original state
        if (currentUserUploadIndex === -1 || currentUserUploadIndex === 0) return state;

        // Create a new array with the current user's upload at the beginning
        const reorderedUploads = [...state.uploads];
        const [currentUserUpload] = reorderedUploads.splice(currentUserUploadIndex, 1);

        reorderedUploads.unshift(currentUserUpload);

        return { uploads: reorderedUploads };
      });
    } catch (error) {
      console.error('Error reordering uploads:', error);
    }
  }
}));