import type { BuilderContextInterface } from '../../context/types'; import { fetchOneEntry } from '../../functions/get-content/index'; import { logger } from '../../helpers/logger'; import type { BuilderContent } from '../../types/builder-content'; export interface SymbolInfo { model?: string; entry?: string; data?: any; content?: BuilderContent; inline?: boolean; dynamic?: boolean; ownerId?: string; global?: boolean; } const isSymbolContentBinding = (key: string) => { const path = key.startsWith('component.') ? key.slice(10) : key; return path === 'options.symbol' || path === 'options.symbol.content' || path.startsWith('options.symbol.content.'); }; // Bound symbol content is evaluated at render time, so it is not in the parent content JSON. export const isSymbolContentInlinedInParent = (symbol: SymbolInfo | undefined, bindings: Record | undefined) => !!symbol?.content && !Object.keys(bindings || {}).some(isSymbolContentBinding); export const fetchSymbolContent = async ({ builderContextValue, symbol }: { symbol: SymbolInfo | undefined; builderContextValue: BuilderContextInterface; }) => { /** * If: * - we have a symbol prop * - yet it does not have any content * - and we have not already stored content from before * - and it has a model name * * then we want to re-fetch the symbol content. */ if (symbol?.model && // This is a hack, we should not need to check for this, but it is needed for Svelte. builderContextValue?.apiKey) { return fetchOneEntry({ model: symbol.model, apiKey: symbol.global && symbol.ownerId ? symbol.ownerId : builderContextValue.apiKey, apiVersion: builderContextValue.apiVersion, ...(symbol?.entry && { query: { id: symbol.entry } }) }).catch(err => { logger.error('Could not fetch symbol content: ', err); return undefined; }); } return undefined; }