import React, {useEffect, useRef, useState} from 'react'; import {ActivityIndicator, TouchableOpacity, View} from 'react-native'; import Icon from 'react-native-vector-icons/MaterialCommunityIcons'; import Clipboard from '@react-native-clipboard/clipboard'; import {useTracked} from '../../provider'; import { eSubscribeEvent, eUnSubscribeEvent, ToastEvent } from '../../services/EventManager'; import Navigation from '../../services/Navigation'; import {db} from '../../utils/database'; import { eClosePublishNoteDialog, eOpenPublishNoteDialog } from '../../utils/Events'; import {openLinkInBrowser} from '../../utils/functions'; import {SIZE} from '../../utils/SizeUtils'; import {ActionIcon} from '../ActionIcon'; import SheetWrapper from '../sheet'; import {Button} from '../Button'; import DialogHeader from '../Dialog/dialog-header'; import Input from '../Input'; import Seperator from '../Seperator'; import Heading from '../Typography/Heading'; import Paragraph from '../Typography/Paragraph'; import {useAttachmentStore} from '../../provider/stores'; import {editing} from '../../utils'; let passwordValue = null; const PublishNoteDialog = () => { const [state] = useTracked(); const colors = state.colors; const [visible, setVisible] = useState(false); const actionSheetRef = useRef(); const loading = useAttachmentStore(state => state.loading); const [selfDestruct, setSelfDestruct] = useState(false); const [isLocked, setIsLocked] = useState(false); const [note, setNote] = useState(null); const [publishing, setPublishing] = useState(false); const publishUrl = note && `https://monograph.notesnook.com/${db?.monographs.monograph(note?.id)}`; const isPublished = note && db?.monographs.isPublished(note?.id); const pwdInput = useRef(); useEffect(() => { eSubscribeEvent(eOpenPublishNoteDialog, open); eSubscribeEvent(eClosePublishNoteDialog, close); return () => { eUnSubscribeEvent(eOpenPublishNoteDialog, open); eUnSubscribeEvent(eClosePublishNoteDialog, close); }; }, []); const open = item => { setNote(item); setPublishing(false); setSelfDestruct(false); setIsLocked(false); setVisible(true); passwordValue = null; }; useEffect(() => { if (visible) { actionSheetRef.current?.show(); } }, [visible]); const close = () => { passwordValue = null; actionSheetRef.current?.hide(); }; const publishNote = async () => { if (publishing) return; setPublishing(true); try { if (note?.id) { if (isLocked && !passwordValue) return; await db.monographs.publish(note.id, { selfDestruct: selfDestruct, password: isLocked && passwordValue }); setNote(db.notes.note(note.id)?.data); Navigation.setRoutesToUpdate([ Navigation.routeNames.Notes, Navigation.routeNames.NotesPage, Navigation.routeNames.Favorites ]); } } catch (e) { ToastEvent.show({ heading: 'Could not publish note', message: e.message, type: 'error', context: 'local' }); } setPublishing(false); }; const deletePublishedNote = async () => { if (publishing) return; setPublishing(true); try { if (note?.id) { await db.monographs.unpublish(note.id); setNote(db.notes.note(note.id)?.data); Navigation.setRoutesToUpdate([ Navigation.routeNames.Notes, Navigation.routeNames.NotesPage, Navigation.routeNames.Favorites ]); } } catch (e) { ToastEvent.show({ heading: 'Could not unpublish note', message: e.message, type: 'error', context: 'local' }); } actionSheetRef.current?.hide(); setPublishing(false); }; return !visible ? null : ( { passwordValue = null; setVisible(false); }}> {publishing ? ( Please wait... {loading && `\nDownloading attachments (${ loading?.current / loading?.total })`} ) : ( <> {isPublished && ( Published at: {publishUrl} { try { await openLinkInBrowser(publishUrl, colors.accent); } catch (e) {} }} size={SIZE.xs} style={{ marginTop: 5, color: colors.pri }}> Open in browser { Clipboard.setString(publishUrl); ToastEvent.show({ heading: 'Note publish url copied', type: 'success', context: 'local' }); }} color={colors.accent} size={SIZE.lg} name="content-copy" /> )} { if (publishing) return; setIsLocked(!isLocked); }} activeOpacity={0.9} style={{ flexDirection: 'row', alignItems: 'center', marginBottom: 10 }}> { if (publishing) return; setIsLocked(!isLocked); }} color={isLocked ? colors.accent : colors.icon} size={SIZE.lg} name={ isLocked ? 'check-circle-outline' : 'checkbox-blank-circle-outline' } /> Password protection Published note can only be viewed by someone with the password. { setSelfDestruct(!selfDestruct); }} activeOpacity={0.9} style={{ flexDirection: 'row', alignItems: 'center' }}> { setSelfDestruct(!selfDestruct); }} color={selfDestruct ? colors.accent : colors.icon} size={SIZE.lg} name={ selfDestruct ? 'check-circle-outline' : 'checkbox-blank-circle-outline' } /> Self destruct Published note link will be automatically deleted once it is viewed by someone. {isLocked ? ( <> (passwordValue = value)} blurOnSubmit secureTextEntry defaultValue={passwordValue} placeholder="Enter Password" /> ) : null}