From a3cce41d1ef810a2b37628cd8352bd85e9ae6ee2 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 1 Oct 2026 16:33:12 -0400 Subject: [PATCH] Normalise about-me data before rendering NULL title/description and an empty skills list reached the components as-is. Normalising once in aboutMeData.ts keeps the presentational components free of undefined checks, and wraps the fetch so a failing API degrades to a skeleton instead of an unhandled error. --- .../aboutMe/AboutMeContent-fixed.tsx | 100 ++++++------- .../components/aboutMe/AboutMeContent.tsx | 98 ++++++------- .../(website)/components/aboutMe/aboutMe.tsx | 48 +++++-- .../components/aboutMe/aboutMeData.ts | 134 ++++++++++++++++++ 4 files changed, 248 insertions(+), 132 deletions(-) create mode 100644 src/app/(routes)/(website)/components/aboutMe/aboutMeData.ts diff --git a/src/app/(routes)/(website)/components/aboutMe/AboutMeContent-fixed.tsx b/src/app/(routes)/(website)/components/aboutMe/AboutMeContent-fixed.tsx index a5bfa14..64edd59 100644 --- a/src/app/(routes)/(website)/components/aboutMe/AboutMeContent-fixed.tsx +++ b/src/app/(routes)/(website)/components/aboutMe/AboutMeContent-fixed.tsx @@ -3,39 +3,22 @@ import React from "react"; import Image from "next/image"; import { motion } from "framer-motion"; -import { AboutMeDataType } from "@/app/types/types"; +import { + categorizeSkills, + normalizeAboutMeData, + resolveImageSrc, +} from "./aboutMeData"; interface AboutMeContentProps { - data: AboutMeDataType; + data: unknown; } -const CATEGORIES = { - "Core Tech": ["React", "Next.js", "TypeScript", "JavaScript"], - "Backend & Tools": ["Laravel", "PHP", "MySQL", "Node.js", "Docker", "Git"], - "Architecture & Others": ["REST API", "TDD", "UI/UX Design", "Clean Architecture"], -}; - -type CategoryKey = keyof typeof CATEGORIES | "Other"; - export default function AboutMeContent({ data }: AboutMeContentProps) { - const categorizedSkills: Record = { - "Core Tech": [], - "Backend & Tools": [], - "Architecture & Others": [], - "Other": [], - }; - - data.skills.forEach((skill) => { - let matched = false; - for (const [cat, skills] of Object.entries(CATEGORIES)) { - if (skills.some((s) => skill.title.toLowerCase().includes(s.toLowerCase()))) { - categorizedSkills[cat as CategoryKey].push(skill.title); - matched = true; - break; - } - } - if (!matched) categorizedSkills["Other"].push(skill.title); - }); + const aboutMe = normalizeAboutMeData(data); + const imageSrc = resolveImageSrc(aboutMe.image); + const skillGroups = Object.entries(categorizeSkills(aboutMe.skills)).filter( + ([, skills]) => skills.length > 0, + ); const isRTL = typeof document !== "undefined" && document.dir === "rtl"; @@ -53,15 +36,15 @@ export default function AboutMeContent({ data }: AboutMeContentProps) { className="flex w-full flex-col items-start gap-6 lg:w-[55%]" >

- {data.title} + {aboutMe.title}

- {data.description} + {aboutMe.description}

-
- {Object.entries(categorizedSkills).map(([category, skills]) => ( - skills.length > 0 && ( + {skillGroups.length > 0 ? ( +
+ {skillGroups.map(([category, skills]) => (
{skills.map((skill, idx) => ( - + {skill} ))}
- ) - ))} -
+ ))} +
+ ) : null} {/* Image Section */} - -
-
-
- profile picture + {imageSrc ? ( + +
+
+
+ {aboutMe.title} +
-
- + + ) : null} ); } diff --git a/src/app/(routes)/(website)/components/aboutMe/AboutMeContent.tsx b/src/app/(routes)/(website)/components/aboutMe/AboutMeContent.tsx index 5c3df81..a83e5cc 100644 --- a/src/app/(routes)/(website)/components/aboutMe/AboutMeContent.tsx +++ b/src/app/(routes)/(website)/components/aboutMe/AboutMeContent.tsx @@ -3,39 +3,22 @@ import React from "react"; import Image from "next/image"; import { motion } from "framer-motion"; -import { AboutMeDataType } from "@/app/types/types"; +import { + categorizeSkills, + normalizeAboutMeData, + resolveImageSrc, +} from "./aboutMeData"; interface AboutMeContentProps { - data: AboutMeDataType; + data: unknown; } -const CATEGORIES = { - "Core Tech": ["React", "Next.js", "TypeScript", "JavaScript"], - "Backend & Tools": ["Laravel", "PHP", "MySQL", "Node.js", "Docker", "Git"], - "Architecture & Others": ["REST API", "TDD", "UI/UX Design", "Clean Architecture"], -}; - -type CategoryKey = keyof typeof CATEGORIES | "Other"; - export default function AboutMeContent({ data }: AboutMeContentProps) { - const categorizedSkills: Record = { - "Core Tech": [], - "Backend & Tools": [], - "Architecture & Others": [], - "Other": [], - }; - - data.skills.forEach((skill) => { - let matched = false; - for (const [cat, skills] of Object.entries(CATEGORIES)) { - if (skills.some((s) => skill.title.toLowerCase().includes(s.toLowerCase()))) { - categorizedSkills[cat as CategoryKey].push(skill.title); - matched = true; - break; - } - } - if (!matched) categorizedSkills["Other"].push(skill.title); - }); + const aboutMe = normalizeAboutMeData(data); + const imageSrc = resolveImageSrc(aboutMe.image); + const skillGroups = Object.entries(categorizeSkills(aboutMe.skills)).filter( + ([, skills]) => skills.length > 0, + ); const isRTL = typeof document !== "undefined" && document.dir === "rtl"; @@ -53,15 +36,15 @@ export default function AboutMeContent({ data }: AboutMeContentProps) { className="flex w-full flex-col items-start gap-6 lg:w-[55%]" >

- {data.title} + {aboutMe.title}

- {data.description} + {aboutMe.description}

-
- {Object.entries(categorizedSkills).map(([category, skills]) => ( - skills.length > 0 && ( + {skillGroups.length > 0 ? ( +
+ {skillGroups.map(([category, skills]) => (
{skills.map((skill, idx) => ( - + {skill} ))}
- ) - ))} -
+ ))} +
+ ) : null} {/* Image Section */} - -
-
- profile picture + {imageSrc ? ( + +
+
+ {aboutMe.title} +
-
- + + ) : null} ); } diff --git a/src/app/(routes)/(website)/components/aboutMe/aboutMe.tsx b/src/app/(routes)/(website)/components/aboutMe/aboutMe.tsx index cdfa056..3c0fec9 100644 --- a/src/app/(routes)/(website)/components/aboutMe/aboutMe.tsx +++ b/src/app/(routes)/(website)/components/aboutMe/aboutMe.tsx @@ -1,27 +1,45 @@ -import { AboutMeDataType } from "@/app/types/types"; import { cookies } from "next/headers"; import React from "react"; import Skeleton from "react-loading-skeleton"; import "react-loading-skeleton/dist/skeleton.css"; import AboutMeContent from "./AboutMeContent"; +async function fetchAboutMeData(): Promise { + const cookieStore = await cookies(); + const locale = + cookieStore.get("selectedLocale")?.value ?? + cookieStore.get("defaultLocale")?.value; + + try { + const res = await fetch( + `${process.env.NEXT_PUBLIC_API_URL}/website/about-me/1/translation/${locale}`, + ); + + return res.json(); + } catch { + return null; + } +} + +function AboutMeSkeleton() { + return ( +
+
+ + +
+
+ +
+
+ ); +} + export default async function AboutMe() { - const aboutMeData: AboutMeDataType = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/website/about-me/1/translation/${(await cookies()).get("selectedLocale")?.value ? (await cookies()).get("selectedLocale")?.value : (await cookies()).get("defaultLocale")?.value}`, - ).then((res) => res.json()); + const aboutMeData = await fetchAboutMeData(); if (!aboutMeData) { - return ( -
-
- - -
-
- -
-
- ); + return ; } return ; diff --git a/src/app/(routes)/(website)/components/aboutMe/aboutMeData.ts b/src/app/(routes)/(website)/components/aboutMe/aboutMeData.ts new file mode 100644 index 0000000..9807e52 --- /dev/null +++ b/src/app/(routes)/(website)/components/aboutMe/aboutMeData.ts @@ -0,0 +1,134 @@ +export const ABOUT_ME_FALLBACK_TITLE = "درباره من"; + +export const ABOUT_ME_FALLBACK_DESCRIPTION = + "معرفی کوتاهی از من و مسیر حرفه‌ای‌ام."; + +export const SKILL_CATEGORIES = { + "Core Tech": ["React", "Next.js", "TypeScript", "JavaScript"], + "Backend & Tools": ["Laravel", "PHP", "MySQL", "Node.js", "Docker", "Git"], + "Architecture & Others": [ + "REST API", + "TDD", + "UI/UX Design", + "Clean Architecture", + ], +} as const; + +export type SkillCategoryKey = keyof typeof SKILL_CATEGORIES | "Other"; + +export type AboutMeSkill = { + title: string; +}; + +export type NormalizedAboutMe = { + title: string; + description: string; + image: string; + skills: AboutMeSkill[]; +}; + +function textOrEmpty(value: unknown): string { + if (typeof value === "string") { + return value.trim(); + } + + if (typeof value === "number" && Number.isFinite(value)) { + return String(value); + } + + return ""; +} + +/** + * Turns the raw about-me payload (which may be null, wrapped in `data`, or + * missing translations) into a fully guarded shape: every field is either a + * non-empty string or an empty list, and the Persian fallbacks are applied for + * the missing title/description. + */ +export function normalizeAboutMeData(raw: unknown): NormalizedAboutMe { + const payload = + raw && typeof raw === "object" && "data" in raw + ? (raw as { data: unknown }).data + : raw; + + const source = + payload && typeof payload === "object" + ? (payload as Record) + : {}; + + const rawSkills = Array.isArray(source.skills) ? source.skills : []; + const skills: AboutMeSkill[] = []; + + for (const rawSkill of rawSkills) { + const title = textOrEmpty( + rawSkill && typeof rawSkill === "object" + ? (rawSkill as Record).title + : rawSkill, + ); + + if (title !== "") { + skills.push({ title }); + } + } + + return { + title: textOrEmpty(source.title) || ABOUT_ME_FALLBACK_TITLE, + description: + textOrEmpty(source.description) || ABOUT_ME_FALLBACK_DESCRIPTION, + image: textOrEmpty(source.image), + skills, + }; +} + +/** Builds a next/image-usable src, or null when there is no usable image. */ +export function resolveImageSrc(value: unknown): string | null { + const src = textOrEmpty(value); + + if (src === "") { + return null; + } + + if (src.startsWith("/") || /^https?:\/\//i.test(src)) { + return src; + } + + const baseUrl = textOrEmpty(process.env.NEXT_PUBLIC_BACK_URL).replace( + /\/+$/, + "", + ); + + return baseUrl === "" ? null : `${baseUrl}/${src.replace(/^\/+/, "")}`; +} + +/** Groups the given skill titles into the known categories, never inventing any. */ +export function categorizeSkills( + skills: AboutMeSkill[], +): Record { + const categorized: Record = { + "Core Tech": [], + "Backend & Tools": [], + "Architecture & Others": [], + Other: [], + }; + + for (const skill of skills) { + const title = textOrEmpty(skill?.title); + const normalizedTitle = title.toLowerCase(); + + if (normalizedTitle === "") { + continue; + } + + const matchedCategory = ( + Object.keys(SKILL_CATEGORIES) as Array + ).find((category) => + SKILL_CATEGORIES[category].some((knownSkill) => + normalizedTitle.includes(knownSkill.toLowerCase()), + ), + ); + + categorized[matchedCategory ?? "Other"].push(title); + } + + return categorized; +}