Files
formbricks/apps/web/app/lib/templates.ts
2024-04-18 14:20:12 +00:00

38 lines
1.4 KiB
TypeScript

import { getLocalizedValue } from "@formbricks/lib/i18n/utils";
import { structuredClone } from "@formbricks/lib/pollyfills/structuredClone";
import { TProduct } from "@formbricks/types/product";
import { TSurveyQuestion } from "@formbricks/types/surveys";
import { TTemplate } from "@formbricks/types/templates";
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 };
};