diff --git a/app/[locale]/works/[slug]/page.tsx b/app/[locale]/works/[slug]/page.tsx index 1257281..6430571 100644 --- a/app/[locale]/works/[slug]/page.tsx +++ b/app/[locale]/works/[slug]/page.tsx @@ -1,6 +1,6 @@ import { notFound } from "next/navigation"; import { MDXRemote } from "next-mdx-remote/rsc"; -import { getAllProjects, getProjectBySlug, getProjectSlugs } from "@/lib/projects/get-projects"; +import { getProjectBySlug, getProjectSlugs, getRandomOtherProjects } from "@/lib/projects/get-projects"; import { mdxComponents } from "@/components/mdx/mdxComponents"; import { ProjectHero } from "@/app/[locale]/works/_components/ProjectHero"; import { ProjectMeta } from "@/app/[locale]/works/_components/ProjectMeta"; @@ -38,15 +38,12 @@ export default async function ProjectPage({ params }: props) { notFound(); } - const otherProjects = getAllProjects(locale) - .filter((item: any) => item.slug !== slug && item.ready) - .slice(0, 2) - .map((item: any) => ({ - slug: item.slug, - title: item.title, - description: item.description, - cover: item.cover, - })); + const otherProjects = getRandomOtherProjects(locale, slug, 2).map((item) => ({ + slug: item.slug, + title: item.title, + description: item.description, + cover: item.cover, + })); return (
diff --git a/lib/projects/get-projects.ts b/lib/projects/get-projects.ts index ce94ec4..c9c9fa1 100644 --- a/lib/projects/get-projects.ts +++ b/lib/projects/get-projects.ts @@ -41,4 +41,17 @@ export function getAllProjects(locale: string): Project[] { .map((slug) => getProjectBySlug(slug, locale)) .filter((project): project is Project => project !== null) .sort((a, b) => a.priority - b.priority); +} + +export function getRandomOtherProjects(locale: string, excludeSlug: string, count: number): Project[] { + const candidates = getAllProjects(locale).filter( + (project) => project.slug !== excludeSlug && project.ready + ); + + for (let i = candidates.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [candidates[i], candidates[j]] = [candidates[j], candidates[i]]; + } + + return candidates.slice(0, count); } \ No newline at end of file