import type { Metadata } from "next"; import Link from "next/link"; import { BlogCoverImage } from "@/components/BlogCoverImage"; import { ContentPageShell } from "@/components/ContentPageShell"; import { Pagination } from "@/components/Pagination"; import { findInstructionCategory, getDescendantInstructionCategoryIds, getRootInstructionCategories, instructionCategoryHref, instructionCategoryLabelFa, topInstructionCategories, type InstructionCategory, } from "@/data/instructionCategories"; import { getInstructionCategories, getInstructionCoverImage, getInstructions, getInstructionsByCategory, } from "@/lib/api"; export const metadata: Metadata = { title: "آموزش‌ها", description: "راهنماها و آموزش‌های کاربردی تیم مشکی.", alternates: { canonical: "/instruction" }, }; const PAGE_SIZE = 12; type InstructionPageProps = { searchParams: Promise<{ page?: string; category?: string }>; }; function mapApiCategories( items: Awaited>, ): InstructionCategory[] { return items.map((item) => ({ id: String(item.id), slug: item.slug, nameEn: item.name, nameFa: item.nameFa || item.name, parentId: item.parentId, sortOrder: item.sortOrder ?? 0, })); } export default async function InstructionListPage({ searchParams, }: InstructionPageProps) { const { page: pageParam, category: categoryParam } = await searchParams; const page = Math.max(1, Number(pageParam) || 1); let allCategories: InstructionCategory[] = topInstructionCategories; try { allCategories = mapApiCategories(await getInstructionCategories()); } catch { allCategories = topInstructionCategories; } const rootCategories = getRootInstructionCategories(allCategories); const activeCategory = findInstructionCategory(categoryParam, allCategories) ?? findInstructionCategory(categoryParam, rootCategories); let items: Awaited>["items"] = []; let total = 0; try { if (activeCategory) { const data = await getInstructionsByCategory({ categoryId: activeCategory.id, categoryIds: getDescendantInstructionCategoryIds( allCategories, activeCategory.id, ), page, pageSize: PAGE_SIZE, }); items = data.items; total = data.total; } else { const data = await getInstructions({ page, pageSize: PAGE_SIZE }); items = data.items; total = data.total; } } catch { items = []; total = 0; } const paginationQuery = activeCategory ? { category: activeCategory.slug } : undefined; const filterCategories = rootCategories.length > 0 ? rootCategories : topInstructionCategories; return (

Instructions

آموزش‌ها

راهنماها و آموزش‌های کاربردی برای استفاده از خدمات مشکی.

همه {filterCategories.map((category) => { const active = activeCategory?.id === category.id; return ( {category.nameFa} ); })}
{items.length === 0 ? (

آموزشی برای نمایش وجود ندارد.

) : (
{items.map((item, index) => { const image = getInstructionCoverImage(item); const categoryFa = instructionCategoryLabelFa( item.categoryName, allCategories, ); const titleFa = item.titleFa || item.title; const titleEn = item.titleEn ?? (item.titleFa && item.title !== item.titleFa ? item.title : null); return (
{image ? ( ) : null}
{categoryFa ? (

{categoryFa}

) : null}

{titleFa}

{titleEn ? (

{titleEn}

) : null} {item.abstract ? (

{item.abstract}

) : null}
); })}
)}
); }