2022-08-31 06:33:37 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
2023-01-16 13:44:52 +05:00
|
|
|
Copyright (C) 2023 Streetwriters (Private) Limited
|
2022-08-31 06:33:37 +05:00
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2022-08-30 16:13:11 +05:00
|
|
|
|
2022-08-26 16:19:39 +05:00
|
|
|
import Clipboard from "@react-native-clipboard/clipboard";
|
2023-03-16 21:22:21 +05:00
|
|
|
import React, { useRef, useState } from "react";
|
2022-08-26 16:19:39 +05:00
|
|
|
import { ActivityIndicator, TouchableOpacity, View } from "react-native";
|
|
|
|
|
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
|
2022-08-29 16:19:17 +05:00
|
|
|
import { db } from "../../../common/database";
|
2023-03-16 21:22:21 +05:00
|
|
|
import { presentSheet, ToastEvent } from "../../../services/event-manager";
|
2022-08-26 16:19:39 +05:00
|
|
|
import Navigation from "../../../services/navigation";
|
2022-08-29 16:19:17 +05:00
|
|
|
import { useAttachmentStore } from "../../../stores/use-attachment-store";
|
2023-08-01 12:07:21 +05:00
|
|
|
import { useThemeColors } from "@notesnook/theme";
|
2022-08-26 16:19:39 +05:00
|
|
|
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 Heading from "../../ui/typography/heading";
|
|
|
|
|
import Paragraph from "../../ui/typography/paragraph";
|
2023-05-17 11:14:49 +05:00
|
|
|
import { requestInAppReview } from "../../../services/app-review";
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2023-03-16 21:22:21 +05:00
|
|
|
const PublishNoteSheet = ({ note: item, update }) => {
|
2023-08-01 12:07:21 +05:00
|
|
|
const { colors } = useThemeColors();
|
2021-06-14 08:38:03 +05:00
|
|
|
const actionSheetRef = useRef();
|
2022-08-26 16:19:39 +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);
|
2023-03-16 21:22:21 +05:00
|
|
|
const [note, setNote] = useState(item);
|
2021-06-15 13:50:48 +05:00
|
|
|
const [publishing, setPublishing] = useState(false);
|
|
|
|
|
const publishUrl =
|
2022-08-26 16:19:39 +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();
|
2023-03-16 21:22:21 +05:00
|
|
|
const passwordValue = useRef();
|
2021-06-14 08:38:03 +05:00
|
|
|
|
|
|
|
|
const publishNote = async () => {
|
2021-06-15 13:50:48 +05:00
|
|
|
if (publishing) return;
|
2023-03-16 21:22:21 +05:00
|
|
|
setPublishLoading(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,
|
2023-03-23 13:23:04 +05:00
|
|
|
password: isLocked && passwordValue.current
|
2021-06-15 13:50:48 +05:00
|
|
|
});
|
|
|
|
|
setNote(db.notes.note(note.id)?.data);
|
2023-04-01 14:06:16 +05:00
|
|
|
Navigation.queueRoutesForUpdate();
|
2023-03-16 21:22:21 +05:00
|
|
|
setPublishLoading(false);
|
2021-06-15 13:50:48 +05:00
|
|
|
}
|
2023-05-17 11:14:49 +05:00
|
|
|
requestInAppReview();
|
2021-06-15 13:50:48 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.show({
|
2022-08-26 16:19:39 +05:00
|
|
|
heading: "Could not publish note",
|
2021-06-15 13:50:48 +05:00
|
|
|
message: e.message,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "error",
|
|
|
|
|
context: "local"
|
2021-06-14 08:38:03 +05:00
|
|
|
});
|
|
|
|
|
}
|
2021-10-21 13:19:14 +05:00
|
|
|
|
2023-03-16 21:22:21 +05:00
|
|
|
setPublishLoading(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setPublishLoading = (value) => {
|
|
|
|
|
setPublishing(value);
|
2021-06-14 08:38:03 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const deletePublishedNote = async () => {
|
2021-06-15 13:50:48 +05:00
|
|
|
if (publishing) return;
|
2023-03-16 21:22:21 +05:00
|
|
|
setPublishLoading(true);
|
2021-06-15 13:50:48 +05:00
|
|
|
try {
|
|
|
|
|
if (note?.id) {
|
|
|
|
|
await db.monographs.unpublish(note.id);
|
|
|
|
|
setNote(db.notes.note(note.id)?.data);
|
2023-04-01 14:06:16 +05:00
|
|
|
Navigation.queueRoutesForUpdate();
|
2023-03-16 21:22:21 +05:00
|
|
|
setPublishLoading(false);
|
2021-06-15 13:50:48 +05:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
ToastEvent.show({
|
2022-08-26 16:19:39 +05:00
|
|
|
heading: "Could not unpublish note",
|
2021-06-15 13:50:48 +05:00
|
|
|
message: e.message,
|
2022-08-26 16:19:39 +05:00
|
|
|
type: "error",
|
|
|
|
|
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();
|
2023-03-16 21:22:21 +05:00
|
|
|
setPublishLoading(false);
|
2021-06-14 08:38:03 +05:00
|
|
|
};
|
|
|
|
|
|
2023-03-16 21:22:21 +05:00
|
|
|
return (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
alignSelf: "center",
|
|
|
|
|
paddingHorizontal: 12
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2023-03-16 21:22:21 +05:00
|
|
|
<DialogHeader
|
|
|
|
|
title={note?.title}
|
|
|
|
|
paragraph={`Anyone with the link${
|
|
|
|
|
isLocked ? " and password" : ""
|
|
|
|
|
} of the published note can view it.`}
|
|
|
|
|
/>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2023-03-16 21:22:21 +05:00
|
|
|
{publishing ? (
|
|
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignContent: "center",
|
|
|
|
|
height: 150,
|
|
|
|
|
width: "100%"
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-08-01 12:07:21 +05:00
|
|
|
<ActivityIndicator size={25} color={colors.primary.accent} />
|
2023-03-16 21:22:21 +05:00
|
|
|
<Paragraph
|
2021-06-14 08:38:03 +05:00
|
|
|
style={{
|
2023-03-16 21:22:21 +05:00
|
|
|
textAlign: "center"
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2023-03-16 21:22:21 +05:00
|
|
|
Please wait...
|
|
|
|
|
{loading && loading.current && loading.total
|
|
|
|
|
? `\nDownloading attachments (${
|
|
|
|
|
loading?.current / loading?.total
|
|
|
|
|
})`
|
|
|
|
|
: ""}
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{isPublished && (
|
|
|
|
|
<View
|
2021-06-14 08:38:03 +05:00
|
|
|
style={{
|
2023-03-16 21:22:21 +05:00
|
|
|
flexDirection: "row",
|
|
|
|
|
alignItems: "center",
|
2023-03-24 00:12:11 +05:00
|
|
|
marginTop: 10,
|
2023-08-01 12:07:21 +05:00
|
|
|
backgroundColor: colors.secondary.background,
|
2023-03-16 21:22:21 +05:00
|
|
|
padding: 12,
|
|
|
|
|
borderRadius: 5
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
2021-06-14 08:38:03 +05:00
|
|
|
style={{
|
2023-03-16 21:22:21 +05:00
|
|
|
width: "100%",
|
|
|
|
|
flexShrink: 1
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2023-03-24 00:12:11 +05:00
|
|
|
<Heading size={SIZE.md}>Published at:</Heading>
|
|
|
|
|
<Paragraph size={SIZE.sm} numberOfLines={1}>
|
2023-03-16 21:22:21 +05:00
|
|
|
{publishUrl}
|
|
|
|
|
</Paragraph>
|
|
|
|
|
<Paragraph
|
|
|
|
|
onPress={async () => {
|
|
|
|
|
try {
|
2023-08-01 12:07:21 +05:00
|
|
|
await openLinkInBrowser(
|
|
|
|
|
publishUrl,
|
|
|
|
|
colors.primary.accent
|
|
|
|
|
);
|
2023-03-16 21:22:21 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
size={SIZE.xs}
|
2021-06-15 13:50:48 +05:00
|
|
|
style={{
|
2023-03-16 21:22:21 +05:00
|
|
|
marginTop: 5,
|
2023-08-01 12:07:21 +05:00
|
|
|
color: colors.primary.paragraph
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2023-08-01 12:07:21 +05:00
|
|
|
<Icon color={colors.primary.accent} name="open-in-new" /> Open
|
|
|
|
|
in browser
|
2023-03-16 21:22:21 +05:00
|
|
|
</Paragraph>
|
2021-06-15 13:50:48 +05:00
|
|
|
</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={() => {
|
2023-03-16 21:22:21 +05:00
|
|
|
Clipboard.setString(publishUrl);
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: "Note publish url copied",
|
|
|
|
|
type: "success",
|
|
|
|
|
context: "local"
|
|
|
|
|
});
|
2021-06-15 13:50:48 +05:00
|
|
|
}}
|
2023-08-01 12:07:21 +05:00
|
|
|
color={colors.primary.accent}
|
2021-06-15 13:50:48 +05:00
|
|
|
size={SIZE.lg}
|
2023-03-16 21:22:21 +05:00
|
|
|
name="content-copy"
|
2021-06-15 13:50:48 +05:00
|
|
|
/>
|
2023-03-16 21:22:21 +05:00
|
|
|
</View>
|
|
|
|
|
)}
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2023-03-16 21:22:21 +05:00
|
|
|
<TouchableOpacity
|
|
|
|
|
onPress={() => {
|
|
|
|
|
if (publishing) return;
|
|
|
|
|
setIsLocked(!isLocked);
|
|
|
|
|
}}
|
|
|
|
|
activeOpacity={0.9}
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: "row",
|
|
|
|
|
alignItems: "center",
|
2023-03-24 00:12:11 +05:00
|
|
|
marginBottom: 10,
|
2023-08-01 12:07:21 +05:00
|
|
|
backgroundColor: colors.secondary.background,
|
2023-03-24 00:12:11 +05:00
|
|
|
paddingVertical: 12,
|
|
|
|
|
borderRadius: 5,
|
|
|
|
|
marginTop: 10
|
2023-03-16 21:22:21 +05:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<IconButton
|
2021-06-15 13:50:48 +05:00
|
|
|
onPress={() => {
|
2023-03-16 21:22:21 +05:00
|
|
|
if (publishing) return;
|
|
|
|
|
setIsLocked(!isLocked);
|
2021-06-15 13:50:48 +05:00
|
|
|
}}
|
2023-08-01 12:07:21 +05:00
|
|
|
color={isLocked ? colors.selected.icon : colors.primary.icon}
|
2023-03-24 00:12:11 +05:00
|
|
|
size={SIZE.xl}
|
2023-03-16 21:22:21 +05:00
|
|
|
name={
|
|
|
|
|
isLocked
|
|
|
|
|
? "check-circle-outline"
|
|
|
|
|
: "checkbox-blank-circle-outline"
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<View
|
2021-06-15 13:50:48 +05:00
|
|
|
style={{
|
2023-03-16 21:22:21 +05:00
|
|
|
width: "100%",
|
|
|
|
|
flexShrink: 1
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2023-03-16 21:22:21 +05:00
|
|
|
<Heading size={SIZE.md}>Password protection</Heading>
|
|
|
|
|
<Paragraph>
|
|
|
|
|
Published note can only be viewed by someone with the password.
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
</TouchableOpacity>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2023-03-16 21:22:21 +05:00
|
|
|
<TouchableOpacity
|
|
|
|
|
onPress={() => {
|
|
|
|
|
setSelfDestruct(!selfDestruct);
|
|
|
|
|
}}
|
|
|
|
|
activeOpacity={0.9}
|
|
|
|
|
style={{
|
|
|
|
|
flexDirection: "row",
|
2023-03-24 00:12:11 +05:00
|
|
|
alignItems: "center",
|
2023-08-01 12:07:21 +05:00
|
|
|
backgroundColor: colors.secondary.background,
|
2023-03-24 00:12:11 +05:00
|
|
|
paddingVertical: 12,
|
|
|
|
|
borderRadius: 5
|
2023-03-16 21:22:21 +05:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<IconButton
|
|
|
|
|
onPress={() => {
|
|
|
|
|
setSelfDestruct(!selfDestruct);
|
|
|
|
|
}}
|
2023-08-01 12:07:21 +05:00
|
|
|
color={selfDestruct ? colors.selected.icon : colors.primary.icon}
|
2023-03-24 00:12:11 +05:00
|
|
|
size={SIZE.xl}
|
2023-03-16 21:22:21 +05:00
|
|
|
name={
|
|
|
|
|
selfDestruct
|
|
|
|
|
? "check-circle-outline"
|
|
|
|
|
: "checkbox-blank-circle-outline"
|
|
|
|
|
}
|
|
|
|
|
/>
|
2021-06-14 08:38:03 +05:00
|
|
|
|
2021-06-15 13:50:48 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
2022-08-26 16:19:39 +05:00
|
|
|
width: "100%",
|
2023-03-16 21:22:21 +05:00
|
|
|
flexShrink: 1
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2023-03-16 21:22:21 +05:00
|
|
|
<Heading size={SIZE.md}>Self destruct</Heading>
|
|
|
|
|
<Paragraph>
|
|
|
|
|
Published note link will be automatically deleted once it is
|
|
|
|
|
viewed by someone.
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
|
|
|
|
</TouchableOpacity>
|
2021-06-15 14:48:51 +05:00
|
|
|
|
2023-03-16 21:22:21 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
|
|
|
|
width: "100%",
|
|
|
|
|
alignSelf: "center",
|
|
|
|
|
marginTop: 10
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{isLocked ? (
|
|
|
|
|
<>
|
|
|
|
|
<Input
|
|
|
|
|
fwdRef={pwdInput}
|
|
|
|
|
onChangeText={(value) => (passwordValue.current = value)}
|
|
|
|
|
blurOnSubmit
|
|
|
|
|
secureTextEntry
|
|
|
|
|
defaultValue={passwordValue.current}
|
|
|
|
|
placeholder="Enter Password"
|
|
|
|
|
/>
|
|
|
|
|
<Seperator half />
|
|
|
|
|
</>
|
|
|
|
|
) : null}
|
2021-06-15 13:50:48 +05:00
|
|
|
|
2023-03-23 13:39:14 +05:00
|
|
|
<View
|
2023-03-16 21:22:21 +05:00
|
|
|
style={{
|
2023-03-23 13:39:14 +05:00
|
|
|
flexDirection: "row",
|
|
|
|
|
width: "100%",
|
2023-03-24 00:12:11 +05:00
|
|
|
justifyContent: "center"
|
2023-03-16 21:22:21 +05:00
|
|
|
}}
|
2023-03-23 13:39:14 +05:00
|
|
|
>
|
|
|
|
|
{isPublished && (
|
|
|
|
|
<>
|
|
|
|
|
<Button
|
|
|
|
|
onPress={deletePublishedNote}
|
|
|
|
|
fontSize={SIZE.md}
|
|
|
|
|
type="error"
|
|
|
|
|
title="Unpublish"
|
|
|
|
|
style={{
|
|
|
|
|
width: "49%"
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2023-03-24 00:12:11 +05:00
|
|
|
<Seperator half />
|
2023-03-23 13:39:14 +05:00
|
|
|
<Button
|
|
|
|
|
onPress={publishNote}
|
|
|
|
|
fontSize={SIZE.md}
|
|
|
|
|
style={{
|
2023-03-24 00:12:11 +05:00
|
|
|
width: isPublished ? "49%" : 250,
|
|
|
|
|
borderRadius: isPublished ? 5 : 100
|
2023-03-23 13:39:14 +05:00
|
|
|
}}
|
|
|
|
|
type="accent"
|
|
|
|
|
title={isPublished ? "Update" : "Publish"}
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
2023-03-16 21:22:21 +05:00
|
|
|
</View>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Paragraph
|
2023-08-01 12:07:21 +05:00
|
|
|
color={colors.secondary.paragraph}
|
2023-03-16 21:22:21 +05:00
|
|
|
size={SIZE.xs}
|
|
|
|
|
style={{
|
|
|
|
|
textAlign: "center",
|
2023-03-24 00:12:11 +05:00
|
|
|
marginTop: 10,
|
2023-03-16 21:22:21 +05:00
|
|
|
textDecorationLine: "underline"
|
|
|
|
|
}}
|
|
|
|
|
onPress={async () => {
|
|
|
|
|
try {
|
|
|
|
|
await openLinkInBrowser(
|
|
|
|
|
"https://docs.notesnook.com/monographs/",
|
2023-08-01 12:07:21 +05:00
|
|
|
colors.primary.accent
|
2023-03-16 21:22:21 +05:00
|
|
|
);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Learn more about Notesnook Monograph
|
|
|
|
|
</Paragraph>
|
|
|
|
|
</View>
|
2021-06-14 08:38:03 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-16 21:22:21 +05:00
|
|
|
PublishNoteSheet.present = (note) => {
|
|
|
|
|
presentSheet({
|
|
|
|
|
component: (ref, close, update) => (
|
|
|
|
|
<PublishNoteSheet
|
|
|
|
|
actionSheetRef={ref}
|
|
|
|
|
close={close}
|
|
|
|
|
update={update}
|
|
|
|
|
note={note}
|
|
|
|
|
/>
|
|
|
|
|
)
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-28 15:32:55 +05:00
|
|
|
export default PublishNoteSheet;
|