|
| 1 | +/** |
| 2 | + * WeekContractCard — 周契约卡(v0.11.4 REQ-200;弹性承诺呈现层)。 |
| 3 | + * |
| 4 | + * @ai-context: P31+N10 的 UI 落点——契约是用户自设本周目标,不是打卡 KPI: |
| 5 | + * 无 streak、无惩罚、欠账不追(后端 review_logs 无 streak 字段)。 |
| 6 | + * 本卡只呈现"承诺 vs 实际"两个数字 + 最小可行日徽标(N9/N11 |
| 7 | + * 低谷生存的最轻形态:本周完成 ≥3 卡即成立,一天崩坏不否定整周)。 |
| 8 | + * @ai-context: 断签不清零视觉——进度按本周(周一零点起)独立计算,下周归零 |
| 9 | + * 重来,无连续记录展示;周界(weekStart UTC 秒)转本地日期展示。 |
| 10 | + */ |
| 11 | +import { useCallback, useEffect, useState } from "react"; |
| 12 | +import { invoke } from "@tauri-apps/api/core"; |
| 13 | +import type { WeekContractStatus } from "../types"; |
| 14 | + |
| 15 | +interface Props { |
| 16 | + groupId: number; |
| 17 | +} |
| 18 | + |
| 19 | +/** UTC 秒 → 本地日期短格式(周界展示用;epoch 前防御性回退空串) */ |
| 20 | +function formatWeekStart(utcSecs: number): string { |
| 21 | + if (!utcSecs) return ""; |
| 22 | + const d = new Date(utcSecs * 1000); |
| 23 | + if (Number.isNaN(d.getTime())) return ""; |
| 24 | + return `${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate()}`; |
| 25 | +} |
| 26 | + |
| 27 | +/** 简单进度条(宽度百分比 0..100 有界) */ |
| 28 | +function bar(ratio: number): React.CSSProperties { |
| 29 | + const pct = Math.max(0, Math.min(100, Math.round(ratio * 100))); |
| 30 | + return { |
| 31 | + width: `${pct}%`, |
| 32 | + height: 6, |
| 33 | + background: pct >= 100 ? "#059669" : "#0f766e", |
| 34 | + borderRadius: 3, |
| 35 | + transition: "width 0.3s", |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +export default function WeekContractCard({ groupId }: Props) { |
| 40 | + const [status, setStatus] = useState<WeekContractStatus | null>(null); |
| 41 | + const [editing, setEditing] = useState(false); |
| 42 | + const [days, setDays] = useState(3); |
| 43 | + const [cards, setCards] = useState(15); |
| 44 | + const [saving, setSaving] = useState(false); |
| 45 | + const [err, setErr] = useState(""); |
| 46 | + |
| 47 | + const load = useCallback(async () => { |
| 48 | + try { |
| 49 | + const s = await invoke<WeekContractStatus>("week_contract_status", { groupId }); |
| 50 | + setStatus(s); |
| 51 | + if (s.contract) { |
| 52 | + setDays(s.contract.targetDays); |
| 53 | + setCards(s.contract.targetCards); |
| 54 | + } |
| 55 | + setErr(""); |
| 56 | + } catch (e) { |
| 57 | + setErr(`周契约加载失败: ${e}`); |
| 58 | + } |
| 59 | + }, [groupId]); |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + void load(); |
| 63 | + }, [load]); |
| 64 | + |
| 65 | + const save = async () => { |
| 66 | + setSaving(true); |
| 67 | + try { |
| 68 | + await invoke<unknown>("upsert_week_contract", { groupId, targetDays: days, targetCards: cards }); |
| 69 | + setEditing(false); |
| 70 | + await load(); |
| 71 | + } catch (e) { |
| 72 | + setErr(`契约保存失败: ${e}`); |
| 73 | + } finally { |
| 74 | + setSaving(false); |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + const c = status?.contract ?? null; |
| 79 | + const dayRatio = status && c ? status.actualDays / c.targetDays : 0; |
| 80 | + const cardRatio = status && c ? status.actualCards / c.targetCards : 0; |
| 81 | + |
| 82 | + return ( |
| 83 | + <div style={{ marginTop: 8, padding: 8, background: "#fff", borderRadius: 6, border: "1px solid #e5e7eb" }}> |
| 84 | + <div style={{ display: "flex", alignItems: "center", gap: 6, marginBottom: 4 }}> |
| 85 | + <span style={{ fontWeight: 600, color: "#374151", fontSize: 12 }}>📅 周契约</span> |
| 86 | + {status?.minimalDayMet && ( |
| 87 | + <span style={{ fontSize: 10, color: "#047857", background: "#ecfdf5", borderRadius: 8, padding: "0 5px" }}> |
| 88 | + ✓ 本周成立 |
| 89 | + </span> |
| 90 | + )} |
| 91 | + {status && ( |
| 92 | + <span style={{ fontSize: 10, color: "#9ca3af", marginLeft: "auto" }}> |
| 93 | + 本周自 {formatWeekStart(status.weekStart)} 起 |
| 94 | + </span> |
| 95 | + )} |
| 96 | + </div> |
| 97 | + |
| 98 | + {/* 未立约:设定表单 */} |
| 99 | + {!c && !editing && ( |
| 100 | + <div> |
| 101 | + <p style={{ fontSize: 11, color: "#9ca3af", margin: "2px 0 6px" }}> |
| 102 | + 给自己一个本周目标——断签不清零,欠账不追。 |
| 103 | + </p> |
| 104 | + <button |
| 105 | + onClick={() => setEditing(true)} |
| 106 | + style={{ fontSize: 11, cursor: "pointer", padding: "2px 10px", borderRadius: 4, border: "1px solid #d1d5db", background: "#fff" }} |
| 107 | + > |
| 108 | + ✍ 设定本周目标 |
| 109 | + </button> |
| 110 | + </div> |
| 111 | + )} |
| 112 | + |
| 113 | + {/* 设定/修改表单 */} |
| 114 | + {editing && ( |
| 115 | + <div style={{ display: "flex", gap: 6, alignItems: "center", flexWrap: "wrap", marginTop: 2 }}> |
| 116 | + <select |
| 117 | + value={days} |
| 118 | + onChange={(e) => setDays(Number(e.target.value))} |
| 119 | + style={{ fontSize: 11, padding: "2px 4px", border: "1px solid #e5e7eb", borderRadius: 4 }} |
| 120 | + title="本周承诺复习天数" |
| 121 | + > |
| 122 | + {[1, 2, 3, 4, 5, 6, 7].map((d) => ( |
| 123 | + <option key={d} value={d}>{d} 天</option> |
| 124 | + ))} |
| 125 | + </select> |
| 126 | + <input |
| 127 | + type="number" |
| 128 | + min={1} |
| 129 | + max={200} |
| 130 | + value={cards} |
| 131 | + onChange={(e) => setCards(Math.max(1, Number(e.target.value) || 1))} |
| 132 | + style={{ width: 64, fontSize: 11, padding: "2px 4px", border: "1px solid #e5e7eb", borderRadius: 4 }} |
| 133 | + title="本周承诺复习卡数" |
| 134 | + /> |
| 135 | + <span style={{ fontSize: 11, color: "#6b7280" }}>卡/周</span> |
| 136 | + <button |
| 137 | + onClick={() => void save()} |
| 138 | + disabled={saving} |
| 139 | + style={{ fontSize: 11, cursor: "pointer", padding: "2px 10px", borderRadius: 4, border: "1px solid #0f766e", background: "#f0fdfa", color: "#0f766e" }} |
| 140 | + > |
| 141 | + {saving ? "保存中…" : c ? "✓ 改目标" : "✓ 立约"} |
| 142 | + </button> |
| 143 | + <button |
| 144 | + onClick={() => { setEditing(false); if (c) { setDays(c.targetDays); setCards(c.targetCards); } }} |
| 145 | + style={{ fontSize: 11, cursor: "pointer", padding: "2px 8px", borderRadius: 4, border: "1px solid #d1d5db", background: "#fff" }} |
| 146 | + > |
| 147 | + 取消 |
| 148 | + </button> |
| 149 | + </div> |
| 150 | + )} |
| 151 | + |
| 152 | + {/* 已立约:承诺 vs 实际(断签不清零——进度按周独立) */} |
| 153 | + {status && c && !editing && ( |
| 154 | + <div> |
| 155 | + <div style={{ margin: "2px 0" }}> |
| 156 | + <div style={{ display: "flex", justifyContent: "space-between", fontSize: 11, color: "#6b7280" }}> |
| 157 | + <span>天数 {status.actualDays}/{c.targetDays}</span> |
| 158 | + <span>卡数 {status.actualCards}/{c.targetCards}</span> |
| 159 | + </div> |
| 160 | + <div style={{ background: "#f3f4f6", borderRadius: 3, marginTop: 2 }}> |
| 161 | + <div style={bar(dayRatio)} /> |
| 162 | + </div> |
| 163 | + <div style={{ background: "#f3f4f6", borderRadius: 3, marginTop: 2 }}> |
| 164 | + <div style={bar(cardRatio)} /> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + <div style={{ display: "flex", alignItems: "center", gap: 6, marginTop: 4 }}> |
| 168 | + <span style={{ fontSize: 10, color: "#9ca3af" }}>弹性承诺:无连续打卡 · 断签不清零</span> |
| 169 | + <button |
| 170 | + onClick={() => setEditing(true)} |
| 171 | + style={{ marginLeft: "auto", fontSize: 10, cursor: "pointer", padding: "1px 8px", borderRadius: 4, border: "1px solid #d1d5db", background: "#fff" }} |
| 172 | + > |
| 173 | + 改目标 |
| 174 | + </button> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + )} |
| 178 | + |
| 179 | + {err && <p style={{ fontSize: 11, color: "#dc2626", marginTop: 4 }}>{err}</p>} |
| 180 | + </div> |
| 181 | + ); |
| 182 | +} |
0 commit comments