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:
@@ -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>
|
||||
);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user