teqo-backup/src/components/campaign/shared/usePublicationToggle.ts
Francisco Solla 448f3845a2 feat(C194): biblioteca de reels na vertical Comunicação (#1153)
Lista publicados-only com capas 9:16, detalhe com player 9:16 e painel de
downloads (vídeo sem áudio primário; com áudio, mp3 e .srt quando existem;
transcrição em texto) e kill switch reversível: despublicar tira da lista e
bloqueia a mídia (C193 decision B), republicar reativa e re-carimba publishedAt.

Fiação: gate communicationCatalog (rename do speechCatalog), 3º sub-item da
vertical no nav, rule da lista no chrome, paths e loader próprios; sem
migration, sem Consent, sem tocar Media.read ou a rota de mídia do C193.

Design tier: DEGRADED — crítica final do designer indisponível no tier
primário (limite de uso); requer sign-off humano antes de push/PR.
2026-09-18 21:54:37 -03:00

54 lines
1.5 KiB
TypeScript

'use client'
import { useRouter } from 'next/navigation'
import { useState } from 'react'
import { postCampaignJson } from '@/lib/campaignJsonRequest'
const PUBLICATION_ERROR_MESSAGE = 'Não foi possível atualizar a publicação.'
type PublicationToggleResponse = { status: 'success' } | { status: 'error'; message: string }
/**
* C194 — the one submit/error/refresh contract of a library publish toggle
* (cut library, reel library): POST the domain body to the mutation route,
* surface the safe domain message or the shared fallback, and refresh the RSC
* page on success so the new status renders. The panels keep their own copy,
* confirmation dialog and status branches.
*/
export const usePublicationToggle = ({
href,
buildBody,
}: {
href: string
buildBody: (published: boolean) => Record<string, unknown>
}) => {
const router = useRouter()
const [submitting, setSubmitting] = useState(false)
const [error, setError] = useState<string | null>(null)
const setPublished = async (published: boolean): Promise<void> => {
setSubmitting(true)
setError(null)
try {
const { ok, payload } = await postCampaignJson<PublicationToggleResponse>(
href,
buildBody(published),
)
if (!ok || payload.status !== 'success') {
setError(payload.status === 'error' ? payload.message : PUBLICATION_ERROR_MESSAGE)
return
}
router.refresh()
} catch {
setError(PUBLICATION_ERROR_MESSAGE)
} finally {
setSubmitting(false)
}
}
return { submitting, error, setPublished }
}