File size: 1,578 Bytes
f70bb71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use client";

import { useState } from "react";
import { motion, AnimatePresence } from "framer-motion";

import { getSpace } from "@/app/actions";
import { Space as SpaceProps } from "@/utils/types";
import { Space } from "@/components/space";

import { ButtonShuffler } from "./button";
import { Card } from "@/components/animate/card";

export const Shuffler = ({
  space: initialSpace,
  nextSpace: initialNextSpace,
}: {
  space: SpaceProps;
  nextSpace: SpaceProps;
}) => {
  const [index, setIndex] = useState(0);
  const [space, setSpace] = useState<SpaceProps>(
    JSON.parse(JSON.stringify(initialSpace))
  );
  const [nextSpace, setNextSpace] = useState<SpaceProps>(
    JSON.parse(JSON.stringify(initialNextSpace))
  );

  return (
    <motion.div className="grid grid-cols-1 gap-10 relative">
      <div className="relative w-full h-[350px]">
        <AnimatePresence initial={false}>
          <Card key={index + 1} front={false}>
            <Space space={nextSpace} />
          </Card>
          <Card key={index} front={true} index={index} setIndex={setIndex}>
            <Space space={space} />
          </Card>
        </AnimatePresence>
      </div>
      <div className="w-4 h-[1px] bg-white/50 mx-auto" />
      <footer className="flex items-center justify-center">
        <ButtonShuffler
          onClick={() => {
            getSpace().then((newSpace) => {
              setSpace(nextSpace);
              setNextSpace(newSpace);
              setIndex(index + 1);
            });
          }}
        />
      </footer>
    </motion.div>
  );
};