PromucFlow_constructor/app/client/src/utils/AppsmithUtils.tsx

169 lines
5.2 KiB
TypeScript
Raw Normal View History

2020-03-16 07:59:07 +00:00
import { ReduxAction } from "constants/ReduxActionConstants";
import { getAppsmithConfigs } from "configs";
2019-09-09 09:08:54 +00:00
import * as Sentry from "@sentry/browser";
import AnalyticsUtil from "./AnalyticsUtil";
import FontFaceObserver from "fontfaceobserver";
import FormControlRegistry from "./FormControlRegistry";
2019-11-25 05:07:27 +00:00
import { Property } from "api/ActionAPI";
import _ from "lodash";
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";
import { providerBackgroundColors } from "constants/providerConstants";
2020-05-05 06:46:54 +00:00
import { FeatureFlagEnum } from "utils/featureFlags";
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 = () => {
FormControlRegistry.registerFormControlBuilders();
const appsmithConfigs = getAppsmithConfigs();
if (appsmithConfigs.sentry.enabled && appsmithConfigs.sentry.config) {
Sentry.init(appsmithConfigs.sentry.config);
}
if (appsmithConfigs.hotjar.enabled && appsmithConfigs.hotjar.config) {
const { id, sv } = appsmithConfigs.hotjar.config;
AnalyticsUtil.initializeHotjar(id, sv);
}
if (appsmithConfigs.segment.enabled) {
AnalyticsUtil.initializeSegment(appsmithConfigs.segment.key);
2019-08-30 10:33:49 +00:00
}
2020-03-27 12:46:29 +00:00
log.setLevel(getEnvLogLevel(appsmithConfigs.logLevel));
2020-05-05 06:46:54 +00:00
setConfigFeatureFlags(appsmithConfigs.featureFlags);
2020-03-23 12:40:17 +00:00
2019-09-06 11:40:00 +00:00
const textFont = new FontFaceObserver("DM Sans");
2019-09-09 09:08:54 +00:00
textFont
.load()
.then(() => {
document.body.className += "fontLoaded";
})
.catch(err => {
console.log(err);
});
};
export const mapToPropList = (map: Record<string, string>): Property[] => {
return _.map(map, (value, key) => {
return { key: key, value: value };
});
};
2020-01-24 09:54:40 +00:00
export const getNextEntityName = (prefix: string, existingNames: string[]) => {
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)) {
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[];
2020-01-24 09:54:40 +00:00
const lastIndex = Math.max(...usedIndices, ...[0]);
return prefix + (lastIndex + 1);
};
2019-11-07 11:17:53 +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
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-05-05 06:46:54 +00:00
const setConfigFeatureFlags = (flags: Array<FeatureFlagEnum>) => {
flags.forEach(flag => {
localStorage.setItem(flag, "true");
});
};
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];
};
const getColorCode = (initials: string): string => {
let asciiSum = 0;
for (let i = 0; i < initials.length; i++) {
asciiSum += initials[i].charCodeAt(0);
}
return providerBackgroundColors[asciiSum % providerBackgroundColors.length];
};