Skip to content

Commit 23a2b70

Browse files
authored
手動で index するのではなく、Queue (中身は Array) を使う (#472)
<!--変更したい場合は、`/.github/pull_request_template.md` を修正して下さい。--> # PRの概要 closes #458 <!-- 変更の目的 もしくは 関連する Issue 番号 --> <!-- 以下のように書くと Issue にリンクでき、マージ時に自動で Issue を閉じられる--> <!-- closes #1 --> ## 具体的な変更内容 <!-- ビューの変更がある場合はスクショによる比較などがあるとわかりやすい --> ## 影響範囲 <!-- この関数を変更したのでこの機能にも影響がある、など --> ## 動作要件 <!-- 動作に必要な 環境変数 / 依存関係 / DBの更新 など --> ## 補足 <!-- レビューをする際に見てほしい点、ローカル環境で試す際の注意点、など --> ## レビューリクエストを出す前にチェック! - [ ] 改めてセルフレビューしたか - [ ] 手動での動作検証を行ったか - [ ] server の機能追加ならば、テストを書いたか - 理由: 書いた | server の機能追加ではない - [ ] 間違った使い方が存在するならば、それのドキュメントをコメントで書いたか - 理由: 書いた | 間違った使い方は存在しない - [ ] わかりやすいPRになっているか <!-- レビューリクエスト後は、Slackでもメンションしてお願いすることを推奨します。 -->
1 parent 3c0920f commit 23a2b70

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

web/app/home/page.tsx

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import CloseIcon from "@mui/icons-material/Close";
4+
import type { User } from "common/types";
45
import { motion, useAnimation } from "framer-motion";
56
import { useCallback, useEffect, useState } from "react";
67
import { MdThumbUp } from "react-icons/md";
@@ -12,26 +13,35 @@ import FullScreenCircularProgress from "~/components/common/FullScreenCircularPr
1213
import { NavigateByAuthState } from "~/components/common/NavigateByAuthState";
1314

1415
export default function Home() {
15-
const { data: recommended, error } = useRecommended();
16-
const [nth, setNth] = useState<number>(0);
17-
const displayedUser = recommended?.[nth];
18-
const nextUser = recommended?.[nth + 1];
16+
const { data, error } = useRecommended();
1917
const controls = useAnimation();
2018
const [clickedButton, setClickedButton] = useState<string>("");
2119
const {
2220
state: { data: myId },
2321
} = useMyID();
2422

25-
const reject = useCallback(() => {
26-
if (!displayedUser) return;
27-
recommended?.push(displayedUser);
28-
setNth((n) => n + 1);
29-
}, [displayedUser, recommended]);
23+
const [_, rerender] = useState({});
24+
const [recommended, setRecommended] = useState<Queue<User>>(
25+
() => new Queue([]),
26+
);
27+
useEffect(() => {
28+
if (data) setRecommended(new Queue(data));
29+
}, [data]);
3030

31+
const displayedUser = recommended.peek(1);
32+
const nextUser = recommended.peek(2);
33+
const reject = useCallback(() => {
34+
const current = recommended.pop();
35+
if (!current) return;
36+
recommended.push(current);
37+
rerender({});
38+
}, [recommended]);
3139
const accept = useCallback(async () => {
32-
setNth((n) => n + 1);
33-
if (displayedUser?.id) request.send(displayedUser.id);
34-
}, [displayedUser?.id]);
40+
const current = recommended.pop();
41+
if (!current) return;
42+
request.send(current.id);
43+
rerender({});
44+
}, [recommended]);
3545

3646
const onClickClose = useCallback(() => {
3747
setClickedButton("cross");
@@ -61,12 +71,6 @@ export default function Home() {
6171
});
6272
}, [controls, accept]);
6373

64-
useEffect(() => {
65-
if (!displayedUser) {
66-
setNth(0);
67-
}
68-
}, [displayedUser]);
69-
7074
if (recommended == null) {
7175
return <FullScreenCircularProgress />;
7276
}
@@ -144,3 +148,23 @@ const CloseIconStyled = () => <CloseIcon className="text-4xl text-gray-500" />;
144148
const FavoriteIconStyled = () => (
145149
<MdThumbUp className="text-3xl text-primary" />
146150
);
151+
152+
class Queue<T> {
153+
private store: T[];
154+
constructor(initial: T[]) {
155+
this.store = initial;
156+
}
157+
push(top: T): void {
158+
this.store.push(top);
159+
}
160+
// peek(1) to peek the next elem to be popped, peek(2) peeks the second next element to be popped.
161+
peek(nth: number): T | undefined {
162+
return this.store[nth - 1];
163+
}
164+
pop(): T | undefined {
165+
return this.store.shift();
166+
// yes, I know what you want to say, it has O(n) time complexity.
167+
// it doesn't really matter if there is only like 100 people in home queue at most.
168+
// if you really care about performance, why don't you go and limit the amount of people to fetch? that probably has significantly more impact to the performance.
169+
}
170+
}

0 commit comments

Comments
 (0)