fix: use uuid npm package instead of crypto

This commit is contained in:
Kirill Siukhin 2025-07-10 19:23:00 +05:00
parent 8aaf8bb42d
commit 8540258576
6 changed files with 73 additions and 7 deletions

16
package-lock.json generated
View File

@ -15,7 +15,8 @@
"next": "15.3.4", "next": "15.3.4",
"pg": "^8.16.3", "pg": "^8.16.3",
"react": "^19.0.0", "react": "^19.0.0",
"react-dom": "^19.0.0" "react-dom": "^19.0.0",
"uuid": "^11.1.0"
}, },
"devDependencies": { "devDependencies": {
"@eslint/eslintrc": "^3", "@eslint/eslintrc": "^3",
@ -7489,6 +7490,19 @@
"punycode": "^2.1.0" "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": { "node_modules/which": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",

View File

@ -16,7 +16,8 @@
"next": "15.3.4", "next": "15.3.4",
"pg": "^8.16.3", "pg": "^8.16.3",
"react": "^19.0.0", "react": "^19.0.0",
"react-dom": "^19.0.0" "react-dom": "^19.0.0",
"uuid": "^11.1.0"
}, },
"devDependencies": { "devDependencies": {
"@eslint/eslintrc": "^3", "@eslint/eslintrc": "^3",

View File

@ -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<Metadata> {
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 (
<div>
{note.name}
</div>
);
}

View File

@ -2,6 +2,7 @@
import { useReducer } from "react"; import { useReducer } from "react";
import { Copy, Plus } from "lucide-react"; import { Copy, Plus } from "lucide-react";
import { v4 as uuidv4 } from "uuid";
import { editorReducer } from "@/lib/editorReducer"; import { editorReducer } from "@/lib/editorReducer";
import IconOnlyButton from "../ui/IconOnlyButton"; import IconOnlyButton from "../ui/IconOnlyButton";
import Block from "./Block"; import Block from "./Block";
@ -9,11 +10,11 @@ import Block from "./Block";
export default function Editor() { export default function Editor() {
const [state, dispatch] = useReducer(editorReducer, [ const [state, dispatch] = useReducer(editorReducer, [
{ {
id: crypto.randomUUID(), id: uuidv4(),
tag: "", tag: "",
locked: false, locked: false,
lines: Array.from({ length: 4 }, () => ({ lines: Array.from({ length: 4 }, () => ({
id: crypto.randomUUID(), id: uuidv4(),
text: "", text: "",
})), })),
} }

View File

@ -1,3 +1,5 @@
import { v4 as uuidv4 } from "uuid";
export type ILine = { export type ILine = {
id: string; id: string;
text: string; text: string;
@ -28,11 +30,11 @@ export function editorReducer(state: EditorState, action: Action): EditorState {
return [ return [
...state, ...state,
{ {
id: crypto.randomUUID(), id: uuidv4(),
tag: "", tag: "",
locked: false, locked: false,
lines: Array.from({ length: 4 }, () => ({ lines: Array.from({ length: 4 }, () => ({
id: crypto.randomUUID(), id: uuidv4(),
text: "", text: "",
})), })),
} }
@ -61,7 +63,7 @@ export function editorReducer(state: EditorState, action: Action): EditorState {
if (block.id === action.blockId) { if (block.id === action.blockId) {
return { return {
...block, ...block,
lines: [...block.lines, { id: crypto.randomUUID(), text: "" }], lines: [...block.lines, { id: uuidv4(), text: "" }],
}; };
} else { } else {
return block; return block;

View File

@ -1,4 +1,5 @@
import { desc, eq } from "drizzle-orm"; import { desc, eq } from "drizzle-orm";
import { validate as uuidValidate } from "uuid";
import { notesTable } from "./db/schema"; import { notesTable } from "./db/schema";
import { getAuth } from "./auth"; import { getAuth } from "./auth";
import { db } from "./db"; import { db } from "./db";
@ -10,6 +11,22 @@ export async function getNotes(authorId: string) {
.orderBy(desc(notesTable.lastEdited)); .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() { export async function createNote() {
const auth = await getAuth(); const auth = await getAuth();
if (!auth) { if (!auth) {
@ -25,6 +42,10 @@ export async function createNote() {
} }
export async function deleteNote(noteId: string) { export async function deleteNote(noteId: string) {
if (!uuidValidate(noteId)) {
return null;
}
const auth = await getAuth(); const auth = await getAuth();
if (!auth) { if (!auth) {
return null; return null;