import { useState } from 'react'; import { Navigate, useParams } from '@tryghost/admin-x-framework'; import { useBrowsePosts } from '@tryghost/admin-x-framework/api/posts'; import type { Post } from '@tryghost/admin-x-framework/api/content-types'; import { useCurrentUser } from '@tryghost/admin-x-framework/api/current-user'; import { isAuthorOrContributor, isContributorUser } from '@tryghost/admin-x-framework/api/users'; import { useBrowseSettings } from '@tryghost/admin-x-framework/api/settings'; import { getSiteTimezone } from '@tryghost/admin-x-framework/utils/get-site-timezone'; import { useEmail, useEmailDebugBatches, useEmailRecipientFailures, } from '@tryghost/admin-x-framework/api/emails'; import { LoadingIndicator, Tabs, TabsContent, TabsList, TabsTrigger, TabsTriggerCount, } from '@tryghost/shade/components'; import { PageHeader } from '@tryghost/shade/patterns'; import { Stack } from '@tryghost/shade/primitives'; import { cn, formatNumber } from '@tryghost/shade/utils'; import { NotFound } from '@/shared/not-found'; import AnalyticsSync from './analytics-sync'; import Batches from './batches'; import Failures from './failures'; import { formatPublishedDate } from './format'; import Overview, { type DebugTab } from './overview'; const PLAIN_COUNT = 'bg-transparent px-0'; function DebugContent({ post }: { post: Post }) { const [tab, setTab] = useState('overview'); const emailId = post.email?.id ?? ''; const emailQuery = useEmail(emailId, { enabled: !!emailId, refetchInterval: 10000, retry: false, defaultErrorHandler: false, }); const batchesQuery = useEmailDebugBatches(emailId, { enabled: !!emailId, defaultErrorHandler: false, }); const failuresQuery = useEmailRecipientFailures(emailId, { enabled: !!emailId, defaultErrorHandler: false, }); const { data: settings } = useBrowseSettings(); const email = emailQuery.data?.emails[0] ?? post.email; const batches = batchesQuery.data?.batches ?? []; const failures = failuresQuery.data?.failures ?? []; const failedBatches = batches.filter((batch) => batch.status === 'failed').length; const timezone = getSiteTimezone(settings?.settings ?? []); const publishedAt = post.published_at ? formatPublishedDate(post.published_at, timezone) : null; const sent = Boolean(email) && (post.status === 'published' || post.status === 'sent') && email?.status !== 'failed'; const publication = sent ? post.email_only ? 'Sent' : 'Published and sent' : email?.status === 'failed' ? 'Published but failed to send' : 'Published on your site'; return ( Posts Analytics Debug {post.title || '(Untitled)'} {publishedAt && ( {publication} on {publishedAt} )} {emailQuery.isError && (

Could not refresh email status. Showing the last available data.

)} {!emailId || !email ? (

No email data for this post.

) : ( setTab(value as DebugTab)}> Overview Failures {failuresQuery.data && ( {formatNumber(failures.length)} )} Batches {batchesQuery.data && (failedBatches > 0 ? ( {formatNumber(failedBatches)} failed ) : ( {formatNumber(batches.length)} ))} Analytics sync )}
); } export default function PostDebug() { const { postId } = useParams(); const { data: user, isLoading: userLoading } = useCurrentUser(); const { data, isLoading, isError } = useBrowsePosts({ searchParams: { filter: `id:${postId}`, include: 'tags,authors,authors.roles,email,tiers,newsletter', }, defaultErrorHandler: false, }); const post = data?.posts[0]; if (isLoading || userLoading) { return ; } if (isError || !user) { return

Could not load this post.

; } if (!post) { return ; } if ( (isAuthorOrContributor(user) && !post.authors?.some((author) => author.id === user.id)) || (isContributorUser(user) && post.status !== 'draft') ) { return ; } return ; }