|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useReducer, useEffect } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | + |
| 6 | +type HotDoc = { |
| 7 | + path: string; |
| 8 | + title?: string; |
| 9 | + views: number; |
| 10 | +}; |
| 11 | + |
| 12 | +type WindowParam = "7d" | "30d" | "all"; |
| 13 | + |
| 14 | +type State = |
| 15 | + | { status: "loading" } |
| 16 | + | { status: "error" } |
| 17 | + | { status: "ok"; docs: HotDoc[] }; |
| 18 | + |
| 19 | +type Action = |
| 20 | + | { type: "fetch" } |
| 21 | + | { type: "ok"; docs: HotDoc[] } |
| 22 | + | { type: "error" }; |
| 23 | + |
| 24 | +function reducer(_: State, action: Action): State { |
| 25 | + if (action.type === "fetch") return { status: "loading" }; |
| 26 | + if (action.type === "ok") return { status: "ok", docs: action.docs }; |
| 27 | + return { status: "error" }; |
| 28 | +} |
| 29 | + |
| 30 | +const BACKEND_URL = |
| 31 | + process.env.NEXT_PUBLIC_BACKEND_URL ?? "http://localhost:8080"; |
| 32 | + |
| 33 | +export function HotDocsTab({ initialWindow }: { initialWindow: WindowParam }) { |
| 34 | + const [windowParam, setWindowParam] = useReducer( |
| 35 | + (_: WindowParam, next: WindowParam) => next, |
| 36 | + initialWindow, |
| 37 | + ); |
| 38 | + const [state, dispatch] = useReducer(reducer, { status: "loading" }); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + dispatch({ type: "fetch" }); |
| 42 | + let cancelled = false; |
| 43 | + fetch( |
| 44 | + `${BACKEND_URL}/api/v1/analytics/top-docs?window=${windowParam}&limit=20`, |
| 45 | + ) |
| 46 | + .then((r) => { |
| 47 | + if (!r.ok) throw new Error(); |
| 48 | + return r.json() as Promise<HotDoc[]>; |
| 49 | + }) |
| 50 | + .then((docs) => { |
| 51 | + if (!cancelled) dispatch({ type: "ok", docs }); |
| 52 | + }) |
| 53 | + .catch(() => { |
| 54 | + if (!cancelled) dispatch({ type: "error" }); |
| 55 | + }); |
| 56 | + return () => { |
| 57 | + cancelled = true; |
| 58 | + }; |
| 59 | + }, [windowParam]); |
| 60 | + |
| 61 | + const handleWindowChange = (w: WindowParam) => { |
| 62 | + setWindowParam(w); |
| 63 | + const url = new URL(globalThis.location.href); |
| 64 | + url.searchParams.set("window", w); |
| 65 | + globalThis.history.replaceState(null, "", url.toString()); |
| 66 | + }; |
| 67 | + |
| 68 | + const windowOptions: { label: string; value: WindowParam }[] = [ |
| 69 | + { label: "7D", value: "7d" }, |
| 70 | + { label: "30D", value: "30d" }, |
| 71 | + { label: "ALL TIME", value: "all" }, |
| 72 | + ]; |
| 73 | + |
| 74 | + return ( |
| 75 | + <div> |
| 76 | + {/* 窗口切换 */} |
| 77 | + <div className="flex gap-0 mb-8 border border-[var(--foreground)]"> |
| 78 | + {windowOptions.map((opt) => ( |
| 79 | + <button |
| 80 | + key={opt.value} |
| 81 | + onClick={() => handleWindowChange(opt.value)} |
| 82 | + className={`flex-1 py-2 font-mono text-xs uppercase tracking-widest transition-colors ${ |
| 83 | + windowParam === opt.value |
| 84 | + ? "bg-[var(--foreground)] text-[var(--background)]" |
| 85 | + : "bg-[var(--background)] text-[var(--foreground)] hover:bg-[var(--foreground)]/10" |
| 86 | + }`} |
| 87 | + > |
| 88 | + {opt.label} |
| 89 | + </button> |
| 90 | + ))} |
| 91 | + </div> |
| 92 | + |
| 93 | + {/* 加载状态 */} |
| 94 | + {state.status === "loading" && ( |
| 95 | + <div className="flex flex-col gap-3"> |
| 96 | + {Array.from({ length: 5 }).map((_, i) => ( |
| 97 | + <div |
| 98 | + key={i} |
| 99 | + className="border border-[var(--foreground)] p-4 animate-pulse bg-[var(--foreground)]/5 h-16" |
| 100 | + /> |
| 101 | + ))} |
| 102 | + </div> |
| 103 | + )} |
| 104 | + |
| 105 | + {/* 错误状态 */} |
| 106 | + {state.status === "error" && ( |
| 107 | + <div className="border border-[var(--foreground)] p-8 text-center"> |
| 108 | + <p className="font-mono text-sm uppercase tracking-widest text-neutral-500"> |
| 109 | + 加载失败,请稍后重试 |
| 110 | + </p> |
| 111 | + </div> |
| 112 | + )} |
| 113 | + |
| 114 | + {/* 空状态 */} |
| 115 | + {state.status === "ok" && state.docs.length === 0 && ( |
| 116 | + <div className="border border-[var(--foreground)] p-8 text-center"> |
| 117 | + <p className="font-mono text-sm uppercase tracking-widest text-neutral-500"> |
| 118 | + 数据积累中… |
| 119 | + </p> |
| 120 | + </div> |
| 121 | + )} |
| 122 | + |
| 123 | + {/* 列表 */} |
| 124 | + {state.status === "ok" && state.docs.length > 0 && ( |
| 125 | + <div className="flex flex-col gap-3"> |
| 126 | + {state.docs.map((doc, idx) => ( |
| 127 | + <Link |
| 128 | + key={doc.path} |
| 129 | + href={doc.path} |
| 130 | + className="group w-full flex items-center gap-4 border border-[var(--foreground)] p-4 bg-[var(--background)] hard-shadow-hover transition-all" |
| 131 | + > |
| 132 | + <div className="font-mono text-2xl font-bold w-12 text-center text-[var(--foreground)] shrink-0"> |
| 133 | + #{idx + 1} |
| 134 | + </div> |
| 135 | + <div className="flex-1 min-w-0"> |
| 136 | + <div className="font-serif text-xl font-bold text-[var(--foreground)] truncate group-hover:underline decoration-2 decoration-[#CC0000] underline-offset-4"> |
| 137 | + {doc.title || doc.path} |
| 138 | + </div> |
| 139 | + <div className="font-mono text-xs uppercase text-neutral-500 mt-1 truncate"> |
| 140 | + {doc.path} |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + <div className="shrink-0 text-right"> |
| 144 | + <div className="font-serif font-black text-2xl text-[#CC0000]"> |
| 145 | + {doc.views.toLocaleString()} |
| 146 | + </div> |
| 147 | + <div className="font-mono text-[10px] uppercase tracking-widest text-neutral-500"> |
| 148 | + VIEWS |
| 149 | + </div> |
| 150 | + </div> |
| 151 | + </Link> |
| 152 | + ))} |
| 153 | + </div> |
| 154 | + )} |
| 155 | + </div> |
| 156 | + ); |
| 157 | +} |
0 commit comments