All files / src/pages/admin/blogs BlogListPage.tsx

100% Statements 23/23
100% Branches 24/24
100% Functions 9/9
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209                      1x             21x 21x 21x   21x 21x   21x           21x 9x               9x                 21x       9x 3x   3x     9x                 21x                                 1x                         1x                       1x                                                                                           9x                                                                   1x                                    
import { useState } from "react";
import { Link } from "@tanstack/react-router";
import { Plus } from "lucide-react";
import type { BlogStatus, IdeaSource } from "../../../lib/api/blogs";
import { MARKETPLACE_URL } from "../../../lib/env";
import { Button } from "../../../components/ui/button";
import {
	useAdminBlogs,
	useAdminBlogCategories,
} from "../../../hooks/queries/useAdminBlogQueries";
 
const OWNER_STYLES: Record<IdeaSource, string> = {
	editorial: "bg-purple-100 text-purple-800",
	pro_request: "bg-blue-100 text-blue-800",
	ai_suggestion: "bg-amber-100 text-amber-800",
};
 
export function BlogListPage() {
	const [filterStatus, setFilterStatus] = useState<BlogStatus | "">("");
	const [filterOwner, setFilterOwner] = useState<IdeaSource | "">("");
	const [searchQuery, setSearchQuery] = useState("");
 
	const { data: categories = [] } = useAdminBlogCategories();
	const categoryMap = new Map(categories.map((c) => [c.id, c.name]));
 
	const { data: blogs = [], isLoading: loading } = useAdminBlogs({
		status: filterStatus || undefined,
		ideaSource: filterOwner || undefined,
		search: searchQuery || undefined,
	});
 
	const getStatusBadge = (status: BlogStatus) => {
		const colors = {
			draft: "bg-background-subtle text-foreground-default",
			pending_approval: "bg-yellow-100 text-yellow-800",
			approved: "bg-green-100 text-green-800",
			published: "bg-blue-100 text-blue-800",
			archived: "bg-background-subtle text-foreground-muted",
		};
 
		return (
			<span
				className={`inline-flex items-center px-2 py-1 rounded text-xs font-medium ${colors[status]}`}
			>
				{status.replace("_", " ")}
			</span>
		);
	};
 
	const getOwnerBadge = (
		ideaSource: IdeaSource | null,
		proName: string | null,
	) => {
		if (!ideaSource) return <span className="text-foreground-subtle">-</span>;
		const style = OWNER_STYLES[ideaSource];
		const label =
			ideaSource === "editorial"
				? "Editorial"
				: proName || "Pro";
		return (
			<span
				className={`inline-flex items-center px-2 py-1 rounded text-xs font-medium ${style}`}
			>
				{label}
			</span>
		);
	};
 
	return (
		<div className="space-y-6">
			{/* Header */}
			<div className="flex items-center justify-between">
				<h1 className="text-2xl font-bold">Blog Management</h1>
				<Link to="/admin/blogs/create">
					<Button>
						<Plus className="h-4 w-4" />
						Create New Blog
					</Button>
				</Link>
			</div>
 
			{/* Filters */}
			<div className="flex gap-4">
				<select
					value={filterStatus}
					onChange={(e) => setFilterStatus(e.target.value as BlogStatus | "")}
					className="h-10 rounded-md border border-border-default bg-background-elevated px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
				>
					<option value="">All Statuses</option>
					<option value="draft">Draft</option>
					<option value="pending_approval">Pending Approval</option>
					<option value="approved">Approved</option>
					<option value="published">Published</option>
					<option value="archived">Archived</option>
				</select>
 
				<select
					value={filterOwner}
					onChange={(e) => setFilterOwner(e.target.value as IdeaSource | "")}
					className="h-10 rounded-md border border-border-default bg-background-elevated px-3 text-sm focus:outline-none focus:ring-2 focus:ring-primary-500/40 focus:border-primary-500"
				>
					<option value="">All Owners</option>
					<option value="editorial">Editorial</option>
					<option value="pro_request">Pro-authored</option>
					<option value="ai_suggestion">AI / Pro</option>
				</select>
 
				<input
					type="text"
					value={searchQuery}
					onChange={(e) => setSearchQuery(e.target.value)}
					placeholder="Search blogs..."
					className="flex-1 h-10 rounded-md border border-border-default bg-background-elevated px-3 text-sm"
				/>
			</div>
 
			{/* Blog List Table */}
			<div className="border border-border-default rounded-lg overflow-hidden">
				<table data-testid="blog-table" className="w-full">
					<thead className="bg-background-muted">
						<tr>
							<th className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider">
								Title
							</th>
							<th className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider">
								Owner
							</th>
							<th className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider">
								Status
							</th>
							<th className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider">
								Category
							</th>
							<th className="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider">
								Published
							</th>
							<th className="px-6 py-3 text-right text-xs font-medium uppercase tracking-wider">
								Actions
							</th>
						</tr>
					</thead>
					<tbody className="bg-background-elevated divide-y divide-border-default">
						{loading ? (
							<tr>
								<td colSpan={6} className="px-6 py-4 text-center text-sm">
									Loading...
								</td>
							</tr>
						) : blogs.length === 0 ? (
							<tr>
								<td colSpan={6} className="px-6 py-4 text-center text-sm">
									No blogs found
								</td>
							</tr>
						) : (
							blogs.map((blog) => (
								<tr key={blog.id} className="hover:bg-background-muted">
									<td className="px-6 py-4">
										<div>
											<p className="text-sm font-medium">{blog.title}</p>
											<p className="text-xs text-foreground-subtle">
												{blog.slug}
											</p>
										</div>
									</td>
									<td className="px-6 py-4">
										{getOwnerBadge(blog.ideaSource, blog.ideaSourceProName)}
									</td>
									<td className="px-6 py-4">{getStatusBadge(blog.status)}</td>
									<td className="px-6 py-4 text-sm">
										{blog.categoryId
											? (categoryMap.get(blog.categoryId) ?? "-")
											: "-"}
									</td>
									<td className="px-6 py-4 text-sm">
										{blog.publishedAt
											? new Date(blog.publishedAt).toLocaleDateString()
											: "-"}
									</td>
									<td className="px-6 py-4 text-right text-sm">
										<Link
											to={`/admin/blogs/${blog.id}/edit`}
											className="text-primary-600 hover:text-primary-800 mr-3"
										>
											Edit
										</Link>
										<button
											type="button"
											className="text-primary-600 hover:text-primary-800"
											onClick={() => {
												window.open(
													`${MARKETPLACE_URL}/blog/${blog.slug}`,
													"_blank",
												);
											}}
										>
											View
										</button>
									</td>
								</tr>
							))
						)}
					</tbody>
				</table>
			</div>
		</div>
	);
}