Fix showcase spacing and and loading timing

This commit is contained in:
2023-09-22 10:39:18 +01:00
parent 5b1003d4d9
commit 049c0a6f5a
3 changed files with 31 additions and 5 deletions
+16 -5
View File
@@ -1,10 +1,11 @@
import { MeshProps, useFrame } from "@react-three/fiber";
import { Suspense, useRef } from "react";
import { Suspense, useEffect, useRef, useState } from "react";
import { CoinInstance } from "@/components/coin";
import { Mesh } from "three";
import { BanknoteInstance } from "@/components/banknote";
import { useShowcaseStore } from "./store";
import { NumistaType } from "@/types/numista";
import { usePrevious } from "@/core/hooks/use-previous";
const SpinninType = ({
typeProps,
@@ -69,13 +70,24 @@ const SpinninType = ({
export const Row = () => {
const ref = useRef<Mesh>(null!);
const category = useShowcaseStore((state) => state.category);
const currentIndex = useShowcaseStore((state) => state.currentIndex);
const getType = useShowcaseStore((state) => state.getType);
const isTransitioning = useShowcaseStore((state) => state.isTransitioning);
const endTransition = useShowcaseStore((state) => state.endTransition);
const types = useShowcaseStore((state) => state.types);
const spacing = category === "banknotes" ? 8 : 6;
const [spacing, setSpacing] = useState<number>(0);
const delayedIndex = usePrevious(currentIndex);
useEffect(() => {
const updateSpacing = () =>
setSpacing(Math.sqrt((48 * window.innerWidth) / window.innerHeight));
updateSpacing();
window.addEventListener("resize", updateSpacing);
return () => window.removeEventListener("resize", updateSpacing);
}, []);
useFrame(() => {
if (!isTransitioning) {
@@ -88,7 +100,6 @@ export const Row = () => {
if (Math.abs(diff) > 1 / 32) {
ref.current.position.x -= diff / 16;
} else {
console.log("ending transition");
endTransition();
}
});
@@ -97,7 +108,7 @@ export const Row = () => {
<mesh ref={ref}>
{types.map(
(type, index) =>
Math.abs(index - currentIndex) <= 1 && (
Math.abs(index - delayedIndex) <= 2 && (
<Suspense key={type.id} fallback={null}>
<SpinninType
typeProps={getType(index)}
+4
View File
@@ -30,6 +30,8 @@ export const useShowcaseStore = create<ShowcaseStore>((set, get) => ({
orbitControlsRef: createRef(),
nextIndex: () =>
set((state) => {
state.orbitControlsRef.current?.setAzimuthalAngle(0);
state.orbitControlsRef.current?.setPolarAngle(Math.PI / 2);
const currentIndex = Math.min(
state.currentIndex + 1,
state.types.length - 1,
@@ -42,6 +44,8 @@ export const useShowcaseStore = create<ShowcaseStore>((set, get) => ({
}),
previousIndex: () =>
set((state) => {
state.orbitControlsRef.current?.setAzimuthalAngle(0);
state.orbitControlsRef.current?.setPolarAngle(Math.PI / 2);
const currentIndex = Math.max(state.currentIndex - 1, 0);
return {
currentIndex,
+11
View File
@@ -0,0 +1,11 @@
import { useEffect, useRef } from "react";
export function usePrevious<T>(value: T): T {
const ref = useRef<T>(value);
useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
}