|
| 1 | +/** |
| 2 | + * ChatSaveNoteDialog — 对话「另存为笔记」对话框(v0.16.1 用户决定② 双入口)。 |
| 3 | + * |
| 4 | + * @ai-context: 入口两处共用本对话框——会话顶栏(整段对话)与 AI 消息悬浮 |
| 5 | + * (至该条的完整上文)。字段:标题(默认由调用方给定)+ 目标组 |
| 6 | + * (可选下拉,默认不归组)→ create_note(source=manual;组 id 经 |
| 7 | + * group_id 直入——NewNote 契约已支持)。成功后内嵌"在笔记页打开" |
| 8 | + * (onOpenNote 已由 ChatPage 透传)+ 关闭;失败红字不关窗(改后再试)。 |
| 9 | + */ |
| 10 | +import { useState } from "react"; |
| 11 | +import { invoke } from "@tauri-apps/api/core"; |
| 12 | +import type { Note, NoteGroup } from "../types"; |
| 13 | + |
| 14 | +interface Props { |
| 15 | + /** 默认标题(会话标题或首条提问) */ |
| 16 | + initialTitle: string; |
| 17 | + /** 转写后的 Markdown 正文(utils/chatTranscript.buildConversationMarkdown) */ |
| 18 | + content: string; |
| 19 | + groups: NoteGroup[]; |
| 20 | + /** 在笔记页打开(成功态按钮;ChatPage 透传) */ |
| 21 | + onOpenNote: (noteId: number) => void; |
| 22 | + onClose: () => void; |
| 23 | +} |
| 24 | + |
| 25 | +const BTN_BASE: React.CSSProperties = { |
| 26 | + fontSize: 12.5, |
| 27 | + cursor: "pointer", |
| 28 | + padding: "5px 14px", |
| 29 | + borderRadius: 6, |
| 30 | + border: "1px solid #d1d5db", |
| 31 | + background: "#fff", |
| 32 | + color: "#374151", |
| 33 | +}; |
| 34 | + |
| 35 | +export default function ChatSaveNoteDialog({ initialTitle, content, groups, onOpenNote, onClose }: Props) { |
| 36 | + const [title, setTitle] = useState(initialTitle || "AI 对话记录"); |
| 37 | + const [groupId, setGroupId] = useState<number | null>(null); |
| 38 | + const [busy, setBusy] = useState(false); |
| 39 | + const [savedId, setSavedId] = useState<number | null>(null); |
| 40 | + const [status, setStatus] = useState(""); |
| 41 | + |
| 42 | + const save = async () => { |
| 43 | + if (busy || !title.trim()) return; |
| 44 | + setBusy(true); |
| 45 | + setStatus(""); |
| 46 | + try { |
| 47 | + const note = await invoke<Note>("create_note", { |
| 48 | + new: { |
| 49 | + title: title.trim(), |
| 50 | + content, |
| 51 | + source: "manual", |
| 52 | + ...(groupId != null ? { group_id: groupId } : {}), |
| 53 | + }, |
| 54 | + }); |
| 55 | + setSavedId(note.id); |
| 56 | + setStatus(""); |
| 57 | + } catch (e) { |
| 58 | + setStatus(`保存失败: ${e}`); |
| 59 | + } finally { |
| 60 | + setBusy(false); |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + return ( |
| 65 | + <> |
| 66 | + <div |
| 67 | + onClick={onClose} |
| 68 | + data-testid="chat-note-backdrop" |
| 69 | + style={{ position: "fixed", inset: 0, zIndex: 50, background: "rgba(0,0,0,0.18)" }} |
| 70 | + /> |
| 71 | + <div |
| 72 | + data-testid="chat-note-dialog" |
| 73 | + style={{ |
| 74 | + position: "fixed", zIndex: 51, top: "50%", left: "50%", |
| 75 | + transform: "translate(-50%, -50%)", |
| 76 | + width: 380, background: "#fff", borderRadius: 10, |
| 77 | + border: "1px solid #e5e7eb", boxShadow: "0 12px 32px rgba(0,0,0,0.18)", |
| 78 | + padding: 14, fontSize: 12.5, |
| 79 | + }} |
| 80 | + > |
| 81 | + <div style={{ fontWeight: 700, fontSize: 14, color: "#111827", marginBottom: 10 }}> |
| 82 | + 📄 另存为笔记 |
| 83 | + </div> |
| 84 | + |
| 85 | + {savedId != null ? ( |
| 86 | + <> |
| 87 | + <div data-testid="chat-note-saved" style={{ color: "#047857", marginBottom: 10 }}> |
| 88 | + ✓ 已保存为笔记 #{savedId}({title}) |
| 89 | + </div> |
| 90 | + <div style={{ display: "flex", gap: 8 }}> |
| 91 | + <button |
| 92 | + data-testid="chat-note-open" |
| 93 | + style={{ ...BTN_BASE, background: "#0d9488", color: "#fff", border: "none", fontWeight: 600 }} |
| 94 | + onClick={() => onOpenNote(savedId)} |
| 95 | + > |
| 96 | + 在笔记页打开 → |
| 97 | + </button> |
| 98 | + <button style={BTN_BASE} onClick={onClose}>关闭</button> |
| 99 | + </div> |
| 100 | + </> |
| 101 | + ) : ( |
| 102 | + <> |
| 103 | + <label style={{ display: "block", color: "#6b7280", marginBottom: 4 }}>笔记标题</label> |
| 104 | + <input |
| 105 | + data-testid="chat-note-title" |
| 106 | + value={title} |
| 107 | + onChange={(e) => setTitle(e.target.value)} |
| 108 | + style={{ width: "100%", padding: "6px 8px", fontSize: 13, border: "1px solid #e5e7eb", borderRadius: 6, boxSizing: "border-box", marginBottom: 8 }} |
| 109 | + /> |
| 110 | + <label style={{ display: "block", color: "#6b7280", marginBottom: 4 }}>目标组(可选)</label> |
| 111 | + <select |
| 112 | + data-testid="chat-note-group" |
| 113 | + value={groupId ?? ""} |
| 114 | + onChange={(e) => setGroupId(e.target.value ? Number(e.target.value) : null)} |
| 115 | + style={{ width: "100%", padding: "6px 8px", fontSize: 13, border: "1px solid #e5e7eb", borderRadius: 6, boxSizing: "border-box", marginBottom: 8, background: "#fff" }} |
| 116 | + > |
| 117 | + <option value="">不归组(全部笔记)</option> |
| 118 | + {groups.map((g) => ( |
| 119 | + <option key={g.id} value={g.id}> |
| 120 | + {g.name} |
| 121 | + </option> |
| 122 | + ))} |
| 123 | + </select> |
| 124 | + <div style={{ fontSize: 11.5, color: "#6b7280", marginBottom: 10 }}> |
| 125 | + 共 {content.length} 字符 · 对话以提问引用 + AI 回答全文的完整形式保存 |
| 126 | + </div> |
| 127 | + {status && ( |
| 128 | + <div data-testid="chat-note-error" style={{ color: "#dc2626", marginBottom: 8 }}>{status}</div> |
| 129 | + )} |
| 130 | + <div style={{ display: "flex", gap: 8, justifyContent: "flex-end" }}> |
| 131 | + <button data-testid="chat-note-cancel" style={BTN_BASE} onClick={onClose} disabled={busy}>取消</button> |
| 132 | + <button |
| 133 | + data-testid="chat-note-save" |
| 134 | + style={{ ...BTN_BASE, background: "#0d9488", color: "#fff", border: "none", fontWeight: 600 }} |
| 135 | + onClick={() => void save()} |
| 136 | + disabled={busy || !title.trim()} |
| 137 | + > |
| 138 | + {busy ? "保存中…" : "保存"} |
| 139 | + </button> |
| 140 | + </div> |
| 141 | + </> |
| 142 | + )} |
| 143 | + </div> |
| 144 | + </> |
| 145 | + ); |
| 146 | +} |
0 commit comments