| 21 | * Uses TanStack Query mutation for proper state management |
| 22 | */ |
| 23 | export function usePublishMutation(deps: UsePublishMutationDeps = {}) { |
| 24 | const { handlePublish = defaultHandlePublish } = deps |
| 25 | |
| 26 | const { setIsPublishing, setSuccessResult, setErrorResult } = usePublishStore( |
| 27 | useShallow((state) => ({ |
| 28 | setIsPublishing: state.setIsPublishing, |
| 29 | setSuccessResult: state.setSuccessResult, |
| 30 | setErrorResult: state.setErrorResult, |
| 31 | })), |
| 32 | ) |
| 33 | |
| 34 | return useMutation({ |
| 35 | mutationFn: async (agentIds: string[]) => { |
| 36 | setIsPublishing(true) |
| 37 | return handlePublish(agentIds) |
| 38 | }, |
| 39 | onSuccess: (result) => { |
| 40 | if (result.success && result.publisherId && result.agents) { |
| 41 | setSuccessResult({ |
| 42 | publisherId: result.publisherId, |
| 43 | agents: result.agents, |
| 44 | }) |
| 45 | } else { |
| 46 | setErrorResult({ |
| 47 | error: result.error || 'Unknown error', |
| 48 | details: result.details, |
| 49 | hint: result.hint, |
| 50 | }) |
| 51 | } |
| 52 | }, |
| 53 | onError: (error) => { |
| 54 | setErrorResult({ |
| 55 | error: 'Publish failed', |
| 56 | details: error instanceof Error ? error.message : String(error), |
| 57 | }) |
| 58 | }, |
| 59 | }) |
| 60 | } |