2023-01-10 14:08:13 +02:00
|
|
|
// Helper functions to reduce the boiler plate of loading and saving profiles on
|
|
|
|
// mobile
|
|
|
|
|
|
|
|
import { Profile, ProfileConfig } from '@joplin/lib/services/profileConfig/types';
|
|
|
|
import { loadProfileConfig as libLoadProfileConfig, saveProfileConfig as libSaveProfileConfig } from '@joplin/lib/services/profileConfig/index';
|
2024-08-02 15:51:49 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
2023-01-10 14:08:13 +02:00
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-01-10 14:08:13 +02:00
|
|
|
let dispatch_: Function = null;
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2023-01-10 14:08:13 +02:00
|
|
|
export const setDispatch = (dispatch: Function) => {
|
|
|
|
dispatch_ = dispatch;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getProfilesRootDir = () => {
|
2024-08-02 15:51:49 +02:00
|
|
|
return shim.fsDriver().getAppDirectoryPath();
|
2023-01-10 14:08:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getProfilesConfigPath = () => {
|
|
|
|
return `${getProfilesRootDir()}/profiles.json`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getResourceDir = (profile: Profile, isSubProfile: boolean) => {
|
|
|
|
if (!isSubProfile) return getProfilesRootDir();
|
|
|
|
return `${getProfilesRootDir()}/resources-${profile.id}`;
|
|
|
|
};
|
|
|
|
|
2024-04-04 13:10:59 +02:00
|
|
|
export const getPluginDataDir = (profile: Profile, isSubProfile: boolean) => {
|
|
|
|
const suffix = isSubProfile ? `-${profile.id}` : '';
|
|
|
|
return `${getProfilesRootDir()}/plugin-data${suffix}`;
|
|
|
|
};
|
|
|
|
|
2023-03-22 20:22:58 +02:00
|
|
|
// The suffix is for debugging only
|
2023-06-30 10:11:26 +02:00
|
|
|
export const getDatabaseName = (profile: Profile, isSubProfile: boolean, suffix = '') => {
|
2023-03-22 20:22:58 +02:00
|
|
|
if (!isSubProfile) return `joplin${suffix}.sqlite`;
|
|
|
|
return `joplin-${profile.id}${suffix}.sqlite`;
|
2023-01-10 14:08:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const loadProfileConfig = async () => {
|
|
|
|
return libLoadProfileConfig(getProfilesConfigPath());
|
|
|
|
};
|
|
|
|
|
|
|
|
export const saveProfileConfig = async (profileConfig: ProfileConfig) => {
|
|
|
|
await libSaveProfileConfig(getProfilesConfigPath(), profileConfig);
|
|
|
|
dispatch_({
|
|
|
|
type: 'PROFILE_CONFIG_SET',
|
|
|
|
value: profileConfig,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const switchProfile = async (profileId: string) => {
|
|
|
|
const config = await loadProfileConfig();
|
|
|
|
if (config.currentProfileId === profileId) throw new Error('This profile is already active');
|
|
|
|
|
|
|
|
config.currentProfileId = profileId;
|
|
|
|
await saveProfileConfig(config);
|
2024-08-02 15:51:49 +02:00
|
|
|
shim.restartApp();
|
2023-01-10 14:08:13 +02:00
|
|
|
};
|