## Description ### Fixes - [x] https://github.com/appsmithorg/appsmith/issues/19383 - [x] https://github.com/appsmithorg/appsmith/issues/19384 - [x] https://github.com/appsmithorg/appsmith/issues/19385 - [x] https://github.com/appsmithorg/appsmith/issues/19386 - [x] https://github.com/appsmithorg/appsmith/issues/19387 - [x] https://github.com/appsmithorg/appsmith/issues/19388 - [x] https://github.com/appsmithorg/appsmith/issues/19389 - [x] https://github.com/appsmithorg/appsmith/issues/19390 - [x] https://github.com/appsmithorg/appsmith/issues/19391 - [x] https://github.com/appsmithorg/appsmith/issues/19392 - [x] https://github.com/appsmithorg/appsmith/issues/19393 - [x] https://github.com/appsmithorg/appsmith/issues/19394 - [x] https://github.com/appsmithorg/appsmith/issues/19395 - [x] https://github.com/appsmithorg/appsmith/issues/19396 - [x] https://github.com/appsmithorg/appsmith/issues/19397 - [x] https://github.com/appsmithorg/appsmith/issues/19398 - [x] https://github.com/appsmithorg/appsmith/issues/19399 - [x] https://github.com/appsmithorg/appsmith/issues/19400 - [x] https://github.com/appsmithorg/appsmith/issues/19401 - [x] https://github.com/appsmithorg/appsmith/issues/19402 - [x] https://github.com/appsmithorg/appsmith/issues/19403 - [x] https://github.com/appsmithorg/appsmith/issues/19404 - [x] https://github.com/appsmithorg/appsmith/issues/19405 - [x] https://github.com/appsmithorg/appsmith/issues/19406 - [x] https://github.com/appsmithorg/appsmith/issues/19407 - [x] https://github.com/appsmithorg/appsmith/issues/19408 - [x] https://github.com/appsmithorg/appsmith/issues/19409 Fixes # (issue) > if no issue exists, please create an issue and ask the maintainers about this first Media > A video or a GIF is preferred. when using Loom, don’t embed because it looks like it’s a GIF. instead, just link to the video ## Type of change > Please delete options that are not relevant. - Bug fix (non-breaking change which fixes an issue) - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) - Chore (housekeeping or task changes that don't impact user perception) - This change requires a documentation update ## How Has This Been Tested? > Please describe the tests that you ran to verify your changes. Provide instructions, so we can reproduce. > Please also list any relevant details for your test configuration. > Delete anything that is not important - Manual - Jest - Cypress ### Test Plan > Add Testsmith test cases links that relate to this PR ### Issues raised during DP testing > Link issues raised during DP testing for better visiblity and tracking (copy link from comments dropped on this PR) ## Checklist: ### Dev activity - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag ### QA activity: - [ ] Test plan has been approved by relevant developers - [ ] Test plan has been peer reviewed by QA - [ ] Cypress test cases have been added and approved by either SDET or manual QA - [ ] Organized project review call with relevant stakeholders after Round 1/2 of QA - [ ] Added Test Plan Approved label after reveiwing all Cypress test --------- Co-authored-by: Ankita Kinger <ankita@appsmith.com> Co-authored-by: akash-codemonk <67054171+akash-codemonk@users.noreply.github.com> Co-authored-by: Tanvi Bhakta <tanvi@appsmith.com> Co-authored-by: Arsalan <arsalanyaldram0211@outlook.com> Co-authored-by: Aman Agarwal <aman@appsmith.com> Co-authored-by: Rohit Agarwal <rohit_agarwal@live.in> Co-authored-by: Nilesh Sarupriya <nilesh@appsmith.com> Co-authored-by: Nilesh Sarupriya <20905988+nsarupr@users.noreply.github.com> Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com> Co-authored-by: Aishwarya UR <aishwarya@appsmith.com> Co-authored-by: Parthvi Goswami <parthvigoswami@Parthvis-MacBook-Pro.local> Co-authored-by: Vijetha-Kaja <vijetha@appsmith.com> Co-authored-by: Parthvi <80334441+Parthvi12@users.noreply.github.com> Co-authored-by: Apple <nandan@thinkify.io> Co-authored-by: Saroj <43822041+sarojsarab@users.noreply.github.com> Co-authored-by: Sangeeth Sivan <74818788+berzerkeer@users.noreply.github.com> Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com> Co-authored-by: Aishwarya-U-R <91450662+Aishwarya-U-R@users.noreply.github.com> Co-authored-by: rahulramesha <rahul@appsmith.com> Co-authored-by: Aswath K <aswath.sana@gmail.com> Co-authored-by: Preet Sidhu <preetsidhu.bits@gmail.com> Co-authored-by: Vijetha-Kaja <119562824+Vijetha-Kaja@users.noreply.github.com> Co-authored-by: Shrikant Sharat Kandula <shrikant@appsmith.com>
459 lines
12 KiB
TypeScript
459 lines
12 KiB
TypeScript
import { getAppsmithConfigs } from "@appsmith/configs";
|
|
import { ERROR_CODES } from "@appsmith/constants/ApiConstants";
|
|
import { createMessage, ERROR_500 } from "@appsmith/constants/messages";
|
|
import * as Sentry from "@sentry/react";
|
|
import type { Property } from "api/ActionAPI";
|
|
import type { AppIconName } from "design-system-old";
|
|
import { AppIconCollection } from "design-system-old";
|
|
import _ from "lodash";
|
|
import * as log from "loglevel";
|
|
import { osName } from "react-device-detect";
|
|
import type { ActionDataState } from "reducers/entityReducers/actionsReducer";
|
|
import type { JSCollectionData } from "reducers/entityReducers/jsActionsReducer";
|
|
import AnalyticsUtil from "./AnalyticsUtil";
|
|
|
|
export const initializeAnalyticsAndTrackers = () => {
|
|
const appsmithConfigs = getAppsmithConfigs();
|
|
|
|
try {
|
|
if (appsmithConfigs.sentry.enabled && !window.Sentry) {
|
|
window.Sentry = Sentry;
|
|
Sentry.init({
|
|
...appsmithConfigs.sentry,
|
|
beforeSend(event) {
|
|
if (
|
|
event.exception &&
|
|
Array.isArray(event.exception.values) &&
|
|
event.exception.values[0].value &&
|
|
event.exception.values[0].type === "ChunkLoadError"
|
|
) {
|
|
// Only log ChunkLoadErrors after the 2 retires
|
|
if (
|
|
!event.exception.values[0].value.includes(
|
|
"failed after 2 retries",
|
|
)
|
|
) {
|
|
return null;
|
|
}
|
|
}
|
|
return event;
|
|
},
|
|
beforeBreadcrumb(breadcrumb) {
|
|
if (
|
|
breadcrumb.category === "console" &&
|
|
breadcrumb.level !== "error"
|
|
) {
|
|
return null;
|
|
}
|
|
if (breadcrumb.category === "sentry.transaction") {
|
|
return null;
|
|
}
|
|
if (breadcrumb.category === "redux.action") {
|
|
if (
|
|
breadcrumb.data &&
|
|
breadcrumb.data.type === "SET_EVALUATED_TREE"
|
|
) {
|
|
breadcrumb.data = undefined;
|
|
}
|
|
}
|
|
return breadcrumb;
|
|
},
|
|
});
|
|
}
|
|
} catch (e) {
|
|
log.error(e);
|
|
}
|
|
|
|
try {
|
|
if (appsmithConfigs.smartLook.enabled && !(window as any).smartlook) {
|
|
const { id } = appsmithConfigs.smartLook;
|
|
AnalyticsUtil.initializeSmartLook(id);
|
|
}
|
|
|
|
if (appsmithConfigs.segment.enabled && !(window as any).analytics) {
|
|
if (appsmithConfigs.segment.apiKey) {
|
|
// This value is only enabled for Appsmith's cloud hosted version. It is not set in self-hosted environments
|
|
return AnalyticsUtil.initializeSegment(appsmithConfigs.segment.apiKey);
|
|
} else if (appsmithConfigs.segment.ceKey) {
|
|
// This value is set in self-hosted environments. But if the analytics are disabled, it's never used.
|
|
return AnalyticsUtil.initializeSegment(appsmithConfigs.segment.ceKey);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
Sentry.captureException(e);
|
|
log.error(e);
|
|
}
|
|
};
|
|
|
|
export const mapToPropList = (map: Record<string, string>): Property[] => {
|
|
return _.map(map, (value, key) => {
|
|
return { key: key, value: value };
|
|
});
|
|
};
|
|
|
|
export const INTERACTION_ANALYTICS_EVENT = "INTERACTION_ANALYTICS_EVENT";
|
|
|
|
export type InteractionAnalyticsEventDetail = {
|
|
key?: string;
|
|
propertyName?: string;
|
|
propertyType?: string;
|
|
widgetType?: string;
|
|
};
|
|
|
|
export const interactionAnalyticsEvent = (
|
|
detail: InteractionAnalyticsEventDetail = {},
|
|
) =>
|
|
new CustomEvent(INTERACTION_ANALYTICS_EVENT, {
|
|
bubbles: true,
|
|
detail,
|
|
});
|
|
|
|
export function emitInteractionAnalyticsEvent<T extends HTMLElement>(
|
|
element: T | null,
|
|
args: Record<string, unknown>,
|
|
) {
|
|
element?.dispatchEvent(interactionAnalyticsEvent(args));
|
|
}
|
|
|
|
export const DS_EVENT = "DS_EVENT";
|
|
|
|
export enum DSEventTypes {
|
|
KEYPRESS = "KEYPRESS",
|
|
}
|
|
|
|
export type DSEventDetail = {
|
|
component: string;
|
|
event: DSEventTypes;
|
|
meta: Record<string, unknown>;
|
|
};
|
|
|
|
export function createDSEvent(detail: DSEventDetail) {
|
|
return new CustomEvent(DS_EVENT, {
|
|
bubbles: true,
|
|
detail,
|
|
});
|
|
}
|
|
|
|
export function emitDSEvent<T extends HTMLElement>(
|
|
element: T | null,
|
|
args: DSEventDetail,
|
|
) {
|
|
element?.dispatchEvent(createDSEvent(args));
|
|
}
|
|
|
|
export const getNextEntityName = (
|
|
prefix: string,
|
|
existingNames: string[],
|
|
startWithoutIndex?: boolean,
|
|
) => {
|
|
const regex = new RegExp(`^${prefix}(\\d+)$`);
|
|
|
|
const usedIndices: number[] = existingNames.map((name) => {
|
|
if (name && regex.test(name)) {
|
|
const matches = name.match(regex);
|
|
const ind =
|
|
matches && Array.isArray(matches) ? parseInt(matches[1], 10) : 0;
|
|
return Number.isNaN(ind) ? 0 : ind;
|
|
}
|
|
return 0;
|
|
}) as number[];
|
|
|
|
const lastIndex = Math.max(...usedIndices, ...[0]);
|
|
|
|
if (startWithoutIndex && lastIndex === 0) {
|
|
const exactMatchFound = existingNames.some(
|
|
(name) => prefix && name.trim() === prefix.trim(),
|
|
);
|
|
if (!exactMatchFound) {
|
|
return prefix.trim();
|
|
}
|
|
}
|
|
|
|
return prefix + (lastIndex + 1);
|
|
};
|
|
|
|
export const getDuplicateName = (prefix: string, existingNames: string[]) => {
|
|
const trimmedPrefix = prefix.replace(/ /g, "");
|
|
const regex = new RegExp(`^${trimmedPrefix}(\\d+)$`);
|
|
const usedIndices: number[] = existingNames.map((name) => {
|
|
if (name && regex.test(name)) {
|
|
const matches = name.match(regex);
|
|
const ind =
|
|
matches && Array.isArray(matches) ? parseInt(matches[1], 10) : 0;
|
|
return Number.isNaN(ind) ? 0 : ind;
|
|
}
|
|
return 0;
|
|
}) as number[];
|
|
|
|
const lastIndex = Math.max(...usedIndices, ...[0]);
|
|
|
|
return trimmedPrefix + `_${lastIndex + 1}`;
|
|
};
|
|
|
|
export const createNewApiName = (actions: ActionDataState, pageId: string) => {
|
|
const pageApiNames = actions
|
|
.filter((a) => a.config.pageId === pageId)
|
|
.map((a) => a.config.name);
|
|
return getNextEntityName("Api", pageApiNames);
|
|
};
|
|
|
|
export const createNewJSFunctionName = (
|
|
jsActions: JSCollectionData[],
|
|
pageId: string,
|
|
) => {
|
|
const pageJsFunctionNames = jsActions
|
|
.filter((a) => a.config.pageId === pageId)
|
|
.map((a) => a.config.name);
|
|
return getNextEntityName("JSObject", pageJsFunctionNames);
|
|
};
|
|
|
|
export const noop = () => {
|
|
log.debug("noop");
|
|
};
|
|
|
|
export const stopEventPropagation = (e: any) => {
|
|
e.stopPropagation();
|
|
};
|
|
|
|
export const createNewQueryName = (
|
|
queries: ActionDataState,
|
|
pageId: string,
|
|
) => {
|
|
const pageApiNames = queries
|
|
.filter((a) => a.config.pageId === pageId)
|
|
.map((a) => a.config.name);
|
|
const newName = getNextEntityName("Query", pageApiNames);
|
|
return newName;
|
|
};
|
|
|
|
export const convertToString = (value: any): string => {
|
|
if (_.isUndefined(value)) {
|
|
return "";
|
|
}
|
|
if (_.isObject(value)) {
|
|
return JSON.stringify(value, null, 2);
|
|
}
|
|
if (_.isString(value)) return value;
|
|
return value.toString();
|
|
};
|
|
|
|
export const getInitialsFromName = (fullName: string) => {
|
|
let inits = "";
|
|
// if name contains space. eg: "Full Name"
|
|
if (fullName && fullName.includes(" ")) {
|
|
const namesArr = fullName.split(" ");
|
|
let initials = namesArr
|
|
.map((name: string) => name.charAt(0))
|
|
.join("")
|
|
.toUpperCase();
|
|
initials = initials;
|
|
inits = initials.slice(0, 2);
|
|
} else {
|
|
// handle for camelCase
|
|
const str = fullName ? fullName.replace(/([a-z])([A-Z])/g, "$1 $2") : "";
|
|
const namesArr = str.split(" ");
|
|
const initials = namesArr
|
|
.map((name: string) => name.charAt(0))
|
|
.join("")
|
|
.toUpperCase();
|
|
inits = initials.slice(0, 2);
|
|
}
|
|
|
|
return inits;
|
|
};
|
|
|
|
export const getInitialsAndColorCode = (
|
|
fullName = "",
|
|
colorPalette: string[],
|
|
): string[] => {
|
|
const initials = getInitialsFromName(fullName);
|
|
const colorCode = getColorCode(initials, colorPalette);
|
|
return [initials, colorCode];
|
|
};
|
|
export const getInitials = (
|
|
fullName: any,
|
|
// colorPalette: string[],
|
|
): string => {
|
|
let inits = "";
|
|
// if name contains space. eg: "Full Name"
|
|
if (fullName && fullName.includes(" ")) {
|
|
const namesArr = fullName.split(" ");
|
|
let initials = namesArr.map((name: string) => name.charAt(0));
|
|
initials = initials.join("").toUpperCase();
|
|
inits = initials.slice(0, 2);
|
|
} else {
|
|
// handle for camelCase
|
|
const str = fullName ? fullName.replace(/([a-z])([A-Z])/g, "$1 $2") : "";
|
|
const namesArr = str.split(" ");
|
|
let initials = namesArr.map((name: string) => name.charAt(0));
|
|
initials = initials.join("").toUpperCase();
|
|
inits = initials.slice(0, 2);
|
|
}
|
|
// const colorCode = getColorCode(inits, colorPalette);
|
|
return inits;
|
|
};
|
|
export const getColorCode = (
|
|
initials: string,
|
|
colorPalette: string[],
|
|
): string => {
|
|
let asciiSum = 0;
|
|
for (let i = 0; i < initials.length; i++) {
|
|
asciiSum += initials[i].charCodeAt(0);
|
|
}
|
|
return colorPalette[asciiSum % colorPalette.length];
|
|
};
|
|
|
|
export const getApplicationIcon = (initials: string): AppIconName => {
|
|
let asciiSum = 0;
|
|
for (let i = 0; i < initials.length; i++) {
|
|
asciiSum += initials[i].charCodeAt(0);
|
|
}
|
|
return AppIconCollection[asciiSum % AppIconCollection.length];
|
|
};
|
|
|
|
export function hexToRgb(hex: string): {
|
|
r: number;
|
|
g: number;
|
|
b: number;
|
|
} {
|
|
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
return result
|
|
? {
|
|
r: parseInt(result[1], 16),
|
|
g: parseInt(result[2], 16),
|
|
b: parseInt(result[3], 16),
|
|
}
|
|
: {
|
|
r: -1,
|
|
g: -1,
|
|
b: -1,
|
|
};
|
|
}
|
|
|
|
export const retryPromise = (
|
|
fn: () => Promise<any>,
|
|
retriesLeft = 5,
|
|
interval = 1000,
|
|
): Promise<any> => {
|
|
return new Promise((resolve, reject) => {
|
|
fn()
|
|
.then(resolve)
|
|
.catch(() => {
|
|
setTimeout(() => {
|
|
if (retriesLeft === 1) {
|
|
return Promise.reject({
|
|
code: ERROR_CODES.SERVER_ERROR,
|
|
message: createMessage(ERROR_500),
|
|
show: false,
|
|
});
|
|
}
|
|
|
|
// Passing on "reject" is the important part
|
|
retryPromise(fn, retriesLeft - 1, interval).then(resolve, reject);
|
|
}, interval);
|
|
});
|
|
});
|
|
};
|
|
|
|
export const getRandomPaletteColor = (colorPalette: string[]) => {
|
|
return colorPalette[Math.floor(Math.random() * colorPalette.length)];
|
|
};
|
|
|
|
export const isBlobUrl = (url: string) => {
|
|
return typeof url === "string" && url.startsWith("blob:");
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @param data string file data
|
|
* @param type string file type
|
|
* @returns string containing blob id and type
|
|
*/
|
|
export const createBlobUrl = (data: Blob | MediaSource, type: string) => {
|
|
let url = URL.createObjectURL(data);
|
|
url = url.replace(`${window.location.origin}/`, "");
|
|
|
|
return `${url}?type=${type}`;
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @param blobId string blob id along with type.
|
|
* @returns [string,string] [blobUrl, type]
|
|
*/
|
|
export const parseBlobUrl = (blobId: string) => {
|
|
const url = `blob:${window.location.origin}/${blobId.substring(5)}`;
|
|
return url.split("?type=");
|
|
};
|
|
|
|
/**
|
|
* Convert a string into camelCase
|
|
* @param sourceString input string
|
|
* @returns camelCase string
|
|
*/
|
|
export const getCamelCaseString = (sourceString: string) => {
|
|
let out = "";
|
|
// Split the input string to separate words using RegEx
|
|
const regEx =
|
|
/[A-Z\xC0-\xD6\xD8-\xDE]?[a-z\xDF-\xF6\xF8-\xFF]+|[A-Z\xC0-\xD6\xD8-\xDE]+(?![a-z\xDF-\xF6\xF8-\xFF])|\d+/g;
|
|
const words = sourceString.match(regEx);
|
|
if (words) {
|
|
words.forEach(function (el, idx) {
|
|
const add = el.toLowerCase();
|
|
out += idx === 0 ? add : add[0].toUpperCase() + add.slice(1);
|
|
});
|
|
}
|
|
|
|
return out;
|
|
};
|
|
|
|
/**
|
|
* Convert Base64 string to Blob
|
|
* @param base64Data
|
|
* @param contentType
|
|
* @param sliceSize
|
|
* @returns
|
|
*/
|
|
export const base64ToBlob = (
|
|
base64Data: string,
|
|
contentType = "",
|
|
sliceSize = 512,
|
|
) => {
|
|
const byteCharacters = atob(base64Data);
|
|
const byteArrays = [];
|
|
|
|
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
|
|
const slice = byteCharacters.slice(offset, offset + sliceSize);
|
|
|
|
const byteNumbers = new Array(slice.length);
|
|
for (let i = 0; i < slice.length; i++) {
|
|
byteNumbers[i] = slice.charCodeAt(i);
|
|
}
|
|
|
|
const byteArray = new Uint8Array(byteNumbers);
|
|
byteArrays.push(byteArray);
|
|
}
|
|
|
|
const blob = new Blob(byteArrays, { type: contentType });
|
|
return blob;
|
|
};
|
|
|
|
// util function to detect current os is Mac
|
|
export const isMacOs = () => {
|
|
return osName === "Mac OS";
|
|
};
|
|
|
|
/**
|
|
* checks if array of strings are equal regardless of order
|
|
* @param arr1
|
|
* @param arr2
|
|
* @returns
|
|
*/
|
|
export function areArraysEqual(arr1: string[], arr2: string[]) {
|
|
if (arr1.length !== arr2.length) return false;
|
|
// Because the array is frozen in strict mode, you'll need to copy the array before sorting it
|
|
if ([...arr1].sort().join(",") === [...arr2].sort().join(",")) return true;
|
|
|
|
return false;
|
|
}
|