2020-03-16 07:59:07 +00:00
|
|
|
import { ReduxAction } from "constants/ReduxActionConstants";
|
2019-11-25 12:22:05 +00:00
|
|
|
import { getAppsmithConfigs } from "configs";
|
2019-09-09 09:08:54 +00:00
|
|
|
import * as Sentry from "@sentry/browser";
|
|
|
|
|
import AnalyticsUtil from "./AnalyticsUtil";
|
2020-04-28 06:52:53 +00:00
|
|
|
import FormControlRegistry from "./FormControlRegistry";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { Property } from "api/ActionAPI";
|
2019-09-18 14:10:57 +00:00
|
|
|
import _ from "lodash";
|
2020-04-14 12:34:14 +00:00
|
|
|
import { ActionDataState } from "reducers/entityReducers/actionsReducer";
|
2020-03-23 12:40:17 +00:00
|
|
|
import * as log from "loglevel";
|
2020-03-27 12:46:29 +00:00
|
|
|
import { LogLevelDesc } from "loglevel";
|
2020-05-28 18:10:26 +00:00
|
|
|
import FeatureFlag from "utils/featureFlags";
|
2020-08-20 16:11:08 +00:00
|
|
|
import { appCardColors } from "constants/AppConstants";
|
2019-08-30 10:33:49 +00:00
|
|
|
|
|
|
|
|
export const createReducer = (
|
|
|
|
|
initialState: any,
|
2019-09-09 09:08:54 +00:00
|
|
|
handlers: { [type: string]: Function },
|
2019-08-30 10:33:49 +00:00
|
|
|
) => {
|
|
|
|
|
return function reducer(state = initialState, action: ReduxAction<any>) {
|
|
|
|
|
if (handlers.hasOwnProperty(action.type)) {
|
2019-09-09 09:08:54 +00:00
|
|
|
return handlers[action.type](state, action);
|
2019-08-30 10:33:49 +00:00
|
|
|
} else {
|
2019-09-09 09:08:54 +00:00
|
|
|
return state;
|
2019-08-30 10:33:49 +00:00
|
|
|
}
|
2019-09-09 09:08:54 +00:00
|
|
|
};
|
|
|
|
|
};
|
2019-08-30 10:33:49 +00:00
|
|
|
|
|
|
|
|
export const appInitializer = () => {
|
2020-04-28 06:52:53 +00:00
|
|
|
FormControlRegistry.registerFormControlBuilders();
|
2019-11-25 12:22:05 +00:00
|
|
|
const appsmithConfigs = getAppsmithConfigs();
|
2020-05-28 18:10:26 +00:00
|
|
|
FeatureFlag.initialize(appsmithConfigs.featureFlag);
|
|
|
|
|
|
2020-07-07 10:22:17 +00:00
|
|
|
if (appsmithConfigs.sentry.enabled) {
|
2020-08-13 04:46:06 +00:00
|
|
|
Sentry.init(appsmithConfigs.sentry);
|
2019-11-25 12:22:05 +00:00
|
|
|
}
|
2020-08-27 12:54:03 +00:00
|
|
|
if (appsmithConfigs.smartLook.enabled) {
|
|
|
|
|
const { id } = appsmithConfigs.smartLook;
|
|
|
|
|
AnalyticsUtil.initializeSmartLook(id);
|
2019-11-25 12:22:05 +00:00
|
|
|
}
|
|
|
|
|
if (appsmithConfigs.segment.enabled) {
|
2020-07-07 10:22:17 +00:00
|
|
|
AnalyticsUtil.initializeSegment(appsmithConfigs.segment.apiKey);
|
2019-08-30 10:33:49 +00:00
|
|
|
}
|
2020-05-28 18:10:26 +00:00
|
|
|
|
2020-03-27 12:46:29 +00:00
|
|
|
log.setLevel(getEnvLogLevel(appsmithConfigs.logLevel));
|
2019-09-09 09:08:54 +00:00
|
|
|
};
|
2019-09-18 14:10:57 +00:00
|
|
|
|
|
|
|
|
export const mapToPropList = (map: Record<string, string>): Property[] => {
|
|
|
|
|
return _.map(map, (value, key) => {
|
|
|
|
|
return { key: key, value: value };
|
|
|
|
|
});
|
|
|
|
|
};
|
2019-10-02 18:13:04 +00:00
|
|
|
|
2020-01-24 09:54:40 +00:00
|
|
|
export const getNextEntityName = (prefix: string, existingNames: string[]) => {
|
2019-10-03 17:06:44 +00:00
|
|
|
const regex = new RegExp(`^${prefix}(\\d+)$`);
|
2020-01-24 09:54:40 +00:00
|
|
|
const usedIndices: number[] = existingNames.map(name => {
|
|
|
|
|
if (name && regex.test(name)) {
|
2019-10-03 17:06:44 +00:00
|
|
|
const matches = name.match(regex);
|
|
|
|
|
const ind =
|
|
|
|
|
matches && Array.isArray(matches) ? parseInt(matches[1], 10) : 0;
|
2019-10-02 18:13:04 +00:00
|
|
|
return Number.isNaN(ind) ? 0 : ind;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}) as number[];
|
|
|
|
|
|
2020-01-24 09:54:40 +00:00
|
|
|
const lastIndex = Math.max(...usedIndices, ...[0]);
|
2019-10-02 18:13:04 +00:00
|
|
|
|
|
|
|
|
return prefix + (lastIndex + 1);
|
|
|
|
|
};
|
2019-11-07 11:17:53 +00:00
|
|
|
|
2020-04-14 12:34:14 +00:00
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-16 08:49:10 +00:00
|
|
|
export const noop = () => {
|
|
|
|
|
console.log("noop");
|
|
|
|
|
};
|
2020-03-16 07:59:07 +00:00
|
|
|
|
2020-05-05 07:50:30 +00:00
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-16 07:59:07 +00:00
|
|
|
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();
|
|
|
|
|
};
|
2020-03-27 12:46:29 +00:00
|
|
|
|
|
|
|
|
const getEnvLogLevel = (configLevel: LogLevelDesc): LogLevelDesc => {
|
|
|
|
|
let logLevel = configLevel;
|
|
|
|
|
const localStorageLevel = localStorage.getItem("logLevel") as LogLevelDesc;
|
|
|
|
|
if (localStorageLevel) logLevel = localStorageLevel;
|
|
|
|
|
return logLevel;
|
|
|
|
|
};
|
2020-04-28 10:47:59 +00:00
|
|
|
|
|
|
|
|
export const getInitialsAndColorCode = (fullName: any): string[] => {
|
|
|
|
|
let inits = "";
|
|
|
|
|
// if name contains space. eg: "Full Name"
|
|
|
|
|
if (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.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);
|
|
|
|
|
return [inits, colorCode];
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-18 06:40:11 +00:00
|
|
|
export const getColorCode = (initials: string): string => {
|
2020-04-28 10:47:59 +00:00
|
|
|
let asciiSum = 0;
|
|
|
|
|
for (let i = 0; i < initials.length; i++) {
|
|
|
|
|
asciiSum += initials[i].charCodeAt(0);
|
|
|
|
|
}
|
2020-08-20 16:11:08 +00:00
|
|
|
return appCardColors[asciiSum % appCardColors.length];
|
2020-04-28 10:47:59 +00:00
|
|
|
};
|
2020-05-28 18:10:26 +00:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-08-19 09:21:32 +00:00
|
|
|
|
|
|
|
|
export function getQueryParams() {
|
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
|
const keys = urlParams.keys();
|
|
|
|
|
let key = keys.next().value;
|
|
|
|
|
const queryParams: Record<string, string> = {};
|
|
|
|
|
while (key) {
|
|
|
|
|
queryParams[key] = urlParams.get(key) as string;
|
|
|
|
|
key = keys.next().value;
|
|
|
|
|
}
|
|
|
|
|
return queryParams;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function convertObjectToQueryParams(object: any): string {
|
|
|
|
|
if (!_.isNil(object)) {
|
|
|
|
|
const paramArray: string[] = _.map(_.keys(object), key => {
|
|
|
|
|
return encodeURIComponent(key) + "=" + encodeURIComponent(object[key]);
|
|
|
|
|
});
|
|
|
|
|
return "?" + _.join(paramArray, "&");
|
|
|
|
|
} else {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|