import { FunctionalComponent } from 'preact' import { Subtitle } from '@/Components/Preferences/PreferencesComponents' import { DecoratedInput } from '@/Components/Input/DecoratedInput' import { Button } from '@/Components/Button/Button' import { useEffect, useState } from 'preact/hooks' import { WebApplication } from '@/UIModels/Application' import { AppState } from '@/UIModels/AppState' import { observer } from 'mobx-react-lite' import { STRING_REMOVE_OFFLINE_KEY_CONFIRMATION } from '@/Strings' import { ButtonType, ClientDisplayableError } from '@standardnotes/snjs' import { HorizontalSeparator } from '@/Components/Shared/HorizontalSeparator' interface IProps { application: WebApplication appState: AppState } export const OfflineSubscription: FunctionalComponent = observer(({ application }) => { const [activationCode, setActivationCode] = useState('') const [isSuccessfullyActivated, setIsSuccessfullyActivated] = useState(false) const [isSuccessfullyRemoved, setIsSuccessfullyRemoved] = useState(false) const [hasUserPreviouslyStoredCode, setHasUserPreviouslyStoredCode] = useState(false) useEffect(() => { if (application.features.hasOfflineRepo()) { setHasUserPreviouslyStoredCode(true) } }, [application]) const shouldShowOfflineSubscription = () => { return !application.hasAccount() || application.isThirdPartyHostUsed() || hasUserPreviouslyStoredCode } const handleSubscriptionCodeSubmit = async (event: Event) => { event.preventDefault() const result = await application.features.setOfflineFeaturesCode(activationCode) if (result instanceof ClientDisplayableError) { await application.alertService.alert(result.text) } else { setIsSuccessfullyActivated(true) setHasUserPreviouslyStoredCode(true) setIsSuccessfullyRemoved(false) } } const handleRemoveOfflineKey = async () => { await application.features.deleteOfflineFeatureRepo() setIsSuccessfullyActivated(false) setHasUserPreviouslyStoredCode(false) setActivationCode('') setIsSuccessfullyRemoved(true) } const handleRemoveClick = async () => { application.alertService .confirm( STRING_REMOVE_OFFLINE_KEY_CONFIRMATION, 'Remove offline key?', 'Remove Offline Key', ButtonType.Danger, 'Cancel', ) .then(async (shouldRemove: boolean) => { if (shouldRemove) { await handleRemoveOfflineKey() } }) .catch((err: string) => { application.alertService.alert(err).catch(console.error) }) } if (!shouldShowOfflineSubscription()) { return null } return ( <>
{!hasUserPreviouslyStoredCode && 'Activate'} Offline Subscription
{!hasUserPreviouslyStoredCode && ( setActivationCode(code)} placeholder={'Offline Subscription Code'} value={activationCode} disabled={isSuccessfullyActivated} className={'mb-3'} /> )}
{(isSuccessfullyActivated || isSuccessfullyRemoved) && (
Your offline subscription code has been successfully {isSuccessfullyActivated ? 'activated' : 'removed'} .
)} {hasUserPreviouslyStoredCode && (
) })