|
| 1 | +/** |
| 2 | + * ReviewSessionOverlay — 组级复习面(v0.11.2;UI 最小化——动机App化防御)。 |
| 3 | + * |
| 4 | + * @ai-context: 提取优先为默认(P4/P18/P25/P30):先显示 front 线索 → 用户 |
| 5 | + * 点击"回忆完成"→ 展开 back 验证 → 四档评分推进 FSRS 调度。 |
| 6 | + * 无被动重读模式、无 streak 展示(弹性承诺——不追债不清零)。 |
| 7 | + * @ai-context: 引擎先有真牙叙事后置——本面板只有卡片与评分,零叙事元素。 |
| 8 | + */ |
| 9 | +import { useCallback, useEffect, useState } from "react"; |
| 10 | +import { invoke } from "@tauri-apps/api/core"; |
| 11 | +import type { Flashcard } from "../types"; |
| 12 | + |
| 13 | +/** 四档评分按钮(文案=回忆质量自评) */ |
| 14 | +const RATINGS: { value: string; label: string; color: string }[] = [ |
| 15 | + { value: "again", label: "忘了", color: "#dc2626" }, |
| 16 | + { value: "hard", label: "困难", color: "#d97706" }, |
| 17 | + { value: "good", label: "记得", color: "#059669" }, |
| 18 | + { value: "easy", label: "轻松", color: "#0284c7" }, |
| 19 | +]; |
| 20 | + |
| 21 | +interface Props { |
| 22 | + /** 组过滤(null=全部到期卡) */ |
| 23 | + groupId: number | null; |
| 24 | + groupName: string; |
| 25 | + onClose: () => void; |
| 26 | +} |
| 27 | + |
| 28 | +export default function ReviewSessionOverlay({ groupId, groupName, onClose }: Props) { |
| 29 | + const [queue, setQueue] = useState<Flashcard[]>([]); |
| 30 | + const [index, setIndex] = useState(0); |
| 31 | + const [revealed, setRevealed] = useState(false); |
| 32 | + const [done, setDone] = useState(0); |
| 33 | + const [status, setStatus] = useState(""); |
| 34 | + const [loaded, setLoaded] = useState(false); |
| 35 | + |
| 36 | + // 加载到期队列(一次性——复习中评分出的卡不回插本队列,下次再现) |
| 37 | + const loadQueue = useCallback(async () => { |
| 38 | + try { |
| 39 | + const cards = await invoke<Flashcard[]>("list_due_cards", { groupId, limit: 50 }); |
| 40 | + setQueue(cards); |
| 41 | + } catch (e) { |
| 42 | + setStatus(`复习队列加载失败: ${e}`); |
| 43 | + } finally { |
| 44 | + setLoaded(true); |
| 45 | + } |
| 46 | + }, [groupId]); |
| 47 | + |
| 48 | + useEffect(() => { void loadQueue(); }, [loadQueue]); |
| 49 | + |
| 50 | + // ESC 退出(复习中误触保护:只在未展开答案时直接退) |
| 51 | + useEffect(() => { |
| 52 | + const handler = (e: KeyboardEvent) => { |
| 53 | + if (e.key === "Escape") onClose(); |
| 54 | + }; |
| 55 | + window.addEventListener("keydown", handler); |
| 56 | + return () => window.removeEventListener("keydown", handler); |
| 57 | + }, [onClose]); |
| 58 | + |
| 59 | + const current = queue[index] ?? null; |
| 60 | + |
| 61 | + const rate = async (rating: string) => { |
| 62 | + if (!current) return; |
| 63 | + try { |
| 64 | + await invoke("review_card", { cardId: current.id, rating }); |
| 65 | + setDone((d) => d + 1); |
| 66 | + setRevealed(false); |
| 67 | + setIndex((i) => i + 1); |
| 68 | + setStatus(""); |
| 69 | + } catch (e) { |
| 70 | + setStatus(`评分失败: ${e}`); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + const finished = loaded && (!current || index >= queue.length); |
| 75 | + |
| 76 | + return ( |
| 77 | + <div |
| 78 | + style={{ |
| 79 | + position: "fixed", inset: 0, background: "rgba(0,0,0,0.45)", zIndex: 1000, |
| 80 | + display: "flex", alignItems: "center", justifyContent: "center", |
| 81 | + }} |
| 82 | + onClick={onClose} |
| 83 | + > |
| 84 | + <div |
| 85 | + onClick={(e) => e.stopPropagation()} |
| 86 | + style={{ |
| 87 | + width: 520, maxWidth: "92vw", background: "#fff", borderRadius: 12, |
| 88 | + padding: 20, boxShadow: "0 12px 40px rgba(0,0,0,0.2)", |
| 89 | + }} |
| 90 | + > |
| 91 | + <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 12 }}> |
| 92 | + <span style={{ fontWeight: 600, fontSize: 14 }}>🎴 复习 · {groupName}</span> |
| 93 | + {current && <span style={{ fontSize: 12, color: "#9ca3af" }}>{index + 1}/{queue.length}</span>} |
| 94 | + <button onClick={onClose} style={{ marginLeft: "auto", cursor: "pointer", fontSize: 13 }}>✕ 退出</button> |
| 95 | + </div> |
| 96 | + |
| 97 | + {!loaded && <p style={{ fontSize: 13, color: "#9ca3af" }}>加载中…</p>} |
| 98 | + |
| 99 | + {finished && ( |
| 100 | + <div style={{ textAlign: "center", padding: "24px 0" }}> |
| 101 | + <p style={{ fontSize: 15, fontWeight: 600 }}> |
| 102 | + {done > 0 ? `本轮复习完成:${done} 张卡片` : "当前没有到期卡片"} |
| 103 | + </p> |
| 104 | + <p style={{ fontSize: 12, color: "#9ca3af", marginTop: 6 }}> |
| 105 | + {done > 0 ? "间隔已由 FSRS 推进,到期后再来。" : "生成闪卡或等待卡片到期。"} |
| 106 | + </p> |
| 107 | + </div> |
| 108 | + )} |
| 109 | + |
| 110 | + {current && !finished && ( |
| 111 | + <div> |
| 112 | + {/* front(线索——先回忆再看) */} |
| 113 | + <div style={{ |
| 114 | + minHeight: 90, padding: 16, background: "#f9fafb", borderRadius: 8, |
| 115 | + fontSize: 16, lineHeight: 1.6, whiteSpace: "pre-wrap", |
| 116 | + }}> |
| 117 | + {current.front} |
| 118 | + </div> |
| 119 | + {!revealed ? ( |
| 120 | + <button |
| 121 | + onClick={() => setRevealed(true)} |
| 122 | + style={{ |
| 123 | + width: "100%", marginTop: 12, padding: "10px 0", fontSize: 14, |
| 124 | + cursor: "pointer", background: "#0f766e", color: "#fff", |
| 125 | + border: "none", borderRadius: 8, |
| 126 | + }} |
| 127 | + > |
| 128 | + 回忆完成 · 查看答案 |
| 129 | + </button> |
| 130 | + ) : ( |
| 131 | + <> |
| 132 | + {/* back(验证材料) */} |
| 133 | + <div style={{ |
| 134 | + marginTop: 10, minHeight: 70, padding: 14, background: "#f0fdfa", |
| 135 | + borderRadius: 8, fontSize: 13, lineHeight: 1.7, whiteSpace: "pre-wrap", |
| 136 | + maxHeight: 220, overflowY: "auto", |
| 137 | + }}> |
| 138 | + {current.back} |
| 139 | + </div> |
| 140 | + {/* 四档评分(提取质量自评 → FSRS 调度) */} |
| 141 | + <div style={{ display: "flex", gap: 8, marginTop: 12 }}> |
| 142 | + {RATINGS.map((r) => ( |
| 143 | + <button |
| 144 | + key={r.value} |
| 145 | + onClick={() => void rate(r.value)} |
| 146 | + style={{ |
| 147 | + flex: 1, padding: "8px 0", fontSize: 13, cursor: "pointer", |
| 148 | + border: `1px solid ${r.color}`, color: r.color, |
| 149 | + background: "#fff", borderRadius: 6, |
| 150 | + }} |
| 151 | + > |
| 152 | + {r.label} |
| 153 | + </button> |
| 154 | + ))} |
| 155 | + </div> |
| 156 | + </> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + )} |
| 160 | + {status && <p style={{ marginTop: 8, fontSize: 12, color: "#dc2626" }}>{status}</p>} |
| 161 | + </div> |
| 162 | + </div> |
| 163 | + ); |
| 164 | +} |
0 commit comments