mirror of
https://github.com/traberph/short.git
synced 2026-11-22 22:55:12 +00:00
connect custom pages and link pages with link blocks
This commit is contained in:
@@ -36,3 +36,4 @@ yarn-error.log*
|
||||
next-env.d.ts
|
||||
|
||||
.env
|
||||
.vol_db
|
||||
|
||||
@@ -6,6 +6,10 @@ const nextConfig = {
|
||||
protocol: 'https',
|
||||
hostname: 'www.google.com',
|
||||
},
|
||||
{
|
||||
protocol: 'https',
|
||||
hostname: 'logo.clearbit.com',
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Generated
+7
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
+65
-14
@@ -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
|
||||
// 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())
|
||||
expires_at DateTime?
|
||||
stats Stat[] // Relation to URL_Statistics
|
||||
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
|
||||
uuid String @id @default(uuid())
|
||||
accessed_at DateTime @default(now())
|
||||
hashed_ip String? @db.VarChar(64)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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",
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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 <CustomPageComponent customPage={customPage}/>
|
||||
|
||||
} else {
|
||||
return notFound()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
+12
-2
@@ -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
|
||||
}
|
||||
@@ -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 (
|
||||
<div>
|
||||
<h2>Stats</h2>
|
||||
<div className="flex items-center">
|
||||
<div className=" h-10 w-10 mr-4 ">
|
||||
<Image src={`https://www.google.com/s2/favicons?domain=${url?.original_url}`} alt="favicon" width={100} height={100} />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<p>Shortcode: {url?.short_code}</p>
|
||||
<p><a className="underline" href={url?.original_url}>{url?.original_url}</a></p>
|
||||
</div>
|
||||
<div className="ml-auto">
|
||||
<DeleteUrlForm url_id={url?.url_id} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{stats.map((stat) => (<div className="my-5" key={stat.stat_id}>
|
||||
<p>{stat.accessed_at.toDateString()}</p>
|
||||
<p>{stat.user_agent}</p>
|
||||
<p>{stat.hashed_ip}</p>
|
||||
</div>))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<div>
|
||||
<div className="flex items-center">
|
||||
{redirectPage || customPage.image ?
|
||||
<div className=" h-10 w-10 mr-4 ">
|
||||
{redirectPage ? <LinkFavicon url={redirectPage.dest} /> :
|
||||
<Image
|
||||
src={`data:image/png;base64,${customPage.image}`}
|
||||
width={100}
|
||||
height={100}
|
||||
alt=" "
|
||||
/>}
|
||||
</div>
|
||||
: ""}
|
||||
|
||||
<div>
|
||||
<p>Shortcode: {page.shortcode}</p>
|
||||
{page.RedirectPage.length > 0 ?
|
||||
<p><a className="underline" href={page.RedirectPage[0].dest}>{page.RedirectPage[0].dest}</a></p> : ""}
|
||||
</div>
|
||||
<div className="ml-auto">
|
||||
<Link href={`/${page.shortcode}`}><button>View</button></Link>
|
||||
</div>
|
||||
<div className="ml-5">
|
||||
<DeletePageForm uuid={page.uuid} />
|
||||
</div>
|
||||
</div>
|
||||
<br className="mt-2"></br>
|
||||
|
||||
{customPage ? <CustomPageDashboard customPage={customPage} /> : ""}
|
||||
|
||||
<h2 className="mt-5">Stats</h2>
|
||||
<p className="mt-5">All visits (not unique): {page._count.Stat}</p>
|
||||
<br></br>
|
||||
<h2>Visitors</h2>
|
||||
{stats.map((stat) => (<div className="my-5" key={stat.hash}>
|
||||
<p>{stat.user_agent}</p>
|
||||
<p style={{ color: hashToColor(stat.hash) }}>{stat.hash}</p>
|
||||
<p>Visits: {"" + stat._count.uuid}</p>
|
||||
<p>Last Visit: {stat._max.accessed_at?.toDateString()}</p>
|
||||
</div>))}
|
||||
|
||||
<h2>All Visits</h2>
|
||||
|
||||
{page.Stat.map((stat) => (<div className="my-5" key={stat.uuid}>
|
||||
<p>{stat.accessed_at.toDateString()}</p>
|
||||
<p>{stat.user_agent}</p>
|
||||
<p style={{ color: hashToColor(stat.hash) }}>{stat.hash}</p>
|
||||
</div>))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+24
-11
@@ -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 (<>
|
||||
<h2>URLs</h2>
|
||||
<div>
|
||||
{urls.map((url) => (
|
||||
<UrlComponent key={url.url_id} url={url}/>
|
||||
))}
|
||||
</div>
|
||||
<h2 className='mt-11'>Create URL</h2>
|
||||
<CreateUrlForm/>
|
||||
<h2>Redirect Pages</h2>
|
||||
{redirectPages.map((page) => (
|
||||
<PageListComponent key={page.uuid} page={page} />))}
|
||||
<h2 className='mt-11'>Create Redirect Page</h2>
|
||||
<CreateRedirectPageForm />
|
||||
|
||||
<h2 className='mt-11'>Custom Pages</h2>
|
||||
{customPages.map((page) => (<PageListComponent key={page.uuid} page={page} />))}
|
||||
<h2 className='mt-11'>Create Custom Page</h2>
|
||||
<CreateCustomPageForm />
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<div className="flex flex-col items-center justify-center">
|
||||
<Image
|
||||
src={`data:image/png;base64,${customPage.image}`}
|
||||
alt=" "
|
||||
width={100}
|
||||
height={100}
|
||||
className="rounded-full"
|
||||
/>
|
||||
|
||||
<h1 className="mt-5 text-center">{customPage.title}</h1>
|
||||
<div className="w-full max-w-96">
|
||||
|
||||
{linkBlocks.map((block) => (
|
||||
<button key={block.uuid} className="bg-slate-600 hover:bg-slate-500 w-full m-2">
|
||||
<a href={`/${block.redirect_page.page.shortcode}`}>
|
||||
<div className="relative flex items-center">
|
||||
<div className="flex-shrink-0"><LinkFavicon url={block.redirect_page.dest} /></div>
|
||||
<div className="absolute left-1/2 transform -translate-x-1/2 text-center font-bold">{block.title}</div>
|
||||
</div>
|
||||
</a>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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) => (
|
||||
|
||||
<div key={block.uuid} className="flex items-center justify-center mb-2 odd:bg-slate-800 p-2">
|
||||
<div className="mr-5">order: {block.order}</div>
|
||||
<div className="mr-5">title: <span className="font-bold">{block.title}</span></div>
|
||||
<div>connected link page: <span className="font-bold">{block.redirect_page.page.shortcode}</span></div>
|
||||
<div className="ml-auto">
|
||||
<button>
|
||||
<a href={`/${block.redirect_page.page.shortcode}`} className="btn btn-primary">Visit</a>
|
||||
</button>
|
||||
</div>
|
||||
<div className="ml-5">
|
||||
<button>
|
||||
<a href={`/~/dash/${block.redirect_page.page.uuid}`} className="btn btn-primary">Details</a>
|
||||
</button>
|
||||
</div>
|
||||
<div className="ml-5">
|
||||
<DeleteLinkBlockForm linkBlockUuid={block.uuid} />
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
<h2>Add new link page connection</h2>
|
||||
<CreateLinkBlockForm customPage={customPage.uuid} shortcodes={shortcodes || []} />
|
||||
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -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 (<div className=" h-10 w-10">
|
||||
<Image
|
||||
src={`https://logo.clearbit.com/${hostname}?`}
|
||||
width={100}
|
||||
height={100}
|
||||
alt=" "
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<Link href={`/~/dash/${page.uuid}`}>
|
||||
<div className="w-full my-3 flex items-center hover:bg-slate-600 rounded p-1">
|
||||
<div className="flex-shrink-0 h-8 w-8 flex items-center justify-center mr-4">
|
||||
{redirectPage ? <LinkFavicon url={redirectPage.dest} /> :
|
||||
<Image
|
||||
loading='lazy'
|
||||
width={100}
|
||||
height={100}
|
||||
src={`data:image/png;base64,${customPage.image}`}
|
||||
alt="icon"
|
||||
/>}
|
||||
</div>
|
||||
<div>
|
||||
<p className="font-bold text-lg text-white">{page.shortcode}</p>
|
||||
<p className="text-sm text-gray-400">{redirectPage ? redirectPage.dest : customPage.title}</p>
|
||||
|
||||
</div>
|
||||
<div className="ml-auto">
|
||||
{page._count.Stat ? <p className="text-gray-400 font-bold">{page._count.Stat} visits</p> : ""}
|
||||
{//<DeletePageForm uuid={page.uuid} />
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
|
||||
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<Link href={`/~/dash/${url.short_code}`}>
|
||||
<div className="w-full my-3 flex items-center hover:bg-slate-600 rounded p-1">
|
||||
<div className="flex-shrink-0 h-8 w-8 flex items-center justify-center mr-4">
|
||||
<Image
|
||||
loading='lazy'
|
||||
width={100}
|
||||
height={100}
|
||||
src={`https://www.google.com/s2/favicons?domain=${url.original_url}`}
|
||||
alt="favicon"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<p className="font-bold text-lg text-white">{url.short_code}</p>
|
||||
<p className="text-sm text-gray-400">{url.original_url}</p>
|
||||
</div>
|
||||
<div className="ml-auto">
|
||||
<DeleteUrlForm url_id={url.url_id} />
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
|
||||
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<form action={formAction}>
|
||||
<div className="form-group">
|
||||
<label htmlFor="shortcode">Shortcode</label>
|
||||
<input type="text" name="shortcode" id="shortcode" className={state.error?.shortcode ? "border-red-400" : ""} />
|
||||
<p className="form-label-error">{state.error?.shortcode}</p>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label htmlFor="title">Title</label>
|
||||
<input type="text" name="title" id="title" className={state.error?.title ? "border-red-400" : ""} />
|
||||
<p className="form-label-error">{state.error?.title}</p>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label htmlFor="image">Image</label>
|
||||
<input type="file" name="image" id="image" className={state.error?.image ? "border-red-400" : ""} />
|
||||
<p className="form-label-error">{state.error?.image}</p>
|
||||
</div>
|
||||
<p>{state.message}</p>
|
||||
<SubmitButton />
|
||||
</form>
|
||||
);
|
||||
|
||||
}
|
||||
@@ -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 (<form action={formAction}>
|
||||
<div className="form-group">
|
||||
<label htmlFor="redirectPageShortcode">Redirect Page</label>
|
||||
<select name="redirectPageShortcode" id="redirectPageShortcode" className={state.error?.redirectPageShortcode ? "border-red-400" : ""}>
|
||||
{shortcodes.map((s) => <option key={s} value={s}>{s}</option>)}
|
||||
</select>
|
||||
<p className="form-label-error">{state.error?.redirectPageShortcode}</p>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label htmlFor="title">Title</label>
|
||||
<input type="text" name="title" id="title" className={state.error?.title ? "border-red-400" : ""} />
|
||||
<p className="form-label-error">{state.error?.title}</p>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label htmlFor="order">Order</label>
|
||||
<input defaultValue={1} type="number" name="order" id="order" className={state.error?.order ? "border-red-400" : ""} />
|
||||
<p className="form-label-error">{state.error?.order}</p>
|
||||
</div>
|
||||
<input type="hidden" name="customPageUuid" value={customPage} />
|
||||
<p className="mb-2">{state.message}</p>
|
||||
<SubmitButton>add to page</SubmitButton>
|
||||
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<form action={formAction}>
|
||||
<div className="form-group">
|
||||
<label htmlFor='shortcode'>Shortcode</label>
|
||||
<input type='text' name='shortcode' id='shortcode' className={state.error?.shortcode ? "border-red-400" : ""} />
|
||||
<p className="form-label-error">{state.error?.shortcode}</p>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label htmlFor='dest'>Destination URL</label>
|
||||
<input type='text' name='dest' id='dest' className={state.error?.dest ? "border-red-400" : ""} />
|
||||
<p className="form-label-error">{state.error?.dest}</p>
|
||||
</div>
|
||||
<p>{state.message}</p>
|
||||
<SubmitButton />
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -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 (
|
||||
<form action={formAction}>
|
||||
<div className="form-group">
|
||||
<label htmlFor='original_url'>Original URL</label>
|
||||
<input type='text' name='original_url' id='original_url' className={state.error?.original_url?"border-red-400":""}/>
|
||||
<p className="form-label-error">{ state.error?.original_url }</p>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label htmlFor='short_code'>Short Code</label>
|
||||
<input type='text' name='short_code' id='short_code' className={state.error?.short_code?"border-red-400":""}/>
|
||||
<p className="form-label-error">{ state.error?.short_code }</p>
|
||||
</div>
|
||||
<p>{state.message}</p>
|
||||
<SubmitButton/>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -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 (<form action={formAction}>
|
||||
|
||||
<input type="hidden" name="uuid" id="uuid" value={linkBlockUuid}/>
|
||||
<SubmitButton>unlink</SubmitButton>
|
||||
</form>
|
||||
)
|
||||
|
||||
}
|
||||
@@ -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 (
|
||||
<form action={formAction}>
|
||||
<input type="hidden" name="uuid" value={uuid}/>
|
||||
<SubmitButton>Delete</SubmitButton>
|
||||
</form>
|
||||
);
|
||||
|
||||
}
|
||||
@@ -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 (
|
||||
<form action={formAction}>
|
||||
<input type="hidden" name="url_id" value={url_id}/>
|
||||
<SubmitButton>Delete</SubmitButton>
|
||||
</form>
|
||||
);
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user