🔧 Refactor content retrieval logic across various input parsing functions to use logical OR for default values

This commit is contained in:
Baptiste Arnaud 2025-08-28 11:17:47 +02:00
parent c8cfa6a66e
commit 1925569b4a
No known key found for this signature in database
9 changed files with 16 additions and 12 deletions

View File

@ -92,11 +92,12 @@ export const parseCardsReply = (
newSetVariableHistory = updatedSetVariableHistory;
}
const content = matchedItem.title || matchedItem.imageUrl;
if (!content) return { status: "fail" };
return {
status: "success",
content: isNotEmpty(matchedItem.title)
? matchedItem.title
: (matchedItem.imageUrl ?? ""),
content,
outgoingEdgeId: matchedPath?.outgoingEdgeId,
newSessionState,
newSetVariableHistory,

View File

@ -27,9 +27,12 @@ export const parseSingleChoiceReply = (
if (!matchedItem) return { status: "fail" };
const content = matchedItem.value || parseItemContent(matchedItem);
if (!content) return { status: "fail" };
return {
status: "success",
content: matchedItem.value ?? parseItemContent(matchedItem) ?? "",
content: matchedItem.value ?? parseItemContent(matchedItem),
outgoingEdgeId: matchedItem.outgoingEdgeId,
};
};

View File

@ -14,13 +14,13 @@ export const parseDateReply = (
block: DateInputBlock,
): ParsedReply => {
const parsedDate = (
block.options?.format ?? defaultDateInputOptions.format
block.options?.format || defaultDateInputOptions.format
).startsWith("dd")
? chronoParser.GB.parse(reply)
: chronoParser.parse(reply);
if (parsedDate.length === 0) return { status: "fail" };
const formatString =
block.options?.format ??
block.options?.format ||
(block.options?.hasTime
? defaultDateInputOptions.formatWithTime
: defaultDateInputOptions.format);

View File

@ -46,7 +46,7 @@ const createStripePaymentIntent = async (
: stripeKeys.live.secretKey,
{ apiVersion: "2024-09-30.acacia" },
);
const currency = options?.currency ?? defaultPaymentInputOptions.currency;
const currency = options?.currency || defaultPaymentInputOptions.currency;
const amount = Math.round(
Number(parseVariables(options.amount, { variables, sessionStore })) *
(isZeroDecimalCurrency(currency) ? 1 : 100),

View File

@ -2,4 +2,4 @@ import { defaultRatingInputOptions } from "@typebot.io/blocks-inputs/rating/cons
import type { RatingInputBlock } from "@typebot.io/blocks-inputs/rating/schema";
export const validateRatingReply = (reply: string, block: RatingInputBlock) =>
Number(reply) <= (block.options?.length ?? defaultRatingInputOptions.length);
Number(reply) <= (block.options?.length || defaultRatingInputOptions.length);

View File

@ -16,7 +16,7 @@ export const parseTime = (
status: "success",
content: format(
parsedDate,
options?.format ?? defaultTimeInputOptions.format,
options?.format || defaultTimeInputOptions.format,
),
};
};

View File

@ -15,7 +15,7 @@ const parseSetUserCode = (
) =>
user?.email || user?.id
? `
window.$chatwoot.setUser(${user?.id ?? user.email ?? `"${resultId}"`}, {
window.$chatwoot.setUser(${user?.id || user.email || `"${resultId}"`}, {
email: ${user?.email ? user.email : "undefined"},
name: ${user?.name ? user.name : "undefined"},
avatar_url: ${user?.avatarUrl ? user.avatarUrl : "undefined"},

View File

@ -33,7 +33,7 @@ import type { Group } from "@typebot.io/groups/schemas";
import { parseAllowedFileTypesMetadata } from "@typebot.io/lib/extensionFromMimeType";
import { isURL } from "@typebot.io/lib/isURL";
import { parseUnknownError } from "@typebot.io/lib/parseUnknownError";
import { byId, isDefined } from "@typebot.io/lib/utils";
import { byId, isDefined, isNotEmpty } from "@typebot.io/lib/utils";
import type { AnswerInSessionState } from "@typebot.io/results/schemas/answers";
import type { SessionStore } from "@typebot.io/runtime-session-store";
import { defaultSystemMessages } from "@typebot.io/settings/constants";

View File

@ -7,5 +7,5 @@ export const parseItemContent = (
// Buttons
if ("content" in item) return item.content;
// Picture choice
if ("title" in item) return item.title ?? item.pictureSrc;
if ("title" in item) return item.title || item.pictureSrc;
};