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

95 lines
2.7 KiB
TypeScript
Raw Normal View History

2019-11-25 05:07:27 +00:00
import { ReduxAction } from "constants/ReduxActionConstants";
2019-09-09 09:08:54 +00:00
import {
SENTRY_PROD_CONFIG,
SENTRY_STAGE_CONFIG,
HOTJAR_PROD_HJID,
HOTJAR_PROD_HJSV,
2019-11-25 05:07:27 +00:00
} from "constants/ThirdPartyConstants";
2019-09-09 09:08:54 +00:00
import * as Sentry from "@sentry/browser";
import AnalyticsUtil from "./AnalyticsUtil";
import netlifyIdentity from "netlify-identity-widget";
import FontFaceObserver from "fontfaceobserver";
import PropertyControlRegistry from "./PropertyControlRegistry";
import WidgetBuilderRegistry from "./WidgetRegistry";
2019-11-25 05:07:27 +00:00
import { Property } from "api/ActionAPI";
import { FlattenedWidgetProps } from "reducers/entityReducers/canvasWidgetsReducer";
import _ from "lodash";
2019-11-06 12:12:41 +00:00
import moment from "moment-timezone";
2019-11-19 12:44:58 +00:00
import ValidationRegistry from "./ValidationRegistry";
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 = () => {
WidgetBuilderRegistry.registerWidgetBuilders();
PropertyControlRegistry.registerPropertyControlBuilders();
2019-11-19 12:44:58 +00:00
ValidationRegistry.registerInternalValidators();
2019-09-02 15:36:24 +00:00
netlifyIdentity.init();
2019-11-06 12:12:41 +00:00
moment.tz.setDefault(moment.tz.guess());
2019-08-30 10:33:49 +00:00
switch (process.env.REACT_APP_ENVIRONMENT) {
case "PRODUCTION":
2019-09-09 09:08:54 +00:00
Sentry.init(SENTRY_PROD_CONFIG);
2019-08-30 11:23:42 +00:00
AnalyticsUtil.initializeHotjar(HOTJAR_PROD_HJID, HOTJAR_PROD_HJSV);
AnalyticsUtil.initializeSegment();
2019-08-30 10:33:49 +00:00
break;
case "STAGING":
2019-09-09 09:08:54 +00:00
Sentry.init(SENTRY_STAGE_CONFIG);
break;
case "DEVELOPMENT":
break;
2019-08-30 10:33:49 +00:00
case "LOCAL":
break;
}
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 };
});
};
export const getNextWidgetName = (
prefix: string,
widgets: {
[id: string]: FlattenedWidgetProps;
},
) => {
const regex = new RegExp(`^${prefix}(\\d+)$`);
const usedIndices: number[] = Object.values(widgets).map(widget => {
if (widget && widget.widgetName && regex.test(widget.widgetName)) {
const name = widget.widgetName || "";
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);
return prefix + (lastIndex + 1);
};
2019-11-07 11:17:53 +00:00
export const noop = () => {};