feat: show all themes and premium icon if not entitled (#854)

This commit is contained in:
Aman Harwara
2022-02-08 20:25:12 +05:30
committed by GitHub
parent 7f5f0d9d17
commit 139853a491
7 changed files with 405 additions and 340 deletions

View File

@@ -23,6 +23,7 @@ import { NotesContextMenu } from '@/components/NotesContextMenu';
import { PurchaseFlowWrapper } from '@/purchaseFlow/PurchaseFlowWrapper'; import { PurchaseFlowWrapper } from '@/purchaseFlow/PurchaseFlowWrapper';
import { render } from 'preact'; import { render } from 'preact';
import { PermissionsModal } from './PermissionsModal'; import { PermissionsModal } from './PermissionsModal';
import { PremiumModalProvider } from './Premium';
type Props = { type Props = {
application: WebApplication; application: WebApplication;
@@ -196,6 +197,7 @@ export class ApplicationView extends PureComponent<Props, State> {
render() { render() {
return ( return (
<PremiumModalProvider state={this.appState.features}>
<div className={this.platformString + ' main-ui-view sn-component'}> <div className={this.platformString + ' main-ui-view sn-component'}>
{!this.state.needsUnlock && this.state.launched && ( {!this.state.needsUnlock && this.state.launched && (
<div <div
@@ -253,6 +255,7 @@ export class ApplicationView extends PureComponent<Props, State> {
appState={this.appState} appState={this.appState}
/> />
</div> </div>
</PremiumModalProvider>
); );
} }
} }

View File

@@ -6,7 +6,6 @@ import { ApplicationEvent, PrefKey } from '@standardnotes/snjs';
import { observer } from 'mobx-react-lite'; import { observer } from 'mobx-react-lite';
import { FunctionComponent } from 'preact'; import { FunctionComponent } from 'preact';
import { useCallback, useEffect, useMemo, useState } from 'preact/hooks'; import { useCallback, useEffect, useMemo, useState } from 'preact/hooks';
import { PremiumModalProvider } from './Premium';
import { import {
PanelSide, PanelSide,
ResizeFinishCallback, ResizeFinishCallback,
@@ -51,7 +50,6 @@ export const Navigation: FunctionComponent<Props> = observer(
}, [appState]); }, [appState]);
return ( return (
<PremiumModalProvider state={appState.features}>
<div <div
id="navigation" id="navigation"
className="sn-component section app-column app-column-first" className="sn-component section app-column app-column-first"
@@ -86,7 +84,6 @@ export const Navigation: FunctionComponent<Props> = observer(
/> />
)} )}
</div> </div>
</PremiumModalProvider>
); );
} }
); );

View File

@@ -29,7 +29,6 @@ import {
import { FunctionComponent } from 'preact'; import { FunctionComponent } from 'preact';
import { useEffect, useRef, useState } from 'preact/hooks'; import { useEffect, useRef, useState } from 'preact/hooks';
import { Icon } from '../Icon'; import { Icon } from '../Icon';
import { PremiumModalProvider } from '../Premium';
import { createEditorMenuGroups } from './changeEditor/createEditorMenuGroups'; import { createEditorMenuGroups } from './changeEditor/createEditorMenuGroups';
import { EditorAccordionMenu } from './changeEditor/EditorAccordionMenu'; import { EditorAccordionMenu } from './changeEditor/EditorAccordionMenu';
@@ -273,7 +272,6 @@ export const ChangeEditorOption: FunctionComponent<ChangeEditorOptionProps> = ({
}} }}
className="sn-dropdown flex flex-col max-h-120 min-w-68 fixed overflow-y-auto" className="sn-dropdown flex flex-col max-h-120 min-w-68 fixed overflow-y-auto"
> >
<PremiumModalProvider state={appState.features}>
<EditorAccordionMenu <EditorAccordionMenu
application={application} application={application}
closeOnBlur={closeOnBlur} closeOnBlur={closeOnBlur}
@@ -282,7 +280,6 @@ export const ChangeEditorOption: FunctionComponent<ChangeEditorOptionProps> = ({
isOpen={changeEditorMenuOpen} isOpen={changeEditorMenuOpen}
selectComponent={selectComponent} selectComponent={selectComponent}
/> />
</PremiumModalProvider>
</DisclosurePanel> </DisclosurePanel>
</Disclosure> </Disclosure>
); );

