Bug fixes for boosts carousel + scrolling

This commit is contained in:
Lim Chee Aun 2023-01-16 20:32:30 +08:00
parent 6e72601833
commit 62a3ba7c5f
4 changed files with 53 additions and 41 deletions

View file

@ -48,8 +48,8 @@ function Home({ hidden }) {
}; };
}); });
{
// BOOSTS CAROUSEL // BOOSTS CAROUSEL
if (snapStates.settings.boostsCarousel) {
let specialHome = []; let specialHome = [];
let boostStash = []; let boostStash = [];
for (let i = 0; i < homeValues.length; i++) { for (let i = 0; i < homeValues.length; i++) {
@ -90,19 +90,22 @@ function Home({ hidden }) {
specialHome, specialHome,
}); });
if (firstLoad) { if (firstLoad) {
states.specialHome = specialHome; states.home = specialHome;
} else { } else {
states.specialHome.push(...specialHome); states.home.push(...specialHome);
} }
} } else {
if (firstLoad) { if (firstLoad) {
states.home = homeValues; states.home = homeValues;
} else { } else {
states.home.push(...homeValues); states.home.push(...homeValues);
} }
}
states.homeLastFetchTime = Date.now(); states.homeLastFetchTime = Date.now();
return allStatuses; return {
done: false,
};
} }
const loadingStatuses = useRef(false); const loadingStatuses = useRef(false);
@ -212,19 +215,24 @@ function Home({ hidden }) {
} }
}); });
const { scrollDirection, reachStart, nearReachStart, nearReachEnd } = const {
useScroll({ scrollDirection,
reachStart,
nearReachStart,
nearReachEnd,
reachEnd,
} = useScroll({
scrollableElement: scrollableRef.current, scrollableElement: scrollableRef.current,
distanceFromStart: 0.1, distanceFromStart: 1,
distanceFromEnd: 0.15, distanceFromEnd: 3,
scrollThresholdStart: 44, scrollThresholdStart: 44,
}); });
useEffect(() => { useEffect(() => {
if (nearReachEnd && showMore) { if (nearReachEnd || (reachEnd && showMore)) {
loadStatuses(); loadStatuses();
} }
}, [nearReachEnd]); }, [nearReachEnd, reachEnd]);
useEffect(() => { useEffect(() => {
if (reachStart) { if (reachStart) {
@ -245,10 +253,6 @@ function Home({ hidden }) {
})(); })();
}, []); }, []);
const snapHome = snapStates.settings.boostsCarousel
? snapStates.specialHome
: snapStates.home;
return ( return (
<div <div
id="home-page" id="home-page"
@ -322,10 +326,12 @@ function Home({ hidden }) {
class="updates-button" class="updates-button"
type="button" type="button"
onClick={() => { onClick={() => {
if (!snapStates.settings.boostsCarousel) {
const uniqueHomeNew = snapStates.homeNew.filter( const uniqueHomeNew = snapStates.homeNew.filter(
(status) => !states.home.some((s) => s.id === status.id), (status) => !states.home.some((s) => s.id === status.id),
); );
states.home.unshift(...uniqueHomeNew); states.home.unshift(...uniqueHomeNew);
}
loadStatuses(true); loadStatuses(true);
states.homeNew = []; states.homeNew = [];
@ -338,10 +344,10 @@ function Home({ hidden }) {
<Icon icon="arrow-up" /> New posts <Icon icon="arrow-up" /> New posts
</button> </button>
)} )}
{snapHome.length ? ( {snapStates.home.length ? (
<> <>
<ul class="timeline"> <ul class="timeline">
{snapHome.map(({ id: statusID, reblog, boosts }) => { {snapStates.home.map(({ id: statusID, reblog, boosts }) => {
const actualStatusID = reblog || statusID; const actualStatusID = reblog || statusID;
if (boosts) { if (boosts) {
return ( return (
@ -429,10 +435,14 @@ function Home({ hidden }) {
function BoostsCarousel({ boosts }) { function BoostsCarousel({ boosts }) {
const carouselRef = useRef(); const carouselRef = useRef();
const { reachStart, reachEnd } = useScroll({ const { reachStart, reachEnd, init } = useScroll({
scrollableElement: carouselRef.current, scrollableElement: carouselRef.current,
direction: 'horizontal', direction: 'horizontal',
}); });
useEffect(() => {
init?.();
}, []);
return ( return (
<div class="boost-carousel"> <div class="boost-carousel">
<header> <header>

View file

@ -297,7 +297,7 @@ function StatusPage({ id }) {
const { nearReachStart } = useScroll({ const { nearReachStart } = useScroll({
scrollableElement: scrollableRef.current, scrollableElement: scrollableRef.current,
distanceFromStart: 0.1, distanceFromStart: 0.5,
}); });
return ( return (

View file

@ -24,7 +24,7 @@ const states = proxy({
showDrafts: false, showDrafts: false,
composeCharacterCount: 0, composeCharacterCount: 0,
settings: { settings: {
boostsCarousel: store.local.get('settings:boostsCarousel') === '1' || true, boostsCarousel: store.local.get('settings:boostsCarousel') === '1' ?? true,
}, },
}); });
export default states; export default states;

View file

@ -2,8 +2,8 @@ import { useEffect, useState } from 'preact/hooks';
export default function useScroll({ export default function useScroll({
scrollableElement, scrollableElement,
distanceFromStart = 0, distanceFromStart = 1, // ratio of clientHeight/clientWidth
distanceFromEnd = 0, distanceFromEnd = 1, // ratio of clientHeight/clientWidth
scrollThresholdStart = 10, scrollThresholdStart = 10,
scrollThresholdEnd = 10, scrollThresholdEnd = 10,
direction = 'vertical', direction = 'vertical',
@ -16,8 +16,8 @@ export default function useScroll({
const isVertical = direction === 'vertical'; const isVertical = direction === 'vertical';
if (!scrollableElement) { if (!scrollableElement) {
console.warn('Scrollable element is not defined'); // Better be explicit instead of auto-assign to window
scrollableElement = window; return {};
} }
useEffect(() => { useEffect(() => {
@ -38,10 +38,8 @@ export default function useScroll({
const scrollDimension = isVertical ? scrollHeight : scrollWidth; const scrollDimension = isVertical ? scrollHeight : scrollWidth;
const clientDimension = isVertical ? clientHeight : clientWidth; const clientDimension = isVertical ? clientHeight : clientWidth;
const scrollDistance = Math.abs(scrollStart - previousScrollStart); const scrollDistance = Math.abs(scrollStart - previousScrollStart);
const distanceFromStartPx = const distanceFromStartPx = clientDimension * distanceFromStart;
scrollDimension * Math.min(1, Math.max(0, distanceFromStart)); const distanceFromEndPx = clientDimension * distanceFromEnd;
const distanceFromEndPx =
scrollDimension * Math.min(1, Math.max(0, distanceFromEnd));
if ( if (
scrollDistance >= scrollDistance >=
@ -62,7 +60,6 @@ export default function useScroll({
} }
scrollableElement.addEventListener('scroll', onScroll, { passive: true }); scrollableElement.addEventListener('scroll', onScroll, { passive: true });
scrollableElement.dispatchEvent(new Event('scroll'));
return () => scrollableElement.removeEventListener('scroll', onScroll); return () => scrollableElement.removeEventListener('scroll', onScroll);
}, [ }, [
@ -79,5 +76,10 @@ export default function useScroll({
reachEnd, reachEnd,
nearReachStart, nearReachStart,
nearReachEnd, nearReachEnd,
init: () => {
if (scrollableElement) {
scrollableElement.dispatchEvent(new Event('scroll'));
}
},
}; };
} }