Files
formbricks-formbricks/packages/lib/utils/templates.ts
2024-09-19 13:34:25 +00:00

38 lines
1.4 KiB
TypeScript

import { TProduct } from "@formbricks/types/product";
import { TSurveyQuestion } from "@formbricks/types/surveys/types";
import { TTemplate } from "@formbricks/types/templates";
import { getLocalizedValue } from "../i18n/utils";
import { structuredClone } from "../pollyfills/structuredClone";
export const replaceQuestionPresetPlaceholders = (
question: TSurveyQuestion,
product: TProduct
): TSurveyQuestion => {
if (!product) return question;
const newQuestion = structuredClone(question);
const defaultLanguageCode = "default";
if (newQuestion.headline) {
newQuestion.headline[defaultLanguageCode] = getLocalizedValue(
newQuestion.headline,
defaultLanguageCode
).replace("{{productName}}", product.name);
}
if (newQuestion.subheader) {
newQuestion.subheader[defaultLanguageCode] = getLocalizedValue(
newQuestion.subheader,
defaultLanguageCode
)?.replace("{{productName}}", product.name);
}
return newQuestion;
};
// replace all occurences of productName with the actual product name in the current template
export const replacePresetPlaceholders = (template: TTemplate, product: any) => {
const preset = structuredClone(template.preset);
preset.name = preset.name.replace("{{productName}}", product.name);
preset.questions = preset.questions.map((question) => {
return replaceQuestionPresetPlaceholders(question, product);
});
return { ...template, preset };
};