rename back to flush-notes and add support of password in URL param
This commit is contained in:
parent
0ed01bee20
commit
63a92cf825
|
@ -1,11 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "flush-notes",
|
"name": "share-notes",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "flush-notes",
|
"name": "share-notes",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@waku/interfaces": "^0.0.20",
|
"@waku/interfaces": "^0.0.20",
|
|
@ -1,5 +1,5 @@
|
||||||
{
|
{
|
||||||
"name": "share-notes",
|
"name": "flush-notes",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB |
|
@ -3,23 +3,23 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import Markdown from "react-markdown";
|
import Markdown from "react-markdown";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useNoteHash } from "@/hooks/useNoteHash";
|
import { useNoteURL } from "@/hooks/useNoteURL";
|
||||||
import { notes } from "@/services/notes";
|
import { notes } from "@/services/notes";
|
||||||
import { Loading } from "../Loading";
|
import { Loading } from "../Loading";
|
||||||
|
|
||||||
const View = () => {
|
const View = () => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const noteHash = useNoteHash();
|
const { id, password } = useNoteURL();
|
||||||
const [note, setNote] = React.useState<string>("");
|
const [note, setNote] = React.useState<string>("");
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!noteHash) {
|
if (!id) {
|
||||||
router.replace("/404");
|
router.replace("/404");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
notes.readNote(noteHash).then((note) => setNote(note || ""));
|
notes.readNote(id, password).then((note) => setNote(note || ""));
|
||||||
}, [noteHash, setNote]);
|
}, [id, password, setNote]);
|
||||||
|
|
||||||
if (!note) {
|
if (!note) {
|
||||||
return <Loading />;
|
return <Loading />;
|
|
@ -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,
|
||||||
|
};
|
||||||
|
};
|
|
@ -54,7 +54,10 @@ export class Notes {
|
||||||
return note.id;
|
return note.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async readNote(id: string): Promise<string | undefined> {
|
public async readNote(
|
||||||
|
id: string,
|
||||||
|
password?: string
|
||||||
|
): Promise<string | undefined> {
|
||||||
await this.initMessages();
|
await this.initMessages();
|
||||||
|
|
||||||
const message = this.messages
|
const message = this.messages
|
||||||
|
@ -75,14 +78,15 @@ export class Notes {
|
||||||
return message?.content;
|
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.");
|
console.log("No password was provided, stopping reading a note.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.decryptNote(message, password);
|
return this.decryptNote(message, passwordReceived);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async initMessages() {
|
private async initMessages() {
|
|
@ -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;
|
|
||||||
};
|
|
Loading…
Reference in New Issue