teqo-backup/src/components/campaign/shared/useCampaignFormSuccessToast.ts
Francisco Ignácio Fontoura Solla 4806c417c8 Engineering Pass 3: consolidate patterns and lock them with guards.
Fold duplicated campanha access, loaders, list/filter/form shells, and scripts into shared modules; reorganize utilities by domain; stabilize e2e; and document prevention-first audit workflow so the same smells cannot creep back in.
2026-07-29 06:11:29 -03:00

28 lines
917 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use client'
import { useEffect, useRef } from 'react'
import { toast } from 'sonner'
/**
* The success→toast effect every form-action consumer spelled by hand (P3-G,
* ×7): fires once per state change, then runs the caller's follow-up (form
* reset, `router.refresh()`, navigation) through a ref so a re-rendered
* callback never re-fires the toast.
*/
export const useCampaignFormSuccessToast = (
state: { status?: string; message?: string },
onSuccess?: () => void,
) => {
const onSuccessRef = useRef(onSuccess)
useEffect(() => {
onSuccessRef.current = onSuccess
})
useEffect(() => {
if (state.status !== 'success') return
toast.success(state.message)
onSuccessRef.current?.()
// `state.message` is part of the change the toast reports — both belong
// to the firing condition, like the seven hand-written effects it replaces.
}, [state.message, state.status])
}