Skip to content

Commit 9905f2c

Browse files
committed
stabilize
1 parent c962fb4 commit 9905f2c

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

web/src/routes/tabs/home.tsx

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,42 @@ import request from "../../api/request";
77
import shadows from "@mui/material/styles/shadows";
88
import { motion, useAnimation } from "framer-motion";
99
import { useMyID, useRecommended } from "../../api/user";
10+
import type { User } from "../../common/types";
1011
import { Card } from "../../components/Card";
1112
import { DraggableCard } from "../../components/DraggableCard";
1213
import FullScreenCircularProgress from "../../components/common/FullScreenCircularProgress";
1314

1415
export default function Home() {
15-
const { data: recommended, error } = useRecommended();
16+
const { data, loading, error } = useRecommended();
17+
const [_, rerender] = useState({});
18+
const [recommended, setRecommended] = useState<Queue<User>>(
19+
() => new Queue([]),
20+
);
21+
useEffect(() => {
22+
if (data) setRecommended(new Queue(data));
23+
}, [data]);
1624

17-
const [nth, setNth] = useState<number>(0);
18-
const displayedUser = recommended?.[nth];
19-
const nextUser = recommended?.[nth + 1];
25+
const displayedUser = recommended.peek(1);
26+
const nextUser = recommended.peek(2);
2027
const controls = useAnimation();
2128
const [clickedButton, setClickedButton] = useState<string>("");
2229
const {
2330
state: { data: myId },
2431
} = useMyID();
2532

2633
const reject = useCallback(() => {
27-
if (!displayedUser) return;
28-
recommended?.push(displayedUser);
29-
setNth((n) => n + 1);
30-
}, [displayedUser, recommended?.push /* ew */]);
34+
const current = recommended.pop();
35+
if (!current) return;
36+
recommended.push(current);
37+
rerender({});
38+
}, [recommended]);
3139

3240
const accept = useCallback(async () => {
33-
setNth((n) => n + 1);
34-
if (displayedUser?.id) request.send(displayedUser.id);
35-
}, [displayedUser?.id]);
41+
const current = recommended.pop();
42+
if (!current) return;
43+
request.send(current.id);
44+
rerender({});
45+
}, [recommended]);
3646

3747
const onClickCross = useCallback(() => {
3848
setClickedButton("cross");
@@ -62,13 +72,7 @@ export default function Home() {
6272
});
6373
}, [controls, accept]);
6474

65-
useEffect(() => {
66-
if (!displayedUser) {
67-
setNth(0);
68-
}
69-
}, [displayedUser]);
70-
71-
if (recommended == null) {
75+
if (recommended == null || loading) {
7276
return <FullScreenCircularProgress />;
7377
}
7478
if (displayedUser == null) {
@@ -169,3 +173,23 @@ const CloseIconStyled = () => {
169173
const FavoriteIconStyled = () => {
170174
return <FavoriteIcon style={{ color: "red", fontSize: "4.5dvh" }} />;
171175
};
176+
177+
class Queue<T> {
178+
private store: T[];
179+
constructor(initial: T[]) {
180+
this.store = initial;
181+
}
182+
push(top: T): void {
183+
this.store.push(top);
184+
}
185+
// peek(1) to peek the next elem to be popped, peek(2) peeks the second next element to be popped.
186+
peek(nth: number): T | undefined {
187+
return this.store[nth - 1];
188+
}
189+
pop(): T | undefined {
190+
return this.store.shift();
191+
// yes, I know what you want to say, it has O(n) time complexity.
192+
// it doesn't really matter if there is only like 100 people in home queue at most.
193+
// 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.
194+
}
195+
}

0 commit comments

Comments
 (0)