exporter.tsx 17 KB

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