mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-14 17:43:04 +00:00
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.
28 lines
917 B
TypeScript
28 lines
917 B
TypeScript
'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])
|
||
}
|