import { useState, useEffect, useCallback } from 'react'; import { Modal, List, Button, Toast, Typography, Spin, Popconfirm } from '@douyinfe/semi-ui'; import { IconHistory, IconRotate } from '@douyinfe/semi-icons'; import { useNoteStore } from '../../stores/useNoteStore'; import noteService from '../../services/note.service'; import type { IHistory } from '../../types'; const { Text } = Typography; interface Props { visible: boolean; onClose: () => void; } export default function NoteHistoryModal({ visible, onClose }: Props) { const { currentNoteId, setCurrentNote } = useNoteStore(); const [list, setList] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { if (!visible || !currentNoteId) return; setLoading(true); noteService .getHistory(currentNoteId) .then((res) => setList(res.data ?? [])) .catch(() => Toast.error('加载历史版本失败')) .finally(() => setLoading(false)); }, [visible, currentNoteId]); const handleRestore = useCallback( async (item: IHistory) => { try { const res = await noteService.getHistoryContent(item.id); const h = res.data; // Restore: update note with history content await noteService.update(h.nid, h.title, h.context); // Reload the current note into the editor const noteRes = await noteService.get(h.nid); const note = noteRes.data; setCurrentNote(note.id, note.title, note.context); Toast.success(`已恢复到 ${h.createtime} 的版本`); onClose(); } catch { Toast.error('恢复失败'); } }, [setCurrentNote, onClose], ); return ( 历史版本 } visible={visible} onCancel={onClose} footer={null} width={520} > {loading ? (
) : list.length === 0 ? (
暂无历史版本
) : ( ( {item.title} {item.createtime} } extra={ void handleRestore(item)} okText="确定" cancelText="取消" > } /> )} /> )}
); }