import type { ConnectionStringCopiedEvent } from 'common/telemetry-constants' import { MCP_CLIENT_DATA } from 'ui-patterns/McpUrlBuilder/clients.data' import { FRAMEWORKS, MOBILES, ORMS } from './Connect.constants' import type { ConnectMode, ConnectState } from './Connect.types' type FieldValue = ConnectState[string] export type ConnectCopyType = 'Install command' | 'Command Line' /** * Properties for a `connection_string_copied` event fired from the Connect sheet. * Without a `copyType` the copy is a framework or ORM file snippet. */ export function getConnectCopyProperties( state: ConnectState, copyType?: ConnectCopyType ): ConnectionStringCopiedEvent['properties'] | undefined { if (state.mode === 'orm') { const orm = ORMS.find((item) => item.key === state.orm) return { connectionTab: 'ORMs', selectedItem: orm?.label, ...(copyType && { connectionType: copyType }), source: 'studio', } } if (state.mode === 'mcp') { if (copyType !== 'Command Line') return undefined const client = MCP_CLIENT_DATA.find((item) => item.key === state.mcpClient) return { connectionTab: 'MCP', selectedItem: client?.label, connectionType: copyType, source: 'studio', } } if (state.mode === 'direct') { if (!copyType) return undefined return { connectionTab: 'Connection String', connectionType: copyType, lang: 'bash', source: 'studio', } } if (state.mode !== 'framework') return undefined const mobile = MOBILES.find((item) => item.key === state.framework) const framework = mobile ?? FRAMEWORKS.find((item) => item.key === state.framework) return { connectionTab: mobile ? 'Mobile Frameworks' : 'App Frameworks', selectedItem: framework?.label, connectionType: copyType ?? 'Framework snippet', lang: String(state.library ?? 'unknown'), source: 'studio', } } /** * Resolves a content path template by replacing {{key}} placeholders with state values. * Empty segments are filtered out to handle optional state values like frameworkVariant. * * Examples: * - '{{framework}}/{{frameworkVariant}}/{{library}}' with state {framework: 'nextjs', frameworkVariant: 'app', library: 'supabasejs'} * → 'nextjs/app/supabasejs' * - '{{orm}}' with state {orm: 'prisma'} * → 'prisma' * - 'steps/install' (no templates) * → 'steps/install' */ export function resolveContentPath(template: string, state: ConnectState): string { return template .replace(/\{\{(\w+)\}\}/g, (_, key) => String(state[key] ?? '')) .split('/') .filter(Boolean) .join('/') } export function shouldShowIpv4AddonNotice({ isAws, isIpv4Enabled, isPlatform, mode, connectionMethod, useSharedPooler, hasIpv4Addon, isHighAvailability, }: { isAws: boolean isIpv4Enabled: boolean isPlatform: boolean mode: ConnectMode connectionMethod: FieldValue useSharedPooler: FieldValue hasIpv4Addon: boolean isHighAvailability: boolean }): boolean { // The IPv4 add-on does not apply to Multigres connections if ( !isPlatform || !isAws || !isIpv4Enabled || mode !== 'direct' || hasIpv4Addon || isHighAvailability ) return false return connectionMethod === 'direct' || (connectionMethod === 'transaction' && !useSharedPooler) } export function shouldShowSessionPoolerNotice({ isPlatform, mode, connectionMethod, }: { isPlatform: boolean mode: ConnectMode connectionMethod: FieldValue }): boolean { return isPlatform && mode === 'direct' && connectionMethod === 'session' } export function shouldShowSelfHostedMcpNotice({ isSelfHosted, mode, }: { isSelfHosted: boolean mode: ConnectMode }): boolean { return isSelfHosted && mode === 'mcp' } export function shouldFetchDataApiConfig({ mode }: { mode: ConnectMode }): boolean { return mode === 'framework' } export function shouldShowDataApiDisabledWarning({ mode, isDataApiEnabled, isPending, isError, }: { mode: ConnectMode isDataApiEnabled: boolean isPending: boolean isError: boolean }): boolean { if (isPending || isError || isDataApiEnabled) return false return shouldFetchDataApiConfig({ mode }) }