exporter.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. import { ChatMessage, useAppConfig, useChatStore } from "../store";
  2. import Locale from "../locales";
  3. import styles from "./exporter.module.scss";
  4. import { List, ListItem, Modal, Select, showToast } from "./ui-lib";
  5. import { IconButton } from "./button";
  6. import { copyToClipboard, downloadAs, useMobileScreen } from "../utils";
  7. import CopyIcon from "../icons/copy.svg";
  8. import LoadingIcon from "../icons/three-dots.svg";
  9. import ChatGptIcon from "../icons/chatgpt.png";
  10. import ShareIcon from "../icons/share.svg";
  11. import BotIcon from "../icons/bot.png";
  12. import DownloadIcon from "../icons/download.svg";
  13. import { useEffect, useMemo, useRef, useState } from "react";
  14. import { MessageSelector, useMessageSelector } from "./message-selector";
  15. import { Avatar } from "./emoji";
  16. import dynamic from "next/dynamic";
  17. import NextImage from "next/image";
  18. import { toBlob, toJpeg, toPng } from "html-to-image";
  19. import { DEFAULT_MASK_AVATAR } from "../store/mask";
  20. import { api } from "../client/api";
  21. import { prettyObject } from "../utils/format";
  22. import { EXPORT_MESSAGE_CLASS_NAME } from "../constant";
  23. const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
  24. loading: () => <LoadingIcon />,
  25. });
  26. export function ExportMessageModal(props: { onClose: () => void }) {
  27. return (
  28. <div className="modal-mask">
  29. <Modal title={Locale.Export.Title} onClose={props.onClose}>
  30. <div style={{ minHeight: "40vh" }}>
  31. <MessageExporter />
  32. </div>
  33. </Modal>
  34. </div>
  35. );
  36. }
  37. function useSteps(
  38. steps: Array<{
  39. name: string;
  40. value: string;
  41. }>,
  42. ) {
  43. const stepCount = steps.length;
  44. const [currentStepIndex, setCurrentStepIndex] = useState(0);
  45. const nextStep = () =>
  46. setCurrentStepIndex((currentStepIndex + 1) % stepCount);
  47. const prevStep = () =>
  48. setCurrentStepIndex((currentStepIndex - 1 + stepCount) % stepCount);
  49. return {
  50. currentStepIndex,
  51. setCurrentStepIndex,
  52. nextStep,
  53. prevStep,
  54. currentStep: steps[currentStepIndex],
  55. };
  56. }
  57. function Steps<
  58. T extends {
  59. name: string;
  60. value: string;
  61. }[],
  62. >(props: { steps: T; onStepChange?: (index: number) => void; index: number }) {
  63. const steps = props.steps;
  64. const stepCount = steps.length;
  65. return (
  66. <div className={styles["steps"]}>
  67. <div className={styles["steps-progress"]}>
  68. <div
  69. className={styles["steps-progress-inner"]}
  70. style={{
  71. width: `${((props.index + 1) / stepCount) * 100}%`,
  72. }}
  73. ></div>
  74. </div>
  75. <div className={styles["steps-inner"]}>
  76. {steps.map((step, i) => {
  77. return (
  78. <div
  79. key={i}
  80. className={`${styles["step"]} ${
  81. styles[i <= props.index ? "step-finished" : ""]
  82. } ${i === props.index && styles["step-current"]} clickable`}
  83. onClick={() => {
  84. props.onStepChange?.(i);
  85. }}
  86. role="button"
  87. >
  88. <span className={styles["step-index"]}>{i + 1}</span>
  89. <span className={styles["step-name"]}>{step.name}</span>
  90. </div>
  91. );
  92. })}
  93. </div>
  94. </div>
  95. );
  96. }
  97. export function MessageExporter() {
  98. const steps = [
  99. {
  100. name: Locale.Export.Steps.Select,
  101. value: "select",
  102. },
  103. {
  104. name: Locale.Export.Steps.Preview,
  105. value: "preview",
  106. },
  107. ];
  108. const { currentStep, setCurrentStepIndex, currentStepIndex } =
  109. useSteps(steps);
  110. const formats = ["text", "image"] as const;
  111. type ExportFormat = (typeof formats)[number];
  112. const [exportConfig, setExportConfig] = useState({
  113. format: "image" as ExportFormat,
  114. includeContext: true,
  115. });
  116. function updateExportConfig(updater: (config: typeof exportConfig) => void) {
  117. const config = { ...exportConfig };
  118. updater(config);
  119. setExportConfig(config);
  120. }
  121. const chatStore = useChatStore();
  122. const session = chatStore.currentSession();
  123. const { selection, updateSelection } = useMessageSelector();
  124. const selectedMessages = useMemo(() => {
  125. const ret: ChatMessage[] = [];
  126. if (exportConfig.includeContext) {
  127. ret.push(...session.mask.context);
  128. }
  129. ret.push(...session.messages.filter((m, i) => selection.has(m.id ?? i)));
  130. return ret;
  131. }, [
  132. exportConfig.includeContext,
  133. session.messages,
  134. session.mask.context,
  135. selection,
  136. ]);
  137. return (
  138. <>
  139. <Steps
  140. steps={steps}
  141. index={currentStepIndex}
  142. onStepChange={setCurrentStepIndex}
  143. />
  144. <div
  145. className={styles["message-exporter-body"]}
  146. style={currentStep.value !== "select" ? { display: "none" } : {}}
  147. >
  148. <List>
  149. <ListItem
  150. title={Locale.Export.Format.Title}
  151. subTitle={Locale.Export.Format.SubTitle}
  152. >
  153. <Select
  154. value={exportConfig.format}
  155. onChange={(e) =>
  156. updateExportConfig(
  157. (config) =>
  158. (config.format = e.currentTarget.value as ExportFormat),
  159. )
  160. }
  161. >
  162. {formats.map((f) => (
  163. <option key={f} value={f}>
  164. {f}
  165. </option>
  166. ))}
  167. </Select>
  168. </ListItem>
  169. <ListItem
  170. title={Locale.Export.IncludeContext.Title}
  171. subTitle={Locale.Export.IncludeContext.SubTitle}
  172. >
  173. <input
  174. type="checkbox"
  175. checked={exportConfig.includeContext}
  176. onChange={(e) => {
  177. updateExportConfig(
  178. (config) => (config.includeContext = e.currentTarget.checked),
  179. );
  180. }}
  181. ></input>
  182. </ListItem>
  183. </List>
  184. <MessageSelector
  185. selection={selection}
  186. updateSelection={updateSelection}
  187. defaultSelectAll
  188. />
  189. </div>
  190. {currentStep.value === "preview" && (
  191. <div className={styles["message-exporter-body"]}>
  192. {exportConfig.format === "text" ? (
  193. <MarkdownPreviewer
  194. messages={selectedMessages}
  195. topic={session.topic}
  196. />
  197. ) : (
  198. <ImagePreviewer messages={selectedMessages} topic={session.topic} />
  199. )}
  200. </div>
  201. )}
  202. </>
  203. );
  204. }
  205. export function RenderExport(props: {
  206. messages: ChatMessage[];
  207. onRender: (messages: ChatMessage[]) => void;
  208. }) {
  209. const domRef = useRef<HTMLDivElement>(null);
  210. useEffect(() => {
  211. if (!domRef.current) return;
  212. const dom = domRef.current;
  213. const messages = Array.from(
  214. dom.getElementsByClassName(EXPORT_MESSAGE_CLASS_NAME),
  215. );
  216. if (messages.length !== props.messages.length) {
  217. return;
  218. }
  219. const renderMsgs = messages.map((v) => {
  220. const [_, role] = v.id.split(":");
  221. return {
  222. role: role as any,
  223. content: v.innerHTML,
  224. date: "",
  225. };
  226. });
  227. props.onRender(renderMsgs);
  228. });
  229. return (
  230. <div ref={domRef}>
  231. {props.messages.map((m, i) => (
  232. <div
  233. key={i}
  234. id={`${m.role}:${i}`}
  235. className={EXPORT_MESSAGE_CLASS_NAME}
  236. >
  237. <Markdown content={m.content} defaultShow />
  238. </div>
  239. ))}
  240. </div>
  241. );
  242. }
  243. export function PreviewActions(props: {
  244. download: () => void;
  245. copy: () => void;
  246. showCopy?: boolean;
  247. messages?: ChatMessage[];
  248. }) {
  249. const [loading, setLoading] = useState(false);
  250. const [shouldExport, setShouldExport] = useState(false);
  251. const onRenderMsgs = (msgs: ChatMessage[]) => {
  252. setShouldExport(false);
  253. api
  254. .share(msgs)
  255. .then((res) => {
  256. if (!res) return;
  257. copyToClipboard(res);
  258. setTimeout(() => {
  259. window.open(res, "_blank");
  260. }, 800);
  261. })
  262. .catch((e) => {
  263. console.error("[Share]", e);
  264. showToast(prettyObject(e));
  265. })
  266. .finally(() => setLoading(false));
  267. };
  268. const share = async () => {
  269. if (props.messages?.length) {
  270. setLoading(true);
  271. setShouldExport(true);
  272. }
  273. };
  274. return (
  275. <>
  276. <div className={styles["preview-actions"]}>
  277. {props.showCopy && (
  278. <IconButton
  279. text={Locale.Export.Copy}
  280. bordered
  281. shadow
  282. icon={<CopyIcon />}
  283. onClick={props.copy}
  284. ></IconButton>
  285. )}
  286. <IconButton
  287. text={Locale.Export.Download}
  288. bordered
  289. shadow
  290. icon={<DownloadIcon />}
  291. onClick={props.download}
  292. ></IconButton>
  293. <IconButton
  294. text={Locale.Export.Share}
  295. bordered
  296. shadow
  297. icon={loading ? <LoadingIcon /> : <ShareIcon />}
  298. onClick={share}
  299. ></IconButton>
  300. </div>
  301. <div
  302. style={{
  303. position: "fixed",
  304. right: "200vw",
  305. pointerEvents: "none",
  306. }}
  307. >
  308. {shouldExport && (
  309. <RenderExport
  310. messages={props.messages ?? []}
  311. onRender={onRenderMsgs}
  312. />
  313. )}
  314. </div>
  315. </>
  316. );
  317. }
  318. function ExportAvatar(props: { avatar: string }) {
  319. if (props.avatar === DEFAULT_MASK_AVATAR) {
  320. return (
  321. <NextImage
  322. src={BotIcon.src}
  323. width={30}
  324. height={30}
  325. alt="bot"
  326. className="user-avatar"
  327. />
  328. );
  329. }
  330. return <Avatar avatar={props.avatar}></Avatar>;
  331. }
  332. export function ImagePreviewer(props: {
  333. messages: ChatMessage[];
  334. topic: string;
  335. }) {
  336. const chatStore = useChatStore();
  337. const session = chatStore.currentSession();
  338. const mask = session.mask;
  339. const config = useAppConfig();
  340. const previewRef = useRef<HTMLDivElement>(null);
  341. const copy = () => {
  342. const dom = previewRef.current;
  343. if (!dom) return;
  344. toBlob(dom).then((blob) => {
  345. if (!blob) return;
  346. try {
  347. navigator.clipboard
  348. .write([
  349. new ClipboardItem({
  350. "image/png": blob,
  351. }),
  352. ])
  353. .then(() => {
  354. showToast(Locale.Copy.Success);
  355. });
  356. } catch (e) {
  357. console.error("[Copy Image] ", e);
  358. showToast(Locale.Copy.Failed);
  359. }
  360. });
  361. };
  362. const isMobile = useMobileScreen();
  363. const download = () => {
  364. const dom = previewRef.current;
  365. if (!dom) return;
  366. toPng(dom)
  367. .then((blob) => {
  368. if (!blob) return;
  369. if (isMobile) {
  370. const image = new Image();
  371. image.src = blob;
  372. const win = window.open("");
  373. win?.document.write(image.outerHTML);
  374. } else {
  375. const link = document.createElement("a");
  376. link.download = `${props.topic}.png`;
  377. link.href = blob;
  378. link.click();
  379. }
  380. })
  381. .catch((e) => console.log("[Export Image] ", e));
  382. };
  383. return (
  384. <div className={styles["image-previewer"]}>
  385. <PreviewActions
  386. copy={copy}
  387. download={download}
  388. showCopy={!isMobile}
  389. messages={props.messages}
  390. />
  391. <div
  392. className={`${styles["preview-body"]} ${styles["default-theme"]}`}
  393. ref={previewRef}
  394. >
  395. <div className={styles["chat-info"]}>
  396. <div className={styles["logo"] + " no-dark"}>
  397. <NextImage
  398. src={ChatGptIcon.src}
  399. alt="logo"
  400. width={50}
  401. height={50}
  402. />
  403. </div>
  404. <div>
  405. <div className={styles["main-title"]}>ChatGPT Next Web</div>
  406. <div className={styles["sub-title"]}>
  407. github.com/Yidadaa/ChatGPT-Next-Web
  408. </div>
  409. <div className={styles["icons"]}>
  410. <ExportAvatar avatar={config.avatar} />
  411. <span className={styles["icon-space"]}>&</span>
  412. <ExportAvatar avatar={mask.avatar} />
  413. </div>
  414. </div>
  415. <div>
  416. <div className={styles["chat-info-item"]}>
  417. {Locale.Exporter.Model}: {mask.modelConfig.model}
  418. </div>
  419. <div className={styles["chat-info-item"]}>
  420. {Locale.Exporter.Messages}: {props.messages.length}
  421. </div>
  422. <div className={styles["chat-info-item"]}>
  423. {Locale.Exporter.Topic}: {session.topic}
  424. </div>
  425. <div className={styles["chat-info-item"]}>
  426. {Locale.Exporter.Time}:{" "}
  427. {new Date(
  428. props.messages.at(-1)?.date ?? Date.now(),
  429. ).toLocaleString()}
  430. </div>
  431. </div>
  432. </div>
  433. {props.messages.map((m, i) => {
  434. return (
  435. <div
  436. className={styles["message"] + " " + styles["message-" + m.role]}
  437. key={i}
  438. >
  439. <div className={styles["avatar"]}>
  440. <ExportAvatar
  441. avatar={m.role === "user" ? config.avatar : mask.avatar}
  442. />
  443. </div>
  444. <div className={styles["body"]}>
  445. <Markdown
  446. content={m.content}
  447. fontSize={config.fontSize}
  448. defaultShow
  449. />
  450. </div>
  451. </div>
  452. );
  453. })}
  454. </div>
  455. </div>
  456. );
  457. }
  458. export function MarkdownPreviewer(props: {
  459. messages: ChatMessage[];
  460. topic: string;
  461. }) {
  462. const mdText =
  463. `# ${props.topic}\n\n` +
  464. props.messages
  465. .map((m) => {
  466. return m.role === "user"
  467. ? `## ${Locale.Export.MessageFromYou}:\n${m.content}`
  468. : `## ${Locale.Export.MessageFromChatGPT}:\n${m.content.trim()}`;
  469. })
  470. .join("\n\n");
  471. const copy = () => {
  472. copyToClipboard(mdText);
  473. };
  474. const download = () => {
  475. downloadAs(mdText, `${props.topic}.md`);
  476. };
  477. return (
  478. <>
  479. <PreviewActions
  480. copy={copy}
  481. download={download}
  482. messages={props.messages}
  483. />
  484. <div className="markdown-body">
  485. <pre className={styles["export-content"]}>{mdText}</pre>
  486. </div>
  487. </>
  488. );
  489. }