| 1 | import { useCallback } from 'react' |
| 2 | import { useShallow } from 'zustand/react/shallow' |
| 3 | import usePubParamsVerify from '@/components/PublishDialog/hooks/usePubParamsVerify' |
| 4 | import { usePublishDialog } from '@/components/PublishDialog/usePublishDialog' |
| 5 | import { toast } from '@/utils/ui/toast' |
| 6 | |
| 7 | export function useValidatedPublishTrigger(onPublish: () => void) { |
| 8 | const { step, pubListChoosed, setExpandedPubItem } = usePublishDialog( |
| 9 | useShallow(state => ({ |
| 10 | step: state.step, |
| 11 | pubListChoosed: state.pubListChoosed, |
| 12 | setExpandedPubItem: state.setExpandedPubItem, |
| 13 | })), |
| 14 | ) |
| 15 | const { errParamsMap } = usePubParamsVerify(pubListChoosed) |
| 16 | |
| 17 | const triggerPublish = useCallback(() => { |
| 18 | if (errParamsMap) { |
| 19 | for (const [key, errVideoItem] of errParamsMap) { |
| 20 | if (!errVideoItem) |
| 21 | continue |
| 22 | |
| 23 | const pubItem = pubListChoosed.find(v => v.account.id === key) |
| 24 | if (pubItem && step === 1) |
| 25 | setExpandedPubItem(pubItem) |
| 26 | |
| 27 | if (errVideoItem.parErrMsg) |
| 28 | toast.warning(errVideoItem.parErrMsg) |
| 29 | |
| 30 | return false |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | onPublish() |
| 35 | return true |
| 36 | }, [errParamsMap, onPublish, pubListChoosed, setExpandedPubItem, step]) |
| 37 | |
| 38 | return { |
| 39 | triggerPublish, |
| 40 | } |
| 41 | } |
| 42 |