From 545eb46b151818ef231ebb73528229e9c232ff5d Mon Sep 17 00:00:00 2001 From: Antonella Sgarlatta Date: Fri, 16 Apr 2021 18:07:38 -0300 Subject: [PATCH] feat: add listed actions --- src/lib/snjs_helper_hooks.ts | 19 +++++++++++++++++++ src/screens/Notes/NoteCell.tsx | 7 ++++--- src/screens/Notes/helpers.ts | 30 ++++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/lib/snjs_helper_hooks.ts b/src/lib/snjs_helper_hooks.ts index 032c379e..9ef84aa2 100644 --- a/src/lib/snjs_helper_hooks.ts +++ b/src/lib/snjs_helper_hooks.ts @@ -503,3 +503,22 @@ export const useProtectOrUnprotectNote = ( return [protectOrUnprotectNote]; }; + +export const useListedExtensions = (note: SNNote) => { + const LISTED_IDENTIFIER = 'org.standardnotes.listed'; + // Context + const application = React.useContext(ApplicationContext); + + const getListedExtensions = useCallback(() => { + return application?.actionsManager + .getExtensions() + .filter( + extension => extension.package_info?.identifier === LISTED_IDENTIFIER + ) + .map(extension => ({ + name: extension.name, + actions: extension.actionsWithContextForItem(note), + })); + }, [application, note]); + return [getListedExtensions]; +}; diff --git a/src/screens/Notes/NoteCell.tsx b/src/screens/Notes/NoteCell.tsx index 77a2681f..2811fc22 100644 --- a/src/screens/Notes/NoteCell.tsx +++ b/src/screens/Notes/NoteCell.tsx @@ -37,7 +37,7 @@ export const NoteCell = ({ }: Props) => { // State const [selected, setSelected] = useState(false); - const actionSections = useNoteActionSections(note); + const getActionSections = useNoteActionSections(note); // Ref const selectionTimeout = useRef(); @@ -89,8 +89,9 @@ export const NoteCell = ({ bottomSheetSections = [noteProtectedSection]; } else { bottomSheetSections = [ - actionSections[ActionSection.History], - actionSections[ActionSection.CommonActions], + ...getActionSections(ActionSection.History), + ...getActionSections(ActionSection.CommonActions), + ...getActionSections(ActionSection.Listed), ]; } onLongPressItem(bottomSheetTitle, bottomSheetSections); diff --git a/src/screens/Notes/helpers.ts b/src/screens/Notes/helpers.ts index f048c2d8..74b06f3b 100644 --- a/src/screens/Notes/helpers.ts +++ b/src/screens/Notes/helpers.ts @@ -4,13 +4,14 @@ import { Editor } from '@Lib/editor'; import { useChangeNote, useDeleteNoteWithPrivileges, + useListedExtensions, useProtectOrUnprotectNote, } from '@Lib/snjs_helper_hooks'; import { useNavigation } from '@react-navigation/native'; import { ApplicationContext } from '@Root/ApplicationContext'; import { SCREEN_NOTE_HISTORY } from '@Screens/screens'; import { SNNote } from '@standardnotes/snjs/dist/@types'; -import { useContext } from 'react'; +import { useCallback, useContext } from 'react'; import { Share } from 'react-native'; // eslint-disable-next-line no-shadow @@ -50,8 +51,24 @@ export const useNoteActionSections = (note: SNNote, editor?: Editor) => { }, editor ); + const [getListedExtensions] = useListedExtensions(note); const navigation = useNavigation(); + const getlistedSections = useCallback( + () => + (getListedExtensions() || []).map((extension, index) => ({ + key: `${extension.name}-${index}-section`, + actions: [ + { + text: `${extension.name} actions`, + key: `${extension.name}-${index}-section`, + iconType: IconType.Listed, + }, + ], + })), + [getListedExtensions] + ); + const sections: Record = { [ActionSection.History]: { key: ActionSection.History, @@ -163,5 +180,14 @@ export const useNoteActionSections = (note: SNNote, editor?: Editor) => { }); } - return sections; + const getActionSections = (sectionType: ActionSection) => { + switch (sectionType) { + case ActionSection.Listed: + return getlistedSections(); + default: + return [sections[sectionType]]; + } + }; + + return getActionSections; };