Farmbot-Web-App/frontend/account/labs/labs_features_list_data.ts

109 lines
3.8 KiB
TypeScript
Raw Normal View History

import { BooleanSetting } from "../../session_keys";
2018-03-13 21:55:15 -06:00
import { Content } from "../../constants";
2019-06-10 15:43:11 -06:00
import {
2020-02-28 09:35:32 -07:00
GetWebAppConfigValue, setWebAppConfigValue,
2019-06-10 15:43:11 -06:00
} from "../../config_storage/actions";
2018-10-22 09:35:44 -06:00
import { BooleanConfigKey } from "farmbot/dist/resources/configs/web_app";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2017-09-28 06:31:58 -06:00
export interface LabsFeature {
2018-03-07 10:43:02 -07:00
/** Toggle label. */
2017-09-28 06:31:58 -06:00
name: string;
description: string;
/** Configuration key name such as "disable_i18n". Must be unique. */
storageKey: BooleanConfigKey;
2018-03-07 10:43:02 -07:00
/** Placeholder value (use false). */
2017-09-28 06:31:58 -06:00
value: boolean;
2018-03-07 10:43:02 -07:00
/** Confirmation message to display before allowing a toggle to true. */
confirmationMessage?: string;
/** Invert displayed toggle value for `disable_` settings. */
2017-10-10 22:43:31 -06:00
displayInvert?: boolean;
2017-10-24 13:58:33 -06:00
/** If the feature requires any special logic after being flipped, add it
* here. */
callback?(): void;
2017-09-28 06:31:58 -06:00
}
2018-08-30 19:25:58 -06:00
export const fetchLabFeatures =
(getConfigValue: GetWebAppConfigValue): LabsFeature[] => ([
{
name: t("Internationalize Web App"),
description: t("Turn off to set Web App to English."),
storageKey: BooleanSetting.disable_i18n,
value: false,
displayInvert: true,
callback: () => window.location.reload()
},
2019-06-21 15:43:46 -06:00
{
name: t("Use 24-hour time format"),
description: t(Content.TIME_FORMAT_24_HOUR),
storageKey: BooleanSetting.time_format_24_hour,
value: false,
},
2018-08-30 19:25:58 -06:00
{
name: t("Hide Webcam widget"),
description: t(Content.HIDE_WEBCAM_WIDGET),
storageKey: BooleanSetting.hide_webcam_widget,
value: false
},
2019-06-07 18:26:32 -06:00
{
name: t("Hide Sensors widget"),
description: t(Content.HIDE_SENSORS_WIDGET),
2019-06-09 12:11:15 -06:00
storageKey: BooleanSetting.hide_sensors,
2019-06-07 18:26:32 -06:00
value: false
},
2018-08-30 19:25:58 -06:00
{
name: t("Read speak logs in browser"),
description: t(Content.BROWSER_SPEAK_LOGS),
storageKey: BooleanSetting.enable_browser_speak,
value: false
},
{
name: t("Discard Unsaved Changes"),
description: t(Content.DISCARD_UNSAVED_CHANGES),
storageKey: BooleanSetting.discard_unsaved,
value: false,
confirmationMessage: t(Content.DISCARD_UNSAVED_CHANGES_CONFIRM)
},
2019-04-17 13:31:18 -06:00
{
name: t("Confirm emergency unlock"),
description: t(Content.EMERGENCY_UNLOCK_CONFIRM_CONFIG),
confirmationMessage: t(Content.CONFIRM_EMERGENCY_UNLOCK_CONFIRM_DISABLE),
storageKey: BooleanSetting.disable_emergency_unlock_confirmation,
value: false,
displayInvert: true,
},
{
name: t("User Interface Read Only Mode"),
description: t(Content.USER_INTERFACE_READ_ONLY_MODE),
storageKey: BooleanSetting.user_interface_read_only_mode,
value: false,
displayInvert: false,
2020-02-28 09:35:32 -07:00
},
2018-08-30 19:25:58 -06:00
].map(fetchSettingValue(getConfigValue)));
2017-09-28 06:31:58 -06:00
/** Always allow toggling from true => false (deactivate).
* Require a disclaimer when going from false => true (activate). */
export const maybeToggleFeature =
2018-08-30 19:25:58 -06:00
(getConfigValue: GetWebAppConfigValue, dispatch: Function) =>
(x: LabsFeature): LabsFeature | undefined =>
(x.value
|| !x.confirmationMessage
|| window.confirm(x.confirmationMessage)) ?
toggleFeatureValue(getConfigValue, dispatch)(x) : undefined;
2017-09-28 06:31:58 -06:00
/** Takes a `LabFeature` (probably one with an uninitialized fallback / default
2018-03-06 13:39:41 -07:00
* value) and sets it to the _real_ value that's in the API. */
2018-08-30 19:25:58 -06:00
const fetchSettingValue = (getConfigValue: GetWebAppConfigValue) =>
(x: LabsFeature): LabsFeature => {
return { ...x, value: !!getConfigValue(x.storageKey) };
};
2017-09-28 06:31:58 -06:00
/** Toggle the `.value` of a `LabsToggle` object */
2018-08-30 19:25:58 -06:00
const toggleFeatureValue =
(getConfigValue: GetWebAppConfigValue, dispatch: Function) =>
(x: LabsFeature) => {
const value = !getConfigValue(x.storageKey);
dispatch(setWebAppConfigValue(x.storageKey, value));
return { ...x, value };
};