1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/ReactNativeClient/setUpQuickActions.ts
roman-r-m 17fd8ee504
Mobile: Fixes #2685: Fix quick actions (#2796)
* Fix quick actions

* Receive quick actions when the app is cold-launched

* Force side menu close before creating quick note

* Fix react warning: Can't perform a react state update on an unmounted component

The warning was:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
    in NoteScreenComponent (created by Connect(NoteScreenComponent))
    in Connect(NoteScreenComponent) (at app-nav.js:74)

* Fix auto title generation for quick notes.

The previous version created a new provisional note but then while handling NAV_BACK at reduxSharedMiddleware:35 the list of provisional note ids was cleared so when NoteScreenComponent was being mounted later the note was no longer considered provisional and Joplin would not generate the note title from its contents.

* Check for quick action data to be present before processing.

For some reason sometimes it gets called with null.
2020-05-13 11:39:16 +01:00

57 lines
1.9 KiB
TypeScript

// Need to require this class instead of importing it
// to disable buggy type-checking, maybe because this
// class is undocumented.
const { DeviceEventEmitter } = require('react-native');
import * as QuickActions from 'react-native-quick-actions';
const { _ } = require('lib/locale.js');
const Note = require('lib/models/Note.js');
const { reg } = require('lib/registry.js');
type TData = {
type: string
}
export default (dispatch: Function, folderId: string) => {
const userInfo = { url: '' };
QuickActions.setShortcutItems([
{ type: 'New note', title: _('New note'), icon: 'Compose', userInfo },
{ type: 'New to-do', title: _('New to-do'), icon: 'Add', userInfo },
]);
const handleQuickAction = (data: TData) => {
if (!data) return;
// This dispatch is to momentarily go back to reset state, similar to what
// happens in onJoplinLinkClick_(). Easier to just go back, then go to the
// note since the Note screen doesn't handle reloading a different note.
//
// This hack is necessary because otherwise you get this problem:
// The first time you create a note from the quick-action menu, it works
// perfectly. But if you do it again immediately later, it re-opens the
// page to that first note you made rather than creating an entirely new
// note. If you navigate around enough (which I think changes the redux
// state sufficiently or something), then it'll work again.
dispatch({ type: 'NAV_BACK' });
dispatch({ type: 'SIDE_MENU_CLOSE' });
const isTodo = data.type === 'New to-do' ? 1 : 0;
Note.save({
parent_id: folderId,
is_todo: isTodo,
}, { provisional: true }).then((newNote: any) => {
dispatch({
type: 'NAV_GO',
noteId: newNote.id,
folderId,
routeName: 'Note',
});
});
};
DeviceEventEmitter.addListener('quickActionShortcut', handleQuickAction);
QuickActions.popInitialAction().then(handleQuickAction).catch((reason: any) => reg.logger().error(reason));
};