feat: add latest updates carousel controls
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { ChevronRight } from "lucide-react";
|
import { ChevronLeft, ChevronRight } from "lucide-react";
|
||||||
import { useLocation } from "react-router-dom";
|
import { useLocation } from "react-router-dom";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { getJSON, itemsOrEmpty, type Category } from "../../api";
|
import { getJSON, itemsOrEmpty, type Category } from "../../api";
|
||||||
@@ -54,9 +54,11 @@ export function Home() {
|
|||||||
const [categoryUnavailableOpen, setCategoryUnavailableOpen] = useState(false);
|
const [categoryUnavailableOpen, setCategoryUnavailableOpen] = useState(false);
|
||||||
const [err, setErr] = useState<string | null>(null);
|
const [err, setErr] = useState<string | null>(null);
|
||||||
const recRowRef = useRef<HTMLDivElement>(null);
|
const recRowRef = useRef<HTMLDivElement>(null);
|
||||||
|
const latestRowRef = useRef<HTMLDivElement>(null);
|
||||||
const categoryRowRef = useRef<HTMLDivElement>(null);
|
const categoryRowRef = useRef<HTMLDivElement>(null);
|
||||||
const [activeCategoryPage, setActiveCategoryPage] = useState(0);
|
const [activeCategoryPage, setActiveCategoryPage] = useState(0);
|
||||||
const [canScrollRec, setCanScrollRec] = useState(false);
|
const [canScrollRec, setCanScrollRec] = useState(false);
|
||||||
|
const [canScrollLatest, setCanScrollLatest] = useState(false);
|
||||||
const [recScroll, setRecScroll] = useState({ ratio: 1, progress: 0 });
|
const [recScroll, setRecScroll] = useState({ ratio: 1, progress: 0 });
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -67,7 +69,7 @@ export function Home() {
|
|||||||
Promise.all([
|
Promise.all([
|
||||||
getJSON<Category[]>(`/api/categories${catQ}`),
|
getJSON<Category[]>(`/api/categories${catQ}`),
|
||||||
getJSON<{ items: Post[] }>(`/api/posts/recommended${postQ}&limit=12`),
|
getJSON<{ items: Post[] }>(`/api/posts/recommended${postQ}&limit=12`),
|
||||||
getJSON<{ items: Post[] }>(`/api/posts${postQ}&sort=latest&limit=5`),
|
getJSON<{ items: Post[] }>(`/api/posts${postQ}&sort=latest&limit=12`),
|
||||||
getJSON<{ items: Post[] }>(
|
getJSON<{ items: Post[] }>(
|
||||||
`/api/posts${postQ}&tag=popular&limit=5`,
|
`/api/posts${postQ}&tag=popular&limit=5`,
|
||||||
).catch((): { items: Post[] } => ({ items: [] })),
|
).catch((): { items: Post[] } => ({ items: [] })),
|
||||||
@@ -166,6 +168,31 @@ export function Home() {
|
|||||||
recRowRef.current?.scrollBy({ left: dir * 280, behavior: "smooth" });
|
recRowRef.current?.scrollBy({ left: dir * 280, behavior: "smooth" });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const row = latestRowRef.current;
|
||||||
|
if (!row) {
|
||||||
|
setCanScrollLatest(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const update = () => {
|
||||||
|
setCanScrollLatest(row.scrollWidth > row.clientWidth + 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
update();
|
||||||
|
const resizeObserver = new ResizeObserver(update);
|
||||||
|
resizeObserver.observe(row);
|
||||||
|
row.addEventListener("scroll", update, { passive: true });
|
||||||
|
return () => {
|
||||||
|
resizeObserver.disconnect();
|
||||||
|
row.removeEventListener("scroll", update);
|
||||||
|
};
|
||||||
|
}, [latest.length]);
|
||||||
|
|
||||||
|
const scrollLatest = (dir: 1 | -1) => {
|
||||||
|
latestRowRef.current?.scrollBy({ left: dir * 360, behavior: "smooth" });
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!hash) return;
|
if (!hash) return;
|
||||||
const id = hash.slice(1);
|
const id = hash.slice(1);
|
||||||
@@ -351,14 +378,24 @@ export function Home() {
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
{canScrollRec ? (
|
{canScrollRec ? (
|
||||||
<button
|
<>
|
||||||
type="button"
|
<button
|
||||||
onClick={() => scrollRec(1)}
|
type="button"
|
||||||
className="absolute right-0 top-[45%] hidden h-9 w-9 -translate-y-1/2 items-center justify-center rounded-lg border border-ark-line bg-[#292a31]/95 text-neutral-200 shadow-lg backdrop-blur transition hover:border-ark-gold hover:text-ark-gold md:flex"
|
onClick={() => scrollRec(-1)}
|
||||||
aria-label={t("viewAll")}
|
className="absolute left-0 top-[45%] hidden h-9 w-9 -translate-y-1/2 items-center justify-center rounded-lg border border-ark-line bg-[#292a31]/95 text-neutral-200 shadow-lg backdrop-blur transition hover:border-ark-gold hover:text-ark-gold md:flex"
|
||||||
>
|
aria-label="Previous recommendations"
|
||||||
<ChevronRight className="h-5 w-5" />
|
>
|
||||||
</button>
|
<ChevronLeft className="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => scrollRec(1)}
|
||||||
|
className="absolute right-0 top-[45%] hidden h-9 w-9 -translate-y-1/2 items-center justify-center rounded-lg border border-ark-line bg-[#292a31]/95 text-neutral-200 shadow-lg backdrop-blur transition hover:border-ark-gold hover:text-ark-gold md:flex"
|
||||||
|
aria-label="Next recommendations"
|
||||||
|
>
|
||||||
|
<ChevronRight className="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
) : null}
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
@@ -376,16 +413,45 @@ export function Home() {
|
|||||||
<MessageBubble key={post.id} post={post} />
|
<MessageBubble key={post.id} post={post} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-7 hidden grid-cols-1 gap-3 min-[576px]:grid-cols-2 md:grid md:grid-cols-2 md:gap-4 lg:grid-cols-3 xl:grid-cols-5">
|
<div className="relative hidden md:block">
|
||||||
{latest.map((r) => (
|
<div
|
||||||
<LatestUpdateRow key={r.id} r={r} iconKey={iconKeyForResource(r)} />
|
ref={latestRowRef}
|
||||||
))}
|
className="mt-7 flex gap-4 overflow-x-auto overflow-y-hidden pb-5 scroll-smooth [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
|
||||||
{Array.from({ length: latestPlaceholderCount }).map((_, index) => (
|
>
|
||||||
<ComingSoonLatestUpdateRow
|
{latest.map((r) => (
|
||||||
key={`latest-coming-soon-${index}`}
|
<div key={r.id} className="w-[340px] shrink-0 xl:w-[352px]">
|
||||||
index={latest.length + index}
|
<LatestUpdateRow r={r} iconKey={iconKeyForResource(r)} />
|
||||||
/>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
{Array.from({ length: latestPlaceholderCount }).map((_, index) => (
|
||||||
|
<div
|
||||||
|
key={`latest-coming-soon-${index}`}
|
||||||
|
className="w-[340px] shrink-0 xl:w-[352px]"
|
||||||
|
>
|
||||||
|
<ComingSoonLatestUpdateRow index={latest.length + index} />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{canScrollLatest ? (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => scrollLatest(-1)}
|
||||||
|
className="absolute left-0 top-[45%] hidden h-9 w-9 -translate-y-1/2 items-center justify-center rounded-lg border border-ark-line bg-[#292a31]/95 text-neutral-200 shadow-lg backdrop-blur transition hover:border-ark-gold hover:text-ark-gold md:flex"
|
||||||
|
aria-label="Previous latest updates"
|
||||||
|
>
|
||||||
|
<ChevronLeft className="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => scrollLatest(1)}
|
||||||
|
className="absolute right-0 top-[45%] hidden h-9 w-9 -translate-y-1/2 items-center justify-center rounded-lg border border-ark-line bg-[#292a31]/95 text-neutral-200 shadow-lg backdrop-blur transition hover:border-ark-gold hover:text-ark-gold md:flex"
|
||||||
|
aria-label="Next latest updates"
|
||||||
|
>
|
||||||
|
<ChevronRight className="h-5 w-5" />
|
||||||
|
</button>
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user