import { CarouselIcon, VideoIcon } from "@app/assets/images/icons"; import { Post } from "@app/models/api"; import { SfSyncConfigListKey, SizeLevel } from "@app/models/global"; import { ReactNode } from "react"; export const replaceNewLinesWithSpace = (text: string) => { return text.replace(/\n/g, " "); }; export const convertToEasternArabic = (westernDigits: string): string => { const arabicDigits = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"]; return westernDigits.replace(/\d/g, (digit) => { return arabicDigits[parseInt(digit)]; }); }; export const formatDate = ( timestamp: string, language: string = "en" ): string => { const dateObj = new Date(timestamp); const options: Intl.DateTimeFormatOptions = { day: "numeric", month: "short", year: "numeric", }; const formattedDate = language === "ar" ? convertToEasternArabic(dateObj.toLocaleDateString("ar", options)) : dateObj.toLocaleDateString("en-GB", options); return formattedDate; }; export const mapLevelToPadding = (level: SizeLevel | false): string => { if (!level) return ""; const paddingMapping = { sm: "sfsync-padding-sm", md: "sfsync-padding-md", lg: "sfsync-padding-lg", }; return paddingMapping[level] ?? ""; }; export const mapLevelToShadow = (level: SizeLevel | false): string => { if (!level) return ""; const elevationMapping = { sm: "sfsync-shadow-sm", md: "sfsync-shadow-md", lg: "sfsync-shadow-lg", }; return elevationMapping[level] ?? ""; }; export const mapSettingToLabel = (setting: SfSyncConfigListKey): string => { const localization = sfsyncI18n.data; const settingsMapping: { [key in SfSyncConfigListKey]: string; } = { ig_user_id: localization.instagram_account_id, biography: "Biography", name: "Name", username: "Username", profile_picture_url: "Profile Picture URL", media_limit: "Media Limit", page_slug: "Page Slug", language: "Language", posts: "Posts", scope: "post", page_header: localization.customizable_header, google_analytics_id: "Google Analytics Measurement ID", page_url: "Page Url", last_modified: "Last Modified", footer_mode: "Select Page Footer Mode", show_footer: "Show Footer", }; return settingsMapping[setting] ?? ""; }; export const containsSlashes = (input: string): boolean => { return /\//.test(input) || /\\/.test(input); }; export const mapMediaToIcon = (post: Post): ReactNode | undefined => { if (post.media_type === "VIDEO") { return VideoIcon; } if (post.media_type === "CAROUSEL_ALBUM") { return CarouselIcon; } return undefined; }; export const mapStatusToErrorMessage = (httpStatus: number): string => { const errorMessageMap: { [status: number]: string; } = { 401: "Request failed. Please verify your access token and try again.", }; return errorMessageMap[httpStatus] || ""; }; export const extractPageIdFromURL = (url: string) => { if (url) { const pageId = url .replace("#", "&") .split("&") .find((elem) => elem.startsWith("page_id")); if (pageId) { return pageId.split("=")[1]; } } return ""; }; export const extractAPIKeyFromURL = (url: string) => { if (url) { const apiKey = url .replace("#", "&") .split("&") .find((elem) => elem.startsWith("api_key")); if (apiKey) { return apiKey.split("=")[1]; } } return ""; };