added supabase

This commit is contained in:
Austin
2026-02-20 09:12:47 -06:00
parent 3c293159ff
commit 76f1241761
54 changed files with 3162 additions and 445 deletions
@@ -0,0 +1,52 @@
import { createClient } from "@/utils/supabase/server";
import { notFound } from "next/navigation";
import PostEditor from "../../../components/PostEditor";
import { updatePost } from "../../actions";
interface PageProps {
params: Promise<{ id: string }>;
}
export default async function EditPostPage({ params }: PageProps) {
const { id } = await params;
const supabase = await createClient();
const { data: post, error } = await supabase
.from("posts")
.select("*")
.eq("id", id)
.single();
if (error || !post) {
notFound();
}
const { data: authors } = await supabase
.from("authors")
.select("name")
.order("name");
return (
<div>
<h1 className="text-3xl font-bold mb-8">Edit Post</h1>
<div className="bg-white rounded-lg shadow-sm p-6">
<PostEditor
action={updatePost}
initialData={{
id: post.id,
title: post.title,
date: post.date,
excerpt: post.excerpt,
content: post.content,
author: post.author,
tags: post.tags || [],
coverImage: post.cover_image,
published: post.published,
}}
authors={authors || []}
isEdit
/>
</div>
</div>
);
}
+119
View File
@@ -0,0 +1,119 @@
"use server";
import { createClient } from "@/utils/supabase/server";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { remark } from "remark";
import html from "remark-html";
export async function createPost(formData: FormData) {
const supabase = await createClient();
const id = formData.get("id") as string;
const title = formData.get("title") as string;
const date = formData.get("date") as string;
const excerpt = formData.get("excerpt") as string;
const content = formData.get("content") as string;
const author = formData.get("author") as string;
const tagsRaw = formData.get("tags") as string;
const coverImage = (formData.get("coverImage") as string) || null;
const published = formData.get("published") === "true";
const tags = tagsRaw
.split(",")
.map((t) => t.trim())
.filter(Boolean);
// Render markdown to HTML
const processedContent = await remark()
.use(html, { sanitize: false })
.process(content);
const contentHtml = processedContent.toString();
const { error } = await supabase.from("posts").insert({
id,
title,
date: new Date(date).toISOString(),
excerpt,
content,
content_html: contentHtml,
author,
tags,
cover_image: coverImage,
published,
});
if (error) {
throw new Error(`Failed to create post: ${error.message}`);
}
revalidatePath("/");
revalidatePath("/posts");
revalidatePath("/admin/posts");
redirect("/admin/posts");
}
export async function updatePost(formData: FormData) {
const supabase = await createClient();
const id = formData.get("id") as string;
const title = formData.get("title") as string;
const date = formData.get("date") as string;
const excerpt = formData.get("excerpt") as string;
const content = formData.get("content") as string;
const author = formData.get("author") as string;
const tagsRaw = formData.get("tags") as string;
const coverImage = (formData.get("coverImage") as string) || null;
const published = formData.get("published") === "true";
const tags = tagsRaw
.split(",")
.map((t) => t.trim())
.filter(Boolean);
// Render markdown to HTML
const processedContent = await remark()
.use(html, { sanitize: false })
.process(content);
const contentHtml = processedContent.toString();
const { error } = await supabase
.from("posts")
.update({
title,
date: new Date(date).toISOString(),
excerpt,
content,
content_html: contentHtml,
author,
tags,
cover_image: coverImage,
published,
})
.eq("id", id);
if (error) {
throw new Error(`Failed to update post: ${error.message}`);
}
revalidatePath("/");
revalidatePath("/posts");
revalidatePath(`/posts/${id}`);
revalidatePath("/admin/posts");
redirect("/admin/posts");
}
export async function deletePost(id: string) {
const supabase = await createClient();
const { error } = await supabase.from("posts").delete().eq("id", id);
if (error) {
throw new Error(`Failed to delete post: ${error.message}`);
}
revalidatePath("/");
revalidatePath("/posts");
revalidatePath("/admin/posts");
redirect("/admin/posts");
}
+21
View File
@@ -0,0 +1,21 @@
import { createClient } from "@/utils/supabase/server";
import PostEditor from "../../components/PostEditor";
import { createPost } from "../actions";
export default async function NewPostPage() {
const supabase = await createClient();
const { data: authors } = await supabase
.from("authors")
.select("name")
.order("name");
return (
<div>
<h1 className="text-3xl font-bold mb-8">New Post</h1>
<div className="bg-white rounded-lg shadow-sm p-6">
<PostEditor action={createPost} authors={authors || []} />
</div>
</div>
);
}
+99
View File
@@ -0,0 +1,99 @@
import { createClient } from "@/utils/supabase/server";
import Link from "next/link";
import { deletePost } from "./actions";
export default async function AdminPostsPage() {
const supabase = await createClient();
const { data: posts } = await supabase
.from("posts")
.select("id, title, date, author, published, tags")
.order("date", { ascending: false });
return (
<div>
<div className="flex justify-between items-center mb-8">
<h1 className="text-3xl font-bold">Posts</h1>
<Link
href="/admin/posts/new"
className="bg-accent text-white px-4 py-2 rounded-md hover:bg-accent-dark"
>
New Post
</Link>
</div>
<div className="bg-white rounded-lg shadow-sm overflow-hidden">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Title
</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Author
</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Date
</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Status
</th>
<th className="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">
Actions
</th>
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{posts?.map((post) => (
<tr key={post.id} className="hover:bg-gray-50">
<td className="px-6 py-4">
<Link
href={`/admin/posts/${post.id}/edit`}
className="text-gray-900 font-medium hover:text-accent-dark"
>
{post.title}
</Link>
<p className="text-gray-500 text-sm">{post.id}</p>
</td>
<td className="px-6 py-4 text-gray-600">{post.author}</td>
<td className="px-6 py-4 text-gray-600">
{new Date(post.date).toLocaleDateString()}
</td>
<td className="px-6 py-4">
{post.published ? (
<span className="text-xs bg-green-100 text-green-700 px-2 py-1 rounded">
Published
</span>
) : (
<span className="text-xs bg-yellow-100 text-yellow-700 px-2 py-1 rounded">
Draft
</span>
)}
</td>
<td className="px-6 py-4 text-right space-x-2">
<Link
href={`/admin/posts/${post.id}/edit`}
className="text-sm text-accent hover:text-accent-dark"
>
Edit
</Link>
<form action={deletePost.bind(null, post.id)} className="inline">
<button
type="submit"
className="text-sm text-red-600 hover:text-red-800"
>
Delete
</button>
</form>
</td>
</tr>
))}
</tbody>
</table>
{(!posts || posts.length === 0) && (
<p className="text-gray-500 text-center py-8">No posts found.</p>
)}
</div>
</div>
);
}