From 1ccc723542d65aa35a76ab3e11a56dd658a47a59 Mon Sep 17 00:00:00 2001 From: Philipp Traber Date: Tue, 15 Oct 2024 23:04:30 +0200 Subject: [PATCH] connect custom pages and link pages with link blocks --- .gitignore | 1 + next.config.mjs | 4 + package-lock.json | 7 + package.json | 1 + prisma/schema.prisma | 85 ++++++++-- src/actions/block_actions.ts | 110 +++++++++++++ src/actions/page_actions.ts | 149 ++++++++++++++++++ src/actions/url_actions.ts | 77 --------- src/app/[shortcode]/page.tsx | 53 +++++++ src/app/[url_shortcode]/page.tsx | 31 ---- src/app/globals.css | 16 +- src/app/~/dash/[url_shortcode]/page.tsx | 48 ------ src/app/~/dash/[uuid]/page.tsx | 109 +++++++++++++ src/app/~/dash/page.tsx | 37 +++-- src/components/CustomPageComponent.tsx | 47 ++++++ src/components/CustomPageDashboard.tsx | 51 ++++++ src/components/LinkFavicon.tsx | 17 ++ src/components/PageListComponent.tsx | 52 ++++++ src/components/UrlComponent.tsx | 33 ---- src/components/forms/createCustomPageForm.tsx | 37 +++++ src/components/forms/createLinkBlockForm.tsx | 40 +++++ .../forms/createRedirectPageForm.tsx | 30 ++++ src/components/forms/createUrlForm.tsx | 33 ---- src/components/forms/deleteLinkBlockForm.tsx | 23 +++ src/components/forms/deletePageForm.tsx | 23 +++ src/components/forms/deleteUrlForm.tsx | 22 --- src/utils.tsx | 10 ++ 27 files changed, 870 insertions(+), 276 deletions(-) create mode 100644 src/actions/block_actions.ts create mode 100644 src/actions/page_actions.ts delete mode 100644 src/actions/url_actions.ts create mode 100644 src/app/[shortcode]/page.tsx delete mode 100644 src/app/[url_shortcode]/page.tsx delete mode 100644 src/app/~/dash/[url_shortcode]/page.tsx create mode 100644 src/app/~/dash/[uuid]/page.tsx create mode 100644 src/components/CustomPageComponent.tsx create mode 100644 src/components/CustomPageDashboard.tsx create mode 100644 src/components/LinkFavicon.tsx create mode 100644 src/components/PageListComponent.tsx delete mode 100644 src/components/UrlComponent.tsx create mode 100644 src/components/forms/createCustomPageForm.tsx create mode 100644 src/components/forms/createLinkBlockForm.tsx create mode 100644 src/components/forms/createRedirectPageForm.tsx delete mode 100644 src/components/forms/createUrlForm.tsx create mode 100644 src/components/forms/deleteLinkBlockForm.tsx create mode 100644 src/components/forms/deletePageForm.tsx delete mode 100644 src/components/forms/deleteUrlForm.tsx create mode 100644 src/utils.tsx diff --git a/.gitignore b/.gitignore index 925ce47..d38f79e 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ yarn-error.log* next-env.d.ts .env +.vol_db diff --git a/next.config.mjs b/next.config.mjs index d4ad6d6..0f3965a 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -6,6 +6,10 @@ const nextConfig = { protocol: 'https', hostname: 'www.google.com', }, + { + protocol: 'https', + hostname: 'logo.clearbit.com', + } ] } diff --git a/package-lock.json b/package-lock.json index 982ea14..a7420f6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.1.0", "dependencies": { "@prisma/client": "^5.18.0", + "js-sha256": "^0.11.0", "next": "^15.0.0-canary.135", "ua-parser-js": "^1.0.38", "zod": "^3.23.8" @@ -3631,6 +3632,12 @@ "jiti": "bin/jiti.js" } }, + "node_modules/js-sha256": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.11.0.tgz", + "integrity": "sha512-6xNlKayMZvds9h1Y1VWc0fQHQ82BxTXizWPEtEeGvmOUYpBRy4gbWroHLpzowe6xiQhHpelCQiE7HEdznyBL9Q==", + "license": "MIT" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/package.json b/package.json index 3b32ceb..d687471 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "@prisma/client": "^5.18.0", + "js-sha256": "^0.11.0", "next": "^15.0.0-canary.135", "ua-parser-js": "^1.0.38", "zod": "^3.23.8" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5c86ab5..7150577 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -7,26 +7,77 @@ datasource db { url = env("DATABASE_URL") } -model URL { - url_id Int @id @default(autoincrement()) - original_url String @db.Text - short_code String @unique - created_at DateTime @default(now()) - expires_at DateTime? - stats Stat[] // Relation to URL_Statistics +// The basis for the pages providing a shortcode to access them +model Page { + uuid String @id @default(uuid()) + shortcode String @unique + created_at DateTime @default(now()) + updated_at DateTime @updatedAt + RedirectPage RedirectPage[] + CustomPage CustomPage[] + Stat Stat[] - @@index([short_code]) + @@index([shortcode]) } +// The redirect page for the url shortener +model RedirectPage { + uuid String @id @default(uuid()) + created_at DateTime @default(now()) + updated_at DateTime @updatedAt + dest String + + page Page @relation(fields: [pageUuid], references: [uuid]) + pageUuid String + LinkBlock LinkBlock[] +} + +// The custom page to display mutliple blocks of text and links +model CustomPage { + uuid String @id @default(uuid()) + title String + image String? + created_at DateTime @default(now()) + updated_at DateTime @updatedAt + + page Page @relation(fields: [pageUuid], references: [uuid]) + pageUuid String + TextBlock TextBlock[] + LinkBlock LinkBlock[] +} + +// Text block to be displayed on the custom page +// To provide additional information +model TextBlock { + uuid String @id @default(uuid()) + order Int + text String + created_at DateTime @default(now()) + updated_at DateTime @updatedAt + + custom_page CustomPage @relation(fields: [customPageUuid], references: [uuid]) + customPageUuid String +} + +// Link block to integrate a RedirectPage into the custom page +model LinkBlock { + uuid String @id @default(uuid()) + order Int + title String + + custom_page CustomPage @relation(fields: [customPageUuid], references: [uuid]) + redirect_page RedirectPage @relation(fields: [redirectPageUuid], references: [uuid]) + customPageUuid String + redirectPageUuid String +} + +// Statts to track the links being accessed model Stat { - stat_id Int @id @default(autoincrement()) - url_id Int - accessed_at DateTime @default(now()) - hashed_ip String? @db.VarChar(64) - user_agent String? @db.Text + uuid String @id @default(uuid()) + accessed_at DateTime @default(now()) + hash String? @db.VarChar(64) + user_agent String? @db.Text - // Foreign key relation to URLs - url URL @relation(fields: [url_id], references: [url_id]) - - @@index([url_id]) + page Page @relation(fields: [pageUuid], references: [uuid]) + pageUuid String } diff --git a/src/actions/block_actions.ts b/src/actions/block_actions.ts new file mode 100644 index 0000000..bcd424f --- /dev/null +++ b/src/actions/block_actions.ts @@ -0,0 +1,110 @@ +"use server" + +import { z } from "zod"; +import prisma from "../../prisma/prisma"; +import { revalidatePath } from "next/cache"; +import { title } from "process"; + + +export async function createLinkBlock(prevState: any, formData: FormData) { + + const schema = z.object({ + customPageUuid: z.string().uuid(), + redirectPageShortcode: z.string(), + title: z.string().max(100), + order: z.coerce.number() + }); + + const data = schema.safeParse({ + customPageUuid: formData.get("customPageUuid"), + redirectPageShortcode: formData.get("redirectPageShortcode"), + title: formData.get("title"), + order: formData.get("order") + }); + + if (!data.success) { + return { + message: "Invalid data", + error: data.error.flatten().fieldErrors, + }; + } + + const customPage = await prisma.customPage.findUnique({ + where: { uuid: data.data.customPageUuid }, + include: { + page: true + } + }); + if (!customPage) { + return { + message: "Custom page not found", + error: {} + }; + } + + const page = await prisma.page.findUnique({ + where: { shortcode: data.data.redirectPageShortcode }, + include: { RedirectPage: true } + }); + const redirectPage = page?.RedirectPage[0] + if (!redirectPage) { + return { + message: "Redirect page not found", + error: {} + }; + } + + try { + await prisma.linkBlock.create({ + data: { + customPageUuid: data.data.customPageUuid, + redirectPageUuid: redirectPage?.uuid, + title: data.data.title, + order: data.data.order + } + }); + } catch (e: any) { + return { + message: "Error creating link block", + error: e.message + }; + } + + revalidatePath("/dash"); + revalidatePath(`/~/dash/${data.data.customPageUuid}/page`); + revalidatePath(`/${customPage.page.shortcode}`); + + return { + message: "Link block created" + }; +} + +export async function deleteLinkBlock(prevState: any, formData: FormData) { + const schema = z.object({ + uuid: z.string().uuid() + }); + + const data = schema.safeParse({ + uuid: formData.get("uuid") + }); + + if (!data.success) { + return { + message: "Invalid data", + error: data.error.flatten().fieldErrors, + }; + } + + await prisma.linkBlock.delete({ + where: { + uuid: data.data.uuid + } + }); + + revalidatePath("/dash"); + + return { + message: "Link block deleted" + }; +} + diff --git a/src/actions/page_actions.ts b/src/actions/page_actions.ts new file mode 100644 index 0000000..ca111e3 --- /dev/null +++ b/src/actions/page_actions.ts @@ -0,0 +1,149 @@ +"use server" + +import { z } from "zod"; +import prisma from "../../prisma/prisma"; +import { revalidatePath } from "next/cache"; + +export async function createCustomPage(prevState: any, fromData: FormData) { + const schema = z.object({ + shortcode: z.string().min(1).max(100), + title: z.string().min(1).max(100), + image: z.instanceof(File) + .refine((file) => ["image/png", "image/jpeg"].includes(file.type) + , { message: "Only PNG and JPEG images are allowed" }) + .refine((file) => file.size < 1024 * 1024 * 10, { message: "Image must be less than 10MB" }) + + }); + const data = schema.safeParse({ + shortcode: fromData.get("shortcode"), + title: fromData.get("title"), + image: fromData.get("image") + }); + if (!data.success) { + return { + message: "Invalid data", + error: data.error.flatten().fieldErrors, + }; + } + + const imageBase64 = await data.data.image.arrayBuffer().then((buffer) => { + return Buffer.from(buffer).toString("base64"); + }); + + + try { + const page = await prisma.page.create({ + data: { + shortcode: data.data.shortcode + } + }); + const customPage = await prisma.customPage.create({ + data: { + pageUuid: page.uuid, + title: data.data.title, + image: imageBase64 + } + }); + } catch (e) { + return { + message: "Shortcode already exists", + error: { + shortcode: "Shortcode already exists" + } + }; + } + + revalidatePath("/dash"); + return { + message: "Page created", + }; +} + + +export async function createRedirectPage(prevState: any, fromData: FormData) { + + const schema = z.object({ + dest: z.string().url().max(1000), + shortcode: z.string().min(1).max(100) + }); + + const data = schema.safeParse({ + dest: fromData.get("dest"), + shortcode: fromData.get("shortcode") + }); + + if (!data.success) { + return { + message: "Invalid data", + error: data.error.flatten().fieldErrors, + }; + } + + const page = await prisma.page.create({ + data: { + shortcode: data.data.shortcode + } + }); + + const redirectPage = await prisma.redirectPage.create({ + data: { + pageUuid: page.uuid, + dest: data.data.dest + } + }); + + revalidatePath("/dash"); + + return { + message: "URL created", + }; + +} + +export async function deletePage(prevState: any, fromData: FormData) { + const schema = z.object({ + uuid: z.string().uuid() + }); + + const data = schema.safeParse({ + uuid: fromData.get("uuid") + }); + + if (!data.success) { + return { + message: "Invalid data", + error: data.error.flatten().fieldErrors, + }; + } + + await prisma.redirectPage.deleteMany({ + where: { + pageUuid: data.data.uuid + } + }); + + await prisma.customPage.deleteMany({ + where: { + pageUuid: data.data.uuid + } + }); + + await prisma.stat.deleteMany({ + where: { + pageUuid: data.data.uuid + } + }); + + await prisma.page.delete({ + where: { + uuid: data.data.uuid + } + }); + + revalidatePath("/dash"); + + return { + message: "Page deleted", + }; + +} \ No newline at end of file diff --git a/src/actions/url_actions.ts b/src/actions/url_actions.ts deleted file mode 100644 index f68578b..0000000 --- a/src/actions/url_actions.ts +++ /dev/null @@ -1,77 +0,0 @@ -"use server" - -import { z } from "zod"; -import prisma from "../../prisma/prisma"; -import { revalidatePath } from "next/cache"; - -export async function createUrl(prevState: any, fromData: FormData){ - - const schema = z.object({ - original_url: z.string().url().max(1000), - short_code: z.string().min(1).max(100) - }); - - const data = schema.safeParse({ - original_url: fromData.get("original_url"), - short_code: fromData.get("short_code") - }); - - console.log("Data received in backend:"); - console.log(data); - - if (!data.success){ - return { - message: "Invalid data", - error: data.error.flatten().fieldErrors - }; - } - - await prisma.uRL.create({ - data: { - original_url: data.data.original_url, - short_code: data.data.short_code - } - }); - - revalidatePath("/dash"); - - return { - message: "URL created", - }; - -} - -export async function deleteUrl(prevState: any, fromData: FormData){ - const schema = z.object({ - url_id: z.coerce.number() - - }); - - const data = schema.safeParse({ - url_id: fromData.get("url_id") - }); - - console.log("Data received in backend:"); - console.log(data); - console.log(data.error?.flatten().fieldErrors); - - if (!data.success){ - return { - message: "Invalid data", - error: data.error.flatten().fieldErrors - }; - } - - await prisma.uRL.delete({ - where: { - url_id: data.data.url_id - } - }); - - revalidatePath("/dash"); - - return { - message: "URL deleted", - }; - -} \ No newline at end of file diff --git a/src/app/[shortcode]/page.tsx b/src/app/[shortcode]/page.tsx new file mode 100644 index 0000000..cc4f8c6 --- /dev/null +++ b/src/app/[shortcode]/page.tsx @@ -0,0 +1,53 @@ +import prisma from "../../../prisma/prisma"; +import { headers } from "next/headers"; +import { notFound, redirect, RedirectType } from "next/navigation"; +import { sha256 } from "js-sha256"; +import CustomPageComponent from "@/components/CustomPageComponent"; + +export default async function RedirectToUrl({ params }: { params: { shortcode: string } }) { + + + const page = await prisma.page.findUnique({ + where: {shortcode: params.shortcode}, + include: { + RedirectPage: true, + CustomPage: true + } + }); + + + if (!page) { + return notFound() + } + + // Logg the visit + const requestHeaders = headers(); + const userAgent = requestHeaders.get('user-agent') || 'unknown'; + const ip = requestHeaders.get('x-forwarded-for') || 'unknown'; + const anonymIp = ip.replace(/(\d+)\.(\d+)\.(\d+)\.(\d+)/, '$1.$2.$3.0'); + const hash = sha256(`${anonymIp}-${userAgent}`); + await prisma.stat.create({ + data: { + pageUuid: page.uuid, + user_agent: userAgent, + hash: hash + } + }); + + if (page.RedirectPage.length>0) { + + // Redirect to the destination + const redirectPage = page.RedirectPage[0]; + redirect(redirectPage.dest); + + }else if (page.CustomPage.length>0) { + + // Show the custom page + const customPage = page.CustomPage[0]; + return + + } else { + return notFound() + } + +} \ No newline at end of file diff --git a/src/app/[url_shortcode]/page.tsx b/src/app/[url_shortcode]/page.tsx deleted file mode 100644 index 3878cfd..0000000 --- a/src/app/[url_shortcode]/page.tsx +++ /dev/null @@ -1,31 +0,0 @@ -import prisma from "../../../prisma/prisma"; -import { headers } from "next/headers"; -import { notFound, redirect, RedirectType } from "next/navigation"; - -export default async function RedirectToUrl({ params }: { params: { url_shortcode: string } }) { - - const url = await prisma.uRL.findUnique({ - where: { - short_code: params.url_shortcode - } - }); - - if (!url) { - return notFound() - } - - const requestHeaders = headers(); - const userAgent = requestHeaders.get('user-agent') || 'unknown'; - const ip = requestHeaders.get('x-forwarded-for') || 'unknown'; - - await prisma.stat.create({ - data: { - url_id: url.url_id, - user_agent: userAgent, - hashed_ip: ip - } - }); - - redirect(url.original_url); - -} \ No newline at end of file diff --git a/src/app/globals.css b/src/app/globals.css index 7509690..85e46c0 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -10,16 +10,19 @@ body { h1 { @apply text-5xl font-bold mb-6 } + h2 { @apply text-2xl font-bold mb-4 } .form-group { - @apply mb-6; + @apply mb-2; } + .form-field-error { @apply border-red-500; } + .form-label-error { @apply text-red-500; } @@ -27,12 +30,19 @@ h2 { form { @apply w-full; } + input { - @apply w-full appearance-none bg-gray-800 border border-gray-800 text-gray-300 py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-gray-700 rounded; + @apply w-full appearance-none bg-gray-800 border border-gray-800 text-gray-300 py-3 px-4 mb-1 leading-tight focus:outline-none focus:bg-gray-700 rounded; } + +select { + @apply w-full appearance-none bg-gray-800 border border-gray-800 text-gray-300 py-3 px-4 mb-1 leading-tight focus:outline-none focus:bg-gray-700 rounded; +} + label { @apply block uppercase tracking-wide text-xs font-bold mb-2; } + button { @apply rounded bg-green-600 hover:bg-green-700 text-white py-2 px-4 disabled:opacity-50 -} +} \ No newline at end of file diff --git a/src/app/~/dash/[url_shortcode]/page.tsx b/src/app/~/dash/[url_shortcode]/page.tsx deleted file mode 100644 index ebbb983..0000000 --- a/src/app/~/dash/[url_shortcode]/page.tsx +++ /dev/null @@ -1,48 +0,0 @@ -import Image from "next/image"; -import prisma from "../../../../../prisma/prisma"; -import DeleteUrlForm from "@/components/forms/deleteUrlForm"; -import { notFound } from "next/navigation"; - -export default async function DetailedStats({ params }: { params: { url_shortcode: string } }) { - - const url = await prisma.uRL.findUnique({ - where: { - short_code: params.url_shortcode - } - }); - - const stats = await prisma.stat.findMany({ - where: { - url_id: url?.url_id - } - }); - - if (!url) { - return notFound(); - } - - return ( -
-

Stats

-
-
- favicon -
- -
-

Shortcode: {url?.short_code}

-

{url?.original_url}

-
-
- -
-
- - {stats.map((stat) => (
-

{stat.accessed_at.toDateString()}

-

{stat.user_agent}

-

{stat.hashed_ip}

-
))} -
- ); -} \ No newline at end of file diff --git a/src/app/~/dash/[uuid]/page.tsx b/src/app/~/dash/[uuid]/page.tsx new file mode 100644 index 0000000..56598ff --- /dev/null +++ b/src/app/~/dash/[uuid]/page.tsx @@ -0,0 +1,109 @@ +import Image from "next/image"; +import prisma from "../../../../../prisma/prisma"; +import { redirect } from "next/navigation"; +import DeletePageForm from "@/components/forms/deletePageForm"; +import { hashToColor } from "@/utils"; +import Link from "next/link"; +import CreateCustomPageForm from "@/components/forms/createCustomPageForm"; +import CreateLinkBlockForm from "@/components/forms/createLinkBlockForm"; +import CustomPageDashboard from "@/components/CustomPageDashboard"; +import LinkFavicon from "@/components/LinkFavicon"; + +export default async function DetailedStats({ params }: { params: { uuid: string } }) { + + const page = await prisma.page.findUnique({ + where: { uuid: params.uuid }, + include: { + RedirectPage: true, + CustomPage: true, + Stat: { orderBy: { accessed_at: "desc" } }, + _count: { + select: { Stat: true } + } + } + }); + + // group stats by hash + + const stats = await prisma.stat.groupBy({ + by: ['hash', 'user_agent'], + where: { + pageUuid: params.uuid + }, + _count: { + uuid: true + }, + _max: { + accessed_at: true + }, + orderBy: { + _max: { + accessed_at: "desc" + } + } + }); + + if (!page) { + redirect("/~/dash"); + //return notFound(); + } + + const redirectPage = page.RedirectPage[0]; + const customPage = page.CustomPage[0]; + + // get shortcodes if the page is a custom page to use in the link block form + const linkPages = customPage ? await prisma.redirectPage.findMany({ include: { page: true } }) : undefined + const shortcodes = linkPages ? linkPages.map((p) => p.page.shortcode) : undefined + + return ( +
+
+ {redirectPage || customPage.image ? +
+ {redirectPage ? : +  } +
+ : ""} + +
+

Shortcode: {page.shortcode}

+ {page.RedirectPage.length > 0 ? +

{page.RedirectPage[0].dest}

: ""} +
+
+ +
+
+ +
+
+

+ + {customPage ? : ""} + +

Stats

+

All visits (not unique): {page._count.Stat}

+

+

Visitors

+ {stats.map((stat) => (
+

{stat.user_agent}

+

{stat.hash}

+

Visits: {"" + stat._count.uuid}

+

Last Visit: {stat._max.accessed_at?.toDateString()}

+
))} + +

All Visits

+ + {page.Stat.map((stat) => (
+

{stat.accessed_at.toDateString()}

+

{stat.user_agent}

+

{stat.hash}

+
))} +
+ ); +} \ No newline at end of file diff --git a/src/app/~/dash/page.tsx b/src/app/~/dash/page.tsx index d8114f4..1783ff8 100644 --- a/src/app/~/dash/page.tsx +++ b/src/app/~/dash/page.tsx @@ -1,23 +1,36 @@ import React from 'react'; import prisma from '@/../prisma/prisma' -import CreateUrlForm from '@/components/forms/createUrlForm'; -import UrlComponent from '@/components/UrlComponent'; +import CreateRedirectPageForm from '@/components/forms/createRedirectPageForm'; +import CreateCustomPageForm from '@/components/forms/createCustomPageForm'; +import PageListComponent from '@/components/PageListComponent'; export default async function Dashboard() { - const urls = await prisma.uRL.findMany(); - + const pages = await prisma.page.findMany({ + include: { + RedirectPage: true, + CustomPage: true, + _count: { + select: { Stat: true } + } + } + }); + + const redirectPages = pages.filter((page) => page.RedirectPage.length > 0); + const customPages = pages.filter((page) => page.CustomPage.length > 0); return (<> -

URLs

-
- {urls.map((url) => ( - - ))} -
-

Create URL

- +

Redirect Pages

+ {redirectPages.map((page) => ( + ))} +

Create Redirect Page

+ + +

Custom Pages

+ {customPages.map((page) => ())} +

Create Custom Page

+ ); } \ No newline at end of file diff --git a/src/components/CustomPageComponent.tsx b/src/components/CustomPageComponent.tsx new file mode 100644 index 0000000..97d2111 --- /dev/null +++ b/src/components/CustomPageComponent.tsx @@ -0,0 +1,47 @@ +import { CustomPage } from "@prisma/client"; +import Image from "next/image"; +import prisma from "../../prisma/prisma"; +import LinkFavicon from "./LinkFavicon"; + +export default async function CustomPageComponent(params: { customPage: CustomPage }) { + const customPage = params.customPage; + + const linkBlocks = await prisma.linkBlock.findMany({ + where: { + customPageUuid: customPage.uuid + }, + orderBy: { + order: "asc" + }, + include: { + redirect_page: { include: { page: true } } + } + }); + + return ( +
+  + +

{customPage.title}

+
+ + {linkBlocks.map((block) => ( + + ))} +
+
+ ); +} \ No newline at end of file diff --git a/src/components/CustomPageDashboard.tsx b/src/components/CustomPageDashboard.tsx new file mode 100644 index 0000000..258f9d2 --- /dev/null +++ b/src/components/CustomPageDashboard.tsx @@ -0,0 +1,51 @@ +import { CustomPage } from "@prisma/client" +import prisma from "../../prisma/prisma" +import CreateLinkBlockForm from "./forms/createLinkBlockForm" +import DeleteLinkBlockForm from "./forms/deleteLinkBlockForm" + +export default async function CustomPageDashboard(params: { customPage: CustomPage }) { + + const customPage = params.customPage + const redirectPages = await prisma.redirectPage.findMany({ include: { page: true } }) + const shortcodes = redirectPages.map((p) => p.page.shortcode) + + const linkBlocks = await prisma.linkBlock.findMany({ + where: { + customPageUuid: customPage.uuid + }, + orderBy: { + order: "asc" + }, + include: { + redirect_page: { include: { page: true } } + } + }); + + return (<> + {linkBlocks.map((block) => ( + +
+
order: {block.order}
+
title: {block.title}
+
connected link page: {block.redirect_page.page.shortcode}
+
+ +
+
+ +
+
+ +
+
+ ))} +

Add new link page connection

+ + + + ) +} \ No newline at end of file diff --git a/src/components/LinkFavicon.tsx b/src/components/LinkFavicon.tsx new file mode 100644 index 0000000..bda1930 --- /dev/null +++ b/src/components/LinkFavicon.tsx @@ -0,0 +1,17 @@ +import Image from "next/image"; + +export default function LinkFavicon({ url }: { url: string }) { + + const hostname = new URL(url).hostname; + console.log(hostname); + + return (
+  +
+ ); +} \ No newline at end of file diff --git a/src/components/PageListComponent.tsx b/src/components/PageListComponent.tsx new file mode 100644 index 0000000..13daf9f --- /dev/null +++ b/src/components/PageListComponent.tsx @@ -0,0 +1,52 @@ +import React from 'react'; +import type { Prisma } from '@prisma/client'; +import DeletePageForm from './forms/deletePageForm'; +import Image from 'next/image'; +import Link from 'next/link'; +import prisma from '../../prisma/prisma'; +import LinkFavicon from './LinkFavicon'; + +type PageWithRelations = Prisma.PageGetPayload<{ + include: { + RedirectPage: true; + CustomPage: true; + _count: { + select: { Stat: true } + } + } +}> + +export default async function PageListComponent({ page }: { page: PageWithRelations }) { + + const redirectPage = page.RedirectPage[0]; + const customPage = page.CustomPage[0]; + + return ( + +
+
+ {redirectPage ? : + icon} +
+
+

{page.shortcode}

+

{redirectPage ? redirectPage.dest : customPage.title}

+ +
+
+ {page._count.Stat ?

{page._count.Stat} visits

: ""} + {// + } +
+
+ + + + ); +} \ No newline at end of file diff --git a/src/components/UrlComponent.tsx b/src/components/UrlComponent.tsx deleted file mode 100644 index 2b73047..0000000 --- a/src/components/UrlComponent.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import React from 'react'; -import type { URL as PrismaURL } from '@prisma/client'; -import DeleteUrlForm from './forms/deleteUrlForm'; -import Image from 'next/image'; -import Link from 'next/link'; - -export default function UrlComponent({ url }: { url: PrismaURL }) { - - return ( - -
-
- favicon -
-
-

{url.short_code}

-

{url.original_url}

-
-
- -
-
- - - - ); -} \ No newline at end of file diff --git a/src/components/forms/createCustomPageForm.tsx b/src/components/forms/createCustomPageForm.tsx new file mode 100644 index 0000000..9ac7eac --- /dev/null +++ b/src/components/forms/createCustomPageForm.tsx @@ -0,0 +1,37 @@ +"use client" +import { createCustomPage } from "@/actions/page_actions"; +import { useActionState } from "react"; +import SubmitButton from "../SubmitButton"; + +export default function CreateCustomPageForm() { + + const initialState = { + message: "", + error: {} + }; + + const [state, formAction] = useActionState(createCustomPage, initialState); + + return ( +
+
+ + +

{state.error?.shortcode}

+
+
+ + +

{state.error?.title}

+
+
+ + +

{state.error?.image}

+
+

{state.message}

+ + + ); + +} diff --git a/src/components/forms/createLinkBlockForm.tsx b/src/components/forms/createLinkBlockForm.tsx new file mode 100644 index 0000000..6420be0 --- /dev/null +++ b/src/components/forms/createLinkBlockForm.tsx @@ -0,0 +1,40 @@ +"use client" +import { createLinkBlock } from "@/actions/block_actions" +import { useActionState } from "react"; +import SubmitButton from "../SubmitButton"; + + +export default function CreateLinkBlockForm({ customPage, shortcodes }: { customPage: string, shortcodes: string[] }) { + + const initialState = { + message: "", + error: {} + }; + + const [state, formAction] = useActionState(createLinkBlock, initialState); + + return (
+
+ + +

{state.error?.redirectPageShortcode}

+
+
+ + +

{state.error?.title}

+
+
+ + +

{state.error?.order}

+
+ +

{state.message}

+ add to page + +
+ ); +} \ No newline at end of file diff --git a/src/components/forms/createRedirectPageForm.tsx b/src/components/forms/createRedirectPageForm.tsx new file mode 100644 index 0000000..f82d6f4 --- /dev/null +++ b/src/components/forms/createRedirectPageForm.tsx @@ -0,0 +1,30 @@ +"use client" + +import { createRedirectPage } from "@/actions/page_actions"; +import { useActionState } from "react"; +import SubmitButton from "../SubmitButton"; + +export default function CreateRedirectPageForm() { + const initialState = { + message: "", + error: {} + }; + const [state, formAction] = useActionState(createRedirectPage, initialState); + + return ( +
+
+ + +

{state.error?.shortcode}

+
+
+ + +

{state.error?.dest}

+
+

{state.message}

+ + + ); +} \ No newline at end of file diff --git a/src/components/forms/createUrlForm.tsx b/src/components/forms/createUrlForm.tsx deleted file mode 100644 index 7483930..0000000 --- a/src/components/forms/createUrlForm.tsx +++ /dev/null @@ -1,33 +0,0 @@ -"use client" - -import { createUrl } from "@/actions/url_actions"; -import { useActionState } from "react"; -import SubmitButton from "../SubmitButton"; -import { error } from "console"; - -export default function CreateUrlForm() { - const initialState = { - message: "", - error: {} - }; - const [state, formAction] = useActionState(createUrl, initialState); - - console.log(state); - - return ( -
-
- - -

{ state.error?.original_url }

-
-
- - -

{ state.error?.short_code }

-
-

{state.message}

- - - ); -} \ No newline at end of file diff --git a/src/components/forms/deleteLinkBlockForm.tsx b/src/components/forms/deleteLinkBlockForm.tsx new file mode 100644 index 0000000..2c8d49b --- /dev/null +++ b/src/components/forms/deleteLinkBlockForm.tsx @@ -0,0 +1,23 @@ +"use client" + +import { deleteLinkBlock } from "@/actions/block_actions"; +import { useActionState } from "react"; +import SubmitButton from "../SubmitButton"; + +export default function DeleteLinkBlockForm({linkBlockUuid}: {linkBlockUuid:string}) { + + const initialState = { + message: "", + error: {} + }; + + const [state, formAction] = useActionState(deleteLinkBlock, initialState); + + return (
+ + + unlink +
+ ) + +} \ No newline at end of file diff --git a/src/components/forms/deletePageForm.tsx b/src/components/forms/deletePageForm.tsx new file mode 100644 index 0000000..a9bbf85 --- /dev/null +++ b/src/components/forms/deletePageForm.tsx @@ -0,0 +1,23 @@ +"use client" + +import { deletePage } from "@/actions/page_actions"; +import { useActionState } from "react"; +import SubmitButton from "../SubmitButton"; +import { redirect } from "next/navigation"; + +export default function DeletePageForm({uuid}: {uuid: string}) { + + const initialState = { + message: "", + error: {}, + }; + const [state, formAction] = useActionState(deletePage, initialState); + + return ( +
+ + Delete +
+ ); + +} \ No newline at end of file diff --git a/src/components/forms/deleteUrlForm.tsx b/src/components/forms/deleteUrlForm.tsx deleted file mode 100644 index 1937abd..0000000 --- a/src/components/forms/deleteUrlForm.tsx +++ /dev/null @@ -1,22 +0,0 @@ -"use client" - -import { deleteUrl } from "@/actions/url_actions"; -import { useActionState } from "react"; -import SubmitButton from "../SubmitButton"; - -export default function DeleteUrlForm({url_id}: {url_id: number}) { - - const initialState = { - message: "", - error: {} - }; - const [state, formAction] = useActionState(deleteUrl, initialState); - - return ( -
- - Delete -
- ); - -} \ No newline at end of file diff --git a/src/utils.tsx b/src/utils.tsx new file mode 100644 index 0000000..4ef2efa --- /dev/null +++ b/src/utils.tsx @@ -0,0 +1,10 @@ +export function hashToColor(hash: string|null): string { + if (!hash) { + return "black"; + } + const r = parseInt(hash.slice(0, 2), 16); + const g = parseInt(hash.slice(2, 4), 16); + const b = parseInt(hash.slice(4, 6), 16); + const color = `rgb(${r}, ${g}, ${b})`; + return color; +} \ No newline at end of file