Skip to content

Commit

Permalink
fix: render change editor menu only if it is open (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara authored Feb 23, 2022
1 parent 209bd99 commit 5df3e59
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 60 deletions.
24 changes: 13 additions & 11 deletions app/assets/javascripts/components/ChangeEditorButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export const ChangeEditorButton: FunctionComponent<Props> = observer(
const [currentEditor, setCurrentEditor] = useState<SNComponent>();

useEffect(() => {
setEditorMenuGroups(createEditorMenuGroups(editors));
}, [editors]);
setEditorMenuGroups(createEditorMenuGroups(application, editors));
}, [application, editors]);

useEffect(() => {
if (note) {
Expand Down Expand Up @@ -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"
onBlur={closeOnBlur}
>
<ChangeEditorMenu
closeOnBlur={closeOnBlur}
application={application}
isOpen={open}
currentEditor={currentEditor}
setSelectedEditor={setCurrentEditor}
note={note}
groups={editorMenuGroups}
/>
{open && (
<ChangeEditorMenu
closeOnBlur={closeOnBlur}
application={application}
isOpen={open}
currentEditor={currentEditor}
setSelectedEditor={setCurrentEditor}
note={note}
groups={editorMenuGroups}
/>
)}
</DisclosurePanel>
</Disclosure>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type AccordionMenuGroup<T> = {
export type EditorMenuItem = {
name: string;
component?: SNComponent;
isPremiumFeature?: boolean;
isEntitled: boolean;
};

export type EditorMenuGroup = AccordionMenuGroup<EditorMenuItem>;
Expand Down Expand Up @@ -162,8 +162,8 @@ export const ChangeEditorOption: FunctionComponent<ChangeEditorOptionProps> = ({
);

useEffect(() => {
setEditorMenuGroups(createEditorMenuGroups(editors));
}, [editors]);
setEditorMenuGroups(createEditorMenuGroups(application, editors));
}, [application, editors]);

useEffect(() => {
setSelectedEditor(application.componentManager.editorForNote(note));
Expand Down Expand Up @@ -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"
>
<ChangeEditorMenu
application={application}
closeOnBlur={closeOnBlur}
currentEditor={selectedEditor}
setSelectedEditor={setSelectedEditor}
note={note}
groups={editorMenuGroups}
isOpen={changeEditorMenuVisible}
/>
{changeEditorMenuOpen && (
<ChangeEditorMenu
application={application}
closeOnBlur={closeOnBlur}
currentEditor={selectedEditor}
setSelectedEditor={setSelectedEditor}
note={note}
groups={editorMenuGroups}
isOpen={changeEditorMenuVisible}
/>
)}
</DisclosurePanel>
</Disclosure>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { STRING_EDIT_LOCKED_ATTEMPT } from '@/strings';
import { WebApplication } from '@/ui_models/application';
import {
ComponentArea,
FeatureStatus,
ItemMutator,
NoteMutator,
PrefKey,
Expand Down Expand Up @@ -48,28 +47,6 @@ export const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
}) => {
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(
(item: EditorMenuItem) => {
if (currentEditor) {
Expand Down Expand Up @@ -163,6 +140,11 @@ export const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
};

const selectEditor = async (itemToBeSelected: EditorMenuItem) => {
if (!itemToBeSelected.isEntitled) {
premiumModal.activate(itemToBeSelected.name);
return;
}

const areBothEditorsPlain = !currentEditor && !itemToBeSelected.component;

if (areBothEditorsPlain) {
Expand All @@ -184,14 +166,6 @@ export const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
}
}

if (
itemToBeSelected.isPremiumFeature ||
!isEntitledToEditor(itemToBeSelected)
) {
premiumModal.activate(itemToBeSelected.name);
shouldSelectEditor = false;
}

if (shouldSelectEditor) {
selectComponent(itemToBeSelected.component ?? null, note);
}
Expand Down Expand Up @@ -238,9 +212,7 @@ export const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
>
<div className="flex flex-grow items-center justify-between">
{item.name}
{(item.isPremiumFeature || !isEntitledToEditor(item)) && (
<Icon type="premium-feature" />
)}
{!item.isEntitled && <Icon type="premium-feature" />}
</div>
</MenuItem>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { WebApplication } from '@/ui_models/application';
import {
ComponentArea,
FeatureDescription,
GetFeatures,
NoteType,
} from '@standardnotes/features';
import { ContentType, SNComponent } from '@standardnotes/snjs';
import { ContentType, FeatureStatus, SNComponent } from '@standardnotes/snjs';
import { EditorMenuItem, EditorMenuGroup } from '../ChangeEditorOption';

export const PLAIN_EDITOR_NAME = 'Plain Editor';
Expand All @@ -31,11 +32,15 @@ const getEditorGroup = (
return 'others';
};

export const createEditorMenuGroups = (editors: SNComponent[]) => {
export const createEditorMenuGroups = (
application: WebApplication,
editors: SNComponent[]
) => {
const editorItems: Record<EditorGroup, EditorMenuItem[]> = {
plain: [
{
name: PLAIN_EDITOR_NAME,
isEntitled: true,
},
],
'rich-text': [],
Expand All @@ -61,7 +66,7 @@ export const createEditorMenuGroups = (editors: SNComponent[]) => {
) {
editorItems[getEditorGroup(editorFeature)].push({
name: editorFeature.name as string,
isPremiumFeature: true,
isEntitled: false,
});
}
});
Expand All @@ -70,6 +75,9 @@ export const createEditorMenuGroups = (editors: SNComponent[]) => {
const editorItem: EditorMenuItem = {
name: editor.name,
component: editor,
isEntitled:
application.getFeatureStatus(editor.identifier) ===
FeatureStatus.Entitled,
};

editorItems[getEditorGroup(editor.package_info)].push(editorItem);
Expand Down

0 comments on commit 5df3e59

Please sign in to comment.