import React from "react" import * as antd from "antd" import { SliderPicker } from "react-color" import { Translation } from "react-i18next" import classnames from "classnames" import config from "config" import { Icons, createIconRender } from "components/Icons" import SettingsList from "schemas/settings" import groupsDecorator from "schemas/settingsGroupsDecorator.json" import "./index.less" const ItemTypes = { Button: antd.Button, Switch: antd.Switch, Slider: antd.Slider, Checkbox: antd.Checkbox, Input: antd.Input, TextArea: antd.Input.TextArea, InputNumber: antd.InputNumber, Select: antd.Select, SliderColorPicker: SliderPicker, } const SettingsFooter = (props) => { const isDevMode = window.__evite?.env?.NODE_ENV !== "production" return
{config.app?.siteName}
v{window.app.version}
{isDevMode ? : } {isDevMode ? "development" : "stable"}
{t => t("about")}
} const SettingItem = (props) => { let { item } = props const [loading, setLoading] = React.useState(true) const [value, setValue] = React.useState(null) const [delayedValue, setDelayedValue] = React.useState(null) const [disabled, setDisabled] = React.useState(false) let SettingComponent = item.component if (!SettingComponent) { console.error(`Item [${item.id}] has no an component!`) return null } if (typeof item.props === "undefined") { item.props = {} } const dispatchUpdate = async (updateValue) => { if (typeof item.onUpdate === "function") { const result = await item.onUpdate(updateValue).catch((error) => { console.error(error) antd.message.error(error.message) return false }) if (!result) { return false } updateValue = result } else { const storagedValue = await window.app.settings.get(item.id) if (typeof updateValue === "undefined") { updateValue = !storagedValue } } if (item.storaged) { await window.app.settings.set(item.id, updateValue) } if (typeof item.emitEvent !== "undefined") { let emissionPayload = updateValue if (typeof item.emissionValueUpdate === "function") { emissionPayload = item.emissionValueUpdate(emissionPayload) } if (Array.isArray(item.emitEvent)) { window.app.eventBus.emit(...item.emitEvent, emissionPayload) } else if (typeof item.emitEvent === "string") { window.app.eventBus.emit(item.emitEvent, emissionPayload) } } if (item.noUpdate) { return false } if (item.debounced) { setDelayedValue(null) } setValue(updateValue) } const onUpdateItem = async (updateValue) => { setValue(updateValue) if (!item.debounced) { await dispatchUpdate(updateValue) } else { setDelayedValue(updateValue) } } const checkDependsValidation = () => { return !Boolean(Object.keys(item.dependsOn).every((key) => { const storagedValue = window.app.settings.get(key) console.debug(`Checking validation for [${key}] with now value [${storagedValue}]`) if (typeof item.dependsOn[key] === "function") { return item.dependsOn[key](storagedValue) } return storagedValue === item.dependsOn[key] })) } const settingInitialization = async () => { if (item.storaged) { const storagedValue = window.app.settings.get(item.id) setValue(storagedValue) } if (typeof item.defaultValue === "function") { setLoading(true) setValue(await item.defaultValue(props.ctx)) setLoading(false) } if (item.disabled === true) { setDisabled(true) } if (typeof item.dependsOn === "object") { // create a event handler to watch changes Object.keys(item.dependsOn).forEach((key) => { window.app.eventBus.on(`setting.update.${key}`, () => { setDisabled(checkDependsValidation()) }) }) // by default check depends validation setDisabled(checkDependsValidation()) } if (typeof item.listenUpdateValue === "string") { window.app.eventBus.on(`setting.update.${item.listenUpdateValue}`, (value) => setValue(value)) } if (item.reloadValueOnUpdateEvent) { window.app.eventBus.on(item.reloadValueOnUpdateEvent, () => { console.log(`Reloading value for item [${item.id}]`) settingInitialization() }) } setLoading(false) } React.useEffect(() => { settingInitialization() return () => { if (typeof item.dependsOn === "object") { for (let key in item.dependsOn) { window.app.eventBus.off(`setting.update.${key}`, onUpdateItem) } } } }, []) if (typeof SettingComponent === "string") { if (typeof ItemTypes[SettingComponent] === "undefined") { console.error(`Item [${item.id}] has an invalid component: ${item.component}`) return null } switch (SettingComponent.toLowerCase()) { case "slidercolorpicker": { item.props.onChange = (color) => { item.props.color = color.hex } item.props.onChangeComplete = (color) => { onUpdateItem(color.hex) } item.props.color = value break } case "textarea": { item.props.defaultValue = value item.props.onPressEnter = (event) => dispatchUpdate(event.target.value) item.props.onChange = (event) => onUpdateItem(event.target.value) break } case "input": { item.props.defaultValue = value item.props.onPressEnter = (event) => dispatchUpdate(event.target.value) item.props.onChange = (event) => onUpdateItem(event.target.value) break } case "switch": { item.props.checked = value item.props.onClick = (event) => onUpdateItem(event) break } case "select": { item.props.onChange = (value) => onUpdateItem(value) item.props.defaultValue = value break } case "slider": { item.props.defaultValue = value item.props.onAfterChange = (value) => onUpdateItem(value) break } default: { if (!item.props.children) { item.props.children = item.title ?? item.id } item.props.value = item.defaultValue item.props.onClick = (event) => onUpdateItem(event) break } } // override with default item component SettingComponent = ItemTypes[SettingComponent] } item.props["disabled"] = disabled return

{Icons[item.icon] ? React.createElement(Icons[item.icon]) : null} { t => t(item.title ?? item.id) }

{ t => t(item.description) }

{item.experimental && Experimental }
{item.extraActions &&
{item.extraActions.map((action, index) => { return
{action.title}
})}
}
{ loading ?
Loading...
: React.createElement(SettingComponent, { ...item.props, ctx: { currentValue: value, dispatchUpdate, onUpdateItem, ...props.ctx, } })}
{ delayedValue &&
} onClick={async () => await dispatchUpdate(value)} > Save
}
} const SettingGroup = React.memo((props) => { const { ctx, groupKey, settings, loading, } = props const fromDecoratorIcon = groupsDecorator[groupKey]?.icon const fromDecoratorTitle = groupsDecorator[groupKey]?.title if (loading) { return } return

{ fromDecoratorIcon ? React.createElement(Icons[fromDecoratorIcon]) : null } { t => t(fromDecoratorTitle ?? groupKey) }

{ settings.map((item) => ) }
}) const SettingTab = React.memo((props) => { const { tab } = props const [loading, setLoading] = React.useState(true) const [ctxData, setCtxData] = React.useState(null) let groupsSettings = {} if (!Array.isArray(tab.settings)) { console.error("Cannot generate settings from non-array") return groupsSettings } tab.settings.forEach((item) => { if (!groupsSettings[item.group]) { groupsSettings[item.group] = [] } groupsSettings[item.group].push(item) }) const processCtx = async () => { setLoading(true) if (typeof tab.ctxData === "function") { const resultCtx = await tab.ctxData() setCtxData(resultCtx) } setLoading(false) } React.useEffect(() => { processCtx() }, []) return Object.keys(groupsSettings).map((groupKey) => { return }) }) const SettingsTabs = Object.keys(SettingsList).map((settingsKey) => { const tab = SettingsList[settingsKey] return { key: settingsKey, label: <> {createIconRender(tab.icon ?? "Settings")} {tab.label} , children: } }) export default class SettingsMenu extends React.PureComponent { state = { activeKey: "app" } componentDidMount = async () => { if (typeof this.props.close === "function") { // register escape key to close settings menu window.addEventListener("keydown", this.handleKeyDown) } } componentWillUnmount() { if (typeof this.props.close === "function") { window.removeEventListener("keydown", this.handleKeyDown) } } handleKeyDown = (event) => { if (event.key === "Escape") { this.props.close() } } onClickAppAbout = () => { window.app.setLocation("/about") if (typeof this.props.close === "function") { this.props.close() } } changeTab = (activeKey) => { this.setState({ activeKey }) } render() { return
} }