|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Spinner from "@/components/Spinner"; |
| 4 | +import UserInfo from "@/components/UserInfo"; |
| 5 | +import fetcher from "@/lib/fetcher"; |
| 6 | +import { UserSnapshot } from "@prisma/client"; |
| 7 | +import Timeline from "rsuite/Timeline"; |
| 8 | +import useSWRInfinite from "swr/infinite"; |
| 9 | +import "rsuite/Timeline/styles/index.css"; |
| 10 | +import { useState } from "react"; |
| 11 | +import { BsChevronDown, BsChevronUp, BsThreeDots } from "react-icons/bs"; |
| 12 | + |
| 13 | +const PER_PAGE = 15; |
| 14 | + |
| 15 | +interface PageData { |
| 16 | + snapshots: UserSnapshot[]; |
| 17 | + nextCursor: string; |
| 18 | +} |
| 19 | + |
| 20 | +export default function SnapshotTimeline({ uid }: { uid: number }) { |
| 21 | + const [open, setOpen] = useState(false); |
| 22 | + |
| 23 | + const { data, size, setSize, isValidating } = useSWRInfinite<PageData>( |
| 24 | + (_pageIndex, prev: PageData | null) => { |
| 25 | + if (prev && !prev.nextCursor) return null; |
| 26 | + let res = `/user/${uid}/snapshots?limit=${PER_PAGE}`; |
| 27 | + if (prev) res += `&offset=${prev.nextCursor}`; |
| 28 | + return res; |
| 29 | + }, |
| 30 | + fetcher, |
| 31 | + ); |
| 32 | + |
| 33 | + const timeline = ( |
| 34 | + <> |
| 35 | + <Timeline endless isItemActive={Timeline.ACTIVE_FIRST}> |
| 36 | + {data?.map((dat) => |
| 37 | + dat.snapshots?.map((snapshot) => ( |
| 38 | + <Timeline.Item> |
| 39 | + <UserInfo |
| 40 | + user={{ |
| 41 | + id: uid, |
| 42 | + userSnapshots: [snapshot], |
| 43 | + }} |
| 44 | + noHref |
| 45 | + /> |
| 46 | + <br /> |
| 47 | + 截至 {new Date(snapshot.until).toLocaleString()} |
| 48 | + </Timeline.Item> |
| 49 | + )), |
| 50 | + )} |
| 51 | + </Timeline> |
| 52 | + {isValidating && <Spinner className="mt-5" />} |
| 53 | + {!isValidating && |
| 54 | + data && |
| 55 | + data[data.length - 1].snapshots.length === PER_PAGE && ( |
| 56 | + <button |
| 57 | + className="btn btn-link w-100 rounded-4 py-2x py-md-3 text-center text-decoration-none" |
| 58 | + onClick={() => { |
| 59 | + // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 60 | + setSize(size + 1); |
| 61 | + }} |
| 62 | + type="button" |
| 63 | + > |
| 64 | + 加载更多 |
| 65 | + <BsThreeDots |
| 66 | + className="ms-1" |
| 67 | + style={{ position: "relative", top: "-.09em" }} |
| 68 | + /> |
| 69 | + </button> |
| 70 | + )} |
| 71 | + </> |
| 72 | + ); |
| 73 | + |
| 74 | + return ( |
| 75 | + <> |
| 76 | + <div className="d-md-none"> |
| 77 | + <button |
| 78 | + type="button" |
| 79 | + className="btn btn-link w-100 d-md-none" |
| 80 | + onMouseDown={() => setOpen(!open)} |
| 81 | + style={{ textDecoration: "none" }} |
| 82 | + > |
| 83 | + 用户名历史 {open ? <BsChevronUp /> : <BsChevronDown />} |
| 84 | + </button> |
| 85 | + |
| 86 | + {open && ( |
| 87 | + <> |
| 88 | + {/* 对于缺失空白的一个并不优雅的解决方案 */} |
| 89 | + <div style={{ height: "10px" }} className="d-md-none" /> |
| 90 | + {timeline} |
| 91 | + </> |
| 92 | + )} |
| 93 | + </div> |
| 94 | + <div className="d-md-block d-none">{timeline}</div> |
| 95 | + </> |
| 96 | + ); |
| 97 | +} |
0 commit comments