-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpage.tsx
More file actions
224 lines (205 loc) · 7.68 KB
/
page.tsx
File metadata and controls
224 lines (205 loc) · 7.68 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"use client";
import type { UserWithCoursesAndSubjects } from "common/types";
import { motion, useAnimation } from "framer-motion";
import {
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from "react";
import { MdClose, MdThumbUp } from "react-icons/md";
import request from "~/api/request";
import { useAboutMe, useRecommended } from "~/api/user";
import { Card } from "~/components/Card";
import { DraggableCard } from "~/components/DraggableCard";
import FullScreenCircularProgress from "~/components/common/FullScreenCircularProgress";
import BackgroundText from "../../components/common/BackgroundText";
import PersonDetailedMenu from "./components/PersonDetailedMenu";
import RoundButton from "./components/RoundButton";
export default function Home() {
const { data, error } = useRecommended();
const controls = useAnimation();
const backCardControls = useAnimation();
const [clickedButton, setClickedButton] = useState<string>("");
const [openDetailedMenu, setOpenDetailedMenu] = useState(false);
const {
state: { data: currentUser },
} = useAboutMe();
const [_, rerender] = useState({});
const [recommended, setRecommended] = useState<
Queue<UserWithCoursesAndSubjects>
>(() => new Queue([]));
const [loading, setLoading] = useState<boolean>(true);
// コンテナと topCard の DOM 参照を用意
const containerRef = useRef<HTMLDivElement>(null);
const topCardRef = useRef<HTMLDivElement>(null);
// topCard のコンテナ内での相対位置(backCard の最終的な配置位置)を保存
const [targetPos, setTargetPos] = useState({ x: 0, y: 0 });
// 初期オフセット:右方向へのずれを防ぐため x は 0、縦方向は必要に応じて設定(例: 20)
const initialOffset = { x: 0, y: 0 };
// レイアウト完了後に topCard の位置を計算する
useLayoutEffect(() => {
if (topCardRef.current && containerRef.current) {
const containerRect = containerRef.current.getBoundingClientRect();
const topCardRect = topCardRef.current.getBoundingClientRect();
setTargetPos({
x: topCardRect.left - containerRect.left,
y: topCardRect.top - containerRect.top,
});
// backCard のコントロールに初期オフセットを設定(レンダリング位置と合わせる)
backCardControls.set(initialOffset);
}
}, [backCardControls]);
useLayoutEffect(() => {
if (data) setRecommended(new Queue(data));
}, [data]);
useEffect(() => {
if (data) {
setRecommended(new Queue(data));
setLoading(false);
}
}, [data]);
const displayedUser = recommended.peek(0);
const nextUser = recommended.peek(1);
const handleAction = useCallback(
async (action: "accept" | "reject") => {
const current = recommended.peek(0);
if (!current) return;
setClickedButton(action === "accept" ? "heart" : "cross");
// アニメーション開始前に backCard を初期レンダリング位置(initialOffset)に設定
backCardControls.set(initialOffset);
await Promise.all([
// トップカードは画面外へ移動(画面サイズに合わせる)
controls.start({
x: action === "accept" ? window.innerWidth : -window.innerWidth,
transition: { duration: 0.5, delay: 0.2 },
}),
// backCard は computed した topCard の位置 (targetPos) に移動
backCardControls.start({
x: targetPos.x,
y: targetPos.y,
transition: { duration: 0.5, delay: 0.2 },
}),
]);
// キューの更新などの処理
recommended.pop();
if (action === "accept") {
await request.send(current.id);
} else if (action === "reject") {
recommended.push(current);
}
rerender({});
// アニメーション後に位置をリセット(backCard は再び初期レンダリング位置に戻す)
controls.set({ x: 0 });
backCardControls.set(initialOffset);
setClickedButton("");
},
[recommended, controls, backCardControls, targetPos],
);
if (loading) {
return <FullScreenCircularProgress />;
}
if (currentUser == null) {
return <FullScreenCircularProgress />;
}
if (recommended.size() === 0 && loading === false) {
return <BackgroundText text="「いいね!」を送るユーザーがいません。" />;
}
if (error) throw error;
return (
<div
ref={containerRef}
className="flex h-full flex-col items-center justify-center p-4"
>
{displayedUser && (
<div className="flex h-full flex-col items-center justify-center">
{nextUser && (
<div className="relative grid h-full w-full grid-cols-1 grid-rows-1">
{/* backCard: 初期レンダリング位置とアニメーション開始位置を両方とも initialOffset に合わせる */}
<motion.div
className="z-0 col-start-1 row-start-1 mt-4"
initial={initialOffset}
animate={backCardControls}
>
<Card displayedUser={nextUser} currentUser={currentUser} />
</motion.div>
{/* トップカード: この位置を基準にするために ref を設定 */}
<motion.div
ref={topCardRef}
className="z-10 col-start-1 row-start-1 mt-4"
animate={controls}
>
<DraggableCard
displayedUser={displayedUser}
currentUser={currentUser}
onSwipeLeft={() => handleAction("reject")}
onSwipeRight={() => handleAction("accept")}
clickedButton={clickedButton}
setOpenDetailedMenu={setOpenDetailedMenu}
/>
</motion.div>
</div>
)}
{nextUser == null && (
<div className="relative grid h-full w-full grid-cols-1 grid-rows-1">
<motion.div
ref={topCardRef}
className="z-10 col-start-1 row-start-1 mt-4 flex items-center justify-center"
animate={controls}
>
<DraggableCard
displayedUser={displayedUser}
currentUser={currentUser}
onSwipeLeft={() => handleAction("reject")}
onSwipeRight={() => handleAction("accept")}
clickedButton={clickedButton}
setOpenDetailedMenu={setOpenDetailedMenu}
/>
</motion.div>
</div>
)}
<div className="mt-2 mb-4 flex w-full justify-around px-8">
<RoundButton
onclick={() => handleAction("reject")}
icon={<MdClose className="text-3xl text-gray-500" />}
/>
<RoundButton
onclick={() => handleAction("accept")}
icon={<MdThumbUp className="text-3xl text-primary" />}
/>
</div>
{openDetailedMenu && (
<PersonDetailedMenu
onClose={() => {
setOpenDetailedMenu(false);
}}
displayedUser={displayedUser}
currentUser={currentUser}
/>
)}
</div>
)}
</div>
);
}
// Queue クラス(状態管理用)
class Queue<T> {
private store: T[];
constructor(initial: T[]) {
this.store = initial;
}
push(top: T): void {
this.store.push(top);
}
// peek(0): 次にポップされる要素、peek(1): その次の要素
peek(nth: number): T | undefined {
return this.store[nth];
}
pop(): T | undefined {
return this.store.shift();
}
size(): number {
return this.store.length;
}
}