2022-02-28 15:32:55 +05:00
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
2022-01-22 12:57:05 +05:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
|
|
|
|
import { ActivityIndicator, TouchableOpacity, View } from 'react-native';
|
2021-06-14 14:41:57 +05:00
|
|
|
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
|
2022-02-28 23:25:18 +05:00
|
|
|
import { useThemeStore } from '../../../stores/theme';
|
|
|
|
|
import { useAttachmentStore } from '../../../stores/stores';
|
|
|
|
|
import { eSubscribeEvent, eUnSubscribeEvent, ToastEvent } from '../../../services/event-manager';
|
|
|
|
|
import Navigation from '../../../services/navigation';
|
2022-02-28 15:32:55 +05:00
|
|
|
import { db } from '../../../utils/database';
|
|
|
|
|
import { eClosePublishNoteDialog, eOpenPublishNoteDialog } from '../../../utils/events';
|
|
|
|
|
import { openLinkInBrowser } from '../../../utils/functions';
|
|
|
|
|
import { SIZE } from '../../../utils/size';
|
|
|
|
|
import DialogHeader from '../../dialog/dialog-header';
|
|
|
|
|
import { Button } from '../../ui/button';
|
|
|
|
|
import { IconButton } from '../../ui/icon-button';
|
|
|
|
|
import Input from '../../ui/input';
|
|
|
|
|
import Seperator from '../../ui/seperator';
|
|
|
|
|
import SheetWrapper from '../../ui/sheet';
|
|
|
|
|
import Heading from '../../ui/typography/heading';
|
|
|
|
|
import Paragraph from '../../ui/typography/paragraph';
|
2022-03-23 12:23:51 +05:00
|
|
|
import SearchService from '../../../services/search';
|
2021-06-14 08:38:03 +05:00
|
|
|
|
|
|
|
|
let passwordValue = null;
|
2022-02-28 15:32:55 +05:00
|
|
|
const PublishNoteSheet = () => {
|
2022-02-28 23:25:18 +05:00
|
|
|
const colors = useThemeStore(state => state.colors);
|
2021-06-14 08:38:03 +05:00
|
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
|
const actionSheetRef = useRef();
|
2021-10-21 13:19:14 +05:00
|
|
|
const loading = useAttachmentStore(state => state.loading);
|
2021-06-14 14:41:57 +05:00
|
|
|
const [selfDestruct, setSelfDestruct] = useState(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
const [isLocked, setIsLocked] = useState(false);
|
|
|
|
|
const [note, setNote] = useState(null);
|
2021-06-15 13:50:48 +05:00
|
|
|
const [publishing, setPublishing] = useState(false);
|
|
|
|
|
const publishUrl =
|
2022-01-22 12:57:05 +05:00
|
|
|
note && `https://monograph.notesnook.com/${db?.monographs.monograph(note?.id)}`;
|
2021-06-15 13:50:48 +05:00
|
|
|
const isPublished = note && db?.monographs.isPublished(note?.id);
|
2021-08-17 11:49:02 +05:00
|
|
|
const pwdInput = useRef();
|
2021-06-14 08:38:03 +05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
eSubscribeEvent(eOpenPublishNoteDialog, open);
|
|
|
|
|
eSubscribeEvent(eClosePublishNoteDialog, close);
|
|
|
|
|
return () => {
|
|
|
|
|
eUnSubscribeEvent(eOpenPublishNoteDialog, open);
|
|
|
|
|
eUnSubscribeEvent(eClosePublishNoteDialog, close);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2021-06-14 14:41:57 +05:00
|
|
|
const open = item => {
|
2021-06-14 08:38:03 +05:00
|
|
|
setNote(item);
|
2021-06-15 13:50:48 +05:00
|
|
|
setPublishing(false);
|
|
|
|
|
setSelfDestruct(false);
|
|
|
|
|
setIsLocked(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
setVisible(true);
|
2021-06-15 13:50:48 +05:00
|
|
|
passwordValue = null;
|
2021-06-14 08:38:03 +05:00
|
|
|
};
|
2021-06-15 13:50:48 +05:00
|
|
|
|
2021-06-14 08:38:03 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (visible) {
|
|
|
|
|
actionSheetRef.current?.show();
|
|
|
|
|
}
|
|
|
|
|
}, [visible]);
|
|
|
|
|
|
|
|
|
|
const close = () => {
|
2021-06-14 14:41:57 +05:00
|
|
|
passwordValue = null;
|
2021-06-14 08:38:03 +05:00
|
|
|
actionSheetRef.current?.hide();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const publishNote = async () => {
|
2021-06-15 13:50:48 +05:00
|
|
|
if (publishing) return;
|
|
|
|
|
setPublishing(true);
|
2021-10-21 13:19:14 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
try {
|
|
|
|
|
if (note?.id) {
|
|
|
|
|
if (isLocked && !passwordValue) return;
|
|
|
|
|
await db.monographs.publish(note.id, {
|
|
|
|
|
selfDestruct: selfDestruct,
|
2021-10-21 13:19:14 +05:00
|
|
|
password: isLocked && passwordValue
|
2021-06-15 13:50:48 +05:00
|
|
|
});
|
|
|
|
|
setNote(db.notes.note(note.id)?.data);
|
|
|
|
|
Navigation.setRoutesToUpdate([
|
|
|
|
|
Navigation.routeNames.Notes,
|
|
|
|
|
Navigation.routeNames.NotesPage,
|
2021-10-21 13:19:14 +05:00
|
|
|
Navigation.routeNames.Favorites
|
2021-06-15 13:50:48 +05:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Could not publish note',
|
|
|
|
|
message: e.message,
|
|
|
|
|
type: 'error',
|
2021-10-21 13:19:14 +05:00
|
|
|
context: 'local'
|
2021-06-14 08:38:03 +05:00
|
|
|
});
|
|
|
|
|
}
|
2021-10-21 13:19:14 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
setPublishing(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const deletePublishedNote = async () => {
|
2021-06-15 13:50:48 +05:00
|
|
|
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,
|
2021-10-21 13:19:14 +05:00
|
|
|
Navigation.routeNames.Favorites
|
2021-06-15 13:50:48 +05:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Could not unpublish note',
|
|
|
|
|
message: e.message,
|
|
|
|
|
type: 'error',
|
2021-10-21 13:19:14 +05:00
|
|
|
context: 'local'
|
2021-06-15 13:50:48 +05:00
|
|
|
});
|
2021-06-14 08:38:03 +05:00
|
|
|
}
|
2021-06-15 13:50:48 +05:00
|
|
|
actionSheetRef.current?.hide();
|
|
|
|
|
setPublishing(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return !visible ? null : (
|
2021-12-25 11:16:33 +05:00
|
|
|
<SheetWrapper
|
2021-06-14 08:38:03 +05:00
|
|
|
centered={false}
|
|
|
|
|
fwdRef={actionSheetRef}
|
2021-06-15 13:50:48 +05:00
|
|
|
closeOnTouchBackdrop={!publishing}
|
2021-10-21 13:19:14 +05:00
|
|
|
gestureEnabled={!publishing}
|
2021-06-14 08:38:03 +05:00
|
|
|
onClose={async () => {
|
|
|
|
|
passwordValue = null;
|
|
|
|
|
setVisible(false);
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-14 08:38:03 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
alignSelf: 'center',
|
2021-10-21 13:19:14 +05:00
|
|
|
paddingHorizontal: 12
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-14 08:38:03 +05:00
|
|
|
<DialogHeader
|
2021-10-21 13:19:14 +05:00
|
|
|
title={note.title}
|
2021-06-14 08:38:03 +05:00
|
|
|
paragraph={`Anyone with the link${
|
|
|
|
|
isLocked ? ' and password' : ''
|
2021-10-21 13:19:14 +05:00
|
|
|
} of the published note can view it.`}
|
2021-06-14 08:38:03 +05:00
|
|
|
/>
|
|
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
{publishing ? (
|
2021-06-14 08:38:03 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
2021-06-15 13:50:48 +05:00
|
|
|
justifyContent: 'center',
|
|
|
|
|
alignContent: 'center',
|
|
|
|
|
height: 150,
|
2021-10-21 13:19:14 +05:00
|
|
|
width: '100%'
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-15 13:50:48 +05:00
|
|
|
<ActivityIndicator size={25} color={colors.accent} />
|
|
|
|
|
<Paragraph
|
2021-06-14 08:38:03 +05:00
|
|
|
style={{
|
2021-10-21 13:19:14 +05:00
|
|
|
textAlign: 'center'
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-15 13:50:48 +05:00
|
|
|
Please wait...
|
2022-01-22 12:57:05 +05:00
|
|
|
{loading && `\nDownloading attachments (${loading?.current / loading?.total})`}
|
2021-06-15 13:50:48 +05:00
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{isPublished && (
|
|
|
|
|
<View
|
2021-06-14 08:38:03 +05:00
|
|
|
style={{
|
2021-06-15 13:50:48 +05:00
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
marginTop: 15,
|
2021-10-21 13:19:14 +05:00
|
|
|
backgroundColor: colors.nav,
|
|
|
|
|
padding: 12,
|
|
|
|
|
borderRadius: 5
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
2021-10-21 13:19:14 +05:00
|
|
|
flexShrink: 1
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-10-21 13:19:14 +05:00
|
|
|
<Heading size={SIZE.sm}>Published at:</Heading>
|
2021-12-16 10:20:34 +05:00
|
|
|
<Paragraph size={SIZE.xs} numberOfLines={1}>
|
2021-10-21 13:19:14 +05:00
|
|
|
{publishUrl}
|
|
|
|
|
</Paragraph>
|
2021-06-15 13:50:48 +05:00
|
|
|
<Paragraph
|
|
|
|
|
onPress={async () => {
|
|
|
|
|
try {
|
|
|
|
|
await openLinkInBrowser(publishUrl, colors.accent);
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
}}
|
2021-12-16 10:20:34 +05:00
|
|
|
size={SIZE.xs}
|
2021-06-15 13:50:48 +05:00
|
|
|
style={{
|
|
|
|
|
marginTop: 5,
|
2021-10-21 13:19:14 +05:00
|
|
|
color: colors.pri
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Icon color={colors.accent} name="open-in-new" /> Open in browser
|
2021-06-15 13:50:48 +05:00
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2022-02-28 13:48:59 +05:00
|
|
|
<IconButton
|
2021-06-15 13:50:48 +05:00
|
|
|
onPress={() => {
|
|
|
|
|
Clipboard.setString(publishUrl);
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Note publish url copied',
|
|
|
|
|
type: 'success',
|
2021-10-21 13:19:14 +05:00
|
|
|
context: 'local'
|
2021-06-15 13:50:48 +05:00
|
|
|
});
|
|
|
|
|
}}
|
2021-10-21 13:19:14 +05:00
|
|
|
color={colors.accent}
|
2021-06-15 13:50:48 +05:00
|
|
|
size={SIZE.lg}
|
|
|
|
|
name="content-copy"
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
|
|
|
|
)}
|
2021-06-15 14:48:51 +05:00
|
|
|
<Seperator />
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<TouchableOpacity
|
|
|
|
|
onPress={() => {
|
|
|
|
|
if (publishing) return;
|
|
|
|
|
setIsLocked(!isLocked);
|
|
|
|
|
}}
|
|
|
|
|
activeOpacity={0.9}
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
|
|
|
|
alignItems: 'center',
|
2021-10-21 13:19:14 +05:00
|
|
|
marginBottom: 10
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2022-02-28 13:48:59 +05:00
|
|
|
<IconButton
|
2021-06-15 13:50:48 +05:00
|
|
|
onPress={() => {
|
|
|
|
|
if (publishing) return;
|
|
|
|
|
setIsLocked(!isLocked);
|
|
|
|
|
}}
|
|
|
|
|
color={isLocked ? colors.accent : colors.icon}
|
|
|
|
|
size={SIZE.lg}
|
2022-01-22 12:57:05 +05:00
|
|
|
name={isLocked ? 'check-circle-outline' : 'checkbox-blank-circle-outline'}
|
2021-06-15 13:50:48 +05:00
|
|
|
/>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
2021-10-21 13:19:14 +05:00
|
|
|
flexShrink: 1
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-16 11:45:42 +05:00
|
|
|
<Heading size={SIZE.md}>Password protection</Heading>
|
2021-06-15 13:50:48 +05:00
|
|
|
<Paragraph>
|
2022-01-22 12:57:05 +05:00
|
|
|
Published note can only be viewed by someone with the password.
|
2021-06-15 13:50:48 +05:00
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
</TouchableOpacity>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<TouchableOpacity
|
|
|
|
|
onPress={() => {
|
|
|
|
|
setSelfDestruct(!selfDestruct);
|
|
|
|
|
}}
|
|
|
|
|
activeOpacity={0.9}
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: 'row',
|
2021-10-21 13:19:14 +05:00
|
|
|
alignItems: 'center'
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2022-02-28 13:48:59 +05:00
|
|
|
<IconButton
|
2021-06-15 13:50:48 +05:00
|
|
|
onPress={() => {
|
|
|
|
|
setSelfDestruct(!selfDestruct);
|
|
|
|
|
}}
|
|
|
|
|
color={selfDestruct ? colors.accent : colors.icon}
|
|
|
|
|
size={SIZE.lg}
|
2022-01-22 12:57:05 +05:00
|
|
|
name={selfDestruct ? 'check-circle-outline' : 'checkbox-blank-circle-outline'}
|
2021-06-15 13:50:48 +05:00
|
|
|
/>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
2021-10-21 13:19:14 +05:00
|
|
|
flexShrink: 1
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-16 11:45:42 +05:00
|
|
|
<Heading size={SIZE.md}>Self destruct</Heading>
|
2021-06-15 13:50:48 +05:00
|
|
|
<Paragraph>
|
2022-01-22 12:57:05 +05:00
|
|
|
Published note link will be automatically deleted once it is viewed by someone.
|
2021-06-15 13:50:48 +05:00
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
</TouchableOpacity>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: '100%',
|
|
|
|
|
alignSelf: 'center',
|
2021-10-21 13:19:14 +05:00
|
|
|
marginTop: 10
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-15 14:48:51 +05:00
|
|
|
{isLocked ? (
|
|
|
|
|
<>
|
|
|
|
|
<Input
|
2021-08-17 11:49:02 +05:00
|
|
|
fwdRef={pwdInput}
|
2021-06-15 14:48:51 +05:00
|
|
|
onChangeText={value => (passwordValue = value)}
|
|
|
|
|
blurOnSubmit
|
|
|
|
|
secureTextEntry
|
|
|
|
|
defaultValue={passwordValue}
|
|
|
|
|
placeholder="Enter Password"
|
|
|
|
|
/>
|
|
|
|
|
<Seperator half />
|
|
|
|
|
</>
|
|
|
|
|
) : null}
|
|
|
|
|
|
2021-06-14 08:38:03 +05:00
|
|
|
<Button
|
2021-06-15 13:50:48 +05:00
|
|
|
onPress={publishNote}
|
2021-06-14 08:38:03 +05:00
|
|
|
fontSize={SIZE.md}
|
|
|
|
|
width="100%"
|
2021-10-21 13:19:14 +05:00
|
|
|
style={{
|
|
|
|
|
marginTop: 10
|
|
|
|
|
}}
|
2021-06-14 08:38:03 +05:00
|
|
|
height={50}
|
2021-06-15 13:50:48 +05:00
|
|
|
type="accent"
|
|
|
|
|
title={isPublished ? 'Update published note' : 'Publish note'}
|
2021-06-14 08:38:03 +05:00
|
|
|
/>
|
2021-06-15 13:50:48 +05:00
|
|
|
|
|
|
|
|
{isPublished && (
|
|
|
|
|
<>
|
|
|
|
|
<Seperator half />
|
|
|
|
|
<Button
|
|
|
|
|
onPress={deletePublishedNote}
|
|
|
|
|
fontSize={SIZE.md}
|
|
|
|
|
width="100%"
|
|
|
|
|
height={50}
|
|
|
|
|
type="error"
|
|
|
|
|
title="Unpublish note"
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</View>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Paragraph
|
|
|
|
|
color={colors.icon}
|
2021-12-16 10:20:34 +05:00
|
|
|
size={SIZE.xs}
|
2021-06-15 13:50:48 +05:00
|
|
|
style={{
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
marginTop: 5,
|
2021-10-21 13:19:14 +05:00
|
|
|
textDecorationLine: 'underline'
|
2021-06-15 13:50:48 +05:00
|
|
|
}}
|
|
|
|
|
onPress={async () => {
|
|
|
|
|
try {
|
2022-01-22 12:57:05 +05:00
|
|
|
await openLinkInBrowser('https://docs.notesnook.com/monographs/', colors.accent);
|
2021-06-15 13:50:48 +05:00
|
|
|
} catch (e) {}
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-15 13:50:48 +05:00
|
|
|
Learn more about Notesnook Monograph
|
|
|
|
|
</Paragraph>
|
2021-06-14 08:38:03 +05:00
|
|
|
</View>
|
2021-12-25 11:16:33 +05:00
|
|
|
</SheetWrapper>
|
2021-06-14 08:38:03 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-28 15:32:55 +05:00
|
|
|
export default PublishNoteSheet;
|