View File

@@ -1,10 +1,10 @@
import { WebApplication } from '@/ui_models/application'; import { WebApplication } from '@/ui_models/application';
import { FeatureStatus, FeatureIdentifier } from '@standardnotes/snjs'; import { FeatureStatus, FeatureIdentifier } from '@standardnotes/snjs';
import { FunctionComponent } from 'preact'; import { FunctionComponent } from 'preact';
import { useCallback, useState } from 'preact/hooks'; import { useCallback } from 'preact/hooks';
import { JSXInternal } from 'preact/src/jsx'; import { JSXInternal } from 'preact/src/jsx';
import { Icon } from '../Icon'; import { Icon } from '../Icon';
import { PremiumFeaturesModal } from '../PremiumFeaturesModal'; import { usePremiumModal } from '../Premium';
import { Switch } from '../Switch'; import { Switch } from '../Switch';
type Props = { type Props = {
@@ -20,7 +20,7 @@ export const FocusModeSwitch: FunctionComponent<Props> = ({
onClose, onClose,
isEnabled, isEnabled,
}) => { }) => {
const [showUpgradeModal, setShowUpgradeModal] = useState(false); const premiumModal = usePremiumModal();
const isEntitled = const isEntitled =
application.getFeatureStatus(FeatureIdentifier.FocusMode) === application.getFeatureStatus(FeatureIdentifier.FocusMode) ===
FeatureStatus.Entitled; FeatureStatus.Entitled;
@@ -33,10 +33,10 @@ export const FocusModeSwitch: FunctionComponent<Props> = ({
onToggle(!isEnabled); onToggle(!isEnabled);
onClose(); onClose();
} else { } else {
setShowUpgradeModal(true); premiumModal.activate('Focused Writing');
} }
}, },
[isEntitled, isEnabled, onToggle, setShowUpgradeModal, onClose] [isEntitled, onToggle, isEnabled, onClose, premiumModal]
); );
return ( return (
@@ -57,11 +57,6 @@ export const FocusModeSwitch: FunctionComponent<Props> = ({
</div> </div>
)} )}
</button> </button>
<PremiumFeaturesModal
showModal={showUpgradeModal}
featureName="Focus Mode"
onClose={() => setShowUpgradeModal(false)}
/>
</> </>
); );
}; };

View File

