Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: race condition where previous note actions may be displayed for current note #613

Merged
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions app/assets/javascripts/directives/views/actionsMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WebApplication } from '@/ui_models/application';
import { WebDirective } from './../../types';
import template from '%/directives/actions-menu.pug';
import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl';
import { SNItem, Action, SNActionsExtension, UuidString } from '@standardnotes/snjs';
import { SNItem, Action, SNActionsExtension, UuidString, CopyPayload } from '@standardnotes/snjs';
import { ActionResponse } from '@standardnotes/snjs';
import { ActionsExtensionMutator } from '@standardnotes/snjs';

Expand All @@ -27,7 +27,7 @@ type ActionsMenuState = {
extensions: SNActionsExtension[]
extensionsState: Record<UuidString, ExtensionState>
selectedActionId?: number
menu: {
menuItems: {
uuid: UuidString,
name: string,
loading: boolean,
Expand Down Expand Up @@ -57,33 +57,40 @@ class ActionsMenuCtrl extends PureViewCtrl<unknown, ActionsMenuState> implements
});
this.loadExtensions();
this.autorun(() => {
this.rebuildMenu({
this.rebuildMenuState({
hiddenExtensions: this.appState.actionsMenu.hiddenExtensions
});
});
}

/** @override */
getInitialState() {
const extensions = this.application.actionsManager!.getExtensions().sort((a, b) => {
const extensions = this.application.actionsManager.getExtensions().sort((a, b) => {
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
}).map((extension) => {
return new SNActionsExtension(CopyPayload(extension.payload, {
content: {
...extension.payload.safeContent,
actions: []
}
}));
});
const extensionsState: Record<UuidString, ExtensionState> = {};
extensions.map((extension) => {
extensionsState[extension.uuid] = {
loading: false,
loading: true,
error: false,
};
});
return {
extensions,
extensionsState,
hiddenExtensions: {},
menu: [],
menuItems: [],
};
}

rebuildMenu({
rebuildMenuState({
extensions = this.state.extensions,
extensionsState = this.state.extensionsState,
selectedActionId = this.state.selectedActionId,
Expand All @@ -93,7 +100,7 @@ class ActionsMenuCtrl extends PureViewCtrl<unknown, ActionsMenuState> implements
extensions,
extensionsState,
selectedActionId,
menu: extensions.map(extension => {
menuItems: extensions.map(extension => {
const state = extensionsState[extension.uuid];
const hidden = hiddenExtensions[extension.uuid];
return {
Expand Down Expand Up @@ -136,7 +143,7 @@ class ActionsMenuCtrl extends PureViewCtrl<unknown, ActionsMenuState> implements

async executeAction(action: Action, extensionUuid: UuidString) {
if (action.verb === 'nested') {
this.rebuildMenu({
this.rebuildMenuState({
selectedActionId: action.id
});
return;
Expand Down Expand Up @@ -220,7 +227,7 @@ class ActionsMenuCtrl extends PureViewCtrl<unknown, ActionsMenuState> implements
}
return ext;
});
await this.rebuildMenu({
await this.rebuildMenuState({
extensions
});
}
Expand All @@ -236,7 +243,7 @@ class ActionsMenuCtrl extends PureViewCtrl<unknown, ActionsMenuState> implements
}
return ext;
});
this.rebuildMenu({
this.rebuildMenuState({
extensions
});
}
Expand All @@ -248,15 +255,15 @@ class ActionsMenuCtrl extends PureViewCtrl<unknown, ActionsMenuState> implements
private setLoadingExtension(extensionUuid: UuidString, value = false) {
const { extensionsState } = this.state;
extensionsState[extensionUuid].loading = value;
this.rebuildMenu({
this.rebuildMenuState({
extensionsState
});
}

private setErrorExtension(extensionUuid: UuidString, value = false) {
const { extensionsState } = this.state;
extensionsState[extensionUuid].error = value;
this.rebuildMenu({
this.rebuildMenuState({
extensionsState
});
}
Expand Down
6 changes: 3 additions & 3 deletions app/assets/templates/directives/actions-menu.pug
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
target='blank'
)
menu-row(label="'Download Actions'")
div(ng-repeat='extension in self.state.menu track by extension.uuid')
div(ng-repeat='extension in self.state.menuItems track by extension.uuid')
.sk-menu-panel-header(
ng-click='self.toggleExtensionVisibility(extension.uuid); $event.stopPropagation();'
)
Expand All @@ -33,11 +33,11 @@
menu-row(
faded='true',
label="'No Actions Available'",
ng-if='!extension.actions.length'
ng-if='!extension.actions.length && !extension.hidden'
)
menu-row(
faded='true',
label="'Error loading actions'",
subtitle="'Please try again later.'"
ng-if='extension.error'
ng-if='extension.error && !extension.hidden'
)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"@reach/checkbox": "^0.13.2",
"@reach/dialog": "^0.13.0",
"@standardnotes/sncrypto-web": "1.2.10",
"@standardnotes/snjs": "2.7.21",
"@standardnotes/snjs": "2.7.23",
"mobx": "^6.3.2",
"mobx-react-lite": "^3.2.0",
"preact": "^10.5.12"
Expand Down