2020-02-19 02:41:49 +02:00
// Need to require this class instead of importing it
// to disable buggy type-checking, maybe because this
// class is undocumented.
2020-02-19 01:52:36 +02:00
import * as QuickActions from 'react-native-quick-actions' ;
2020-11-07 17:59:37 +02:00
import { _ } from '@joplin/lib/locale' ;
2020-06-02 22:13:15 +02:00
const { DeviceEventEmitter } = require ( 'react-native' ) ;
2021-01-22 19:41:11 +02:00
import Note from '@joplin/lib/models/Note' ;
2021-01-29 20:45:11 +02:00
import { reg } from '@joplin/lib/registry' ;
2020-02-19 01:52:36 +02:00
type TData = {
2020-11-12 21:29:22 +02:00
type : string ;
} ;
2020-02-19 01:52:36 +02:00
2023-06-30 11:30:29 +02:00
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
2020-02-19 01:52:36 +02:00
export default ( dispatch : Function , folderId : string ) = > {
const userInfo = { url : '' } ;
QuickActions . setShortcutItems ( [
2020-02-19 02:09:19 +02:00
{ type : 'New note' , title : _ ( 'New note' ) , icon : 'Compose' , userInfo } ,
{ type : 'New to-do' , title : _ ( 'New to-do' ) , icon : 'Add' , userInfo } ,
2020-02-19 01:52:36 +02:00
] ) ;
2020-05-13 12:39:16 +02:00
const handleQuickAction = ( data : TData ) = > {
if ( ! data ) return ;
2020-02-19 01:52:36 +02:00
// 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.
2020-02-19 02:09:19 +02:00
dispatch ( { type : 'NAV_BACK' } ) ;
2020-05-13 12:39:16 +02:00
dispatch ( { type : 'SIDE_MENU_CLOSE' } ) ;
2020-02-19 01:52:36 +02:00
2020-05-13 12:39:16 +02:00
const isTodo = data . type === 'New to-do' ? 1 : 0 ;
2020-02-19 01:52:36 +02:00
2021-01-22 19:41:11 +02:00
void Note . save ( {
2020-05-13 12:39:16 +02:00
parent_id : folderId ,
is_todo : isTodo ,
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line promise/prefer-await-to-then, @typescript-eslint/no-explicit-any -- Old code before rule was applied, Old code before rule was applied
2020-05-13 12:39:16 +02:00
} , { provisional : true } ) . then ( ( newNote : any ) = > {
2020-02-19 01:52:36 +02:00
dispatch ( {
type : 'NAV_GO' ,
2020-05-13 12:39:16 +02:00
noteId : newNote.id ,
2020-02-19 01:52:36 +02:00
folderId ,
routeName : 'Note' ,
} ) ;
2020-05-13 12:39:16 +02:00
} ) ;
} ;
DeviceEventEmitter . addListener ( 'quickActionShortcut' , handleQuickAction ) ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line promise/prefer-await-to-then, @typescript-eslint/no-explicit-any -- Old code before rule was applied, Old code before rule was applied
2020-05-13 12:39:16 +02:00
QuickActions . popInitialAction ( ) . then ( handleQuickAction ) . catch ( ( reason : any ) = > reg . logger ( ) . error ( reason ) ) ;
2020-02-19 01:52:36 +02:00
} ;
2020-05-13 12:39:16 +02:00