mind.tsx
6.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import chatStyles from "@/app/components/chat.module.scss";
import homeStyles from "@/app/components/home.module.scss";
import { MindSiderBar } from "./mind-siderBar";
import MindElixir, { type Options, type MindElixirData } from "mind-elixir";
import { WindowContent } from "@/app/components/home";
import { useMobileScreen } from "@/app/utils";
import { IconButton } from "../button";
import Locale from "@/app/locales";
import { Path } from "@/app/constant";
import { useNavigate, useLocation } from "react-router-dom";
import clsx from "clsx";
import { getClientConfig } from "@/app/config/client";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { useAppConfig } from "@/app/store";
import ReturnIcon from "@/app/icons/return.svg";
import MinIcon from "@/app/icons/min.svg";
import MaxIcon from "@/app/icons/max.svg";
import SDIcon from "@/app/icons/sd.svg";
import { useChatStore, useMindMapStore } from "@/app/store";
import { message } from "antd";
// 常量配置抽取
const INITIAL_DATA: MindElixirData = {
nodeData: {
id: "root",
topic: "中心主题",
},
};
const LOADING_DATA: MindElixirData = {
nodeData: {
id: "root",
topic: "生成中....",
},
};
export function MindPage() {
const isMobileScreen = useMobileScreen();
const navigate = useNavigate();
const clientConfig = useMemo(() => getClientConfig(), []);
const showMaxIcon = !isMobileScreen && !clientConfig?.isApp;
const config = useAppConfig();
const scrollRef = useRef<HTMLDivElement>(null);
const isMind = location.pathname === Path.Mind;
const [isLoading, setIsLoading] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
const mindInstance = useRef<InstanceType<typeof MindElixir> | null>(null);
const chatStore = useChatStore();
const { newMessages, content } = useMindMapStore.getState();
const query = useLocation();
let { msg } = query.state || {};
const [data, setData] = useState<MindElixirData>(INITIAL_DATA);
useEffect(() => {
// 确保容器元素已挂载
if (!containerRef.current) return;
// 初始化配置项
const options: Options = {
el: containerRef.current,
locale: "zh_CN",
draggable: true,
contextMenu: true,
toolBar: true,
nodeMenu: true,
};
// 创建实例
mindInstance.current = new MindElixir(options);
mindInstance.current.init(data);
const fetchData = async () => {
if (msg) {
if (content) {
setIsLoading(true);
try {
const response = await chatStore.getMindData(
newMessages,
"gpt-4o-mini",
);
const cleanedContent = response.replace(/^```json|```$/g, "");
const parsedData: MindElixirData = JSON.parse(cleanedContent);
// 增强校验逻辑
if (
!parsedData?.nodeData?.id ||
!Array.isArray(parsedData.nodeData.children)
) {
throw new Error("数据结构不完整");
}
setData(parsedData);
navigate(Path.Mind, { replace: true, state: { msg: null } });
} catch (error) {
console.log(error);
message.error("请求失败,请重试");
} finally {
setIsLoading(false); // 确保关闭加载状态
}
}
}
};
fetchData();
return () => {
if (mindInstance.current) {
mindInstance.current.destroy();
}
};
}, []);
useEffect(() => {
mindInstance.current?.refresh(data);
}, [data]);
useEffect(() => {
if (isLoading) {
mindInstance.current?.refresh(LOADING_DATA);
}
}, [isLoading]);
return (
<>
<MindSiderBar
className={clsx({ [homeStyles["sidebar-show"]]: isMind })}
setData={setData}
isLoading={isLoading}
setIsLoading={setIsLoading}
/>
<WindowContent>
<div className={chatStyles.chat} key={"1"}>
<div className="window-header" data-tauri-drag-region>
{isMobileScreen && (
<div className="window-actions">
<div className={"window-action-button"}>
<IconButton
icon={<ReturnIcon />}
bordered
title={Locale.Chat.Actions.ChatList}
onClick={() => navigate(Path.BgRemoval)}
/>
</div>
</div>
)}
<div
className={clsx(
"window-header-title",
chatStyles["chat-body-title"],
)}
>
<div className={`window-header-main-title`}>生成思维导图</div>
</div>
<div className={chatStyles["chat-message-actions"]}>
<div className={chatStyles["chat-input-actions"]}>
{/* <ChatAction
text={Locale.Chat.Actions.ReWrite}
icon={<ReloadIcon />}
onClick={() => { }
}
/> */}
</div>
</div>
<div className="window-actions">
{showMaxIcon && (
<div className="window-action-button">
<IconButton
aria={Locale.Chat.Actions.FullScreen}
icon={config.tightBorder ? <MinIcon /> : <MaxIcon />}
bordered
onClick={() => {
config.update(
(config) => (config.tightBorder = !config.tightBorder),
);
}}
/>
</div>
)}
{isMobileScreen && <SDIcon width={50} height={50} />}
</div>
</div>
<div className={chatStyles["chat-body"]} ref={scrollRef}>
<div ref={containerRef} style={{ width: "100%", height: "100%" }} />
</div>
</div>
</WindowContent>
</>
);
}