import { type Config } from '@tryghost/admin-x-framework/api/config'; import { type Setting, checkStripeEnabled, getSettingValue, } from '@tryghost/admin-x-framework/api/settings'; import { type SiteData, getHomepageUrl } from '@tryghost/admin-x-framework/api/site'; import { isContributorUser } from '@tryghost/admin-x-framework/api/users'; import { z } from 'zod'; import type { AutocompleteLink } from '@/shared/autocomplete-links'; import type { LinkSearchGroup } from './link-suggestions'; export type PostType = 'post' | 'page'; export interface CardConfigPost { displayName: PostType; isPage: boolean; showTitleAndFeatureImage: boolean; visibility: string; } export interface CardConfigPostSource { displayName: PostType; showTitleAndFeatureImage?: boolean; visibility?: string | null; } export interface CardConfigSnippet { id: string; name: string; value: string; } export interface CardConfigSnippetInput { name: string; value: string; } export interface PostCardConfigSources { settings: Setting[]; config: Config; site: SiteData; currentUser: Parameters[0]; unsplashHeaders: Record; pinturaConfig: { jsUrl: string; cssUrl: string } | null; post: CardConfigPost | undefined; snippets: CardConfigSnippet[]; } export interface PostCardConfigPorts { fetchEmbed: (url: string, options: { type?: string }) => Promise; fetchAutocompleteLinks: () => Promise; searchLinks: (term?: string) => Promise; fetchLabels: () => Promise; createSnippet?: (snippet: CardConfigSnippetInput) => void; deleteSnippet?: (snippet: { name: string }) => void; } export type CardVisibilitySettings = 'web only' | 'web and email'; export interface PostCardConfig extends PostCardConfigPorts { unsplash: Record | null; klipy: NonNullable | null; pinturaConfig: { jsUrl: string; cssUrl: string } | null; embedPreviewUrl: string | undefined; renderLabels: boolean; feature: { transistor: boolean; paywallImprovements: boolean }; deprecated: { headerV1: boolean }; membersEnabled: boolean; siteTitle: string; siteDescription: string; siteOgImage: string | null; siteTwitterImage: string | null; siteCoverImage: string | null; siteUrl: string; siteUuid: string; stripeEnabled: boolean; post: CardConfigPost | undefined; snippets: CardConfigSnippet[]; visibilitySettings: CardVisibilitySettings; visibilityCardTypes: string[]; } // An unsaved post has no visibility until the first save applies the site // default, so it is resolved here to keep `visibility` present for cards. export function buildCardConfigPost( post: CardConfigPostSource | undefined, defaultContentVisibility: string, ): CardConfigPost | undefined { if (!post) { return undefined; } return { displayName: post.displayName, isPage: post.displayName === 'page', showTitleAndFeatureImage: post.showTitleAndFeatureImage ?? true, visibility: post.visibility || defaultContentVisibility, }; } export function getCardVisibilitySettings( post: Pick | undefined, ): CardVisibilitySettings { const isPage = post?.isPage || post?.displayName === 'page'; return isPage ? 'web only' : 'web and email'; } // Cards that had visibility before the server started listing supported cards // in config. Admin can deploy ahead of the server, so an older server only // gets visibility settings for these. export const LEGACY_VISIBILITY_CARD_TYPES = ['html', 'call-to-action', 'transistor']; const cardVisibilitySchema = z.array(z.string()); export function getVisibilityCardTypes(config: Pick): string[] { const cardVisibility = cardVisibilitySchema.safeParse(config.cardVisibility); return cardVisibility.success ? cardVisibility.data : LEGACY_VISIBILITY_CARD_TYPES; } function imageSetting(settings: Setting[], key: string): string | null { const value = getSettingValue(settings, key); return typeof value === 'string' ? value : null; } export function buildPostCardConfig( sources: PostCardConfigSources, ports: PostCardConfigPorts, ): PostCardConfig { const { settings, config, site, currentUser } = sources; return { unsplash: getSettingValue(settings, 'unsplash') ? sources.unsplashHeaders : null, klipy: config.klipy?.apiKey ? config.klipy : null, pinturaConfig: sources.pinturaConfig, embedPreviewUrl: config.security?.embedPreviewUrl || undefined, fetchAutocompleteLinks: ports.fetchAutocompleteLinks, fetchEmbed: ports.fetchEmbed, fetchLabels: ports.fetchLabels, renderLabels: !isContributorUser(currentUser), feature: { transistor: getSettingValue(settings, 'transistor') === true, paywallImprovements: config.labs?.paywallImprovements === true, }, deprecated: { headerV1: true, }, membersEnabled: getSettingValue(settings, 'members_signup_access') === 'all', searchLinks: ports.searchLinks, siteTitle: getSettingValue(settings, 'title') ?? '', siteDescription: getSettingValue(settings, 'description') ?? '', siteOgImage: imageSetting(settings, 'og_image'), siteTwitterImage: imageSetting(settings, 'twitter_image'), siteCoverImage: imageSetting(settings, 'cover_image'), siteUrl: getHomepageUrl(site), siteUuid: site.site_uuid, stripeEnabled: checkStripeEnabled(settings, config), post: sources.post, snippets: sources.snippets, createSnippet: ports.createSnippet, deleteSnippet: ports.deleteSnippet, visibilitySettings: getCardVisibilitySettings(sources.post), visibilityCardTypes: getVisibilityCardTypes(config), }; } export interface LiveCardConfigSettings { visibility?: string | null; showTitleAndFeatureImage?: boolean | null; } // The live settings fields, not the saved record: a visibility or a hidden // title the writer has only staged still decides what the cards describe. export function withLiveSettings( cardConfig: PostCardConfig, live: LiveCardConfigSettings, ): PostCardConfig { if (!cardConfig.post) { return cardConfig; } return { ...cardConfig, post: { ...cardConfig.post, visibility: live.visibility || cardConfig.post.visibility, showTitleAndFeatureImage: live.showTitleAndFeatureImage ?? true, }, }; }