import React, {createRef, useEffect, useState} from 'react'; import { Dimensions, StatusBar, Text, TextInput, TouchableOpacity, View, KeyboardAvoidingView, } from 'react-native'; import Share from 'react-native-share'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import { ACCENT, COLOR_SCHEME, COLOR_SCHEME_DARK, COLOR_SCHEME_LIGHT, opacity, ph, pv, setColorScheme, SIZE, WEIGHT, } from '../../common/common'; import {useTracked} from '../../provider'; import {ACTIONS} from '../../provider/actions'; import NavigationService from '../../services/NavigationService'; import {timeConverter, ToastEvent, DDS, db} from '../../utils/utils'; import {openVault, eSendEvent} from '../../services/eventManager'; import {refreshNotesPage, eOpenPremiumDialog, eOpenExportDialog} from '../../services/events'; import {PremiumTag} from '../Premium/PremiumTag'; import { MMKV } from '../../utils/storage'; const w = Dimensions.get('window').width; const h = Dimensions.get('window').height; const tagsInputRef = createRef(); export const ActionSheetComponent = ({ close = () => {}, item, hasColors = false, hasTags = false, rowItems = [], columnItems = [], }) => { const [state, dispatch] = useTracked(); const {colors, tags, premiumUser} = state; const [focused, setFocused] = useState(false); const [note, setNote] = useState( item ? item : { colors: [], tags: [], pinned: false, favorite: false, locked: false, content: { text: null, delta: null, }, dateCreated: null, }, ); function changeColorScheme(colors = COLOR_SCHEME, accent = ACCENT) { let newColors = setColorScheme(colors, accent); StatusBar.setBarStyle(colors.night ? 'light-content' : 'dark-content'); dispatch({type: ACTIONS.THEME, colors: newColors}); } useEffect(() => { if (item.dateCreated !== null) { setNote({...item}); } }, [item]); let tagToAdd = null; let backPressCount = 0; const _onSubmit = async () => { if (!tagToAdd || tagToAdd === '' || tagToAdd.trimStart().length == 0) { ToastEvent.show('Empty Tag', 'success'); return; } let tag = tagToAdd; tag = tag.trim(); if (tag.includes(' ')) { tag = tag.replace(' ', '_'); } if (tag.includes(',')) { tag = tag.replace(',', ''); } await db.notes.note(note.id).tag(tag); setNote({...db.notes.note(note.id).data}); dispatch({type: ACTIONS.TAGS}); tagsInputRef.current?.setNativeProps({ text: '', }); tagToAdd = ''; }; const _onKeyPress = async (event) => { if (event.nativeEvent.key === 'Backspace') { if (backPressCount === 0 && !tagToAdd) { backPressCount = 1; return; } if (backPressCount === 1 && !tagToAdd) { backPressCount = 0; let tagInputValue = note.tags[note.tags.length - 1]; let oldProps = {...note}; if (oldProps.tags.length === 0) return; await db.notes .note(note.id) .untag(oldProps.tags[oldProps.tags.length - 1]); setNote({...db.notes.note(note.id).data}); tagsInputRef.current?.setNativeProps({ text: tagInputValue, }); } } else if (event.nativeEvent.key === ' ') { _onSubmit(); tagsInputRef.current?.setNativeProps({ text: '', }); return; } else if (event.nativeEvent.key === ',') { _onSubmit(); tagsInputRef.current?.setNativeProps({ text: '', }); return; } }; 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}); dispatch({type: ACTIONS.PINNED}); dispatch({type: ACTIONS.FAVORITES}); } setNote({...toAdd}); }; const rowItemsData = [ { name: 'Add to', icon: 'book-outline', func: () => { dispatch({type: ACTIONS.MODAL_NAVIGATOR, enabled: true}); dispatch({type: ACTIONS.SELECTED_ITEMS, item: note}); close('movenote'); }, }, { name: 'Share', icon: 'share-variant', func: () => { if (note.locked) { openVault(item, false, true, false, false, true); } else { close(); let m = `${note.title}\n \n ${note.content.text}`; Share.open({ title: 'Share note to', failOnCancel: false, message: m, }); } }, }, { name: 'Export', icon: 'export', func: () => { close('export') }, }, { name: 'Delete', icon: 'delete', func: () => close('delete'), }, { name: 'Edit Notebook', icon: 'square-edit-outline', func: () => { close('notebook'); }, }, { name: 'Edit Topic', icon: 'square-edit-outline', func: () => { close('topic'); }, }, { name: 'Open', icon: 'open-in-app', func: () => { NavigationService.navigate('Editor', { note: item, }); close(); }, }, { name: 'Restore', icon: 'delete-restore', func: async () => { await db.trash.restore(note.id); dispatch({type: ACTIONS.TRASH}); localRefresh(note.type); 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, }, { name: 'Pin', icon: 'tag-outline', func: async () => { if (!premiumUser) { close('premium'); return; } if (!note.id) return; if (note.type === 'note') { await db.notes.note(note.id).pin(); } else { await db.notebooks.notebook(note.id).pin(); } dispatch({type: ACTIONS.PINNED}); localRefresh(item.type); }, close: false, check: true, on: note.pinned, }, { name: 'Favorite', icon: 'star', func: async () => { if (!premiumUser) { close('premium'); return; } 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}); localRefresh(item.type); }, close: false, check: true, on: note.favorite, }, { name: 'Add to Vault', icon: 'shield', func: () => { if (!premiumUser) { close('premium'); return; } if (!note.id) return; if (note.locked) { close('unlock'); return; } else { db.vault .add(note.id) .then(() => { dispatch({type: ACTIONS.NOTES}); eSendEvent(refreshNotesPage); dispatch({type: ACTIONS.PINNED}); close(); }) .catch(async (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; } }); } }, close: true, check: true, on: note.locked, }, ]; const _renderTag = (tag) => ( { let oldProps = {...note}; try { await db.notes .note(note.id) .untag(oldProps.tags[oldProps.tags.indexOf(tag)]); localRefresh(oldProps.type); dispatch({type: ACTIONS.TAGS}); } catch (e) { localRefresh(oldProps.type); } }} style={{ flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', margin: 1, paddingHorizontal: 5, paddingVertical: 2.5, }}> # {tag} ); const _renderColor = (color) => ( { let noteColors = note.colors; if (noteColors.includes(color)) { await db.notes.note(note.id).uncolor(color); } else { await db.notes.note(note.id).color(color); } dispatch({type: ACTIONS.COLORS}); localRefresh(note.type); }} style={{ flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', borderColor: colors.nav, }}> {note && note.colors && note.colors.includes(color) ? ( ) : null} ); 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; return ( { if (!item.dateDeleted) { localRefresh(item.type, true); } }} style={{ paddingBottom: 30, backgroundColor: colors.bg, width: w, paddingHorizontal: 0, }}> {!note.id && !note.dateCreated ? ( Please 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' ? null : ( {note && note.topics ? note.topics.slice(1, 4).map((topic) => ( {topic.title} )) : null} )} {note.type !== 'note' ? null : ( Synced )} )} {note.id || note.dateCreated ? ( {rowItemsData.map(_renderRowItem)} ) : null} {hasColors && note.id ? ( {['red', 'yellow', 'green', 'blue', 'purple', 'orange', 'gray'].map( _renderColor, )} ) : null} {hasTags && (note.id || note.dateCreated) ? ( {tags.filter( (o) => o.count > 1 && !note.tags.find((t) => t === o.title), ).length === 0 ? '' : 'Suggestions '} {tags .filter( (o) => o.count > 1 && !note.tags.find((t) => t === o.title), ) .map((tag) => ( { tagToAdd = tag.title; _onSubmit(); }} style={{ flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', margin: 1, marginRight: 5, paddingHorizontal: 5, paddingVertical: 1, borderRadius: 2.5, borderBottomWidth: 1, borderBottomColor: colors.nav, }}> # {tag.title}{' '} {tag.count} ))} { if (!premiumUser) { close('premium'); return; } tagsInputRef.current?.focus(); }} style={{ position: 'absolute', width: '100%', height: '100%', justifyContent: 'flex-start', alignItems: 'flex-end', }}> {!premiumUser ? ( PRO ) : null} {note && note.tags ? note.tags.map(_renderTag) : null} { setFocused(true); }} selectionColor={colors.accent} onBlur={() => { setFocused(false); }} placeholder="#hashtag" onChangeText={(value) => { tagToAdd = value; if (tagToAdd.length > 0) backPressCount = 0; }} onSubmitEditing={_onSubmit} onKeyPress={_onKeyPress} /> ) : null} {columnItems.length > 0 ? ( {columnItemsData.map(_renderColumnItem)} ) : null} {DDS.isTab ? ( ) : null} ); };