返回 AiToEarn
useAccountClickHandler.ts
根目录 / project / aitoearn-web / src / components / PublishDialog / hooks / useAccountClickHandler.ts
1 /**
2 * 账户点击处理 Hook
3 * 统一处理账户选择/取消选择的逻辑
4 */
5
6 import type { PubItem } from '@/components/PublishDialog/publishDialog.type'
7 import { useCallback } from 'react'
8
9 interface UseAccountClickHandlerParams {
10 pubListChoosed: PubItem[]
11 step: number
12 setStep: (step: number) => void
13 setExpandedPubItem: (item: PubItem | undefined) => void
14 setPubListChoosed: (list: PubItem[]) => void
15 }
16
17 /**
18 * 账户点击处理 Hook
19 * 处理账户选择、步骤切换等逻辑
20 */
21 export function useAccountClickHandler({
22 pubListChoosed,
23 step,
24 setStep,
25 setExpandedPubItem,
26 setPubListChoosed,
27 }: UseAccountClickHandlerParams) {
28 /**
29 * 处理账户点击
30 * - 点击已选中的账户:取消选中
31 * - 点击未选中的账户:添加到选中列表
32 * - 自动切换步骤
33 */
34 const handleAccountClick = useCallback(
35 (pubItem: PubItem) => {
36 const newPubListChoosed = [...pubListChoosed]
37 const index = newPubListChoosed.findIndex(v => v.account.id === pubItem.account.id)
38
39 if (index !== -1) {
40 newPubListChoosed.splice(index, 1)
41 }
42 else {
43 newPubListChoosed.push(pubItem)
44 }
45
46 // 是否自动回到第一步
47 if (newPubListChoosed.length === 0 && step === 1) {
48 const isBack = newPubListChoosed.every(
49 v => !v.params.des && !v.params.video && !v.params.images?.length,
50 )
51 if (isBack) {
52 setStep(0)
53 }
54 }
55
56 // 是否自动前往第二步
57 if (step === 0 && newPubListChoosed.length !== 0) {
58 const isFront = newPubListChoosed.every(
59 v => v.params.des || v.params.video || v.params.images?.length !== 0,
60 )
61 if (isFront) {
62 setStep(1)
63 }
64 }
65
66 // 如果只有一个账户,自动展开
67 if (newPubListChoosed.length === 1) {
68 setExpandedPubItem(newPubListChoosed[0])
69 }
70
71 setPubListChoosed(newPubListChoosed)
72 },
73 [pubListChoosed, step, setStep, setExpandedPubItem, setPubListChoosed],
74 )
75
76 return {
77 handleAccountClick,
78 }
79 }
80
80 lines TYPESCRIPT