mirror of
https://github.com/traberph/short.git
synced 2026-11-22 22:55:12 +00:00
add functionality to pin page && refactoring
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
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) {
|
||||
|
||||
@@ -44,9 +42,9 @@ export async function createLinkBlock(prevState: any, formData: FormData) {
|
||||
|
||||
const page = await prisma.page.findUnique({
|
||||
where: { shortcode: data.data.redirectPageShortcode },
|
||||
include: { RedirectPage: true }
|
||||
include: { redirectPage: true }
|
||||
});
|
||||
const redirectPage = page?.RedirectPage[0]
|
||||
const redirectPage = page?.redirectPage[0]
|
||||
if (!redirectPage) {
|
||||
return {
|
||||
message: "Redirect page not found",
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import { z } from "zod";
|
||||
import prisma from "../../prisma/prisma";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { error } from "console";
|
||||
|
||||
export async function createCustomPage(prevState: any, fromData: FormData) {
|
||||
const schema = z.object({
|
||||
@@ -146,4 +147,42 @@ export async function deletePage(prevState: any, fromData: FormData) {
|
||||
message: "Page deleted",
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
export async function pinPageToRoot(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.pinnedPage.upsert({
|
||||
where: {
|
||||
id: 1
|
||||
},
|
||||
update: {
|
||||
pageUuid: data.data.uuid
|
||||
},
|
||||
create: {
|
||||
id: 1,
|
||||
pageUuid: data.data.uuid
|
||||
}
|
||||
});
|
||||
|
||||
revalidatePath("/dash");
|
||||
revalidatePath("/");
|
||||
|
||||
return {
|
||||
message: "Page pinned",
|
||||
};
|
||||
|
||||
}
|
||||
@@ -6,12 +6,13 @@ import CustomPageComponent from "@/components/CustomPageComponent";
|
||||
|
||||
export default async function RedirectToUrl({ params }: { params: { shortcode: string } }) {
|
||||
|
||||
const { shortcode } = await params;
|
||||
|
||||
const page = await prisma.page.findUnique({
|
||||
where: {shortcode: params.shortcode},
|
||||
where: {shortcode: shortcode},
|
||||
include: {
|
||||
RedirectPage: true,
|
||||
CustomPage: true
|
||||
redirectPage: true,
|
||||
customPage: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -21,7 +22,7 @@ export default async function RedirectToUrl({ params }: { params: { shortcode: s
|
||||
}
|
||||
|
||||
// Logg the visit
|
||||
const requestHeaders = headers();
|
||||
const requestHeaders = await 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');
|
||||
@@ -29,21 +30,21 @@ export default async function RedirectToUrl({ params }: { params: { shortcode: s
|
||||
await prisma.stat.create({
|
||||
data: {
|
||||
pageUuid: page.uuid,
|
||||
user_agent: userAgent,
|
||||
userAgent: userAgent,
|
||||
hash: hash
|
||||
}
|
||||
});
|
||||
|
||||
if (page.RedirectPage.length>0) {
|
||||
if (page.redirectPage.length>0) {
|
||||
|
||||
// Redirect to the destination
|
||||
const redirectPage = page.RedirectPage[0];
|
||||
const redirectPage = page.redirectPage[0];
|
||||
redirect(redirectPage.dest);
|
||||
|
||||
}else if (page.CustomPage.length>0) {
|
||||
}else if (page.customPage.length>0) {
|
||||
|
||||
// Show the custom page
|
||||
const customPage = page.CustomPage[0];
|
||||
const customPage = page.customPage[0];
|
||||
return <CustomPageComponent customPage={customPage}/>
|
||||
|
||||
} else {
|
||||
|
||||
+9
-1
@@ -4,7 +4,7 @@
|
||||
|
||||
|
||||
body {
|
||||
@apply text-gray-300 bg-gray-900 m-10
|
||||
@apply text-gray-300 bg-gray-900
|
||||
}
|
||||
|
||||
h1 {
|
||||
@@ -45,4 +45,12 @@ label {
|
||||
|
||||
button {
|
||||
@apply rounded bg-green-600 hover:bg-green-700 text-white py-2 px-4 disabled:opacity-50
|
||||
}
|
||||
|
||||
.zebra {
|
||||
@apply odd:bg-slate-800 p-2 rounded
|
||||
}
|
||||
|
||||
.common-red {
|
||||
@apply text-red-500
|
||||
}
|
||||
+10
-1
@@ -1,7 +1,16 @@
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import prisma from "../../prisma/prisma";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export default async function Home() {
|
||||
|
||||
const pinnedPage = await prisma.pinnedPage.findFirst({where: {id: 1}, include: {page: true}});
|
||||
|
||||
if (pinnedPage) {
|
||||
redirect(pinnedPage.page.shortcode);
|
||||
}
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
(<>
|
||||
<h1>Home</h1>
|
||||
|
||||
@@ -1,44 +1,48 @@
|
||||
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 Image from "next/image";
|
||||
import DeletePageForm from "@/components/forms/deletePageForm";
|
||||
import CustomPageDashboard from "@/components/CustomPageDashboard";
|
||||
import LinkFavicon from "@/components/LinkFavicon";
|
||||
import { hashToColor } from "@/utils";
|
||||
import PinPageToRootForm from "@/components/forms/pinPageToRootForm";
|
||||
|
||||
export default async function DetailedStats({ params }: { params: { uuid: string } }) {
|
||||
|
||||
const { uuid } = await params;
|
||||
|
||||
const page = await prisma.page.findUnique({
|
||||
where: { uuid: params.uuid },
|
||||
where: { uuid: uuid },
|
||||
include: {
|
||||
RedirectPage: true,
|
||||
CustomPage: true,
|
||||
Stat: { orderBy: { accessed_at: "desc" } },
|
||||
pinnedPage: true,
|
||||
redirectPage: true,
|
||||
customPage: true,
|
||||
stat: { orderBy: { accessedAt: "desc" } },
|
||||
_count: {
|
||||
select: { Stat: true }
|
||||
select: { stat: true }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
// group stats by hash
|
||||
|
||||
const stats = await prisma.stat.groupBy({
|
||||
by: ['hash', 'user_agent'],
|
||||
by: ['hash', 'userAgent'],
|
||||
where: {
|
||||
pageUuid: params.uuid
|
||||
pageUuid: uuid
|
||||
},
|
||||
_count: {
|
||||
uuid: true
|
||||
},
|
||||
_max: {
|
||||
accessed_at: true
|
||||
accessedAt: true
|
||||
},
|
||||
orderBy: {
|
||||
_max: {
|
||||
accessed_at: "desc"
|
||||
accessedAt: "desc"
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -48,8 +52,8 @@ export default async function DetailedStats({ params }: { params: { uuid: string
|
||||
//return notFound();
|
||||
}
|
||||
|
||||
const redirectPage = page.RedirectPage[0];
|
||||
const customPage = page.CustomPage[0];
|
||||
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
|
||||
@@ -66,18 +70,25 @@ export default async function DetailedStats({ params }: { params: { uuid: string
|
||||
width={100}
|
||||
height={100}
|
||||
alt=" "
|
||||
className="rounded-full"
|
||||
/>}
|
||||
</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> : ""}
|
||||
{page.redirectPage.length > 0 ?
|
||||
<p><a className="underline" href={page.redirectPage[0].dest}>{page.redirectPage[0].dest}</a></p> : ""}
|
||||
</div>
|
||||
<div className="ml-5">
|
||||
{page.pinnedPage ? <p className="common-red">Pinned to root</p> : ""}
|
||||
</div>
|
||||
<div className="ml-auto">
|
||||
<Link href={`/${page.shortcode}`}><button>View</button></Link>
|
||||
</div>
|
||||
<div className="ml-5">
|
||||
<PinPageToRootForm pageUuid={page.uuid} disabled={!!page.pinnedPage} />
|
||||
</div>
|
||||
<div className="ml-5">
|
||||
<DeletePageForm uuid={page.uuid} />
|
||||
</div>
|
||||
@@ -87,23 +98,22 @@ export default async function DetailedStats({ params }: { params: { uuid: string
|
||||
{customPage ? <CustomPageDashboard customPage={customPage} /> : ""}
|
||||
|
||||
<h2 className="mt-5">Stats</h2>
|
||||
<p className="mt-5">All visits (not unique): {page._count.Stat}</p>
|
||||
<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>
|
||||
{stats.map((stat) => (<div className="zebra" key={stat.hash}>
|
||||
<p>{stat.userAgent}</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>
|
||||
<p>Last Visit: {stat._max.accessedAt?.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>
|
||||
{/* <h2>All Visits</h2>
|
||||
{page.stat.map((stat) => (<div className="my-5" key={stat.uuid}>
|
||||
<p>{stat.accessedAt.toDateString()}</p>
|
||||
<p>{stat.userAgent}</p>
|
||||
<p style={{ color: hashToColor(stat.hash) }}>{stat.hash}</p>
|
||||
</div>))}
|
||||
</div>))} */}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import React, { ReactNode } from 'react';
|
||||
|
||||
export default function Dashboard({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<div>
|
||||
<div className='p-10 max-w-7xl mx-auto'>
|
||||
<Link href="/~/dash/">
|
||||
<h1>Dashboard</h1>
|
||||
</Link>
|
||||
|
||||
@@ -9,16 +9,17 @@ export default async function Dashboard() {
|
||||
|
||||
const pages = await prisma.page.findMany({
|
||||
include: {
|
||||
RedirectPage: true,
|
||||
CustomPage: true,
|
||||
pinnedPage: true,
|
||||
redirectPage: true,
|
||||
customPage: true,
|
||||
_count: {
|
||||
select: { Stat: true }
|
||||
select: { stat: true }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const redirectPages = pages.filter((page) => page.RedirectPage.length > 0);
|
||||
const customPages = pages.filter((page) => page.CustomPage.length > 0);
|
||||
const redirectPages = pages.filter((page) => page.redirectPage.length > 0);
|
||||
const customPages = pages.filter((page) => page.customPage.length > 0);
|
||||
|
||||
return (<>
|
||||
<h2>Redirect Pages</h2>
|
||||
|
||||
@@ -14,12 +14,12 @@ export default async function CustomPageComponent(params: { customPage: CustomPa
|
||||
order: "asc"
|
||||
},
|
||||
include: {
|
||||
redirect_page: { include: { page: true } }
|
||||
redirectPage: { include: { page: true } }
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center">
|
||||
<div className="flex flex-col items-center justify-center min-h-screen">
|
||||
<Image
|
||||
src={`data:image/png;base64,${customPage.image}`}
|
||||
alt=" "
|
||||
@@ -29,13 +29,13 @@ export default async function CustomPageComponent(params: { customPage: CustomPa
|
||||
/>
|
||||
|
||||
<h1 className="mt-5 text-center">{customPage.title}</h1>
|
||||
<div className="w-full max-w-96">
|
||||
<div className="w-full max-w-96 mb-40 p-2">
|
||||
|
||||
{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}`}>
|
||||
<button key={block.uuid} className="bg-slate-600 hover:bg-slate-500 w-full my-2">
|
||||
<a href={`/${block.redirectPage.page.shortcode}`}>
|
||||
<div className="relative flex items-center">
|
||||
<div className="flex-shrink-0"><LinkFavicon url={block.redirect_page.dest} /></div>
|
||||
<div className="flex-shrink-0"><LinkFavicon url={block.redirectPage.dest} /></div>
|
||||
<div className="absolute left-1/2 transform -translate-x-1/2 text-center font-bold">{block.title}</div>
|
||||
</div>
|
||||
</a>
|
||||
|
||||
@@ -17,25 +17,25 @@ export default async function CustomPageDashboard(params: { customPage: CustomPa
|
||||
order: "asc"
|
||||
},
|
||||
include: {
|
||||
redirect_page: { include: { page: true } }
|
||||
redirectPage: { 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 key={block.uuid} className="flex items-center justify-center mb-2 zebra">
|
||||
<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>connected link page: <span className="font-bold">{block.redirectPage.page.shortcode}</span></div>
|
||||
<div className="ml-auto">
|
||||
<button>
|
||||
<a href={`/${block.redirect_page.page.shortcode}`} className="btn btn-primary">Visit</a>
|
||||
<a href={`/${block.redirectPage.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>
|
||||
<a href={`/~/dash/${block.redirectPage.page.uuid}`} className="btn btn-primary">Details</a>
|
||||
</button>
|
||||
</div>
|
||||
<div className="ml-5">
|
||||
|
||||
@@ -3,7 +3,6 @@ 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
|
||||
@@ -11,6 +10,7 @@ export default function LinkFavicon({ url }: { url: string }) {
|
||||
width={100}
|
||||
height={100}
|
||||
alt=" "
|
||||
className="rounded-full"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
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';
|
||||
import PinPageToRootForm from './forms/pinPageToRootForm';
|
||||
|
||||
type PageWithRelations = Prisma.PageGetPayload<{
|
||||
include: {
|
||||
RedirectPage: true;
|
||||
CustomPage: true;
|
||||
pinnedPage: true;
|
||||
redirectPage: true;
|
||||
customPage: true;
|
||||
_count: {
|
||||
select: { Stat: true }
|
||||
select: { stat: true }
|
||||
}
|
||||
}
|
||||
}>
|
||||
|
||||
export default async function PageListComponent({ page }: { page: PageWithRelations }) {
|
||||
|
||||
const redirectPage = page.RedirectPage[0];
|
||||
const customPage = page.CustomPage[0];
|
||||
const redirectPage = page.redirectPage[0];
|
||||
const customPage = page.customPage[0];
|
||||
|
||||
|
||||
return (
|
||||
<Link href={`/~/dash/${page.uuid}`}>
|
||||
@@ -32,6 +33,7 @@ export default async function PageListComponent({ page }: { page: PageWithRelati
|
||||
height={100}
|
||||
src={`data:image/png;base64,${customPage.image}`}
|
||||
alt="icon"
|
||||
className="rounded-full"
|
||||
/>}
|
||||
</div>
|
||||
<div>
|
||||
@@ -39,8 +41,11 @@ export default async function PageListComponent({ page }: { page: PageWithRelati
|
||||
<p className="text-sm text-gray-400">{redirectPage ? redirectPage.dest : customPage.title}</p>
|
||||
|
||||
</div>
|
||||
<div className="ml-5">
|
||||
{page.pinnedPage ? <p className="text-red-500">Pinned to root</p> : ""}
|
||||
</div>
|
||||
<div className="ml-auto">
|
||||
{page._count.Stat ? <p className="text-gray-400 font-bold">{page._count.Stat} visits</p> : ""}
|
||||
{page._count.stat ? <p className="text-gray-400 font-bold">{page._count.stat} visits</p> : ""}
|
||||
{//<DeletePageForm uuid={page.uuid} />
|
||||
}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
"use client"
|
||||
import { pinPageToRoot } from "@/actions/page_actions";
|
||||
import { BookmarkIcon } from "@heroicons/react/24/solid";
|
||||
import { useActionState, useState } from "react";
|
||||
|
||||
export default function PinPageToRootForm({ pageUuid, disabled }: { pageUuid: string, disabled: boolean }) {
|
||||
|
||||
const initialState = {
|
||||
message: "",
|
||||
error: {}
|
||||
};
|
||||
|
||||
const [state, formAction] = useActionState(pinPageToRoot, initialState);
|
||||
|
||||
|
||||
return ( <form action={formAction}>
|
||||
<input type="hidden" name="uuid" value={pageUuid} />
|
||||
{/* <p>{state.message}</p> */}
|
||||
<button type="submit" className="btn btn-primary" disabled={disabled}><BookmarkIcon className="h-5 w-5 inline-block align-middle"/></button>
|
||||
</form>)
|
||||
}
|
||||
Reference in New Issue
Block a user