rename back to flush-notes and add support of password in URL param

This commit is contained in:
Sasha 2023-11-26 14:34:13 +01:00
parent 0ed01bee20
commit 63a92cf825
No known key found for this signature in database
20 changed files with 31 additions and 21 deletions

View File

@ -1,11 +1,11 @@
{
"name": "flush-notes",
"name": "share-notes",
"version": "0.1.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "flush-notes",
"name": "share-notes",
"version": "0.1.0",
"dependencies": {
"@waku/interfaces": "^0.0.20",

View File

@ -1,5 +1,5 @@
{
"name": "share-notes",
"name": "flush-notes",
"version": "0.1.0",
"private": true,
"scripts": {

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -3,23 +3,23 @@
import React from "react";
import Markdown from "react-markdown";
import { useRouter } from "next/navigation";
import { useNoteHash } from "@/hooks/useNoteHash";
import { useNoteURL } from "@/hooks/useNoteURL";
import { notes } from "@/services/notes";
import { Loading } from "../Loading";
const View = () => {
const router = useRouter();
const noteHash = useNoteHash();
const { id, password } = useNoteURL();
const [note, setNote] = React.useState<string>("");
React.useEffect(() => {
if (!noteHash) {
if (!id) {
router.replace("/404");
return;
}
notes.readNote(noteHash).then((note) => setNote(note || ""));
}, [noteHash, setNote]);
notes.readNote(id, password).then((note) => setNote(note || ""));
}, [id, password, setNote]);
if (!note) {
return <Loading />;

View File

@ -0,0 +1,15 @@
"use client";
import { usePathname } from "next/navigation";
export const useNoteURL = (): undefined | string => {
const pathname = usePathname();
const urlParams = new URLSearchParams(window.location.search);
const segments = pathname.split("/");
const viewIndex = segments.indexOf("view");
const password = urlParams.get("password");
return {
password,
id: segments[viewIndex + 1] || undefined,
};
};

View File

@ -54,7 +54,10 @@ export class Notes {
return note.id;
}
public async readNote(id: string): Promise<string | undefined> {
public async readNote(
id: string,
password?: string
): Promise<string | undefined> {
await this.initMessages();
const message = this.messages
@ -75,14 +78,15 @@ export class Notes {
return message?.content;
}
const password = window.prompt("This note is encrypted, need password:");
const passwordReceived =
password || window.prompt("This note is encrypted, need password:");
if (!password) {
if (!passwordReceived) {
console.log("No password was provided, stopping reading a note.");
return;
}
return this.decryptNote(message, password);
return this.decryptNote(message, passwordReceived);
}
private async initMessages() {

View File

@ -1,9 +0,0 @@
import { usePathname } from "next/navigation";
export const useNoteHash = (): undefined | string => {
const pathname = usePathname();
const segments = pathname.split("/");
const viewIndex = segments.indexOf("view");
return segments[viewIndex + 1] || undefined;
};