import React, {createRef, useEffect, useState} from 'react'; import { FlatList, KeyboardAvoidingView, Modal, SafeAreaView, TextInput, TouchableOpacity, View, } from 'react-native'; import {useSafeAreaInsets} from 'react-native-safe-area-context'; 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 { eSubscribeEvent, eUnSubscribeEvent, ToastEvent, } from '../../services/EventManager'; import {getElevation} from '../../utils'; import {db} from '../../utils/DB'; import {eOpenMoveNoteDialog} from '../../utils/Events'; import {pv, SIZE, WEIGHT} from '../../utils/SizeUtils'; import DialogButtons from '../Dialog/dialog-buttons'; import DialogHeader from '../Dialog/dialog-header'; import {PressableButton} from '../PressableButton'; import {Toast} from '../Toast'; import Heading from '../Typography/Heading'; import Paragraph from '../Typography/Paragraph'; let newNotebookTitle = null; let newTopicTitle = null; const notebookInput = createRef(); const topicInput = createRef(); const MoveNoteDialog = () => { const [state, dispatch] = useTracked(); const {notebooks, colors, selectedItemsList} = state; const [visible, setVisible] = useState(false); const [expanded, setExpanded] = useState(''); const [notebookInputFocused, setNotebookInputFocused] = useState(false); const [topicInputFocused, setTopicInputFocused] = useState(false); function open() { setVisible(true); } const close = () => { setVisible(false); setExpanded(false); newTopicTitle = null; newNotebookTitle = null; setNotebookInputFocused(false); setTopicInputFocused(false); }; useEffect(() => { eSubscribeEvent(eOpenMoveNoteDialog, open); return () => { eUnSubscribeEvent(eOpenMoveNoteDialog, open); }; }, []); const addNewNotebook = async () => { if (!newNotebookTitle || newNotebookTitle.trim().length === 0) return ToastEvent.show('Title is required', 'error', 'local'); await db.notebooks.add({ title: newNotebookTitle, description: 'this.description', topics: [], id: null, }); notebookInput.current?.clear(); notebookInput.current?.blur(); dispatch({type: Actions.NOTEBOOKS}); dispatch({type: Actions.PINNED}); }; const addNewTopic = async () => { if (!newTopicTitle || newTopicTitle.trim().length === 0) { return ToastEvent.show('Title is required', 'error', 'local'); } let res = await db.notebooks.notebook(expanded).topics.add(newTopicTitle); dispatch({type: Actions.NOTEBOOKS}); dispatch({type: Actions.PINNED}); topicInput.current?.clear(); topicInput.current?.blur(); newTopicTitle = null; }; return !visible ? null : ( { newNotebookTitle = value; }} testID={notesnook.ids.dialogs.addTo.addNotebook} blurOnSubmit={false} onFocus={() => { setNotebookInputFocused(true); }} onBlur={() => { setNotebookInputFocused(false); }} onSubmitEditing={addNewNotebook} style={[ { color: colors.pri, width: '85%', maxWidth: '85%', paddingHorizontal: 0, borderRadius: 5, minHeight: 45, fontSize: SIZE.md, fontFamily: WEIGHT.regular, padding: pv - 2, }, ]} placeholder="Add a notebook" placeholderTextColor={colors.icon} /> } renderItem={({item, index}) => ( { setExpanded(item.id === expanded ? null : item.id); }} type={expanded === item.id ? 'shade' : 'transparent'} customStyle={{ height: 50, width: '100%', borderRadius: 0, alignItems: 'flex-start', paddingHorizontal: 12, marginBottom: 5, }}> {item.title} {'\n'} {item.totalNotes + ' notes' + ' & ' + item.topics.length + ' topics'} {expanded === item.id ? ( { newTopicTitle = value; }} testID={notesnook.ids.dialogs.addTo.addTopic} blurOnSubmit={false} onFocus={() => { setTopicInputFocused(true); }} onBlur={() => { setTopicInputFocused(false); }} onSubmitEditing={addNewTopic} style={[ { color: colors.pri, width: '85%', maxWidth: '85%', paddingHorizontal: 0, borderRadius: 5, height: 40, fontSize: SIZE.sm, fontFamily: WEIGHT.regular, padding: pv - 2, }, ]} placeholder="Add a Topic" placeholderTextColor={colors.icon} /> } renderItem={({item, index}) => ( { let noteIds = []; selectedItemsList.forEach((i) => noteIds.push(i.id), ); let res = await db.notes.move( { topic: item.title, id: item.notebookId, }, ...noteIds, ); dispatch({type: Actions.CLEAR_SELECTION}); dispatch({type: Actions.NOTEBOOKS}); dispatch({type: Actions.PINNED}); close(); let notebookName = db.notebooks.notebook( item.notebookId, ).title; ToastEvent.show( `Note moved to ${item.title} in ${notebookName}`, 'success', ); }} type="gray" customStyle={{ height: 50, borderTopWidth: index === 0 ? 0 : 1, borderTopColor: colors.nav, width: '87%', alignSelf: 'flex-end', borderRadius: 0, alignItems: 'center', marginRight: 12, flexDirection: 'row', justifyContent: 'space-between', }}> {item.title} {'\n'} {item.totalNotes + ' notes'} )} /> ) : null} )} /> ); }; export default MoveNoteDialog;