Added redirect page

This commit is contained in:
2025-06-26 20:48:29 +02:00
parent 9b377a83ec
commit 3bd9a73760
9 changed files with 619 additions and 12 deletions

View File

@@ -16,16 +16,10 @@ export default async function Dashboard() {
// Determine the most visited URL display value
const mostVisitedDisplay = stats.mostVisitedUrl
? stats.mostVisitedUrl.visitCount > 0
? `${stats.mostVisitedUrl.title} (${stats.mostVisitedUrl.visitCount})`
? `${stats.mostVisitedUrl.title || stats.mostVisitedUrl.slug || "Untitled"} (${stats.mostVisitedUrl.visitCount})`
: "No visits"
: "No URLs"
const mostVisitedDescription = stats.mostVisitedUrl
? stats.mostVisitedUrl.visitCount > 0
? `/${stats.mostVisitedUrl.slug} - ${stats.mostVisitedUrl.visitCount} visits`
: "No URLs have been visited yet"
: "Create your first shortened URL"
return (
<div className="p-6">
<h1 className="text-2xl font-bold text-foreground mb-4 block">Dashboard</h1>
@@ -46,7 +40,7 @@ export default async function Dashboard() {
title="Most Visited URL"
value={mostVisitedDisplay}
icon={<TrendingUp className="h-4 w-4 text-muted-foreground" />}
description={mostVisitedDescription}
description="Most popular shortened link"
/>
</div>

View File

@@ -0,0 +1,21 @@
export default function NotFound() {
return (
<div className="flex min-h-screen items-center justify-center">
<div className="text-center">
<h1 className="text-6xl font-bold text-gray-900 dark:text-gray-100">404</h1>
<h2 className="mt-4 text-2xl font-semibold text-gray-700 dark:text-gray-300">
Link Not Found
</h2>
<p className="mt-2 text-gray-600 dark:text-gray-400">
The link you're looking for doesn't exist or has expired.
</p>
<a
href="/"
className="mt-6 inline-block rounded-lg bg-blue-600 px-6 py-3 text-white hover:bg-blue-700 transition-colors"
>
Go Home
</a>
</div>
</div>
)
}

41
src/app/r/[slug]/page.tsx Normal file
View File

@@ -0,0 +1,41 @@
import { getUrlBySlug, getVisitsBySlugById, trackVisit } from "@/lib/db/urls"
import { headers } from "next/headers"
import { notFound, redirect } from "next/navigation"
export default async function RedirectPage({
params
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
const urlRecord = await getUrlBySlug(slug)
if (!urlRecord) {
notFound()
}
const visits = await getVisitsBySlugById(urlRecord.id)
if (urlRecord.expDate && new Date() > urlRecord.expDate) {
notFound()
}
if (urlRecord.maxVisits && visits[0].count >= urlRecord.maxVisits) {
notFound()
}
const headersList = await headers()
const userAgent = headersList.get("user-agent") || "Unknown"
const forwardedFor = headersList.get("x-forwarded-for")
const realIp = headersList.get("x-real-ip")
const ipAddress = forwardedFor?.split(",")[0] || realIp || "Unknown"
await trackVisit({
urlId: urlRecord.id,
ipAddress: ipAddress,
userAgent
})
redirect(urlRecord.url)
}

View File

@@ -207,7 +207,7 @@ export function AdvancedUrlFormCard() {
className="w-full"
disabled={form.formState.isSubmitting}
>
{form.formState.isSubmitting ? "Creating..." : "Create Advanced Short Link"}
{form.formState.isSubmitting ? "Creating..." : "Create Short Link"}
</Button>
</form>
</Form>

View File

@@ -1,6 +1,6 @@
import { eq, desc } from "drizzle-orm";
import { db } from "../drizzle/db";
import { urls } from "../drizzle/schema";
import { urls, visits } from "../drizzle/schema";
export function getAllUrls() {
return db.query.urls.findMany({
@@ -8,6 +8,12 @@ export function getAllUrls() {
})
}
export function getUrlBySlug(slug: string) {
return db.query.urls.findFirst({
where: eq(urls.slug, slug)
})
}
export function insertUrl(data: typeof urls.$inferInsert) {
return db.insert(urls).values(data)
}
@@ -19,3 +25,7 @@ export function updateUrl(id: string, data: Omit<Partial<typeof urls.$inferInser
export function deleteUrl(id: string) {
return db.delete(urls).where(eq(urls.id, id))
}
export function trackVisit(data: typeof visits.$inferInsert) {
return db.insert(visits).values(data)
}

View File

@@ -0,0 +1 @@
ALTER TABLE "visits" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();

View File

@@ -0,0 +1,533 @@
{
"id": "96b665f9-0981-41a0-88cb-97443c744c97",
"prevId": "3be7b31f-8b1b-457a-839a-2d490f48e833",
"version": "7",
"dialect": "postgresql",
"tables": {
"public.account": {
"name": "account",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"account_id": {
"name": "account_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"provider_id": {
"name": "provider_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
},
"access_token": {
"name": "access_token",
"type": "text",
"primaryKey": false,
"notNull": false
},
"refresh_token": {
"name": "refresh_token",
"type": "text",
"primaryKey": false,
"notNull": false
},
"id_token": {
"name": "id_token",
"type": "text",
"primaryKey": false,
"notNull": false
},
"access_token_expires_at": {
"name": "access_token_expires_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
},
"refresh_token_expires_at": {
"name": "refresh_token_expires_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
},
"scope": {
"name": "scope",
"type": "text",
"primaryKey": false,
"notNull": false
},
"password": {
"name": "password",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"account_user_id_user_id_fk": {
"name": "account_user_id_user_id_fk",
"tableFrom": "account",
"tableTo": "user",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.session": {
"name": "session",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"expires_at": {
"name": "expires_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"token": {
"name": "token",
"type": "text",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"ip_address": {
"name": "ip_address",
"type": "text",
"primaryKey": false,
"notNull": false
},
"user_agent": {
"name": "user_agent",
"type": "text",
"primaryKey": false,
"notNull": false
},
"user_id": {
"name": "user_id",
"type": "text",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {
"session_user_id_user_id_fk": {
"name": "session_user_id_user_id_fk",
"tableFrom": "session",
"tableTo": "user",
"columnsFrom": [
"user_id"
],
"columnsTo": [
"id"
],
"onDelete": "cascade",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"session_token_unique": {
"name": "session_token_unique",
"nullsNotDistinct": false,
"columns": [
"token"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.urls": {
"name": "urls",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"url": {
"name": "url",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"slug": {
"name": "slug",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"title": {
"name": "title",
"type": "varchar",
"primaryKey": false,
"notNull": false
},
"max_visits": {
"name": "max_visits",
"type": "integer",
"primaryKey": false,
"notNull": false
},
"exp_date": {
"name": "exp_date",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false
},
"forward_query_params": {
"name": "forward_query_params",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": true
},
"crawable": {
"name": "crawable",
"type": "boolean",
"primaryKey": false,
"notNull": false,
"default": false
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {
"urls_slug_idx": {
"name": "urls_slug_idx",
"columns": [
{
"expression": "slug",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"urls_url_idx": {
"name": "urls_url_idx",
"columns": [
{
"expression": "url",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
},
"exp_date_idx": {
"name": "exp_date_idx",
"columns": [
{
"expression": "exp_date",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"urls_slug_unique": {
"name": "urls_slug_unique",
"nullsNotDistinct": false,
"columns": [
"slug"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.user": {
"name": "user",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"email": {
"name": "email",
"type": "text",
"primaryKey": false,
"notNull": true
},
"email_verified": {
"name": "email_verified",
"type": "boolean",
"primaryKey": false,
"notNull": true
},
"image": {
"name": "image",
"type": "text",
"primaryKey": false,
"notNull": false
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {
"user_email_unique": {
"name": "user_email_unique",
"nullsNotDistinct": false,
"columns": [
"email"
]
}
},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.verification": {
"name": "verification",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "text",
"primaryKey": true,
"notNull": true
},
"identifier": {
"name": "identifier",
"type": "text",
"primaryKey": false,
"notNull": true
},
"value": {
"name": "value",
"type": "text",
"primaryKey": false,
"notNull": true
},
"expires_at": {
"name": "expires_at",
"type": "timestamp",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
},
"updated_at": {
"name": "updated_at",
"type": "timestamp",
"primaryKey": false,
"notNull": false
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
},
"public.visits": {
"name": "visits",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"url_id": {
"name": "url_id",
"type": "uuid",
"primaryKey": false,
"notNull": true
},
"ip_address": {
"name": "ip_address",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"user_agent": {
"name": "user_agent",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
"created_at": {
"name": "created_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
},
"updated_at": {
"name": "updated_at",
"type": "timestamp with time zone",
"primaryKey": false,
"notNull": false,
"default": "now()"
}
},
"indexes": {
"visits_url_id_idx": {
"name": "visits_url_id_idx",
"columns": [
{
"expression": "url_id",
"isExpression": false,
"asc": true,
"nulls": "last"
}
],
"isUnique": false,
"concurrently": false,
"method": "btree",
"with": {}
}
},
"foreignKeys": {
"visits_url_id_urls_id_fk": {
"name": "visits_url_id_urls_id_fk",
"tableFrom": "visits",
"tableTo": "urls",
"columnsFrom": [
"url_id"
],
"columnsTo": [
"id"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"sequences": {},
"roles": {},
"policies": {},
"views": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}

View File

@@ -15,6 +15,13 @@
"when": 1750938276218,
"tag": "0001_flawless_goblin_queen",
"breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1750963366705,
"tag": "0002_boring_joshua_kane",
"breakpoints": true
}
]
}

View File

@@ -27,9 +27,9 @@ export const urls = pgTable("urls", {
})
export const visits = pgTable("visits", {
id: uuid("id").primaryKey().notNull(),
id: uuid("id").primaryKey().notNull().defaultRandom(),
urlId: uuid("url_id").notNull().references(() => urls.id),
ipAdress: varchar("ip_address").notNull(),
ipAddress: varchar("ip_address").notNull(),
userAgent: varchar("user_agent").notNull(),
createdAt,
updatedAt