Fix Persian OG image shaping, add category/tag social images, fix project category tabs
Deploy Frontend / deploy (push) Successful in 1m2s
Deploy Frontend / deploy (push) Successful in 1m2s
This commit is contained in:
1 parent
abfcd87668
commit
cb134b1cf1
9 files changed
+213
-88
No files matched your search
@@ -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<ProjectCategory[]>([]);
|
||||
const [projects, setProjects] = useState<Project[]>([]);
|
||||
// The selected tab is a stable category key, not a translated label.
|
||||
const [selectedCategory, setSelectedCategory] = useState<string | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [selectedProject, setSelectedProject] = useState<Project | null>(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<Record<string, Project[]>>((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) {
|
||||
|
||||
<div className="flex flex-wrap items-center justify-center gap-3 mb-12">
|
||||
{categories.length > 0
|
||||
? categories.map((category, index) => (
|
||||
? categories.map((category) => (
|
||||
<motion.button
|
||||
key={index}
|
||||
key={category.key}
|
||||
whileHover={{ scale: 1.05 }}
|
||||
whileTap={{ scale: 0.95 }}
|
||||
onClick={() => 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
|
||||
})`}
|
||||
</motion.button>
|
||||
))
|
||||
: renderSkeletons(4)}
|
||||
|
||||
@@ -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<Me
|
||||
locale,
|
||||
);
|
||||
|
||||
const name = categoryData.category.name;
|
||||
const description = categoryData.category.short_description;
|
||||
|
||||
/**
|
||||
* The API image is used when it is a real url, otherwise a generated card
|
||||
* carries the category name (Persian names go through the OG renderer, which
|
||||
* shapes Arabic script). Platforms need the explicit size and the Twitter
|
||||
* block, so they are always emitted.
|
||||
*/
|
||||
const categoryImage = getCoverImageSrc(categoryData.category.image);
|
||||
const image = categoryImage
|
||||
? { url: categoryImage, width: 1200, height: 630, alt: name }
|
||||
: { url: ogImageUrl(name), width: 1200, height: 630, alt: name };
|
||||
const pageUrl = `${SITE_URL}/blog/categories/${params.categorySlug}`;
|
||||
|
||||
return {
|
||||
title: categoryData.category.name,
|
||||
description: categoryData.category.short_description,
|
||||
title: name,
|
||||
description,
|
||||
alternates: { canonical: pageUrl },
|
||||
openGraph: {
|
||||
title: categoryData.category.name,
|
||||
description: categoryData.category.short_description,
|
||||
images: [categoryData.category.image],
|
||||
title: name,
|
||||
description,
|
||||
url: pageUrl,
|
||||
type: "website",
|
||||
siteName: SITE_NAME,
|
||||
locale: locale === "fa" ? "fa_IR" : "en_US",
|
||||
alternateLocale: locale === "fa" ? ["en_US"] : ["fa_IR"],
|
||||
images: [image],
|
||||
},
|
||||
twitter: {
|
||||
card: "summary_large_image",
|
||||
title: name,
|
||||
description,
|
||||
images: [image.url],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,17 +10,29 @@ import NoDataFounded from "../components/no-data-founded/noDataFounded";
|
||||
import { Metadata } from "next";
|
||||
import { getDictionary } from "@/app/lib/functions/dictionaries";
|
||||
import { getDefaultLocale } from "@/app/lib/functions/utils";
|
||||
import { ogImageUrl } from "@/app/lib/site-config";
|
||||
import { ogImageUrl, SITE_NAME, SITE_URL } from "@/app/lib/site-config";
|
||||
|
||||
const TITLE = "All Categories List";
|
||||
const DESCRIPTION =
|
||||
"Browse through our collection of categories to find posts on your favorite topics. Whether you`re looking for insightful articles, how-tos, or the latest trends, our categories make it easy to discover content tailored to your interests.";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "All Categories List",
|
||||
description:
|
||||
"Browse through our collection of categories to find posts on your favorite topics. Whether you`re looking for insightful articles, how-tos, or the latest trends, our categories make it easy to discover content tailored to your interests.",
|
||||
title: TITLE,
|
||||
description: DESCRIPTION,
|
||||
alternates: { canonical: `${SITE_URL}/blog/categories` },
|
||||
openGraph: {
|
||||
title: "All Categories List",
|
||||
description:
|
||||
"Browse through our collection of categories to find posts on your favorite topics. Whether you`re looking for insightful articles, how-tos, or the latest trends, our categories make it easy to discover content tailored to your interests.",
|
||||
images: [ogImageUrl("All Categories List")],
|
||||
title: TITLE,
|
||||
description: DESCRIPTION,
|
||||
url: `${SITE_URL}/blog/categories`,
|
||||
type: "website",
|
||||
siteName: SITE_NAME,
|
||||
images: [{ url: ogImageUrl(TITLE), width: 1200, height: 630, alt: TITLE }],
|
||||
},
|
||||
twitter: {
|
||||
card: "summary_large_image",
|
||||
title: TITLE,
|
||||
description: DESCRIPTION,
|
||||
images: [ogImageUrl(TITLE)],
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import { formatDate } from "@/app/lib/functions/formatDate";
|
||||
import ShareButtons from "../../components/share/shareButtons";
|
||||
import CommentSection from "./comments";
|
||||
import PostStats from "./postStats";
|
||||
import { getCoverImageSrc } from "./coverImage";
|
||||
import { getCoverImageSrc } from "@/app/lib/cover-image";
|
||||
import { AUTHOR_PROFILE_URL } from "@/app/lib/social-links";
|
||||
import { SITE_NAME, SITE_NAME_EN, SITE_URL } from "@/app/lib/site-config";
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
getDefaultLocale,
|
||||
slugForLocale,
|
||||
} from "@/app/lib/functions/utils";
|
||||
import { ogImageUrl, SITE_NAME, SITE_URL } from "@/app/lib/site-config";
|
||||
|
||||
async function fetchTagData(
|
||||
slug: string,
|
||||
@@ -58,9 +59,36 @@ export async function generateMetadata(props: tagSlugPropsType): Promise<Metadat
|
||||
locale,
|
||||
);
|
||||
|
||||
const name = tagData.tag.name;
|
||||
const description = `Posts tagged with "${name}" on Parsa Aghayi's blog.`;
|
||||
const pageUrl = `${SITE_URL}/blog/tags/${params.slug}`;
|
||||
const image = {
|
||||
url: ogImageUrl(name),
|
||||
width: 1200,
|
||||
height: 630,
|
||||
alt: name,
|
||||
};
|
||||
|
||||
return {
|
||||
title: tagData.tag.name,
|
||||
description: `Posts tagged with "${tagData.tag.name}" on Parsa Aghayi's blog.`,
|
||||
title: name,
|
||||
description,
|
||||
alternates: { canonical: pageUrl },
|
||||
openGraph: {
|
||||
title: name,
|
||||
description,
|
||||
url: pageUrl,
|
||||
type: "website",
|
||||
siteName: SITE_NAME,
|
||||
locale: locale === "fa" ? "fa_IR" : "en_US",
|
||||
alternateLocale: locale === "fa" ? ["en_US"] : ["fa_IR"],
|
||||
images: [image],
|
||||
},
|
||||
twitter: {
|
||||
card: "summary_large_image",
|
||||
title: name,
|
||||
description,
|
||||
images: [image.url],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -32,12 +32,15 @@ export async function GET(request: Request) {
|
||||
fontFamily: "Vazirmatn",
|
||||
lineHeight: 1.35,
|
||||
padding: 64,
|
||||
background: "rgb(110 100 260)",
|
||||
background: "rgb(124, 58, 237)",
|
||||
color: "white",
|
||||
width: "100%",
|
||||
height: "100%",
|
||||
display: "flex",
|
||||
textAlign: "center",
|
||||
unicodeBidi: "isolate",
|
||||
wordBreak: "break-word",
|
||||
overflowWrap: "break-word",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
direction: isRtl ? ("rtl" as const) : ("ltr" as const),
|
||||
@@ -48,7 +51,7 @@ export async function GET(request: Request) {
|
||||
),
|
||||
{
|
||||
width: 1200,
|
||||
height: 1200,
|
||||
height: 630,
|
||||
fonts: [
|
||||
{ name: "Vazirmatn", data: regular, weight: 400, style: "normal" },
|
||||
{ name: "Vazirmatn", data: bold, weight: 700, style: "normal" },
|
||||
|
||||
@@ -47,21 +47,6 @@ const DUAL_FORMS: Record<number, [number, number, number, number]> = {
|
||||
* 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<number>([
|
||||
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("");
|
||||
}
|
||||
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("");
|
||||
}
|
||||
@@ -42,4 +42,4 @@ export function getCoverImageSrc(value: unknown): string | null {
|
||||
}
|
||||
|
||||
return src;
|
||||
}
|
||||
}
|
||||
+12
-2
@@ -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
|
||||
|
||||
Reference in new issue
Block a user