exporter.tsx 14 KB

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