From 8540258576ded23a529fd77fc8d4b1eb0bd91cf7 Mon Sep 17 00:00:00 2001 From: misterkirill Date: Thu, 10 Jul 2025 19:23:00 +0500 Subject: [PATCH] fix: use uuid npm package instead of crypto --- package-lock.json | 16 +++++++++++++++- package.json | 3 ++- src/app/notes/[id]/page.tsx | 27 +++++++++++++++++++++++++++ src/components/editor/Editor.tsx | 5 +++-- src/lib/editorReducer.ts | 8 +++++--- src/lib/notes.ts | 21 +++++++++++++++++++++ 6 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 src/app/notes/[id]/page.tsx diff --git a/package-lock.json b/package-lock.json index ae4f2c7..efcbce4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,7 +15,8 @@ "next": "15.3.4", "pg": "^8.16.3", "react": "^19.0.0", - "react-dom": "^19.0.0" + "react-dom": "^19.0.0", + "uuid": "^11.1.0" }, "devDependencies": { "@eslint/eslintrc": "^3", @@ -7489,6 +7490,19 @@ "punycode": "^2.1.0" } }, + "node_modules/uuid": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.0.tgz", + "integrity": "sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist/esm/bin/uuid" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", diff --git a/package.json b/package.json index 01c86c8..15089ce 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "next": "15.3.4", "pg": "^8.16.3", "react": "^19.0.0", - "react-dom": "^19.0.0" + "react-dom": "^19.0.0", + "uuid": "^11.1.0" }, "devDependencies": { "@eslint/eslintrc": "^3", diff --git a/src/app/notes/[id]/page.tsx b/src/app/notes/[id]/page.tsx new file mode 100644 index 0000000..4855480 --- /dev/null +++ b/src/app/notes/[id]/page.tsx @@ -0,0 +1,27 @@ +import { notFound } from "next/navigation"; +import { getNote } from "@/lib/notes"; +import { Metadata } from "next"; + +export async function generateMetadata({ params }: { params: Promise<{ id: string }> }): Promise { + const { id } = await params; + const note = await getNote(id); + if (!note) { + notFound(); + } + + return { title: note.name }; +} + +export default async function Note({ params }: { params: Promise<{ id: string }> }) { + const { id } = await params; + const note = await getNote(id); + if (!note) { + notFound(); + } + + return ( +
+ {note.name} +
+ ); +} diff --git a/src/components/editor/Editor.tsx b/src/components/editor/Editor.tsx index 0c925de..8f0c38d 100644 --- a/src/components/editor/Editor.tsx +++ b/src/components/editor/Editor.tsx @@ -2,6 +2,7 @@ import { useReducer } from "react"; import { Copy, Plus } from "lucide-react"; +import { v4 as uuidv4 } from "uuid"; import { editorReducer } from "@/lib/editorReducer"; import IconOnlyButton from "../ui/IconOnlyButton"; import Block from "./Block"; @@ -9,11 +10,11 @@ import Block from "./Block"; export default function Editor() { const [state, dispatch] = useReducer(editorReducer, [ { - id: crypto.randomUUID(), + id: uuidv4(), tag: "", locked: false, lines: Array.from({ length: 4 }, () => ({ - id: crypto.randomUUID(), + id: uuidv4(), text: "", })), } diff --git a/src/lib/editorReducer.ts b/src/lib/editorReducer.ts index c1ec579..0b0a6bd 100644 --- a/src/lib/editorReducer.ts +++ b/src/lib/editorReducer.ts @@ -1,3 +1,5 @@ +import { v4 as uuidv4 } from "uuid"; + export type ILine = { id: string; text: string; @@ -28,11 +30,11 @@ export function editorReducer(state: EditorState, action: Action): EditorState { return [ ...state, { - id: crypto.randomUUID(), + id: uuidv4(), tag: "", locked: false, lines: Array.from({ length: 4 }, () => ({ - id: crypto.randomUUID(), + id: uuidv4(), text: "", })), } @@ -61,7 +63,7 @@ export function editorReducer(state: EditorState, action: Action): EditorState { if (block.id === action.blockId) { return { ...block, - lines: [...block.lines, { id: crypto.randomUUID(), text: "" }], + lines: [...block.lines, { id: uuidv4(), text: "" }], }; } else { return block; diff --git a/src/lib/notes.ts b/src/lib/notes.ts index 0ee1727..57e3087 100644 --- a/src/lib/notes.ts +++ b/src/lib/notes.ts @@ -1,4 +1,5 @@ import { desc, eq } from "drizzle-orm"; +import { validate as uuidValidate } from "uuid"; import { notesTable } from "./db/schema"; import { getAuth } from "./auth"; import { db } from "./db"; @@ -10,6 +11,22 @@ export async function getNotes(authorId: string) { .orderBy(desc(notesTable.lastEdited)); } +export async function getNote(noteId: string) { + if (!uuidValidate(noteId)) { + return null; + } + + const notes = await db.select() + .from(notesTable) + .where(eq(notesTable.id, noteId)); + + if (notes.length === 0) { + return null; + } else { + return notes[0]; + } +} + export async function createNote() { const auth = await getAuth(); if (!auth) { @@ -25,6 +42,10 @@ export async function createNote() { } export async function deleteNote(noteId: string) { + if (!uuidValidate(noteId)) { + return null; + } + const auth = await getAuth(); if (!auth) { return null;