Files
standardnotes-app-web/app/assets/javascripts/preferences/panes/backups-segments/cloud-backups/index.tsx
Vardan Hakobyan a342a3a224 feat: add "Email Backups" to "Backups" section (#778)
* feat: add "Email Backups" to "Backups" section

* chore: remove comment

* chore: better wording

* chore: put working snjs version

* chore: better wording

* style: reuse existing css classes and add the missing one

* feat: add "No email backup" option

* refactor: move the function outside of the useEffect, remove unused utility function

* feat (WIP): move CloudLink to backups section

* chore: versions bump, type fixes

* fix: handle the case when the setting update fails

* style: remove dashed border from the confirmation code, UI improvements

* feat: implement removing integration, improve interaction on different events

* feat: implement non-interactive textarea for showing and copying the code

* fix: fix TS errors

* feat: implement "Perform backup" logic
- remove the code for copying the confirmation code for backup integration
- also remove unnecessary parameters passed to Provider

* feat: don't show "CloudLink" in preferences pane

* chore: show error in console on exception

* refactor: better naming, add `coverage` folder to gitignore

* fix: return correct setting name

* refactor: use async/await for the sake of consistency

* chore: remove duplicate line

* feat: get urls for cloud backup from snjs

* chore: update dependencies

* refactor: set both `token` and `frequency` settings when enabling cloud integration; get only `frequency` when checking the integration status

* refactor: once the setting is successfully saved, don't get its value from backend; instead, use its value that's still in frontend

* feat: move "Receive a notification email if a cloud backup fails." into cloud backups section

* fix: text correction

* fix: get correct cloud integration url from snjs based on prod/dev environment
2022-01-12 18:48:46 +04:00

151 lines
4.7 KiB
TypeScript

import React from 'react';
import { CloudBackupProvider } from './CloudBackupProvider';
import { useCallback, useEffect, useState } from 'preact/hooks';
import { WebApplication } from '@/ui_models/application';
import {
PreferencesGroup,
PreferencesSegment, Subtitle,
Text,
Title
} from '@/preferences/components';
import { HorizontalSeparator } from '@/components/shared/HorizontalSeparator';
import { FeatureIdentifier } from '@standardnotes/features';
import { FeatureStatus } from '@standardnotes/snjs';
import { FunctionComponent } from 'preact';
import { CloudProvider, EmailBackupFrequency, SettingName } from '@standardnotes/settings';
import { Switch } from '@/components/Switch';
import { convertStringifiedBooleanToBoolean } from '@/utils';
import { STRING_FAILED_TO_UPDATE_USER_SETTING } from '@/strings';
const providerData = [{
name: CloudProvider.Dropbox
}, {
name: CloudProvider.Google
}, {
name: CloudProvider.OneDrive
}
];
type Props = {
application: WebApplication;
};
export const CloudLink: FunctionComponent<Props> = ({ application }) => {
const [isEntitledForCloudBackups, setIsEntitledForCloudBackups] = useState(false);
const [isFailedCloudBackupEmailMuted, setIsFailedCloudBackupEmailMuted] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const loadIsFailedCloudBackupEmailMutedSetting = useCallback(async () => {
setIsLoading(true);
try {
const userSettings = await application.listSettings();
setIsFailedCloudBackupEmailMuted(
convertStringifiedBooleanToBoolean(
userSettings[SettingName.MuteFailedCloudBackupsEmails] as string
)
);
} catch (error) {
console.error(error);
} finally {
setIsLoading(false);
}
}, [application]);
useEffect(() => {
const cloudBackupsFeatureStatus = application.getFeatureStatus(
FeatureIdentifier.CloudLink
);
setIsEntitledForCloudBackups(
cloudBackupsFeatureStatus === FeatureStatus.Entitled
);
loadIsFailedCloudBackupEmailMutedSetting();
}, [application, loadIsFailedCloudBackupEmailMutedSetting]);
const updateSetting = async (
settingName: SettingName,
payload: string
): Promise<boolean> => {
try {
await application.updateSetting(settingName, payload);
return true;
} catch (e) {
application.alertService.alert(STRING_FAILED_TO_UPDATE_USER_SETTING);
return false;
}
};
const toggleMuteFailedCloudBackupEmails = async () => {
const previousValue = isFailedCloudBackupEmailMuted;
setIsFailedCloudBackupEmailMuted(!isFailedCloudBackupEmailMuted);
const updateResult = await updateSetting(
SettingName.MuteFailedCloudBackupsEmails,
`${!isFailedCloudBackupEmailMuted}`
);
if (!updateResult) {
setIsFailedCloudBackupEmailMuted(previousValue);
}
};
return (
<PreferencesGroup>
<PreferencesSegment>
<Title>Cloud Backups</Title>
{!isEntitledForCloudBackups && (
<>
<Text>
A <span className={'font-bold'}>Plus</span> or{' '}
<span className={'font-bold'}>Pro</span> subscription plan is
required to enable Cloud Backups.{' '}
<a target='_blank' href='https://standardnotes.com/features'>
Learn more
</a>
.
</Text>
<HorizontalSeparator classes='mt-3 mb-3' />
</>
)}
<div
className={
isEntitledForCloudBackups
? ''
: 'faded cursor-default pointer-events-none'
}
>
<Text>
Configure the integrations below to enable automatic daily backups
of your encrypted data set to your third-party cloud provider.
</Text>
<div>
<HorizontalSeparator classes={'mt-3 mb-3'} />
<div>
{providerData.map(({ name }) => (
<>
<CloudBackupProvider application={application} providerName={name} />
<HorizontalSeparator classes={'mt-3 mb-3'} />
</>
))}
</div>
</div>
<Subtitle>Email preferences</Subtitle>
<div className="flex items-center justify-between mt-1">
<div className="flex flex-col">
<Text>Receive a notification email if a cloud backup fails.</Text>
</div>
{isLoading ? (
<div className={'sk-spinner info small'} />
) : (
<Switch
onChange={toggleMuteFailedCloudBackupEmails}
checked={!isFailedCloudBackupEmailMuted}
/>
)}
</div>
</div>
</PreferencesSegment>
</PreferencesGroup>
);
};