|
| 1 | +/* |
| 2 | +This file is part of the Notesnook project (https://notesnook.com/) |
| 3 | +
|
| 4 | +Copyright (C) 2023 Streetwriters (Private) Limited |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful, |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +import { useState } from "react"; |
| 21 | +import { Button, Flex, Text } from "@theme-ui/components"; |
| 22 | +import Dialog from "../components/dialog"; |
| 23 | +import { BaseDialogProps, DialogManager } from "../common/dialog-manager"; |
| 24 | +import { db } from "../common/db"; |
| 25 | +import Field from "../components/field"; |
| 26 | +import { showToast } from "../utils/toast"; |
| 27 | +import { SerializedKeyPair } from "@notesnook/crypto"; |
| 28 | +import { ConfirmDialog } from "./confirm"; |
| 29 | + |
| 30 | +type InboxPGPKeysDialogProps = BaseDialogProps<boolean> & { |
| 31 | + keys?: SerializedKeyPair | null; |
| 32 | +}; |
| 33 | + |
| 34 | +export const InboxPGPKeysDialog = DialogManager.register( |
| 35 | + function InboxPGPKeysDialog(props: InboxPGPKeysDialogProps) { |
| 36 | + const { keys: initialKeys, onClose } = props; |
| 37 | + const [mode, setMode] = useState<"choose" | "edit">( |
| 38 | + initialKeys ? "edit" : "choose" |
| 39 | + ); |
| 40 | + const [publicKey, setPublicKey] = useState(initialKeys?.publicKey || ""); |
| 41 | + const [privateKey, setPrivateKey] = useState(initialKeys?.privateKey || ""); |
| 42 | + const [isLoading, setIsLoading] = useState(false); |
| 43 | + |
| 44 | + const hasChanges = |
| 45 | + publicKey !== (initialKeys?.publicKey || "") || |
| 46 | + privateKey !== (initialKeys?.privateKey || ""); |
| 47 | + |
| 48 | + async function handleAutoGenerate() { |
| 49 | + try { |
| 50 | + setIsLoading(true); |
| 51 | + await db.user.getInboxKeys(); |
| 52 | + showToast("success", "Inbox keys generated"); |
| 53 | + onClose(true); |
| 54 | + } catch (error) { |
| 55 | + showToast("error", "Failed to generate inbox keys"); |
| 56 | + console.error(error); |
| 57 | + } finally { |
| 58 | + setIsLoading(false); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + async function handleSave() { |
| 63 | + const trimmedPublicKey = publicKey.trim(); |
| 64 | + const trimmedPrivateKey = privateKey.trim(); |
| 65 | + if (!trimmedPublicKey || !trimmedPrivateKey) { |
| 66 | + showToast("error", "Both public and private keys are required"); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + setIsLoading(true); |
| 72 | + const isValid = await db.storage().validatePGPKeyPair({ |
| 73 | + publicKey: trimmedPublicKey, |
| 74 | + privateKey: trimmedPrivateKey |
| 75 | + }); |
| 76 | + if (!isValid) { |
| 77 | + showToast( |
| 78 | + "error", |
| 79 | + "Invalid PGP key pair. Please check your keys and try again." |
| 80 | + ); |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + if (initialKeys) { |
| 85 | + const ok = await ConfirmDialog.show({ |
| 86 | + title: "Change Inbox PGP Keys", |
| 87 | + message: |
| 88 | + "Changing Inbox PGP keys will delete all your unsynced inbox items. Are you sure?", |
| 89 | + positiveButtonText: "Yes", |
| 90 | + negativeButtonText: "No" |
| 91 | + }); |
| 92 | + if (!ok) return; |
| 93 | + } |
| 94 | + |
| 95 | + await db.user.saveInboxKeys({ |
| 96 | + publicKey: trimmedPublicKey, |
| 97 | + privateKey: trimmedPrivateKey |
| 98 | + }); |
| 99 | + showToast("success", "Inbox keys saved"); |
| 100 | + onClose(true); |
| 101 | + } catch (error) { |
| 102 | + showToast("error", "Failed to save inbox keys"); |
| 103 | + console.error(error); |
| 104 | + } finally { |
| 105 | + setIsLoading(false); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (mode === "choose") { |
| 110 | + return ( |
| 111 | + <Dialog |
| 112 | + isOpen={true} |
| 113 | + title="Setup Inbox PGP Keys" |
| 114 | + width={500} |
| 115 | + negativeButton={{ |
| 116 | + text: "Cancel", |
| 117 | + onClick: () => onClose(false) |
| 118 | + }} |
| 119 | + > |
| 120 | + <Flex sx={{ flexDirection: "column", gap: 3 }}> |
| 121 | + <Text sx={{ fontSize: "body", color: "paragraph" }}> |
| 122 | + Choose how you want to set up your Inbox PGP keys: |
| 123 | + </Text> |
| 124 | + <Flex sx={{ flexDirection: "column", gap: 2 }}> |
| 125 | + <Button |
| 126 | + variant="secondary" |
| 127 | + onClick={handleAutoGenerate} |
| 128 | + disabled={isLoading} |
| 129 | + sx={{ width: "100%" }} |
| 130 | + > |
| 131 | + {isLoading ? "Generating..." : "Auto-generate keys"} |
| 132 | + </Button> |
| 133 | + <Text |
| 134 | + sx={{ |
| 135 | + fontSize: "body", |
| 136 | + color: "paragraph", |
| 137 | + textAlign: "center" |
| 138 | + }} |
| 139 | + > |
| 140 | + Or |
| 141 | + </Text> |
| 142 | + <Button |
| 143 | + variant="secondary" |
| 144 | + onClick={() => setMode("edit")} |
| 145 | + disabled={isLoading} |
| 146 | + sx={{ width: "100%" }} |
| 147 | + > |
| 148 | + Provide your own keys |
| 149 | + </Button> |
| 150 | + </Flex> |
| 151 | + </Flex> |
| 152 | + </Dialog> |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + return ( |
| 157 | + <Dialog |
| 158 | + isOpen={true} |
| 159 | + title="Inbox PGP Keys" |
| 160 | + width={600} |
| 161 | + positiveButton={{ |
| 162 | + text: isLoading ? "Saving..." : "Save", |
| 163 | + onClick: handleSave, |
| 164 | + disabled: isLoading || !hasChanges |
| 165 | + }} |
| 166 | + negativeButton={{ |
| 167 | + text: "Cancel", |
| 168 | + onClick: () => onClose(false) |
| 169 | + }} |
| 170 | + > |
| 171 | + <Flex sx={{ flexDirection: "column", gap: 3 }}> |
| 172 | + <Field |
| 173 | + label="Public Key" |
| 174 | + id="publicKey" |
| 175 | + name="publicKey" |
| 176 | + as="textarea" |
| 177 | + required |
| 178 | + value={publicKey} |
| 179 | + onChange={(e) => setPublicKey(e.target.value)} |
| 180 | + sx={{ |
| 181 | + fontFamily: "monospace", |
| 182 | + fontSize: "body", |
| 183 | + minHeight: 150, |
| 184 | + resize: "vertical" |
| 185 | + }} |
| 186 | + placeholder="Enter your PGP public key..." |
| 187 | + disabled={isLoading} |
| 188 | + /> |
| 189 | + <Field |
| 190 | + label="Private Key" |
| 191 | + id="privateKey" |
| 192 | + name="privateKey" |
| 193 | + as="textarea" |
| 194 | + required |
| 195 | + value={privateKey} |
| 196 | + onChange={(e) => setPrivateKey(e.target.value)} |
| 197 | + sx={{ |
| 198 | + fontFamily: "monospace", |
| 199 | + fontSize: "body", |
| 200 | + minHeight: 150, |
| 201 | + resize: "vertical" |
| 202 | + }} |
| 203 | + placeholder="Enter your PGP private key..." |
| 204 | + disabled={isLoading} |
| 205 | + /> |
| 206 | + </Flex> |
| 207 | + </Dialog> |
| 208 | + ); |
| 209 | + } |
| 210 | +); |
0 commit comments