"use client" import React, { useCallback, useEffect, useRef, useState } from "react" import Image from "next/image" import NoResultIcon from "@/public/icons/no-result.svg" import { useProjectFiltersState } from "@/state/useProjectFiltersState" import { cva } from "class-variance-authority" import { LangProps } from "@/types/common" import { ProjectSection, ProjectSectionLabelMapping, ProjectSections, } from "@/lib/types" import { cn } from "@/lib/utils" import { useTranslation } from "@/app/i18n/client" import ProjectCard from "./project-card" const sectionTitleClass = cva( "relative font-sans text-base font-bold uppercase tracking-[3.36px] text-anakiwa-950 after:ml-8 after:absolute after:top-1/2 after:h-[1px] after:w-full after:translate-y-1/2 after:bg-anakiwa-300 after:content-['']" ) const NoResults = ({ lang }: LangProps["params"]) => { const { t } = useTranslation(lang, "common") return (
no result icon
{t("noResults")} {t("noResultsDescription")}
) } export const ProjectList = ({ lang }: LangProps["params"]) => { const { t } = useTranslation(lang, "resources-page") const SCROLL_OFFSET = -400 const [activeId, setActiveId] = useState("") const [isManualScroll, setIsManualScroll] = useState(false) const { projects } = useProjectFiltersState((state) => state) const noItems = projects?.length === 0 const sectionsRef = useRef | null>(null) // sections are constant so useRef might be better here useEffect(() => { if (sectionsRef.current === null) sectionsRef.current = document.querySelectorAll(`div[data-section]`) if (!activeId) setActiveId("pse") const handleScroll = () => { if (isManualScroll) return sectionsRef.current?.forEach((section: any) => { const sectionTop = section.offsetTop - SCROLL_OFFSET if (window.scrollY >= sectionTop && window.scrollY > 0) { setActiveId(section.getAttribute("id")) } }) } window.addEventListener("scroll", handleScroll) return () => window.removeEventListener("scroll", handleScroll) }, [SCROLL_OFFSET, activeId, isManualScroll]) const scrollToId = useCallback((id: string) => { const element = document.getElementById(id) const top = element?.offsetTop ?? 0 if (element) { setActiveId(id) // active clicked id setIsManualScroll(true) // tell the window event listener to ignore this scrolling window?.scrollTo({ behavior: "smooth", top: (top ?? 0) - SCROLL_OFFSET, }) } setTimeout(() => setIsManualScroll(false), 800) }, []) if (noItems) return return (
{ProjectSections.map((section) => { const sectionProjects = projects.filter( (project) => project.section?.toLowerCase() === section?.toLowerCase() ) ?? [] const hasProjectsForSection = sectionProjects.length > 0 const sectionTitle = ProjectSectionLabelMapping[section as ProjectSection] if (!hasProjectsForSection) return null return (

{sectionTitle}

{sectionProjects.map((project) => ( ))}
) })}
) }