exporter.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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 = () => {
  406. showToast(Locale.Export.Image.Toast);
  407. const dom = previewRef.current;
  408. if (!dom) return;
  409. toPng(dom)
  410. .then((blob) => {
  411. if (!blob) return;
  412. if (isMobile || getClientConfig()?.isApp) {
  413. showImageModal(blob);
  414. } else {
  415. const link = document.createElement("a");
  416. link.download = `${props.topic}.png`;
  417. link.href = blob;
  418. link.click();
  419. refreshPreview();
  420. }
  421. })
  422. .catch((e) => console.log("[Export Image] ", e));
  423. };
  424. const refreshPreview = () => {
  425. const dom = previewRef.current;
  426. if (dom) {
  427. dom.innerHTML = dom.innerHTML; // Refresh the content of the preview by resetting its HTML for fix a bug glitching
  428. }
  429. };
  430. return (
  431. <div className={styles["image-previewer"]}>
  432. <PreviewActions
  433. copy={copy}
  434. download={download}
  435. showCopy={!isMobile}
  436. messages={props.messages}
  437. />
  438. <div
  439. className={`${styles["preview-body"]} ${styles["default-theme"]}`}
  440. ref={previewRef}
  441. >
  442. <div className={styles["chat-info"]}>
  443. <div className={styles["logo"] + " no-dark"}>
  444. <NextImage
  445. src={ChatGptIcon.src}
  446. alt="logo"
  447. width={50}
  448. height={50}
  449. />
  450. </div>
  451. <div>
  452. <div className={styles["main-title"]}>ChatGPT Next Web</div>
  453. <div className={styles["sub-title"]}>
  454. github.com/Yidadaa/ChatGPT-Next-Web
  455. </div>
  456. <div className={styles["icons"]}>
  457. <ExportAvatar avatar={config.avatar} />
  458. <span className={styles["icon-space"]}>&</span>
  459. <ExportAvatar avatar={mask.avatar} />
  460. </div>
  461. </div>
  462. <div>
  463. <div className={styles["chat-info-item"]}>
  464. {Locale.Exporter.Model}: {mask.modelConfig.model}
  465. </div>
  466. <div className={styles["chat-info-item"]}>
  467. {Locale.Exporter.Messages}: {props.messages.length}
  468. </div>
  469. <div className={styles["chat-info-item"]}>
  470. {Locale.Exporter.Topic}: {session.topic}
  471. </div>
  472. <div className={styles["chat-info-item"]}>
  473. {Locale.Exporter.Time}:{" "}
  474. {new Date(
  475. props.messages.at(-1)?.date ?? Date.now(),
  476. ).toLocaleString()}
  477. </div>
  478. </div>
  479. </div>
  480. {props.messages.map((m, i) => {
  481. return (
  482. <div
  483. className={styles["message"] + " " + styles["message-" + m.role]}
  484. key={i}
  485. >
  486. <div className={styles["avatar"]}>
  487. <ExportAvatar
  488. avatar={m.role === "user" ? config.avatar : mask.avatar}
  489. />
  490. </div>
  491. <div className={styles["body"]}>
  492. <Markdown
  493. content={m.content}
  494. fontSize={config.fontSize}
  495. defaultShow
  496. />
  497. </div>
  498. </div>
  499. );
  500. })}
  501. </div>
  502. </div>
  503. );
  504. }
  505. export function MarkdownPreviewer(props: {
  506. messages: ChatMessage[];
  507. topic: string;
  508. }) {
  509. const mdText =
  510. `# ${props.topic}\n\n` +
  511. props.messages
  512. .map((m) => {
  513. return m.role === "user"
  514. ? `## ${Locale.Export.MessageFromYou}:\n${m.content}`
  515. : `## ${Locale.Export.MessageFromChatGPT}:\n${m.content.trim()}`;
  516. })
  517. .join("\n\n");
  518. const copy = () => {
  519. copyToClipboard(mdText);
  520. };
  521. const download = () => {
  522. downloadAs(mdText, `${props.topic}.md`);
  523. };
  524. return (
  525. <>
  526. <PreviewActions
  527. copy={copy}
  528. download={download}
  529. showCopy={true}
  530. messages={props.messages}
  531. />
  532. <div className="markdown-body">
  533. <pre className={styles["export-content"]}>{mdText}</pre>
  534. </div>
  535. </>
  536. );
  537. }
  538. // modified by BackTrackZ now it's looks better
  539. export function JsonPreviewer(props: {
  540. messages: ChatMessage[];
  541. topic: string;
  542. }) {
  543. const msgs = {
  544. messages: [
  545. {
  546. role: "system",
  547. content: "You are an assistant that " + props.topic,
  548. },
  549. ...props.messages.map((m) => ({
  550. role: m.role,
  551. content: m.content,
  552. })),
  553. ],
  554. };
  555. const mdText = "```json\n" + JSON.stringify(msgs, null, 2) + "\n```";
  556. const minifiedJson = JSON.stringify(msgs);
  557. const copy = () => {
  558. copyToClipboard(minifiedJson);
  559. };
  560. const download = () => {
  561. downloadAs(JSON.stringify(msgs), `${props.topic}.json`);
  562. };
  563. return (
  564. <>
  565. <PreviewActions
  566. copy={copy}
  567. download={download}
  568. showCopy={false}
  569. messages={props.messages}
  570. />
  571. <div className="markdown-body" onClick={copy}>
  572. <Markdown content={mdText} />
  573. </div>
  574. </>
  575. );
  576. }