2023-11-09 21:19:08 +02:00
|
|
|
import Setting, { AppType } from '../../../models/Setting';
|
|
|
|
import SyncTargetRegistry from '../../../SyncTargetRegistry';
|
2023-08-14 19:12:49 +02:00
|
|
|
const ObjectUtils = require('../../../ObjectUtils');
|
|
|
|
const { _ } = require('../../../locale');
|
2023-11-09 21:19:08 +02:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import Logger from '@joplin/utils/Logger';
|
|
|
|
|
|
|
|
import { type ReactNode } from 'react';
|
|
|
|
import { type Registry } from '../../../registry';
|
2024-01-27 20:20:51 +02:00
|
|
|
import settingValidations from '../../../models/settings/settingValidations';
|
2022-04-13 13:38:09 +02:00
|
|
|
|
2023-01-02 20:36:39 +02:00
|
|
|
const logger = Logger.create('config-shared');
|
2018-02-06 20:59:36 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
interface ConfigScreenState {
|
|
|
|
checkSyncConfigResult: { ok: boolean; errorMessage: string }|'checking'|null;
|
|
|
|
settings: any;
|
|
|
|
changedSettingKeys: string[];
|
|
|
|
showAdvancedSettings: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultScreenState: ConfigScreenState = {
|
|
|
|
checkSyncConfigResult: null,
|
|
|
|
settings: {},
|
|
|
|
changedSettingKeys: [],
|
|
|
|
showAdvancedSettings: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
interface ConfigScreenComponent {
|
|
|
|
settingToComponent(settingId: string, setting: any): ReactNode;
|
|
|
|
sectionToComponent(sectionName: string, section: any, settings: any, isSelected: boolean): ReactNode;
|
|
|
|
|
|
|
|
state: Partial<ConfigScreenState>;
|
|
|
|
|
|
|
|
setState(callbackOrNew: any, callback?: ()=> void): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SettingsSavedEvent {
|
|
|
|
savedSettingKeys: string[];
|
|
|
|
}
|
2018-02-06 20:59:36 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
type OnSettingsSavedCallback = (event: SettingsSavedEvent)=> void;
|
2022-04-13 13:38:09 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
let onSettingsSaved: OnSettingsSavedCallback = () => {};
|
2022-04-13 13:38:09 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
export const init = (reg: Registry) => {
|
|
|
|
onSettingsSaved = (event) => {
|
2022-04-13 13:38:09 +02:00
|
|
|
const savedSettingKeys = event.savedSettingKeys;
|
|
|
|
|
|
|
|
// After changing the sync settings we immediately trigger a sync
|
|
|
|
// operation. This will ensure that the client gets the sync info as
|
|
|
|
// early as possible, in particular the encryption state (encryption
|
|
|
|
// keys, whether it's enabled, etc.). This should prevent situations
|
|
|
|
// where the user tried to setup E2EE on the client even though it's
|
|
|
|
// already been done on another client.
|
|
|
|
if (savedSettingKeys.find(s => s.startsWith('sync.'))) {
|
|
|
|
logger.info('Sync settings have been changed - scheduling a sync');
|
|
|
|
void reg.scheduleSync();
|
|
|
|
}
|
|
|
|
};
|
2019-12-13 03:16:34 +02:00
|
|
|
};
|
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
export const advancedSettingsButton_click = (comp: ConfigScreenComponent) => {
|
|
|
|
comp.setState((state: ConfigScreenState) => {
|
2019-12-13 03:16:34 +02:00
|
|
|
return { showAdvancedSettings: !state.showAdvancedSettings };
|
|
|
|
});
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-06 20:59:36 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
export const checkSyncConfig = async (comp: ConfigScreenComponent, settings: any) => {
|
2018-03-09 22:59:12 +02:00
|
|
|
const syncTargetId = settings['sync.target'];
|
2018-02-06 20:59:36 +02:00
|
|
|
const SyncTargetClass = SyncTargetRegistry.classById(syncTargetId);
|
2021-04-25 10:50:52 +02:00
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
const options = {
|
|
|
|
...Setting.subValues(`sync.${syncTargetId}`, settings),
|
|
|
|
...Setting.subValues('net', settings) };
|
2021-04-25 10:50:52 +02:00
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
comp.setState({ checkSyncConfigResult: 'checking' });
|
2018-03-15 19:57:11 +02:00
|
|
|
const result = await SyncTargetClass.checkConfig(ObjectUtils.convertValuesToFunctions(options));
|
2018-02-06 20:59:36 +02:00
|
|
|
comp.setState({ checkSyncConfigResult: result });
|
2019-12-13 03:16:34 +02:00
|
|
|
|
|
|
|
if (result.ok) {
|
|
|
|
// Users often expect config to be auto-saved at this point, if the config check was successful
|
2024-01-27 20:20:51 +02:00
|
|
|
await saveSettings(comp);
|
2019-12-13 03:16:34 +02:00
|
|
|
}
|
2021-04-25 10:50:52 +02:00
|
|
|
return result;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-06 20:59:36 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
export const checkSyncConfigMessages = (comp: ConfigScreenComponent) => {
|
2018-02-06 20:59:36 +02:00
|
|
|
const result = comp.state.checkSyncConfigResult;
|
|
|
|
const output = [];
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
if (result === 'checking') {
|
|
|
|
output.push(_('Checking... Please wait.'));
|
2018-02-06 20:59:36 +02:00
|
|
|
} else if (result && result.ok) {
|
2018-03-09 22:59:12 +02:00
|
|
|
output.push(_('Success! Synchronisation configuration appears to be correct.'));
|
2018-02-06 20:59:36 +02:00
|
|
|
} else if (result && !result.ok) {
|
2018-03-09 22:59:12 +02:00
|
|
|
output.push(_('Error. Please check that URL, username, password, etc. are correct and that the sync target is accessible. The reported error was:'));
|
2018-02-06 20:59:36 +02:00
|
|
|
output.push(result.errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-06 20:59:36 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
export const updateSettingValue = (comp: ConfigScreenComponent, key: string, value: any, callback?: ()=> void) => {
|
2023-05-29 14:14:09 +02:00
|
|
|
if (!callback) callback = () => {};
|
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
comp.setState((state: ConfigScreenState) => {
|
2023-01-02 20:36:39 +02:00
|
|
|
// @react-native-community/slider (4.4.0) will emit a valueChanged event
|
|
|
|
// when the component is mounted, even though the value hasn't changed.
|
|
|
|
// We should ignore this, otherwise it will mark the settings as
|
|
|
|
// unsaved.
|
|
|
|
//
|
|
|
|
// Upstream: https://github.com/callstack/react-native-slider/issues/395
|
|
|
|
//
|
|
|
|
// https://github.com/laurent22/joplin/issues/7503
|
|
|
|
if (state.settings[key] === value) {
|
|
|
|
logger.info('Trying to update a setting that has not changed - skipping it.', key, value);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
const settings = { ...state.settings };
|
2019-12-18 17:32:19 +02:00
|
|
|
const changedSettingKeys = state.changedSettingKeys.slice();
|
|
|
|
settings[key] = Setting.formatValue(key, value);
|
|
|
|
if (changedSettingKeys.indexOf(key) < 0) changedSettingKeys.push(key);
|
|
|
|
|
|
|
|
return {
|
|
|
|
settings: settings,
|
|
|
|
changedSettingKeys: changedSettingKeys,
|
|
|
|
};
|
2023-05-29 14:14:09 +02:00
|
|
|
}, callback);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-13 20:26:33 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
let scheduleSaveSettingsIID: ReturnType<typeof setTimeout>|null = null;
|
|
|
|
export const scheduleSaveSettings = (comp: ConfigScreenComponent) => {
|
|
|
|
if (scheduleSaveSettingsIID) clearTimeout(scheduleSaveSettingsIID);
|
2020-11-19 14:34:49 +02:00
|
|
|
|
2024-01-27 20:20:51 +02:00
|
|
|
scheduleSaveSettingsIID = setTimeout(async () => {
|
2023-11-09 21:19:08 +02:00
|
|
|
scheduleSaveSettingsIID = null;
|
2024-01-27 20:20:51 +02:00
|
|
|
await saveSettings(comp);
|
2020-11-19 14:34:49 +02:00
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
|
2024-01-27 20:20:51 +02:00
|
|
|
export const saveSettings = async (comp: ConfigScreenComponent) => {
|
2022-04-13 13:38:09 +02:00
|
|
|
const savedSettingKeys = comp.state.changedSettingKeys.slice();
|
|
|
|
|
2024-01-27 20:20:51 +02:00
|
|
|
const validationMessage = await settingValidations(savedSettingKeys, comp.state.settings);
|
|
|
|
if (validationMessage) {
|
|
|
|
alert(validationMessage);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
for (const key in comp.state.settings) {
|
2018-02-13 20:26:33 +02:00
|
|
|
if (!comp.state.settings.hasOwnProperty(key)) continue;
|
|
|
|
if (comp.state.changedSettingKeys.indexOf(key) < 0) continue;
|
|
|
|
Setting.setValue(key, comp.state.settings[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
comp.setState({ changedSettingKeys: [] });
|
2022-04-13 13:38:09 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
onSettingsSaved({ savedSettingKeys });
|
2024-01-27 20:20:51 +02:00
|
|
|
|
|
|
|
return true;
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-13 20:26:33 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
export const settingsToComponents = (comp: ConfigScreenComponent, device: AppType, settings: any) => {
|
2018-02-13 20:26:33 +02:00
|
|
|
const keys = Setting.keys(true, device);
|
|
|
|
const settingComps = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (!Setting.isPublic(key)) continue;
|
|
|
|
|
|
|
|
const md = Setting.settingMetadata(key);
|
|
|
|
if (md.show && !md.show(settings)) continue;
|
|
|
|
|
|
|
|
const settingComp = comp.settingToComponent(key, settings[key]);
|
|
|
|
if (!settingComp) continue;
|
|
|
|
settingComps.push(settingComp);
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
return settingComps;
|
|
|
|
};
|
2018-02-13 20:26:33 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
type SettingsSelectorState = { device: AppType; settings: any };
|
|
|
|
const deviceSelector = (state: SettingsSelectorState) => state.device;
|
|
|
|
const settingsSelector = (state: SettingsSelectorState) => state.settings;
|
2019-01-28 01:15:56 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
export const settingsSections = createSelector(
|
2019-09-11 01:53:01 +02:00
|
|
|
deviceSelector,
|
|
|
|
settingsSelector,
|
|
|
|
(device, settings) => {
|
|
|
|
const keys = Setting.keys(true, device);
|
|
|
|
const metadatas = [];
|
2019-01-28 01:15:56 +02:00
|
|
|
|
2019-09-11 01:53:01 +02:00
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
if (!Setting.isPublic(key)) continue;
|
2019-01-28 01:15:56 +02:00
|
|
|
|
2019-09-11 01:53:01 +02:00
|
|
|
const md = Setting.settingMetadata(key);
|
|
|
|
if (md.show && !md.show(settings)) continue;
|
|
|
|
|
|
|
|
metadatas.push(md);
|
|
|
|
}
|
|
|
|
|
|
|
|
const output = Setting.groupMetadatasBySections(metadatas);
|
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
if (device === AppType.Desktop || device === AppType.Cli) {
|
|
|
|
output.push({
|
|
|
|
name: 'encryption',
|
|
|
|
metadatas: [],
|
|
|
|
isScreen: true,
|
|
|
|
});
|
2019-09-11 01:53:01 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
output.push({
|
|
|
|
name: 'server',
|
|
|
|
metadatas: [],
|
|
|
|
isScreen: true,
|
|
|
|
});
|
2019-09-11 01:53:01 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
output.push({
|
|
|
|
name: 'keymap',
|
|
|
|
metadatas: [],
|
|
|
|
isScreen: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
output.push(...([
|
|
|
|
'tools', 'export', 'moreInfo',
|
|
|
|
].map(name => {
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
metadatas: [],
|
|
|
|
};
|
|
|
|
})));
|
|
|
|
}
|
2020-09-06 14:00:25 +02:00
|
|
|
|
2024-02-26 12:16:23 +02:00
|
|
|
// Ideally we would also check if the user was able to synchronize
|
2023-07-18 21:15:45 +02:00
|
|
|
// but we don't have a way of doing that besides making a request to Joplin Cloud
|
|
|
|
const syncTargetIsJoplinCloud = settings['sync.target'] === SyncTargetRegistry.nameToId('joplinCloud');
|
|
|
|
if (syncTargetIsJoplinCloud) {
|
|
|
|
output.push({
|
|
|
|
name: 'joplinCloud',
|
|
|
|
metadatas: [],
|
|
|
|
isScreen: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-14 23:54:11 +02:00
|
|
|
const order = Setting.sectionOrder();
|
|
|
|
|
|
|
|
output.sort((a, b) => {
|
|
|
|
const o1 = order.indexOf(a.name);
|
|
|
|
const o2 = order.indexOf(b.name);
|
|
|
|
return o1 < o2 ? -1 : +1;
|
|
|
|
});
|
|
|
|
|
2019-09-11 01:53:01 +02:00
|
|
|
return output;
|
2023-08-22 12:58:53 +02:00
|
|
|
},
|
2019-09-11 01:53:01 +02:00
|
|
|
);
|
2019-01-28 01:15:56 +02:00
|
|
|
|
2023-11-09 21:19:08 +02:00
|
|
|
export const settingsToComponents2 = (
|
|
|
|
comp: ConfigScreenComponent, device: AppType, settings: any, selectedSectionName = '',
|
|
|
|
) => {
|
|
|
|
const sectionComps: ReactNode[] = [];
|
|
|
|
const sections = settingsSections({ device, settings });
|
2019-01-28 01:15:56 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < sections.length; i++) {
|
|
|
|
const section = sections[i];
|
2019-09-11 01:53:01 +02:00
|
|
|
const sectionComp = comp.sectionToComponent(section.name, section, settings, selectedSectionName === section.name);
|
2019-01-28 01:15:56 +02:00
|
|
|
if (!sectionComp) continue;
|
|
|
|
sectionComps.push(sectionComp);
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
return sectionComps;
|
|
|
|
};
|