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. showToast,
  12. } from "./ui-lib";
  13. import { IconButton } from "./button";
  14. import { copyToClipboard, downloadAs, useMobileScreen } from "../utils";
  15. import CopyIcon from "../icons/copy.svg";
  16. import LoadingIcon from "../icons/three-dots.svg";
  17. import ChatGptIcon from "../icons/chatgpt.png";
  18. import ShareIcon from "../icons/share.svg";
  19. import BotIcon from "../icons/bot.png";
  20. import DownloadIcon from "../icons/download.svg";
  21. import { useEffect, useMemo, useRef, useState } from "react";
  22. import { MessageSelector, useMessageSelector } from "./message-selector";
  23. import { Avatar } from "./emoji";
  24. import dynamic from "next/dynamic";
  25. import NextImage from "next/image";
  26. import { toBlob, toJpeg, toPng } from "html-to-image";
  27. import { DEFAULT_MASK_AVATAR } from "../store/mask";
  28. import { api } from "../client/api";
  29. import { prettyObject } from "../utils/format";
  30. import { EXPORT_MESSAGE_CLASS_NAME } from "../constant";
  31. import { getClientConfig } from "../config/client";
  32. const Markdown = dynamic(async () => (await import("./markdown")).Markdown, {
  33. loading: () => <LoadingIcon />,
  34. });
  35. export function ExportMessageModal(props: { onClose: () => void }) {
  36. return (
  37. <div className="modal-mask">
  38. <Modal title={Locale.Export.Title} onClose={props.onClose}>
  39. <div style={{ minHeight: "40vh" }}>
  40. <MessageExporter />
  41. </div>
  42. </Modal>
  43. </div>
  44. );
  45. }
  46. function useSteps(
  47. steps: Array<{
  48. name: string;
  49. value: string;
  50. }>,
  51. ) {
  52. const stepCount = steps.length;
  53. const [currentStepIndex, setCurrentStepIndex] = useState(0);
  54. const nextStep = () =>
  55. setCurrentStepIndex((currentStepIndex + 1) % stepCount);
  56. const prevStep = () =>
  57. setCurrentStepIndex((currentStepIndex - 1 + stepCount) % stepCount);
  58. return {
  59. currentStepIndex,
  60. setCurrentStepIndex,
  61. nextStep,
  62. prevStep,
  63. currentStep: steps[currentStepIndex],
  64. };
  65. }
  66. function Steps<
  67. T extends {
  68. name: string;
  69. value: string;
  70. }[],
  71. >(props: { steps: T; onStepChange?: (index: number) => void; index: number }) {
  72. const steps = props.steps;
  73. const stepCount = steps.length;
  74. return (
  75. <div className={styles["steps"]}>
  76. <div className={styles["steps-progress"]}>
  77. <div
  78. className={styles["steps-progress-inner"]}
  79. style={{
  80. width: `${((props.index + 1) / stepCount) * 100}%`,
  81. }}
  82. ></div>
  83. </div>
  84. <div className={styles["steps-inner"]}>
  85. {steps.map((step, i) => {
  86. return (
  87. <div
  88. key={i}
  89. className={`${styles["step"]} ${
  90. styles[i <= props.index ? "step-finished" : ""]
  91. } ${i === props.index && styles["step-current"]} clickable`}
  92. onClick={() => {
  93. props.onStepChange?.(i);
  94. }}
  95. role="button"
  96. >
  97. <span className={styles["step-index"]}>{i + 1}</span>
  98. <span className={styles["step-name"]}>{step.name}</span>
  99. </div>
  100. );
  101. })}
  102. </div>
  103. </div>
  104. );
  105. }
  106. export function MessageExporter() {
  107. const steps = [
  108. {
  109. name: Locale.Export.Steps.Select,
  110. value: "select",
  111. },
  112. {
  113. name: Locale.Export.Steps.Preview,
  114. value: "preview",
  115. },
  116. ];
  117. const { currentStep, setCurrentStepIndex, currentStepIndex } =
  118. useSteps(steps);
  119. const formats = ["text", "image"] as const;
  120. type ExportFormat = (typeof formats)[number];
  121. const [exportConfig, setExportConfig] = useState({
  122. format: "image" as ExportFormat,
  123. includeContext: true,
  124. });
  125. function updateExportConfig(updater: (config: typeof exportConfig) => void) {
  126. const config = { ...exportConfig };
  127. updater(config);
  128. setExportConfig(config);
  129. }
  130. const chatStore = useChatStore();
  131. const session = chatStore.currentSession();
  132. const { selection, updateSelection } = useMessageSelector();
  133. const selectedMessages = useMemo(() => {
  134. const ret: ChatMessage[] = [];
  135. if (exportConfig.includeContext) {
  136. ret.push(...session.mask.context);
  137. }
  138. ret.push(...session.messages.filter((m, i) => selection.has(m.id)));
  139. return ret;
  140. }, [
  141. exportConfig.includeContext,
  142. session.messages,
  143. session.mask.context,
  144. selection,
  145. ]);
  146. return (
  147. <>
  148. <Steps
  149. steps={steps}
  150. index={currentStepIndex}
  151. onStepChange={setCurrentStepIndex}
  152. />
  153. <div
  154. className={styles["message-exporter-body"]}
  155. style={currentStep.value !== "select" ? { display: "none" } : {}}
  156. >
  157. <List>
  158. <ListItem
  159. title={Locale.Export.Format.Title}
  160. subTitle={Locale.Export.Format.SubTitle}
  161. >
  162. <Select
  163. value={exportConfig.format}
  164. onChange={(e) =>
  165. updateExportConfig(
  166. (config) =>
  167. (config.format = e.currentTarget.value as ExportFormat),
  168. )
  169. }
  170. >
  171. {formats.map((f) => (
  172. <option key={f} value={f}>
  173. {f}
  174. </option>
  175. ))}
  176. </Select>
  177. </ListItem>
  178. <ListItem
  179. title={Locale.Export.IncludeContext.Title}
  180. subTitle={Locale.Export.IncludeContext.SubTitle}
  181. >
  182. <input
  183. type="checkbox"
  184. checked={exportConfig.includeContext}
  185. onChange={(e) => {
  186. updateExportConfig(
  187. (config) => (config.includeContext = e.currentTarget.checked),
  188. );
  189. }}
  190. ></input>
  191. </ListItem>
  192. </List>
  193. <MessageSelector
  194. selection={selection}
  195. updateSelection={updateSelection}
  196. defaultSelectAll
  197. />
  198. </div>
  199. {currentStep.value === "preview" && (
  200. <div className={styles["message-exporter-body"]}>
  201. {exportConfig.format === "text" ? (
  202. <MarkdownPreviewer
  203. messages={selectedMessages}
  204. topic={session.topic}
  205. />
  206. ) : (
  207. <ImagePreviewer messages={selectedMessages} topic={session.topic} />
  208. )}
  209. </div>
  210. )}
  211. </>
  212. );
  213. }
  214. export function RenderExport(props: {
  215. messages: ChatMessage[];
  216. onRender: (messages: ChatMessage[]) => void;
  217. }) {
  218. const domRef = useRef<HTMLDivElement>(null);
  219. useEffect(() => {
  220. if (!domRef.current) return;
  221. const dom = domRef.current;
  222. const messages = Array.from(
  223. dom.getElementsByClassName(EXPORT_MESSAGE_CLASS_NAME),
  224. );
  225. if (messages.length !== props.messages.length) {
  226. return;
  227. }
  228. const renderMsgs = messages.map((v, i) => {
  229. const [_, role] = v.id.split(":");
  230. return {
  231. id: i.toString(),
  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. }