diff --git a/components/ModalContent/NewLinkModal.tsx b/components/ModalContent/NewLinkModal.tsx index 46c9ffe..756f0e4 100644 --- a/components/ModalContent/NewLinkModal.tsx +++ b/components/ModalContent/NewLinkModal.tsx @@ -155,7 +155,7 @@ export default function NewLinkModal({ onClose }: Props) { setLink({ ...link, name: e.target.value })} - placeholder="e.g. Example Link" + placeholder="Will be auto generated if left empty." className="bg-base-200" /> @@ -177,7 +177,7 @@ export default function NewLinkModal({ onClose }: Props) { onChange={(e) => setLink({ ...link, description: e.target.value }) } - placeholder="Will be auto generated if nothing is provided." + placeholder="Notes, thoughts, etc." className="resize-none w-full rounded-md p-2 border-neutral-content bg-base-200 focus:border-primary border-solid border outline-none duration-100" /> diff --git a/lib/api/controllers/links/postLink.ts b/lib/api/controllers/links/postLink.ts index 4c42f6c..7f77aeb 100644 --- a/lib/api/controllers/links/postLink.ts +++ b/lib/api/controllers/links/postLink.ts @@ -160,12 +160,13 @@ export default async function postLink( link.collection.name = link.collection.name.trim(); - const description = - link.description && link.description !== "" - ? link.description - : link.url - ? await getTitle(link.url) - : undefined; + const title = + !(link.name && link.name !== "") && link.url + ? await getTitle(link.url) + : ""; + + const name = + link.name && link.name !== "" ? link.name : link.url ? title : ""; const validatedUrl = link.url ? await validateUrlSize(link.url) : undefined; @@ -184,8 +185,8 @@ export default async function postLink( const newLink = await prisma.link.create({ data: { url: link.url?.trim().replace(/\/+$/, "") || null, - name: link.name, - description, + name, + description: link.description, type: linkType, collection: { connect: { diff --git a/prisma/migrations/20240525002215_add_default_value_to_link_name/migration.sql b/prisma/migrations/20240525002215_add_default_value_to_link_name/migration.sql new file mode 100644 index 0000000..b91c845 --- /dev/null +++ b/prisma/migrations/20240525002215_add_default_value_to_link_name/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Link" ALTER COLUMN "name" SET DEFAULT ''; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index bd4e982..6bc3200 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -123,7 +123,7 @@ model UsersAndCollections { model Link { id Int @id @default(autoincrement()) - name String + name String @default("") type String @default("url") description String @default("") pinnedBy User[] diff --git a/scripts/migration/descriptionToName.js b/scripts/migration/descriptionToName.js new file mode 100644 index 0000000..16b85cc --- /dev/null +++ b/scripts/migration/descriptionToName.js @@ -0,0 +1,70 @@ +// [Optional, but recommended] + +// We decided that the "name" field should be the auto-generated field instead of the "description" field, so we need to +// move the data from the "description" field to the "name" field for links that have an empty name. + +// This script is meant to be run only once. + +const { PrismaClient } = require("@prisma/client"); + +const prisma = new PrismaClient(); + +async function main() { + console.log("Starting..."); + + const count = await prisma.link.count({ + where: { + name: "", + description: { + not: "", + }, + }, + }); + + console.log( + `Applying the changes to ${count} ${ + count == 1 ? "link" : "links" + } in 10 seconds...` + ); + + await new Promise((resolve) => setTimeout(resolve, 10000)); + + console.log("Applying the changes..."); + + const links = await prisma.link.findMany({ + where: { + name: "", + description: { + not: "", + }, + }, + select: { + id: true, + description: true, + }, + }); + + for (const link of links) { + await prisma.link.update({ + where: { + id: link.id, + }, + data: { + name: link.description, + description: "", + }, + }); + } + + console.log("Done!"); +} + +main() + .catch((e) => { + throw e; + }) + .finally(async () => { + await prisma.$disconnect(); + }); + +// Run the script with `node scripts/migration/descriptionToName.js`