@@ -9,6 +9,7 @@ import {
ComponentArea, ComponentArea,
ContentType, ContentType,
FeatureIdentifier, FeatureIdentifier,
Features,
SNComponent, SNComponent,
SNTheme, SNTheme,
} from '@standardnotes/snjs'; } from '@standardnotes/snjs';
@@ -31,6 +32,12 @@ const focusModeAnimationDuration = 1255;
const MENU_CLASSNAME = const MENU_CLASSNAME =
'sn-menu-border sn-dropdown min-w-80 max-h-120 max-w-xs flex flex-col py-2 overflow-y-auto'; 'sn-menu-border sn-dropdown min-w-80 max-h-120 max-w-xs flex flex-col py-2 overflow-y-auto';
export type ThemeItem = {
name: string;
identifier: FeatureIdentifier;
component?: SNTheme;
};
type MenuProps = { type MenuProps = {
appState: AppState; appState: AppState;
application: WebApplication; application: WebApplication;
@@ -72,7 +79,7 @@ export const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
focusModeEnabled, focusModeEnabled,
setFocusModeEnabled, setFocusModeEnabled,
} = appState.quickSettingsMenu; } = appState.quickSettingsMenu;
const [themes, setThemes] = useState<SNTheme[]>([]); const [themes, setThemes] = useState<ThemeItem[]>([]);
const [toggleableComponents, setToggleableComponents] = useState< const [toggleableComponents, setToggleableComponents] = useState<
SNComponent[] SNComponent[]
>([]); >([]);
@@ -96,12 +103,39 @@ export const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
}, [focusModeEnabled]); }, [focusModeEnabled]);
const reloadThemes = useCallback(() => { const reloadThemes = useCallback(() => {
const themes = application.getDisplayableItems( const themes = (
ContentType.Theme application.getDisplayableItems(ContentType.Theme) as SNTheme[]
) as SNTheme[]; )
setThemes(themes.sort(sortThemes)); .sort(sortThemes)
.map((item) => {
return {
name: item.name,
identifier: item.identifier,
component: item,
};
}) as ThemeItem[];
Features.filter(
(feature) =>
feature.content_type === ContentType.Theme && !feature.layerable
).forEach((theme) => {
if (
themes.findIndex((item) => item.identifier === theme.identifier) ===
-1
) {
themes.push({
name: theme.name as string,
identifier: theme.identifier,
});
}
});
setThemes(themes);
setDefaultThemeOn( setDefaultThemeOn(
!themes.find((theme) => theme.active && !theme.isLayerable()) !themes
.map((item) => item?.component)
.find((theme) => theme?.active && !theme.isLayerable())
); );
}, [application]); }, [application]);
@@ -117,6 +151,12 @@ export const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
setToggleableComponents(toggleableComponents); setToggleableComponents(toggleableComponents);
}, [application]); }, [application]);
useEffect(() => {
if (!themes.length) {
reloadThemes();
}
}, [reloadThemes, themes.length]);
useEffect(() => { useEffect(() => {
const cleanupItemStream = application.streamItems( const cleanupItemStream = application.streamItems(
ContentType.Theme, ContentType.Theme,
@@ -153,10 +193,7 @@ export const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
prefsButtonRef.current?.focus(); prefsButtonRef.current?.focus();
}, []); }, []);
const [closeOnBlur] = useCloseOnBlur( const [closeOnBlur] = useCloseOnBlur(themesMenuRef, setThemesMenuOpen);
themesMenuRef as any,
setThemesMenuOpen
);
const toggleThemesMenu = () => { const toggleThemesMenu = () => {
if (!themesMenuOpen && themesButtonRef.current) { if (!themesMenuOpen && themesButtonRef.current) {
@@ -224,9 +261,9 @@ export const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
}; };
const toggleDefaultTheme = () => { const toggleDefaultTheme = () => {
const activeTheme = themes.find( const activeTheme = themes
(theme) => theme.active && !theme.isLayerable() .map((item) => item.component)
); .find((theme) => theme?.active && !theme.isLayerable());
if (activeTheme) application.toggleTheme(activeTheme); if (activeTheme) application.toggleTheme(activeTheme);
}; };
@@ -244,7 +281,6 @@ export const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
<div className="px-3 mt-1 mb-2 font-semibold color-text uppercase"> <div className="px-3 mt-1 mb-2 font-semibold color-text uppercase">
Quick Settings Quick Settings
</div> </div>
{themes && themes.length ? (
<Disclosure open={themesMenuOpen} onChange={toggleThemesMenu}> <Disclosure open={themesMenuOpen} onChange={toggleThemesMenu}>
<DisclosureButton <DisclosureButton
onKeyDown={handleBtnKeyDown} onKeyDown={handleBtnKeyDown}
@@ -285,15 +321,14 @@ export const QuickSettingsMenu: FunctionComponent<MenuProps> = observer(
</button> </button>
{themes.map((theme) => ( {themes.map((theme) => (
<ThemesMenuButton <ThemesMenuButton
theme={theme} item={theme}
application={application} application={application}
key={theme.uuid} key={theme.component?.uuid ?? theme.identifier}
onBlur={closeOnBlur} onBlur={closeOnBlur}
/> />
))} ))}
</DisclosurePanel> </DisclosurePanel>
</Disclosure> </Disclosure>
) : null}
{toggleableComponents.map((component) => ( {toggleableComponents.map((component) => (
<button <button
className="sn-dropdown-item justify-between focus:bg-info-backdrop focus:shadow-none" className="sn-dropdown-item justify-between focus:bg-info-backdrop focus:shadow-none"

View File

@@ -1,58 +1,94 @@
import { WebApplication } from '@/ui_models/application'; import { WebApplication } from '@/ui_models/application';
import { SNTheme } from '@standardnotes/snjs'; import { FeatureStatus } from '@standardnotes/snjs';
import { FunctionComponent } from 'preact'; import { FunctionComponent } from 'preact';
import { useMemo } from 'preact/hooks';
import { JSXInternal } from 'preact/src/jsx'; import { JSXInternal } from 'preact/src/jsx';
import { Icon } from '../Icon';
import { usePremiumModal } from '../Premium';
import { Switch } from '../Switch'; import { Switch } from '../Switch';
import { ThemeItem } from './QuickSettingsMenu';
type Props = { type Props = {
theme: SNTheme; item: ThemeItem;
application: WebApplication; application: WebApplication;
onBlur: (event: { relatedTarget: EventTarget | null }) => void; onBlur: (event: { relatedTarget: EventTarget | null }) => void;
}; };
export const ThemesMenuButton: FunctionComponent<Props> = ({ export const ThemesMenuButton: FunctionComponent<Props> = ({
application, application,
theme, item,
onBlur, onBlur,
}) => { }) => {
const premiumModal = usePremiumModal();
const isThirdPartyTheme = useMemo(
() => application.isThirdPartyFeature(item.identifier),
[application, item.identifier]
);
const isEntitledToTheme = useMemo(
() =>
application.getFeatureStatus(item.identifier) === FeatureStatus.Entitled,
[application, item.identifier]
);
const canActivateTheme = useMemo(
() => isEntitledToTheme || isThirdPartyTheme,
[isEntitledToTheme, isThirdPartyTheme]
);
const toggleTheme: JSXInternal.MouseEventHandler<HTMLButtonElement> = (e) => { const toggleTheme: JSXInternal.MouseEventHandler<HTMLButtonElement> = (e) => {
e.preventDefault(); e.preventDefault();
if (theme.isLayerable() || !theme.active) {
application.toggleTheme(theme); if (item.component && canActivateTheme) {
const themeIsLayerableOrNotActive =
item.component.isLayerable() || !item.component.active;
if (themeIsLayerableOrNotActive) {
application.toggleTheme(item.component);
}
} else {
premiumModal.activate(`${item.name} theme`);
} }
}; };
return ( return (
<button <button
className={`sn-dropdown-item focus:bg-info-backdrop focus:shadow-none ${ className={`sn-dropdown-item focus:bg-info-backdrop focus:shadow-none justify-between`}
theme.isLayerable() ? `justify-start` : `justify-between`
}`}
onClick={toggleTheme} onClick={toggleTheme}
onBlur={onBlur} onBlur={onBlur}
> >
{theme.isLayerable() ? ( {item.component?.isLayerable() ? (
<> <>
<Switch className="px-0 mr-2" checked={theme.active} /> <div className="flex items-center">
{theme.package_info.name} <Switch className="px-0 mr-2" checked={item.component?.active} />
{item.name}
</div>
{!canActivateTheme && <Icon type="premium-feature" />}
</> </>
) : ( ) : (
<> <>
<div className="flex items-center"> <div className="flex items-center">
<div <div
className={`pseudo-radio-btn ${ className={`pseudo-radio-btn ${
theme.active ? 'pseudo-radio-btn--checked' : '' item.component?.active ? 'pseudo-radio-btn--checked' : ''
} mr-2`} } mr-2`}
></div> ></div>
<span className={theme.active ? 'font-semibold' : undefined}> <span
{theme.package_info.name} className={item.component?.active ? 'font-semibold' : undefined}
>
{item.name}
</span> </span>
</div> </div>
{item.component && canActivateTheme ? (
<div <div
className="w-5 h-5 rounded-full" className="w-5 h-5 rounded-full"
style={{ style={{
backgroundColor: theme.package_info?.dock_icon?.background_color, backgroundColor:
item.component.package_info?.dock_icon?.background_color,
}} }}
></div> ></div>
) : (
<Icon type="premium-feature" />
)}
</> </>
)} )}
</button> </button>

View File

@@ -1,5 +1,5 @@
import { Dropdown, DropdownItem } from '@/components/Dropdown'; import { Dropdown, DropdownItem } from '@/components/Dropdown';
import { PremiumModalProvider, usePremiumModal } from '@/components/Premium'; import { usePremiumModal } from '@/components/Premium';
import { sortThemes } from '@/components/QuickSettingsMenu/QuickSettingsMenu'; import { sortThemes } from '@/components/QuickSettingsMenu/QuickSettingsMenu';
import { HorizontalSeparator } from '@/components/shared/HorizontalSeparator'; import { HorizontalSeparator } from '@/components/shared/HorizontalSeparator';
import { Switch } from '@/components/Switch'; import { Switch } from '@/components/Switch';
@@ -28,7 +28,8 @@ type Props = {
application: WebApplication; application: WebApplication;
}; };
const AppearancePane: FunctionComponent<Props> = observer(({ application }) => { export const Appearance: FunctionComponent<Props> = observer(
({ application }) => {
const premiumModal = usePremiumModal(); const premiumModal = usePremiumModal();
const isEntitledToMidnightTheme = const isEntitledToMidnightTheme =
application.getFeatureStatus(FeatureIdentifier.MidnightTheme) === application.getFeatureStatus(FeatureIdentifier.MidnightTheme) ===
@@ -46,12 +47,17 @@ const AppearancePane: FunctionComponent<Props> = observer(({ application }) => {
() => () =>
application.getPreference( application.getPreference(
PrefKey.AutoDarkThemeIdentifier, PrefKey.AutoDarkThemeIdentifier,
isEntitledToMidnightTheme ? FeatureIdentifier.MidnightTheme : 'Default' isEntitledToMidnightTheme
? FeatureIdentifier.MidnightTheme
: 'Default'
) as string ) as string
); );
const [useDeviceSettings, setUseDeviceSettings] = useState( const [useDeviceSettings, setUseDeviceSettings] = useState(
() => () =>
application.getPreference(PrefKey.UseSystemColorScheme, false) as boolean application.getPreference(
PrefKey.UseSystemColorScheme,
false
) as boolean
); );
useEffect(() => { useEffect(() => {
@@ -92,7 +98,10 @@ const AppearancePane: FunctionComponent<Props> = observer(({ application }) => {
}, [application]); }, [application]);
const toggleUseDeviceSettings = () => { const toggleUseDeviceSettings = () => {
application.setPreference(PrefKey.UseSystemColorScheme, !useDeviceSettings); application.setPreference(
PrefKey.UseSystemColorScheme,
!useDeviceSettings
);
if (!application.getPreference(PrefKey.AutoLightThemeIdentifier)) { if (!application.getPreference(PrefKey.AutoLightThemeIdentifier)) {
application.setPreference( application.setPreference(
PrefKey.AutoLightThemeIdentifier, PrefKey.AutoLightThemeIdentifier,
@@ -186,12 +195,5 @@ const AppearancePane: FunctionComponent<Props> = observer(({ application }) => {
</PreferencesGroup> </PreferencesGroup>
</PreferencesPane> </PreferencesPane>
); );
}); }
export const Appearance: FunctionComponent<Props> = observer(
({ application }) => (
<PremiumModalProvider state={application.getAppState().features}>
<AppearancePane application={application} />
</PremiumModalProvider>
)
); );