import React, {useEffect, useState} from 'react'; import { ActivityIndicator, Clipboard, Dimensions, Keyboard, ScrollView, TouchableOpacity, View, } from 'react-native'; import Share from 'react-native-share'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import {notesnook} from '../../../e2e/test.ids'; import {useTracked} from '../../provider'; import {Actions} from '../../provider/Actions'; import {DDS} from '../../services/DeviceDetection'; import { eSendEvent, openVault, sendNoteEditedEvent, ToastEvent, } from '../../services/EventManager'; import PremiumService from '../../services/PremiumService'; import Sync from '../../services/Sync'; import {editing, toTXT} from '../../utils'; import { ACCENT, COLOR_SCHEME, COLOR_SCHEME_DARK, COLOR_SCHEME_LIGHT, setColorScheme, } from '../../utils/Colors'; import {db} from '../../utils/DB'; import { eOnNewTopicAdded, eOpenMoveNoteDialog, refreshNotesPage, } from '../../utils/Events'; import {deleteItems} from '../../utils/functions'; import {MMKV} from '../../utils/mmkv'; import {opacity, pv, SIZE} from '../../utils/SizeUtils'; import {timeConverter} from '../../utils/TimeUtils'; import {PremiumTag} from '../Premium/PremiumTag'; import {PressableButton} from '../PressableButton'; import {Toast} from '../Toast'; import Heading from '../Typography/Heading'; import Paragraph from '../Typography/Paragraph'; import {ActionSheetColorsSection} from './ActionSheetColorsSection'; import {ActionSheetTagsSection} from './ActionSheetTagsSection'; const w = Dimensions.get('window').width; export const ActionSheetComponent = ({ close = () => {}, item, hasColors = false, hasTags = false, rowItems = [], columnItems = [], getRef, }) => { const [state, dispatch] = useTracked(); const {colors, premiumUser, user} = state; const [refreshing, setRefreshing] = useState(false); const [isPinnedToMenu, setIsPinnedToMenu] = useState(false); const [note, setNote] = useState(item); function changeColorScheme(colors = COLOR_SCHEME, accent = ACCENT) { let newColors = setColorScheme(colors, accent); dispatch({type: Actions.THEME, colors: newColors}); } useEffect(() => { if (item.dateCreated !== null) { setNote({...item}); if (item.type !== note) { setIsPinnedToMenu(db.settings.isPinned(note.id)); } } }, [item]); const localRefresh = (type, nodispatch = false) => { if (!note || !note.id) return; let toAdd; switch (type) { case 'note': { toAdd = db.notes.note(note.id); if (toAdd) { toAdd = toAdd.data; } else { setTimeout(() => { toAdd = db.notes.note(note.id); if (toAdd) { toAdd = toAdd.data; } }, 500); } break; } case 'notebook': { toAdd = db.notebooks.notebook(note.id); if (toAdd) { toAdd = toAdd.data; } else { setTimeout(() => { toAdd = db.notebooks.notebook(note.id); if (toAdd) { toAdd = toAdd.data; } }, 500); } break; } case 'topic': { toAdd = db.notebooks.notebook(note.notebookId).topics.topic(note.title); break; } } if (!toAdd || !toAdd.id) return; if (!nodispatch) { dispatch({type: type}); if (type === 'note') { eSendEvent(refreshNotesPage); } dispatch({type: Actions.FAVORITES}); } setNote({...toAdd}); }; const rowItemsData = [ { name: 'Add to', icon: 'book-outline', func: () => { dispatch({type: Actions.CLEAR_SELECTION}); dispatch({type: Actions.SELECTED_ITEMS, item: note}); close(); setTimeout(() => { eSendEvent(eOpenMoveNoteDialog, note); }, 400); }, }, { name: 'Share', icon: 'share-variant', func: async () => { if (note.locked) { openVault({ item: item, novault: true, locked: true, share: true, }); } else { let text = await db.notes.note(note.id).export('txt'); let m = `${note.title}\n \n ${text}`; Share.open({ title: 'Share note to', failOnCancel: false, message: m, }); } }, }, { name: 'Export', icon: 'export', func: () => { close('export'); }, }, { name: 'Delete', icon: 'delete', func: async () => { try { await deleteItems(note); } catch (e) { console.log(e); } //close(); }, }, { name: 'Edit Notebook', icon: 'square-edit-outline', func: () => { close('notebook'); }, }, { name: 'Edit Topic', icon: 'square-edit-outline', func: () => { close('topic'); }, }, { name: 'Copy', icon: 'content-copy', func: async () => { if (note.locked) { openVault({ copyNote: true, novault: true, locked: true, item: note, }); } else { let text = await db.notes.note(note.id).content(); text = toTXT(text); text = `${note.title}\n \n ${text}`; Clipboard.setString(text); ToastEvent.show('Note copied to clipboard', 'success', 'local'); } }, }, { name: 'Restore', icon: 'delete-restore', func: async () => { await db.trash.restore(note.id); dispatch({type: Actions.TRASH}); dispatch({type: note.itemType}); dispatch({type: Actions.FAVORITES}); eSendEvent(refreshNotesPage); ToastEvent.show( item.type === 'note' ? 'Note restored' : 'Notebook restored', 'success', ); close(); }, }, { name: 'Remove', icon: 'delete', func: () => { close('permanant_delete'); }, }, ]; const columnItemsData = [ { name: 'Dark Mode', icon: 'theme-light-dark', func: () => { if (!colors.night) { MMKV.setStringAsync('theme', JSON.stringify({night: true})); changeColorScheme(COLOR_SCHEME_DARK); } else { MMKV.setStringAsync('theme', JSON.stringify({night: false})); changeColorScheme(COLOR_SCHEME_LIGHT); } }, switch: true, on: colors.night ? true : false, close: false, nopremium: true, id: notesnook.ids.dialogs.actionsheet.night, }, { name: 'Pin', icon: 'pin', func: async () => { if (!note.id) return; if (note.type === 'note') { if (db.notes.pinned.length === 3 && !note.pinned) { ToastEvent.show( 'You cannot pin more than 3 notes', 'error', 'local', ); return; } await db.notes.note(note.id).pin(); } else { if (db.notebooks.pinned.length === 3 && !note.pinned) { ToastEvent.show( 'You cannot pin more than 3 notes', 'error', 'local', ); return; } await db.notebooks.notebook(note.id).pin(); } localRefresh(item.type); }, close: false, check: true, on: note.pinned, id: notesnook.ids.dialogs.actionsheet.pin, }, { name: 'Favorite', icon: 'star', func: async () => { if (!note.id) return; if (note.type === 'note') { await db.notes.note(note.id).favorite(); } else { await db.notebooks.notebook(note.id).favorite(); } dispatch({type: Actions.FAVORITES}); sendNoteEditedEvent(note.id, false, true); localRefresh(item.type, true); }, close: false, check: true, on: note.favorite, id: notesnook.ids.dialogs.actionsheet.favorite, color: 'orange', }, { name: isPinnedToMenu ? 'Unpin from Menu' : 'Pin to Menu', icon: 'tag-outline', func: async () => { try { if (isPinnedToMenu) { await db.settings.unpin(note.id); } else { if (item.type === 'topic') { await db.settings.pin(note.type, { id: note.id, notebookId: note.notebookId, }); } else { await db.settings.pin(note.type, {id: note.id}); } } setIsPinnedToMenu(db.settings.isPinned(note.id)); dispatch({type: Actions.MENU_PINS}); } catch (e) {} }, close: false, check: true, on: isPinnedToMenu, id: notesnook.ids.dialogs.actionsheet.pinMenu, }, ]; const _renderRowItem = (rowItem) => rowItems.includes(rowItem.name) ? ( {rowItem.name} ) : null; const _renderColumnItem = (item) => (note.id && columnItems.includes(item.name)) || (item.name === 'Dark Mode' && columnItems.includes(item.name)) ? ( { item.func(); }} style={{ width: '100%', alignSelf: 'center', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'flex-end', paddingHorizontal: 12, paddingVertical: pv, }}> {item.name} {item.switch ? ( ) : undefined} {item.nopremium ? null : } {item.check ? ( ) : null} ) : null; const onPressVaultButton = async () => { if (!note.id) return; if (note.locked) { close('unlock'); } else { db.vault .add(note.id) .then((r) => { console.log(r); sendNoteEditedEvent(note.id, false, true); if (note.locked && PremiumService.get()) { close(); } }) .catch(async (e) => { console.log(e); switch (e.message) { case db.vault.ERRORS.noVault: close('novault'); break; case db.vault.ERRORS.vaultLocked: close('locked'); break; case db.vault.ERRORS.wrongPassword: close(); break; } }); } }; const onScrollEnd = () => { getRef().current?.handleChildScrollEnd(); }; return ( { if (!item.dateDeleted) { localRefresh(item.type, true); } }} style={{ paddingBottom: 30, backgroundColor: colors.bg, paddingHorizontal: 0, borderBottomRightRadius: DDS.isLargeTablet() ? 10 : 1, borderBottomLeftRadius: DDS.isLargeTablet() ? 10 : 1, }}> { Keyboard.dismiss(); }} /> {!note || !note.id ? ( Start writing to save your note. ) : ( {note?.title.replace('\n', '')} {note.type === 'notebook' && note.description ? note.description : null} {note.type === 'note' ? note.headline[item.headline.length - 1] === '\n' ? note.headline.slice(0, note.headline.length - 1) : note.headline : null} {note.type === 'note' ? 'Last edited on ' + timeConverter(note.dateEdited) : null} {note.type !== 'note' && !note.dateDeleted ? 'Created on ' + timeConverter(note.dateCreated) : null} {note.dateDeleted ? 'Deleted on ' + timeConverter(note.dateDeleted) : null} {note.type === 'notebook' && ( {note && note.topics && note.topics.length > 0 ? note.topics .slice() .sort((a, b) => a.dateEdited - b.dateEdited) .slice(0, 6) .map((topic) => ( {topic.title.length > 16 ? topic.title.slice(0, 16) + '...' : topic.title} )) : null} )} {note.type !== 'note' || refreshing ? null : ( await Sync.run('local')} style={{ borderColor: colors.accent, paddingHorizontal: 5, borderRadius: 2.5, justifyContent: 'center', alignItems: 'center', marginTop: 5, borderWidth: 1, height: 18, }}> {user && user.lastSynced > note.dateEdited ? 'Synced' : 'Sync Now'} )} {refreshing ? ( ) : null} )} {note.id || note.dateCreated ? ( {rowItemsData.map(_renderRowItem)} ) : null} {note.type === 'note' ? ( {note.locked ? 'Remove from Vault' : 'Add to Vault'} ) : null} {editing.actionAfterFirstSave.type === 'topic' && note.notebooks?.length > 0 && note?.notebooks?.findIndex( (o) => o.topics.indexOf(editing.actionAfterFirstSave.id) > -1, ) > -1 ? ( { await db.notebooks .notebook(editing.actionAfterFirstSave.notebook) .topics.topic(editing.actionAfterFirstSave.id) .delete(note.id); eSendEvent(refreshNotesPage); eSendEvent(eOnNewTopicAdded); dispatch({type: Actions.NOTEBOOKS}); dispatch({type: Actions.NOTES}); setNote(db.notes.note(note.id).data); close(); }} testID={notesnook.ids.dialogs.actionsheet.vault} customStyle={{ width: '95%', alignSelf: 'center', height: 50, flexDirection: 'row', justifyContent: 'center', alignItems: 'center', marginTop: 10, }}> Remove from Topic ) : null} {hasColors && note.id ? ( ) : null} {hasTags && note ? ( ) : null} {columnItems.length > 0 ? ( {columnItemsData.map(_renderColumnItem)} ) : null} {DDS.isTab ? ( ) : null} ); };