exporter.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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"] 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 ?? i)));
  140. return ret;
  141. }, [
  142. exportConfig.includeContext,
  143. session.messages,
  144. session.mask.context,
  145. selection,
  146. ]);
  147. return (
  148. <>
  149. <Steps
  150. steps={steps}
  151. index={currentStepIndex}
  152. onStepChange={setCurrentStepIndex}
  153. />
  154. <div
  155. className={styles["message-exporter-body"]}
  156. style={currentStep.value !== "select" ? { display: "none" } : {}}
  157. >
  158. <List>
  159. <ListItem
  160. title={Locale.Export.Format.Title}
  161. subTitle={Locale.Export.Format.SubTitle}
  162. >
  163. <Select
  164. value={exportConfig.format}
  165. onChange={(e) =>
  166. updateExportConfig(
  167. (config) =>
  168. (config.format = e.currentTarget.value as ExportFormat),
  169. )
  170. }
  171. >
  172. {formats.map((f) => (
  173. <option key={f} value={f}>
  174. {f}
  175. </option>
  176. ))}
  177. </Select>
  178. </ListItem>
  179. <ListItem
  180. title={Locale.Export.IncludeContext.Title}
  181. subTitle={Locale.Export.IncludeContext.SubTitle}
  182. >
  183. <input
  184. type="checkbox"
  185. checked={exportConfig.includeContext}
  186. onChange={(e) => {
  187. updateExportConfig(
  188. (config) => (config.includeContext = e.currentTarget.checked),
  189. );
  190. }}
  191. ></input>
  192. </ListItem>
  193. </List>
  194. <MessageSelector
  195. selection={selection}
  196. updateSelection={updateSelection}
  197. defaultSelectAll
  198. />
  199. </div>
  200. {currentStep.value === "preview" && (
  201. <div className={styles["message-exporter-body"]}>
  202. {exportConfig.format === "text" ? (
  203. <MarkdownPreviewer
  204. messages={selectedMessages}
  205. topic={session.topic}
  206. />
  207. ) : (
  208. <ImagePreviewer messages={selectedMessages} topic={session.topic} />
  209. )}
  210. </div>
  211. )}
  212. </>
  213. );
  214. }
  215. export function RenderExport(props: {
  216. messages: ChatMessage[];
  217. onRender: (messages: ChatMessage[]) => void;
  218. }) {
  219. const domRef = useRef<HTMLDivElement>(null);
  220. useEffect(() => {
  221. if (!domRef.current) return;
  222. const dom = domRef.current;
  223. const messages = Array.from(
  224. dom.getElementsByClassName(EXPORT_MESSAGE_CLASS_NAME),
  225. );
  226. if (messages.length !== props.messages.length) {
  227. return;
  228. }
  229. const renderMsgs = messages.map((v) => {
  230. const [_, role] = v.id.split(":");
  231. return {
  232. role: role as any,
  233. content: v.innerHTML,
  234. date: "",
  235. };
  236. });
  237. props.onRender(renderMsgs);
  238. });
  239. return (
  240. <div ref={domRef}>
  241. {props.messages.map((m, i) => (
  242. <div
  243. key={i}
  244. id={`${m.role}:${i}`}
  245. className={EXPORT_MESSAGE_CLASS_NAME}
  246. >
  247. <Markdown content={m.content} defaultShow />
  248. </div>
  249. ))}
  250. </div>
  251. );
  252. }
  253. export function PreviewActions(props: {
  254. download: () => void;
  255. copy: () => void;
  256. showCopy?: boolean;
  257. messages?: ChatMessage[];
  258. }) {
  259. const [loading, setLoading] = useState(false);
  260. const [shouldExport, setShouldExport] = useState(false);
  261. const onRenderMsgs = (msgs: ChatMessage[]) => {
  262. setShouldExport(false);
  263. api
  264. .share(msgs)
  265. .then((res) => {
  266. if (!res) return;
  267. copyToClipboard(res);
  268. setTimeout(() => {
  269. window.open(res, "_blank");
  270. }, 800);
  271. })
  272. .catch((e) => {
  273. console.error("[Share]", e);
  274. showToast(prettyObject(e));
  275. })
  276. .finally(() => setLoading(false));
  277. };
  278. const share = async () => {
  279. if (props.messages?.length) {
  280. setLoading(true);
  281. setShouldExport(true);
  282. }
  283. };
  284. return (
  285. <>
  286. <div className={styles["preview-actions"]}>
  287. {props.showCopy && (
  288. <IconButton
  289. text={Locale.Export.Copy}
  290. bordered
  291. shadow
  292. icon={<CopyIcon />}
  293. onClick={props.copy}
  294. ></IconButton>
  295. )}
  296. <IconButton
  297. text={Locale.Export.Download}
  298. bordered
  299. shadow
  300. icon={<DownloadIcon />}
  301. onClick={props.download}
  302. ></IconButton>
  303. <IconButton
  304. text={Locale.Export.Share}
  305. bordered
  306. shadow
  307. icon={loading ? <LoadingIcon /> : <ShareIcon />}
  308. onClick={share}
  309. ></IconButton>
  310. </div>
  311. <div
  312. style={{
  313. position: "fixed",
  314. right: "200vw",
  315. pointerEvents: "none",
  316. }}
  317. >
  318. {shouldExport && (
  319. <RenderExport
  320. messages={props.messages ?? []}
  321. onRender={onRenderMsgs}
  322. />
  323. )}
  324. </div>
  325. </>
  326. );
  327. }
  328. function ExportAvatar(props: { avatar: string }) {
  329. if (props.avatar === DEFAULT_MASK_AVATAR) {
  330. return (
  331. <NextImage
  332. src={BotIcon.src}
  333. width={30}
  334. height={30}
  335. alt="bot"
  336. className="user-avatar"
  337. />
  338. );
  339. }
  340. return <Avatar avatar={props.avatar}></Avatar>;
  341. }
  342. export function ImagePreviewer(props: {
  343. messages: ChatMessage[];
  344. topic: string;
  345. }) {
  346. const chatStore = useChatStore();
  347. const session = chatStore.currentSession();
  348. const mask = session.mask;
  349. const config = useAppConfig();
  350. const previewRef = useRef<HTMLDivElement>(null);
  351. const copy = () => {
  352. showToast(Locale.Export.Image.Toast);
  353. const dom = previewRef.current;
  354. if (!dom) return;
  355. toBlob(dom).then((blob) => {
  356. if (!blob) return;
  357. try {
  358. navigator.clipboard
  359. .write([
  360. new ClipboardItem({
  361. "image/png": blob,
  362. }),
  363. ])
  364. .then(() => {
  365. showToast(Locale.Copy.Success);
  366. });
  367. } catch (e) {
  368. console.error("[Copy Image] ", e);
  369. showToast(Locale.Copy.Failed);
  370. }
  371. });
  372. };
  373. const isMobile = useMobileScreen();
  374. const download = () => {
  375. showToast(Locale.Export.Image.Toast);
  376. const dom = previewRef.current;
  377. if (!dom) return;
  378. toPng(dom)
  379. .then((blob) => {
  380. if (!blob) return;
  381. if (isMobile || getClientConfig()?.isApp) {
  382. showImageModal(blob);
  383. } else {
  384. const link = document.createElement("a");
  385. link.download = `${props.topic}.png`;
  386. link.href = blob;
  387. link.click();
  388. }
  389. })
  390. .catch((e) => console.log("[Export Image] ", e));
  391. };
  392. return (
  393. <div className={styles["image-previewer"]}>
  394. <PreviewActions
  395. copy={copy}
  396. download={download}
  397. showCopy={!isMobile}
  398. messages={props.messages}
  399. />
  400. <div
  401. className={`${styles["preview-body"]} ${styles["default-theme"]}`}
  402. ref={previewRef}
  403. >
  404. <div className={styles["chat-info"]}>
  405. <div className={styles["logo"] + " no-dark"}>
  406. <NextImage
  407. src={ChatGptIcon.src}
  408. alt="logo"
  409. width={50}
  410. height={50}
  411. />
  412. </div>
  413. <div>
  414. <div className={styles["main-title"]}>ChatGPT Next Web</div>
  415. <div className={styles["sub-title"]}>
  416. github.com/Yidadaa/ChatGPT-Next-Web
  417. </div>
  418. <div className={styles["icons"]}>
  419. <ExportAvatar avatar={config.avatar} />
  420. <span className={styles["icon-space"]}>&</span>
  421. <ExportAvatar avatar={mask.avatar} />
  422. </div>
  423. </div>
  424. <div>
  425. <div className={styles["chat-info-item"]}>
  426. {Locale.Exporter.Model}: {mask.modelConfig.model}
  427. </div>
  428. <div className={styles["chat-info-item"]}>
  429. {Locale.Exporter.Messages}: {props.messages.length}
  430. </div>
  431. <div className={styles["chat-info-item"]}>
  432. {Locale.Exporter.Topic}: {session.topic}
  433. </div>
  434. <div className={styles["chat-info-item"]}>
  435. {Locale.Exporter.Time}:{" "}
  436. {new Date(
  437. props.messages.at(-1)?.date ?? Date.now(),
  438. ).toLocaleString()}
  439. </div>
  440. </div>
  441. </div>
  442. {props.messages.map((m, i) => {
  443. return (
  444. <div
  445. className={styles["message"] + " " + styles["message-" + m.role]}
  446. key={i}
  447. >
  448. <div className={styles["avatar"]}>
  449. <ExportAvatar
  450. avatar={m.role === "user" ? config.avatar : mask.avatar}
  451. />
  452. </div>
  453. <div className={styles["body"]}>
  454. <Markdown
  455. content={m.content}
  456. fontSize={config.fontSize}
  457. defaultShow
  458. />
  459. </div>
  460. </div>
  461. );
  462. })}
  463. </div>
  464. </div>
  465. );
  466. }
  467. export function MarkdownPreviewer(props: {
  468. messages: ChatMessage[];
  469. topic: string;
  470. }) {
  471. const mdText =
  472. `# ${props.topic}\n\n` +
  473. props.messages
  474. .map((m) => {
  475. return m.role === "user"
  476. ? `## ${Locale.Export.MessageFromYou}:\n${m.content}`
  477. : `## ${Locale.Export.MessageFromChatGPT}:\n${m.content.trim()}`;
  478. })
  479. .join("\n\n");
  480. const copy = () => {
  481. copyToClipboard(mdText);
  482. };
  483. const download = () => {
  484. downloadAs(mdText, `${props.topic}.md`);
  485. };
  486. return (
  487. <>
  488. <PreviewActions
  489. copy={copy}
  490. download={download}
  491. messages={props.messages}
  492. />
  493. <div className="markdown-body">
  494. <pre className={styles["export-content"]}>{mdText}</pre>
  495. </div>
  496. </>
  497. );
  498. }