Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor Tags Section from ActionSheet

ammarahm-ed1aa3806d2216
diff --git a/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js b/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js
new file mode 100644
index 000000000..a707f7b1a
--- /dev/null
+++ b/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js
@@ -0,0 +1,260 @@
+import React, {createRef, useState} from 'react';
+import {Text, TextInput, TouchableOpacity, View} from 'react-native';
+import {useTracked} from '../../provider';
+import {Actions} from '../../provider/Actions';
+import {sendNoteEditedEvent, ToastEvent} from '../../services/EventManager';
+import {db} from '../../utils/DB';
+import {SIZE, WEIGHT} from '../../utils/SizeUtils';
+
+const tagsInputRef = createRef();
+
+export const ActionSheetTagsSection = ({note, localRefresh}) => {
+ const [state, dispatch] = useTracked();
+ const {colors, tags, premiumUser} = state;
+ const [suggestions, setSuggestions] = useState([]);
+ const [focused, setFocused] = useState(false);
+ 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);
+ localRefresh(note.type);
+ 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]);
+
+ localRefresh(note.type);
+
+ tagsInputRef.current?.setNativeProps({
+ text: tagInputValue,
+ });
+ }
+ } else if (event.nativeEvent.key === ' ') {
+ await _onSubmit();
+ tagsInputRef.current?.setNativeProps({
+ text: '',
+ });
+ } else if (event.nativeEvent.key === ',') {
+ await _onSubmit();
+ tagsInputRef.current?.setNativeProps({
+ text: '',
+ });
+ }
+ };
+
+ const getSuggestions = (query) => {
+ let _suggestions = tags.filter(t => t.title.startsWith(query))
+ if (_suggestions && _suggestions.length > 0) {
+ setSuggestions(_suggestions);
+ }
+ }
+
+ const _renderTag = (tag) => (
+ <TouchableOpacity
+ key={tag}
+ onPress={async () => {
+ let oldProps = {...note};
+ try {
+ await db.notes
+ .note(note.id)
+ .untag(oldProps.tags[oldProps.tags.indexOf(tag)]);
+ sendNoteEditedEvent(note.id, false, true);
+ dispatch({type: Actions.TAGS});
+ } catch (e) {
+ sendNoteEditedEvent(note.id, false, true);
+ }
+ }}
+ style={{
+ flexDirection: 'row',
+ justifyContent: 'flex-start',
+ alignItems: 'center',
+ margin: 1,
+ paddingHorizontal: 5,
+ paddingVertical: 2.5,
+ }}>
+ <Text
+ style={{
+ fontFamily: WEIGHT.regular,
+ fontSize: SIZE.sm,
+ color: colors.pri,
+ }}>
+ <Text
+ style={{
+ color: colors.accent,
+ }}>
+ #
+ </Text>
+ {tag}
+ </Text>
+ </TouchableOpacity>
+ );
+
+ return note.id || note.dateCreated ? (
+ <View
+ style={{
+ marginHorizontal: 12,
+ }}>
+ <View
+ style={{
+ flexDirection: 'row',
+ flexWrap: 'wrap',
+ marginBottom: 5,
+ marginTop: 5,
+ alignItems: 'center',
+ }}>
+ <Text
+ style={{
+ fontFamily: WEIGHT.regular,
+ fontSize: SIZE.xs,
+ color: colors.pri,
+ }}>
+ {tags.filter(
+ (o) =>
+ o.noteIds.length >= 1 && !note.tags.find((t) => t === o.title),
+ ).length === 0
+ ? ''
+ : 'Suggested: '}
+ </Text>
+
+ {[...suggestions,...suggestions,...suggestions,...suggestions].map((tag) => (
+ <TouchableOpacity
+ key={tag.title}
+ onPress={() => {
+ tagToAdd = tag.title;
+ _onSubmit();
+ }}
+ style={{
+ flexDirection: 'row',
+ justifyContent: 'flex-start',
+ alignItems: 'center',
+ margin: 1,
+ marginRight: 5,
+ paddingHorizontal: 0,
+ paddingVertical: 2.5,
+ }}>
+ <Text
+ style={{
+ fontFamily: WEIGHT.regular,
+ fontSize: SIZE.xs,
+ color: colors.pri,
+ }}>
+ <Text
+ style={{
+ color: colors.accent,
+ }}>
+ #
+ </Text>
+ {tag.title}
+ </Text>
+ </TouchableOpacity>
+ ))}
+ </View>
+ <View
+ style={{
+ flexDirection: 'row',
+ flexWrap: 'wrap',
+ borderRadius: 5,
+ borderWidth: 1.5,
+ borderColor: focused ? colors.accent : colors.nav,
+ alignItems: 'center',
+ height: 40,
+ }}>
+ <TouchableOpacity
+ onPress={() => {
+ if (!premiumUser) {
+ close('premium');
+ return;
+ }
+ tagsInputRef.current?.focus();
+ }}
+ activeOpacity={1}
+ style={{
+ position: 'absolute',
+ width: '100%',
+ height: '100%',
+ justifyContent: 'flex-start',
+ alignItems: 'flex-end',
+ zIndex: 10,
+ }}>
+ {!premiumUser ? (
+ <Text
+ style={{
+ color: colors.accent,
+ fontFamily: WEIGHT.regular,
+ fontSize: 10,
+ marginRight: 4,
+ marginTop: 2.5,
+ }}>
+ PRO
+ </Text>
+ ) : null}
+ </TouchableOpacity>
+ {note && note.tags ? note.tags.map(_renderTag) : null}
+ <TextInput
+ style={{
+ backgroundColor: 'transparent',
+ minWidth: 100,
+ zIndex: 10,
+ fontFamily: WEIGHT.regular,
+ color: colors.pri,
+ paddingHorizontal: 5,
+ paddingVertical: 0,
+ height: 40,
+ textAlignVertical: 'center',
+ }}
+ blurOnSubmit={false}
+ ref={tagsInputRef}
+ placeholderTextColor={colors.icon}
+ onFocus={() => {
+ setFocused(true);
+ }}
+ selectionColor={colors.accent}
+ onBlur={() => {
+ setFocused(false);
+ }}
+ placeholder="#hashtag"
+ onChangeText={(value) => {
+ tagToAdd = value;
+ getSuggestions(value)
+ if (tagToAdd.length > 0) backPressCount = 0;
+ }}
+ onSubmitEditing={_onSubmit}
+ onKeyPress={_onKeyPress}
+ />
+ </View>
+ </View>
+ ) : null;
+};
diff --git a/apps/mobile/src/components/ActionSheetComponent/index.js b/apps/mobile/src/components/ActionSheetComponent/index.js
index 3723aa1f5..c300a1e59 100644
--- a/apps/mobile/src/components/ActionSheetComponent/index.js
+++ b/apps/mobile/src/components/ActionSheetComponent/index.js
@@ -13,7 +13,12 @@ import Share from 'react-native-share';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
import {useTracked} from '../../provider';
import {Actions} from '../../provider/Actions';
-import {eSendEvent, openVault, sendNoteEditedEvent, ToastEvent} from '../../services/EventManager';
+import {
+ eSendEvent,
+ openVault,
+ sendNoteEditedEvent,
+ ToastEvent,
+} from '../../services/EventManager';
import {
eOnNoteEdited,
eOpenLoginDialog,
@@ -23,26 +28,26 @@ import {
import {PremiumTag} from '../Premium/PremiumTag';
import {PressableButton} from '../PressableButton';
import {Toast} from '../Toast';
-import {hexToRGBA, RGB_Linear_Shade} from "../../utils/ColorUtils";
-import {timeConverter} from "../../utils/TimeUtils";
+import {hexToRGBA, RGB_Linear_Shade} from '../../utils/ColorUtils';
+import {timeConverter} from '../../utils/TimeUtils';
import {
ACCENT,
COLOR_SCHEME,
COLOR_SCHEME_DARK,
COLOR_SCHEME_LIGHT,
COLORS_NOTE,
- setColorScheme
-} from "../../utils/Colors";
-import {opacity, ph, pv, SIZE, WEIGHT} from "../../utils/SizeUtils";
-import {db} from "../../utils/DB";
-import {DDS} from "../../services/DeviceDetection";
-import {MMKV} from "../../utils/MMKV";
-import id from "notes-core/utils/id";
+ setColorScheme,
+} from '../../utils/Colors';
+import {opacity, ph, pv, SIZE, WEIGHT} from '../../utils/SizeUtils';
+import {db} from '../../utils/DB';
+import {DDS} from '../../services/DeviceDetection';
+import {MMKV} from '../../utils/mmkv';
+import { ActionSheetTagsSection } from './ActionSheetTagsSection';
const w = Dimensions.get('window').width;
const h = Dimensions.get('window').height;
-const tagsInputRef = createRef();
+
export const ActionSheetComponent = ({
close = () => {},
item,
@@ -53,7 +58,7 @@ export const ActionSheetComponent = ({
}) => {
const [state, dispatch] = useTracked();
const {colors, tags, premiumUser, user} = state;
- const [focused, setFocused] = useState(false);
+
const [refreshing, setRefreshing] = useState(false);
const [note, setNote] = useState(
item
@@ -88,65 +93,7 @@ export const ActionSheetComponent = ({
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 === ' ') {
- await _onSubmit();
- tagsInputRef.current?.setNativeProps({
- text: '',
- });
- } else if (event.nativeEvent.key === ',') {
- await _onSubmit();
- tagsInputRef.current?.setNativeProps({
- text: '',
- });
- }
- };
const localRefresh = (type, nodispatch = false) => {
if (!note || !note.id) return;
@@ -339,8 +286,8 @@ export const ActionSheetComponent = ({
await db.notebooks.notebook(note.id).favorite();
}
dispatch({type: Actions.FAVORITES});
- sendNoteEditedEvent(note.id, false,true)
- localRefresh(item.type,true);
+ sendNoteEditedEvent(note.id, false, true);
+ localRefresh(item.type, true);
},
close: false,
check: true,
@@ -358,12 +305,11 @@ export const ActionSheetComponent = ({
if (note.locked) {
close('unlock');
-
} else {
db.vault
.add(note.id)
.then(() => {
- sendNoteEditedEvent(note.id, false,true)
+ sendNoteEditedEvent(note.id, false, true);
close();
})
.catch(async (e) => {
@@ -387,45 +333,6 @@ export const ActionSheetComponent = ({
},
];
- const _renderTag = (tag) => (
- <TouchableOpacity
- key={tag}
- onPress={async () => {
- let oldProps = {...note};
- try {
- await db.notes
- .note(note.id)
- .untag(oldProps.tags[oldProps.tags.indexOf(tag)]);
- sendNoteEditedEvent(note.id, false,true)
- dispatch({type: Actions.TAGS});
- } catch (e) {
- sendNoteEditedEvent(note.id, false,true)
- }
- }}
- style={{
- flexDirection: 'row',
- justifyContent: 'flex-start',
- alignItems: 'center',
- margin: 1,
- paddingHorizontal: 5,
- paddingVertical: 2.5,
- }}>
- <Text
- style={{
- fontFamily: WEIGHT.regular,
- fontSize: SIZE.sm,
- color: colors.pri,
- }}>
- <Text
- style={{
- color: colors.accent,
- }}>
- #
- </Text>
- {tag}
- </Text>
- </TouchableOpacity>
- );
const _renderColor = (c) => {
const color = {
@@ -452,8 +359,8 @@ export const ActionSheetComponent = ({
await db.notes.note(note.id).color(color.name);
}
dispatch({type: Actions.COLORS});
- sendNoteEditedEvent(note.id, false,true)
- localRefresh(note.type,true);
+ sendNoteEditedEvent(note.id, false, true);
+ localRefresh(note.type, true);
}}
customStyle={{
width: DDS.isTab ? 400 / 10 : w / 10,
@@ -609,9 +516,12 @@ export const ActionSheetComponent = ({
dispatch({type: Actions.ALL});
setRefreshing(false);
}
-
} else {
- console.log('here',user?.lastSynced,user?.lastSynced < note?.dateEdited)
+ console.log(
+ 'here',
+ user?.lastSynced,
+ user?.lastSynced < note?.dateEdited,
+ );
}
};
@@ -792,162 +702,10 @@ export const ActionSheetComponent = ({
</View>
) : null}
- {hasTags && (note.id || note.dateCreated) ? (
- <View
- style={{
- marginHorizontal: 12,
- }}>
- <View
- style={{
- flexDirection: 'row',
- flexWrap: 'wrap',
- marginBottom: 5,
- marginTop: 5,
- alignItems: 'center',
- }}>
- <Text
- style={{
- fontFamily: WEIGHT.regular,
- fontSize: SIZE.xs,
- color: colors.accent,
- }}>
- {tags.filter(
- (o) => o.count > 1 && !note.tags.find((t) => t === o.title),
- ).length === 0
- ? ''
- : 'Suggestions '}
- </Text>
- {tags
- .filter(
- (o) => o.count > 1 && !note.tags.find((t) => t === o.title),
- )
- .map((tag) => (
- <TouchableOpacity
- key={tag.title}
- onPress={() => {
- 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,
- }}>
- <Text
- style={{
- fontFamily: WEIGHT.regular,
- fontSize: SIZE.xs,
- color: colors.pri,
- }}>
- <Text
- style={{
- color: colors.accent,
- }}>
- #
- </Text>
- {tag.title}{' '}
- </Text>
- <View
- style={{
- borderRadius: 2.5,
- backgroundColor: colors.accent,
- alignItems: 'center',
- justifyContent: 'center',
- }}>
- <Text
- style={{
- color: 'white',
- fontSize: SIZE.xxs - 2,
- minWidth: 12,
- textAlign: 'center',
- }}>
- {tag.count}
- </Text>
- </View>
- </TouchableOpacity>
- ))}
- </View>
- <View
- style={{
- flexDirection: 'row',
- flexWrap: 'wrap',
- borderRadius: 5,
- borderWidth: 1.5,
- borderColor: focused ? colors.accent : colors.nav,
- alignItems: 'center',
- height:40
- }}>
- <TouchableOpacity
- onPress={() => {
- if (!premiumUser) {
- close('premium');
- return;
- }
- tagsInputRef.current?.focus();
- }}
- activeOpacity={1}
- style={{
- position: 'absolute',
- width: '100%',
- height: '100%',
- justifyContent: 'flex-start',
- alignItems: 'flex-end',
- zIndex:10
- }}>
- {!premiumUser ? (
- <Text
- style={{
- color: colors.accent,
- fontFamily: WEIGHT.regular,
- fontSize: 10,
- marginRight: 4,
- marginTop:2.5
- }}>
- PRO
- </Text>
- ) : null}
- </TouchableOpacity>
- {note && note.tags ? note.tags.map(_renderTag) : null}
- <TextInput
- style={{
- backgroundColor: 'transparent',
- minWidth: 100,
- zIndex: 10,
- fontFamily: WEIGHT.regular,
- color: colors.pri,
- paddingHorizontal: 5,
- paddingVertical:0,
- height:40,
- textAlignVertical:"center"
- }}
- blurOnSubmit={false}
- ref={tagsInputRef}
- placeholderTextColor={colors.icon}
- onFocus={() => {
- setFocused(true);
- }}
- selectionColor={colors.accent}
- onBlur={() => {
- setFocused(false);
- }}
- placeholder="#hashtag"
- onChangeText={(value) => {
- tagToAdd = value;
- if (tagToAdd.length > 0) backPressCount = 0;
- }}
- onSubmitEditing={_onSubmit}
- onKeyPress={_onKeyPress}
- />
- </View>
- </View>
- ) : null}
+ {
+ hasTags? <ActionSheetTagsSection note={note} localRefresh={localRefresh} /> : null
+ }
+
{columnItems.length > 0 ? (
<View>{columnItemsData.map(_renderColumnItem)}</View>