fix: render change editor menu only if it is open (#894)

This commit is contained in:
Aman Harwara
2022-02-23 20:55:26 +05:30
committed by GitHub
parent 209bd99fe5
commit 5df3e59604
4 changed files with 44 additions and 60 deletions

View File

@@ -49,8 +49,8 @@ export const ChangeEditorButton: FunctionComponent<Props> = observer(
const [currentEditor, setCurrentEditor] = useState<SNComponent>(); const [currentEditor, setCurrentEditor] = useState<SNComponent>();
useEffect(() => { useEffect(() => {
setEditorMenuGroups(createEditorMenuGroups(editors)); setEditorMenuGroups(createEditorMenuGroups(application, editors));
}, [editors]); }, [application, editors]);
useEffect(() => { useEffect(() => {
if (note) { if (note) {
@@ -121,15 +121,17 @@ export const ChangeEditorButton: FunctionComponent<Props> = observer(
className="sn-dropdown sn-dropdown--animated min-w-68 max-h-120 max-w-xs flex flex-col overflow-y-auto fixed" className="sn-dropdown sn-dropdown--animated min-w-68 max-h-120 max-w-xs flex flex-col overflow-y-auto fixed"
onBlur={closeOnBlur} onBlur={closeOnBlur}
> >
<ChangeEditorMenu {open && (
closeOnBlur={closeOnBlur} <ChangeEditorMenu
application={application} closeOnBlur={closeOnBlur}
isOpen={open} application={application}
currentEditor={currentEditor} isOpen={open}
setSelectedEditor={setCurrentEditor} currentEditor={currentEditor}
note={note} setSelectedEditor={setCurrentEditor}
groups={editorMenuGroups} note={note}
/> groups={editorMenuGroups}
/>
)}
</DisclosurePanel> </DisclosurePanel>
</Disclosure> </Disclosure>
</div> </div>

View File

@@ -39,7 +39,7 @@ type AccordionMenuGroup<T> = {
export type EditorMenuItem = { export type EditorMenuItem = {
name: string; name: string;
component?: SNComponent; component?: SNComponent;
isPremiumFeature?: boolean; isEntitled: boolean;
}; };
export type EditorMenuGroup = AccordionMenuGroup<EditorMenuItem>; export type EditorMenuGroup = AccordionMenuGroup<EditorMenuItem>;
@@ -162,8 +162,8 @@ export const ChangeEditorOption: FunctionComponent<ChangeEditorOptionProps> = ({
); );
useEffect(() => { useEffect(() => {
setEditorMenuGroups(createEditorMenuGroups(editors)); setEditorMenuGroups(createEditorMenuGroups(application, editors));
}, [editors]); }, [application, editors]);
useEffect(() => { useEffect(() => {
setSelectedEditor(application.componentManager.editorForNote(note)); setSelectedEditor(application.componentManager.editorForNote(note));
@@ -248,15 +248,17 @@ 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"
> >
<ChangeEditorMenu {changeEditorMenuOpen && (
application={application} <ChangeEditorMenu
closeOnBlur={closeOnBlur} application={application}
currentEditor={selectedEditor} closeOnBlur={closeOnBlur}
setSelectedEditor={setSelectedEditor} currentEditor={selectedEditor}
note={note} setSelectedEditor={setSelectedEditor}
groups={editorMenuGroups} note={note}
isOpen={changeEditorMenuVisible} groups={editorMenuGroups}
/> isOpen={changeEditorMenuVisible}
/>
)}
</DisclosurePanel> </DisclosurePanel>
</Disclosure> </Disclosure>
); );

View File

@@ -11,7 +11,6 @@ import { STRING_EDIT_LOCKED_ATTEMPT } from '@/strings';
import { WebApplication } from '@/ui_models/application'; import { WebApplication } from '@/ui_models/application';
import { import {
ComponentArea, ComponentArea,
FeatureStatus,
ItemMutator, ItemMutator,
NoteMutator, NoteMutator,
PrefKey, PrefKey,
@@ -48,28 +47,6 @@ export const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
}) => { }) => {
const premiumModal = usePremiumModal(); const premiumModal = usePremiumModal();
const isEntitledToEditor = useCallback(
(item: EditorMenuItem) => {
const isPlainEditor = !item.component;
if (item.isPremiumFeature) {
return false;
}
if (isPlainEditor) {
return true;
}
if (item.component) {
return (
application.getFeatureStatus(item.component.identifier) ===
FeatureStatus.Entitled
);
}
},
[application]
);
const isSelectedEditor = useCallback( const isSelectedEditor = useCallback(
(item: EditorMenuItem) => { (item: EditorMenuItem) => {
if (currentEditor) { if (currentEditor) {
@@ -163,6 +140,11 @@ export const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
}; };
const selectEditor = async (itemToBeSelected: EditorMenuItem) => { const selectEditor = async (itemToBeSelected: EditorMenuItem) => {
if (!itemToBeSelected.isEntitled) {
premiumModal.activate(itemToBeSelected.name);
return;
}
const areBothEditorsPlain = !currentEditor && !itemToBeSelected.component; const areBothEditorsPlain = !currentEditor && !itemToBeSelected.component;
if (areBothEditorsPlain) { if (areBothEditorsPlain) {
@@ -184,14 +166,6 @@ export const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
} }
} }
if (
itemToBeSelected.isPremiumFeature ||
!isEntitledToEditor(itemToBeSelected)
) {
premiumModal.activate(itemToBeSelected.name);
shouldSelectEditor = false;
}
if (shouldSelectEditor) { if (shouldSelectEditor) {
selectComponent(itemToBeSelected.component ?? null, note); selectComponent(itemToBeSelected.component ?? null, note);
} }
@@ -238,9 +212,7 @@ export const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
> >
<div className="flex flex-grow items-center justify-between"> <div className="flex flex-grow items-center justify-between">
{item.name} {item.name}
{(item.isPremiumFeature || !isEntitledToEditor(item)) && ( {!item.isEntitled && <Icon type="premium-feature" />}
<Icon type="premium-feature" />
)}
</div> </div>
</MenuItem> </MenuItem>
); );

View File

@@ -1,10 +1,11 @@
import { WebApplication } from '@/ui_models/application';
import { import {
ComponentArea, ComponentArea,
FeatureDescription, FeatureDescription,
GetFeatures, GetFeatures,
NoteType, NoteType,
} from '@standardnotes/features'; } from '@standardnotes/features';
import { ContentType, SNComponent } from '@standardnotes/snjs'; import { ContentType, FeatureStatus, SNComponent } from '@standardnotes/snjs';
import { EditorMenuItem, EditorMenuGroup } from '../ChangeEditorOption'; import { EditorMenuItem, EditorMenuGroup } from '../ChangeEditorOption';
export const PLAIN_EDITOR_NAME = 'Plain Editor'; export const PLAIN_EDITOR_NAME = 'Plain Editor';
@@ -31,11 +32,15 @@ const getEditorGroup = (
return 'others'; return 'others';
}; };
export const createEditorMenuGroups = (editors: SNComponent[]) => { export const createEditorMenuGroups = (
application: WebApplication,
editors: SNComponent[]
) => {
const editorItems: Record<EditorGroup, EditorMenuItem[]> = { const editorItems: Record<EditorGroup, EditorMenuItem[]> = {
plain: [ plain: [
{ {
name: PLAIN_EDITOR_NAME, name: PLAIN_EDITOR_NAME,
isEntitled: true,
}, },
], ],
'rich-text': [], 'rich-text': [],
@@ -61,7 +66,7 @@ export const createEditorMenuGroups = (editors: SNComponent[]) => {
) { ) {
editorItems[getEditorGroup(editorFeature)].push({ editorItems[getEditorGroup(editorFeature)].push({
name: editorFeature.name as string, name: editorFeature.name as string,
isPremiumFeature: true, isEntitled: false,
}); });
} }
}); });
@@ -70,6 +75,9 @@ export const createEditorMenuGroups = (editors: SNComponent[]) => {
const editorItem: EditorMenuItem = { const editorItem: EditorMenuItem = {
name: editor.name, name: editor.name,
component: editor, component: editor,
isEntitled:
application.getFeatureStatus(editor.identifier) ===
FeatureStatus.Entitled,
}; };
editorItems[getEditorGroup(editor.package_info)].push(editorItem); editorItems[getEditorGroup(editor.package_info)].push(editorItem);