pleroma/src/actions/soapbox.ts

110 lines
3.4 KiB
TypeScript
Raw Normal View History

import { createSelector } from 'reselect';
import { getHost } from 'soapbox/actions/instance';
2022-03-28 12:58:50 -07:00
import { normalizeSoapboxConfig } from 'soapbox/normalizers';
import KVStore from 'soapbox/storage/kv-store';
import { getClient, staticFetch } from '../api';
import type { AppDispatch, RootState } from 'soapbox/store';
import type { APIEntity } from 'soapbox/types/entities';
const SOAPBOX_CONFIG_REQUEST_SUCCESS = 'SOAPBOX_CONFIG_REQUEST_SUCCESS' as const;
const SOAPBOX_CONFIG_REQUEST_FAIL = 'SOAPBOX_CONFIG_REQUEST_FAIL' as const;
const SOAPBOX_CONFIG_REMEMBER_SUCCESS = 'SOAPBOX_CONFIG_REMEMBER_SUCCESS' as const;
const getSoapboxConfig = createSelector([
(state: RootState) => state.soapbox,
(state: RootState) => state.auth.client.features,
], (soapbox, features) => {
// Do some additional normalization with the state
return normalizeSoapboxConfig(soapbox);
});
const rememberSoapboxConfig = (host: string | null) =>
(dispatch: AppDispatch) => {
return KVStore.getItemOrError(`soapbox_config:${host}`).then(soapboxConfig => {
dispatch({ type: SOAPBOX_CONFIG_REMEMBER_SUCCESS, host, soapboxConfig });
return soapboxConfig;
}).catch(() => {});
};
const fetchFrontendConfigurations = () =>
(dispatch: AppDispatch, getState: () => RootState) =>
getClient(getState).instance.getFrontendConfigurations();
/** Conditionally fetches Soapbox config depending on backend features */
const fetchSoapboxConfig = (host: string | null) =>
(dispatch: AppDispatch, getState: () => RootState) => {
const features = getState().auth.client.features;
if (features.frontendConfigurations) {
return dispatch(fetchFrontendConfigurations()).then(data => {
if (data.pl_fe) {
dispatch(importSoapboxConfig(data.pl_fe, host));
return data.pl_fe;
} else {
return dispatch(fetchSoapboxJson(host));
}
});
} else {
return dispatch(fetchSoapboxJson(host));
}
};
/** Tries to remember the config from browser storage before fetching it */
const loadSoapboxConfig = () =>
(dispatch: AppDispatch, getState: () => RootState) => {
const host = getHost(getState());
return dispatch(rememberSoapboxConfig(host)).then(() =>
dispatch(fetchSoapboxConfig(host)),
);
2020-08-23 13:32:44 -07:00
};
const fetchSoapboxJson = (host: string | null) =>
(dispatch: AppDispatch) =>
staticFetch('/instance/soapbox.json').then(({ json: data }) => {
if (!isObject(data)) throw 'soapbox.json failed';
dispatch(importSoapboxConfig(data, host));
return data;
2020-08-23 13:32:44 -07:00
}).catch(error => {
dispatch(soapboxConfigFail(error, host));
});
const importSoapboxConfig = (soapboxConfig: APIEntity, host: string | null) => {
2020-09-02 19:47:46 -07:00
if (!soapboxConfig.brandColor) {
soapboxConfig.brandColor = '#0482d8';
2021-08-03 12:22:51 -07:00
}
return {
type: SOAPBOX_CONFIG_REQUEST_SUCCESS,
2020-04-14 11:44:40 -07:00
soapboxConfig,
host,
};
};
2023-10-23 15:22:10 -07:00
const soapboxConfigFail = (error: unknown, host: string | null) => ({
type: SOAPBOX_CONFIG_REQUEST_FAIL,
error,
skipAlert: true,
host,
});
// https://stackoverflow.com/a/46663081
const isObject = (o: any) => o instanceof Object && o.constructor === Object;
export {
SOAPBOX_CONFIG_REQUEST_SUCCESS,
SOAPBOX_CONFIG_REQUEST_FAIL,
SOAPBOX_CONFIG_REMEMBER_SUCCESS,
getSoapboxConfig,
rememberSoapboxConfig,
fetchFrontendConfigurations,
fetchSoapboxConfig,
loadSoapboxConfig,
fetchSoapboxJson,
importSoapboxConfig,
soapboxConfigFail,
};