From cb134b1cf18a1d4162d0f1209c1a560ffce5ab19 Mon Sep 17 00:00:00 2001 From: Parsa Aghayi Date: Sun, 4 Oct 2026 01:29:57 -0400 Subject: [PATCH] Fix Persian OG image shaping, add category/tag social images, fix project category tabs --- .../components/projects/projects.tsx | 61 ++++++--- .../blog/categories/[categorySlug]/page.tsx | 39 +++++- src/app/(routes)/blog/categories/page.tsx | 28 +++-- .../(routes)/blog/posts/[postSlug]/page.tsx | 2 +- src/app/(routes)/blog/tags/[slug]/page.tsx | 32 ++++- src/app/(routes)/og-image/route.tsx | 7 +- src/app/(routes)/og-image/shape.ts | 116 +++++++++++------- .../coverImage.ts => lib/cover-image.ts} | 2 +- src/app/types/types.ts | 14 ++- 9 files changed, 213 insertions(+), 88 deletions(-) rename src/app/{(routes)/blog/posts/[postSlug]/coverImage.ts => lib/cover-image.ts} (99%) diff --git a/src/app/(routes)/(website)/components/projects/projects.tsx b/src/app/(routes)/(website)/components/projects/projects.tsx index f0f124e..7d7c647 100644 --- a/src/app/(routes)/(website)/components/projects/projects.tsx +++ b/src/app/(routes)/(website)/components/projects/projects.tsx @@ -31,9 +31,14 @@ interface MyProjectsProps { const MAX_PROJECTS = 6; const MAX_CATEGORIES = 5; +const NEWEST_KEY = "newest"; +const UNCATEGORIZED_KEY = "uncategorized"; +const categoryKey = (id: number) => `category-${id}`; + export default function Projects({ dict }: MyProjectsProps) { const [categories, setCategories] = useState([]); const [projects, setProjects] = useState([]); + // The selected tab is a stable category key, not a translated label. const [selectedCategory, setSelectedCategory] = useState(null); const [loading, setLoading] = useState(true); const [selectedProject, setSelectedProject] = useState(null); @@ -58,14 +63,16 @@ export default function Projects({ dict }: MyProjectsProps) { const uniqueCategories: ProjectCategory[] = [ { + key: NEWEST_KEY, + id: null, label: newestLabel, - active: true, }, ...resCategories .slice(0, MAX_CATEGORIES) .map((category: CategoryResponse) => ({ + key: categoryKey(category.id), + id: category.id, label: category.title, - active: false, })), ]; @@ -82,31 +89,41 @@ export default function Projects({ dict }: MyProjectsProps) { /** * The selection is derived rather than reset from an effect: whenever the * categories list is rebuilt (locale switch, refetch) a selection that is no - * longer present falls back to the first category, which is always "newest". + * longer present falls back to the first tab, which is always "newest". */ const activeCategory = - selectedCategory && categories.some((c) => c.label === selectedCategory) + selectedCategory && categories.some((c) => c.key === selectedCategory) ? selectedCategory - : (categories[0]?.label ?? ""); + : (categories[0]?.key ?? NEWEST_KEY); + const activeCategoryId = + categories.find((c) => c.key === activeCategory)?.id ?? null; + + // Projects are grouped by the category id, so a locale switch or a missing + // translation can never make every tab fall back to the same list. const groupedProjects = useMemo( () => projects.reduce>((acc, project) => { - const category = - project.category || - (locale === "fa" ? "بدون دسته‌بندی" : "Uncategorized"); - if (!acc[category]) acc[category] = []; - acc[category].push(project); + const key = project.category_id ? categoryKey(project.category_id) : UNCATEGORIZED_KEY; + if (!acc[key]) acc[key] = []; + acc[key].push(project); return acc; }, {}), - [projects, locale], + [projects], ); const filteredProjects: Project[] = useMemo(() => { - if (activeCategory === newestLabel) { + if (activeCategory === NEWEST_KEY) { return projects.slice(0, MAX_PROJECTS); } - return groupedProjects[activeCategory]?.slice(0, MAX_PROJECTS) || []; - }, [activeCategory, projects, groupedProjects, newestLabel]); + const grouped = groupedProjects[activeCategory]; + if (grouped?.length) { + return grouped.slice(0, MAX_PROJECTS); + } + // A project with no category has no tab of its own; show it under "newest". + return activeCategoryId === null && activeCategory === UNCATEGORIZED_KEY + ? (groupedProjects[UNCATEGORIZED_KEY]?.slice(0, MAX_PROJECTS) ?? []) + : []; + }, [activeCategory, activeCategoryId, projects, groupedProjects]); const currentModalIndex = useMemo( () => filteredProjects.findIndex((p) => p.id === selectedProject?.id), @@ -171,20 +188,24 @@ export default function Projects({ dict }: MyProjectsProps) {
{categories.length > 0 - ? categories.map((category, index) => ( + ? categories.map((category) => ( setSelectedCategory(category.label)} - aria-pressed={category.label === activeCategory} + onClick={() => setSelectedCategory(category.key)} + aria-pressed={category.key === activeCategory} className={`px-5 py-2 rounded-full text-sm font-medium transition-all duration-300 ${ - category.label === activeCategory + category.key === activeCategory ? "bg-primary text-primary-foreground shadow-lg shadow-primary/30" : "bg-muted text-muted-foreground hover:bg-muted-foreground/20" }`} > - {category.label} + {category.key === NEWEST_KEY + ? category.label + : `${category.label} (${ + groupedProjects[category.key]?.length ?? 0 + })`} )) : renderSkeletons(4)} diff --git a/src/app/(routes)/blog/categories/[categorySlug]/page.tsx b/src/app/(routes)/blog/categories/[categorySlug]/page.tsx index 639a1cc..9f9ac8e 100644 --- a/src/app/(routes)/blog/categories/[categorySlug]/page.tsx +++ b/src/app/(routes)/blog/categories/[categorySlug]/page.tsx @@ -10,6 +10,8 @@ import { getDefaultLocale, slugForLocale, } from "@/app/lib/functions/utils"; +import { getCoverImageSrc } from "@/app/lib/cover-image"; +import { ogImageUrl, SITE_NAME, SITE_URL } from "@/app/lib/site-config"; async function fetchCategoryData( categorySlug: string, @@ -55,13 +57,40 @@ export async function generateMetadata(props: categorySlugPropsType): Promise = { * Letters that attach to a preceding letter but never grow an initial/medial * form of their own (they only ever take isolated or final shapes). */ -const RIGHT_ONLY = new Set([ - 0x0622, // آ - 0x0623, // أ - 0x0624, // ؤ - 0x0625, // إ - 0x0627, // ا - 0x0629, // ة - 0x062f, // د - 0x0630, // ذ - 0x0631, // ر - 0x0632, // ز - 0x0648, // و - 0x0698, // ژ - 0x0621, // ء -]); const HAS_ARABIC_SCRIPT = /[\u0600-\u06ff\u0750-\u077f\u08a0-\u08ff]/; @@ -70,7 +55,7 @@ export function hasArabicScript(text: string): boolean { } function isJoinable(codePoint: number): boolean { - return codePoint in DUAL_FORMS || RIGHT_ONLY.has(codePoint); + return codePoint in DUAL_FORMS; } /** @@ -78,37 +63,74 @@ function isJoinable(codePoint: number): boolean { * so a non-shaping renderer shows them connected, not letter by letter. */ export function shapeArabic(text: string): string { - const codePoints = Array.from(text.trim()); - const out: string[] = []; - - for (let i = 0; i < codePoints.length; i += 1) { - const char = codePoints[i]; - const codePoint = char.codePointAt(0) as number; - - const forms = DUAL_FORMS[codePoint]; - - if (!forms) { - out.push(char); + const chars = Array.from(text.trim()); + // Lam-Alef ligatures must be resolved on the base letters, before the + // per-letter pass turns the lam into its own presentation form. + const out0: string[] = []; + for (let i = 0; i < chars.length; i++) { + const c = chars[i].codePointAt(0); + const n = chars[i+1]?.codePointAt(0); + if ((c === 0x0644 || c === 0xFEDF || c === 0xFEE0 || c === 0xFEDE) && n === 0x0627) { // lam followed by alef + out0.push(String.fromCodePoint(0xFEFB)); // use isolated ligature as simple fix; renderer will place + i++; continue; } - - const previous = i > 0 ? (codePoints[i - 1].codePointAt(0) as number) : null; - const next = - i + 1 < codePoints.length ? (codePoints[i + 1].codePointAt(0) as number) : null; - - const connectsToPrevious = previous !== null && isJoinable(previous); - const connectsToNext = next !== null && isJoinable(next); - - const formIndex = connectsToPrevious - ? connectsToNext - ? 3 // medial - : 1 // final - : connectsToNext - ? 2 // initial - : 0; // isolated - - out.push(String.fromCodePoint(forms[formIndex] as number)); + if (c === 0x0644 && n === 0x0622) { // lām + alef with hamza above + out0.push(String.fromCodePoint(0xFEF7)); + i++; + continue; + } + if (c === 0x0644 && n === 0x0623) { + out0.push(String.fromCodePoint(0xFEF9)); + i++; + continue; + } + out0.push(chars[i]); } - - return out.join(""); -} \ No newline at end of file + const chars2 = out0; + const result: string[] = []; + const transparent = new Set([ + 0x200c, 0x0640, 0x064b, 0x064c, 0x064d, 0x064e, 0x064f, 0x0650, 0x0651, 0x0652, + ]); + const isTrans = (cp: number) => transparent.has(cp); + const getPrevJoinable = (idx: number): number | undefined => { + for (let j = idx - 1; j >= 0; j--) { + const cp = chars2[j].codePointAt(0); + if (cp === undefined) continue; + if (isTrans(cp)) continue; + return cp; + } + return undefined; + }; + const getNextJoinable = (idx: number): number | undefined => { + for (let j = idx + 1; j < chars2.length; j++) { + const cp = chars2[j].codePointAt(0); + if (cp === undefined) continue; + if (isTrans(cp)) continue; + return cp; + } + return undefined; + }; + for (let i = 0; i < chars2.length; i++) { + const ch = chars2[i]; + const cp = ch.codePointAt(0); + if (cp === undefined) continue; + if (isTrans(cp)) { result.push(ch); continue; } + // skip if already ligature + if (cp === 0xFEFB || cp === 0xFEFC || cp === 0xFEF7 || cp === 0xFEF8 || cp === 0xFEF9 || cp === 0xFEFA) { + result.push(ch); + continue; + } + const forms = DUAL_FORMS[cp]; + if (!forms) { result.push(ch); continue; } + const prevCp = getPrevJoinable(i); + const nextCp = getNextJoinable(i); + const connectsToPrevious = prevCp !== undefined && isJoinable(prevCp); + const connectsToNext = nextCp !== undefined && isJoinable(nextCp); + if (connectsToPrevious && connectsToNext) result.push(String.fromCodePoint(forms[3])); + else if (connectsToPrevious) result.push(String.fromCodePoint(forms[1])); + else if (connectsToNext) result.push(String.fromCodePoint(forms[2])); + else result.push(String.fromCodePoint(forms[0])); + } + return result.join(""); +} diff --git a/src/app/(routes)/blog/posts/[postSlug]/coverImage.ts b/src/app/lib/cover-image.ts similarity index 99% rename from src/app/(routes)/blog/posts/[postSlug]/coverImage.ts rename to src/app/lib/cover-image.ts index 4d9631a..23bac72 100644 --- a/src/app/(routes)/blog/posts/[postSlug]/coverImage.ts +++ b/src/app/lib/cover-image.ts @@ -42,4 +42,4 @@ export function getCoverImageSrc(value: unknown): string | null { } return src; -} +} \ No newline at end of file diff --git a/src/app/types/types.ts b/src/app/types/types.ts index 982c377..1b07bf8 100644 --- a/src/app/types/types.ts +++ b/src/app/types/types.ts @@ -324,13 +324,21 @@ export interface Image { export interface Project { id: number; title: string; - category: string; + category: string | null; + category_id: number | null; images: Image[]; } +/** + * A tab in the projects section. Tabs are keyed by the stable category id + * (`newest` is the synthetic first tab), never by the translated label, because + * labels change with the locale and fall back to English when a translation is + * missing. + */ export interface ProjectCategory { + key: string; + id: number | null; label: string; - active: boolean; } // API response structure for projects @@ -340,7 +348,9 @@ export interface ProjectResponse { // API response structure for categories export interface CategoryResponse { + id: number; title: string; + image?: string; } // Props for localization support