index.community/frontend/src/util.ts

76 lines
2.6 KiB
TypeScript
Raw Normal View History

import { createMatchSelector } from "connected-react-router";
2019-02-21 12:33:07 +00:00
import fetch from "cross-fetch";
2019-07-27 17:58:40 +00:00
import { range } from "lodash";
2020-05-19 13:45:27 +00:00
import { DESKTOP_WIDTH_THRESHOLD, InstanceDomainPath, INSTANCE_DOMAIN_PATH } from "./constants";
import { AppState } from "./redux/types";
2018-08-27 21:31:27 +00:00
2021-07-30 16:53:51 +00:00
let API_ROOT = "https://api.index.community/api/";
2020-05-19 13:45:27 +00:00
if (["true", true, 1, "1"].includes(process.env.REACT_APP_STAGING || "")) {
2021-07-30 16:53:51 +00:00
API_ROOT = "https://api.index.community/api/";
} else if (process.env.NODE_ENV === "production") {
2021-07-30 16:53:51 +00:00
API_ROOT = "https://api.index.community/api/";
}
2018-08-27 21:31:27 +00:00
2019-07-26 14:34:23 +00:00
export const getFromApi = (path: string, token?: string): Promise<any> => {
2020-05-19 13:45:27 +00:00
const domain = API_ROOT.endsWith("/") ? API_ROOT : `${API_ROOT}/`;
2019-07-26 14:34:23 +00:00
const headers = token ? { token } : undefined;
return fetch(encodeURI(domain + path), {
2020-05-19 13:45:27 +00:00
headers,
}).then((response) => response.json());
2019-07-26 14:34:23 +00:00
};
export const postToApi = (path: string, body: any, token?: string): Promise<any> => {
2020-05-19 13:45:27 +00:00
const domain = API_ROOT.endsWith("/") ? API_ROOT : `${API_ROOT}/`;
2019-07-26 14:34:23 +00:00
const defaultHeaders = {
Accept: "application/json",
2020-05-19 13:45:27 +00:00
"Content-Type": "application/json",
2019-07-26 14:34:23 +00:00
};
const headers = token ? { ...defaultHeaders, token } : defaultHeaders;
return fetch(encodeURI(domain + path), {
body: JSON.stringify(body),
headers,
2020-05-19 13:45:27 +00:00
method: "POST",
}).then((response) => response.json());
2019-02-21 12:33:07 +00:00
};
2020-05-19 13:45:27 +00:00
export const domainMatchSelector = createMatchSelector<AppState, InstanceDomainPath>(INSTANCE_DOMAIN_PATH);
2019-07-23 16:32:43 +00:00
export const isSmallScreen = window.innerWidth < DESKTOP_WIDTH_THRESHOLD;
2019-07-24 15:51:44 +00:00
export const capitalize = (s: string): string => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
2019-07-26 14:34:23 +00:00
export const setAuthToken = (token: string) => {
sessionStorage.setItem("adminToken", token);
};
export const unsetAuthToken = () => {
sessionStorage.removeItem("adminToken");
};
2020-05-19 13:45:27 +00:00
export const getAuthToken = () => sessionStorage.getItem("adminToken");
2019-07-27 17:58:40 +00:00
export const getBuckets = (min: number, max: number, steps: number, exponential: boolean) => {
if (exponential) {
2020-05-19 13:45:27 +00:00
const logSpace = range(steps).map((i) => Math.E ** i);
2019-07-27 17:58:40 +00:00
// Scale the log space to the linear range
const logRange = logSpace[logSpace.length - 1] - logSpace[0];
const linearRange = max - min;
const scalingFactor = linearRange / logRange;
const translation = min - logSpace[0];
2020-05-19 13:45:27 +00:00
return logSpace.map((i) => (i + translation) * scalingFactor);
2019-07-27 17:58:40 +00:00
}
2020-05-19 13:45:27 +00:00
// Linear
const bucketSize = (max - min) / steps;
return range(min, max, bucketSize);
2019-07-26 14:34:23 +00:00
};
2020-05-19 13:45:27 +00:00
const typeToDisplay: { [field: string]: string } = {
gnusocial: "GNU Social",
};
export const getTypeDisplayString = (key: string) => {
if (key in typeToDisplay) {
return typeToDisplay[key];
}
return capitalize(key);
};