返回 AiToEarn
usePublishDialogData.ts
根目录 / project / aitoearn-web / src / components / PublishDialog / usePublishDialogData.ts
1 import type { PublishOptionValueItem } from '@/api/channels/channel.types'
2 import type { PinterestBoardItem } from '@/api/platforms/pinterest.types'
3 import type {
4 BiblPartItem,
5 YouTubeCategoryItem,
6 } from '@/components/PublishDialog/publishDialog.type'
7 import lodash from 'lodash'
8 import { create } from 'zustand'
9 import { combine } from 'zustand/middleware'
10 import { apiGetBilibiliPartitions } from '@/api/platforms/bilibili.api'
11 import { getPinterestBoardListApi } from '@/api/platforms/pinterest.api'
12 import { apiGetYouTubeCategories } from '@/api/platforms/youtube.api'
13 import { PlatType } from '@/app/config/platConfig'
14 import { useAccountStore } from '@/store/account'
15
16 export interface IPublishDialogDataStore {
17 // b站分区列表
18 bilibiliPartitions: BiblPartItem[]
19 // b站分区加载状态
20 bilibiliPartitionsLoading: boolean
21 // YouTube视频分类列表
22 youTubeCategories: YouTubeCategoryItem[]
23 // YouTube视频分类加载状态
24 youTubeCategoriesLoading: boolean
25 // Pinterest Board列表
26 pinterestBoards: PinterestBoardItem[]
27 }
28
29 const store: IPublishDialogDataStore = {
30 bilibiliPartitions: [],
31 bilibiliPartitionsLoading: false,
32 youTubeCategories: [],
33 youTubeCategoriesLoading: false,
34 pinterestBoards: [],
35 }
36
37 function getStore() {
38 return lodash.cloneDeep(store)
39 }
40
41 function mapYouTubeCategory(item: PublishOptionValueItem): YouTubeCategoryItem {
42 return {
43 id: item.value,
44 snippet: {
45 title: item.label,
46 description: item.description,
47 },
48 }
49 }
50
51 function mapPinterestBoard(item: PublishOptionValueItem): PinterestBoardItem {
52 return {
53 id: item.value,
54 name: item.label,
55 description: item.description,
56 }
57 }
58
59 /**
60 * 存放发布弹框一些平台获取的三方数据
61 * 如:b站的分区列表
62 */
63 export const usePublishDialogData = create(
64 combine(
65 {
66 ...getStore(),
67 },
68 (set, get, storeApi) => {
69 const methods = {
70 // 获取b站分区列表
71 async getBilibiliPartitions() {
72 if (get().bilibiliPartitions.length !== 0)
73 return get().bilibiliPartitions
74 if (get().bilibiliPartitionsLoading)
75 return get().bilibiliPartitions
76
77 set({ bilibiliPartitionsLoading: true })
78 try {
79 const res = await apiGetBilibiliPartitions(
80 useAccountStore.getState().accountList.find(v => v.type === PlatType.BILIBILI)!.id,
81 )
82 const partitions = res?.data ?? []
83 set({
84 bilibiliPartitions: partitions,
85 })
86 return partitions
87 }
88 finally {
89 set({ bilibiliPartitionsLoading: false })
90 }
91 },
92 // 获取YouTube视频分类
93 async getYouTubeCategories(accountId?: string, regionCode?: string) {
94 if (get().youTubeCategoriesLoading)
95 return get().youTubeCategories
96
97 let youtubeAccount
98
99 if (accountId) {
100 // 如果提供了账户ID,使用指定的账户
101 youtubeAccount = useAccountStore
102 .getState()
103 .accountList
104 .find(v => v.id === accountId && v.type === PlatType.YouTube)
105 }
106 else {
107 // 如果没有提供账户ID,使用第一个找到的YouTube账户(保持向后兼容)
108 youtubeAccount = useAccountStore
109 .getState()
110 .accountList
111 .find(v => v.type === PlatType.YouTube)
112 }
113
114 if (!youtubeAccount) {
115 console.warn('没有找到YouTube账户')
116 return
117 }
118
119 // 如果没有提供 regionCode,使用默认值 "US"
120 const defaultRegionCode = regionCode || 'US'
121
122 set({ youTubeCategoriesLoading: true })
123 try {
124 const res = await apiGetYouTubeCategories(
125 youtubeAccount?.id || '',
126 defaultRegionCode,
127 )
128 const categories = res?.data?.items.map(mapYouTubeCategory) || []
129 set({
130 youTubeCategories: categories,
131 })
132 return categories
133 }
134 finally {
135 set({ youTubeCategoriesLoading: false })
136 }
137 },
138 // 获取Pinterest Board列表
139 async getPinterestBoards(forceRefresh = false, accountId?: string) {
140 if (!forceRefresh && get().pinterestBoards.length !== 0)
141 return
142
143 let pinterestAccount
144
145 if (accountId) {
146 // 如果提供了账户ID,使用指定的账户
147 pinterestAccount = useAccountStore
148 .getState()
149 .accountList
150 .find(v => v.id === accountId && v.type === PlatType.Pinterest)
151 }
152 else {
153 // 如果没有提供账户ID,使用第一个找到的Pinterest账户(保持向后兼容)
154 pinterestAccount = useAccountStore
155 .getState()
156 .accountList
157 .find(v => v.type === PlatType.Pinterest)
158 }
159
160 if (!pinterestAccount) {
161 console.warn('没有找到Pinterest账户')
162 return
163 }
164
165 const res = await getPinterestBoardListApi(pinterestAccount.id)
166 const pinterestBoards = res?.code === 0
167 ? res.data.items.map(mapPinterestBoard)
168 : []
169
170 set({
171 pinterestBoards,
172 })
173 return pinterestBoards
174 },
175 }
176
177 return methods
178 },
179 ),
180 )
181
181 lines TYPESCRIPT