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 210 211 212 213 214 215 216 217 218 219 220 221                                                          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 {
	AdminPageHead,
	AdminToolbar,
} from "../../../components/admin/AdminPageHead";
import { Button } from "../../../components/ui/button";
import { Card } from "../../../components/ui/card";
import { Input } from "../../../components/ui/input";
import { formatDate } from "../../../lib/utils";
import { STATUS_INK, STATUS_TINT } from "../../../components/ui/status-ink";
import {
	useAdminBlogs,
	useAdminBlogCategories,
} from "../../../hooks/queries/useAdminBlogQueries";
import { Select } from "../../../components/ui/select";
import {
	Table,
	TableBody,
	TableCaption,
	TableCell,
	TableHead,
	TableHeader,
	TableRow,
} from "../../../components/ui/table";
 
const OWNER_STYLES: Record<IdeaSource, string> = {
	editorial: "bg-primary-100 text-primary-900",
	pro_request: `${STATUS_TINT.info} ${STATUS_INK.info}`,
	ai_suggestion: `${STATUS_TINT.warning} ${STATUS_INK.warning}`,
};
 
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: `${STATUS_TINT.warning} ${STATUS_INK.warning}`,
			approved: `${STATUS_TINT.success} ${STATUS_INK.success}`,
			published: `${STATUS_TINT.info} ${STATUS_INK.info}`,
			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">
			<AdminPageHead
				title="Blog Management"
				actions={
					<Link to="/admin/blogs/create">
						<Button>
							<Plus aria-hidden="true" />
							Create New Blog
						</Button>
					</Link>
				}
			/>
 
			<AdminToolbar aria-label="Blog filters">
				<Select
					aria-label="Filter by status"
					value={filterStatus}
					onChange={(e) => setFilterStatus(e.target.value as BlogStatus | "")}
				>
					<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
					aria-label="Filter by owner"
					value={filterOwner}
					onChange={(e) => setFilterOwner(e.target.value as IdeaSource | "")}
				>
					<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>
 
				<div className="min-w-56 grow">
					<Input
						type="text"
						aria-label="Search blogs"
						value={searchQuery}
						onChange={(e) => setSearchQuery(e.target.value)}
						placeholder="Search blogs..."
					/>
				</div>
			</AdminToolbar>
 
			{/* Blog List Table */}
			<Card className="overflow-hidden">
				<Table density="dense" data-testid="blog-table">
					<TableCaption>
						Every blog post on the platform, with its publish status.
					</TableCaption>
					<TableHeader>
						<TableRow>
							<TableHead>Title</TableHead>
							<TableHead>Owner</TableHead>
							<TableHead>Status</TableHead>
							<TableHead>Category</TableHead>
							<TableHead numeric>Published</TableHead>
							<TableHead className="text-right">Actions</TableHead>
						</TableRow>
					</TableHeader>
					<TableBody>
						{loading ? (
							<TableRow>
								<TableCell colSpan={6} className="text-center text-sm">
									Loading...
								</TableCell>
							</TableRow>
						) : blogs.length === 0 ? (
							<TableRow>
								<TableCell colSpan={6} className="text-center text-sm">
									No blogs found
								</TableCell>
							</TableRow>
						) : (
							blogs.map((blog) => (
								<TableRow key={blog.id}>
									<TableCell>
										<div>
											<p className="text-sm font-medium">{blog.title}</p>
											<p className="text-xs text-foreground-subtle">
												{blog.slug}
											</p>
										</div>
									</TableCell>
									<TableCell>
										{getOwnerBadge(blog.ideaSource, blog.ideaSourceProName)}
									</TableCell>
									<TableCell>{getStatusBadge(blog.status)}</TableCell>
									<TableCell className="text-sm">
										{blog.categoryId
											? (categoryMap.get(blog.categoryId) ?? "-")
											: "-"}
									</TableCell>
									{/* `en-IN` from one place. */}
									<TableCell
										numeric
										className="whitespace-nowrap text-sm text-foreground-muted"
									>
										{blog.publishedAt
											? formatDate(new Date(blog.publishedAt))
											: "-"}
									</TableCell>
									<TableCell className="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>
									</TableCell>
								</TableRow>
							))
						)}
					</TableBody>
				</Table>
			</Card>
		</div>
	);
}