new-chat.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { useEffect, useRef, useState } from "react";
  2. import { Path, SlotID } from "../constant";
  3. import { IconButton } from "./button";
  4. import { EmojiAvatar } from "./emoji";
  5. import styles from "./new-chat.module.scss";
  6. import LeftIcon from "../icons/left.svg";
  7. import LightningIcon from "../icons/lightning.svg";
  8. import EyeIcon from "../icons/eye.svg";
  9. import { useLocation, useNavigate } from "react-router-dom";
  10. import { Mask, useMaskStore } from "../store/mask";
  11. import Locale from "../locales";
  12. import { useAppConfig, useChatStore } from "../store";
  13. import { MaskAvatar } from "./mask";
  14. import { useCommand } from "../command";
  15. import { showConfirm } from "./ui-lib";
  16. import { BUILTIN_MASK_STORE } from "../masks";
  17. function MaskItem(props: { mask: Mask; onClick?: () => void }) {
  18. return (
  19. <div className={styles["mask"]} onClick={props.onClick}>
  20. <MaskAvatar
  21. avatar={props.mask.avatar}
  22. model={props.mask.modelConfig.model}
  23. />
  24. <div className={styles["mask-name"] + " one-line"}>{props.mask.name}</div>
  25. </div>
  26. );
  27. }
  28. function useMaskGroup(masks: Mask[]) {
  29. const [groups, setGroups] = useState<Mask[][]>([]);
  30. useEffect(() => {
  31. const computeGroup = () => {
  32. const appBody = document.getElementById(SlotID.AppBody);
  33. if (!appBody || masks.length === 0) return;
  34. const rect = appBody.getBoundingClientRect();
  35. const maxWidth = rect.width;
  36. const maxHeight = rect.height * 0.6;
  37. const maskItemWidth = 120;
  38. const maskItemHeight = 50;
  39. const randomMask = () => masks[Math.floor(Math.random() * masks.length)];
  40. let maskIndex = 0;
  41. const nextMask = () => masks[maskIndex++ % masks.length];
  42. const rows = Math.ceil(maxHeight / maskItemHeight);
  43. const cols = Math.ceil(maxWidth / maskItemWidth);
  44. const newGroups = new Array(rows)
  45. .fill(0)
  46. .map((_, _i) =>
  47. new Array(cols)
  48. .fill(0)
  49. .map((_, j) => (j < 1 || j > cols - 2 ? randomMask() : nextMask())),
  50. );
  51. setGroups(newGroups);
  52. };
  53. computeGroup();
  54. window.addEventListener("resize", computeGroup);
  55. return () => window.removeEventListener("resize", computeGroup);
  56. // eslint-disable-next-line react-hooks/exhaustive-deps
  57. }, []);
  58. return groups;
  59. }
  60. export function NewChat() {
  61. const chatStore = useChatStore();
  62. const maskStore = useMaskStore();
  63. const masks = maskStore.getAll();
  64. const groups = useMaskGroup(masks);
  65. const navigate = useNavigate();
  66. const config = useAppConfig();
  67. const maskRef = useRef<HTMLDivElement>(null);
  68. const { state } = useLocation();
  69. const startChat = (mask?: Mask) => {
  70. setTimeout(() => {
  71. chatStore.newSession(mask);
  72. navigate(Path.Chat);
  73. }, 10);
  74. };
  75. useCommand({
  76. mask: (id) => {
  77. try {
  78. const mask = maskStore.get(id) ?? BUILTIN_MASK_STORE.get(id);
  79. startChat(mask ?? undefined);
  80. } catch {
  81. console.error("[New Chat] failed to create chat from mask id=", id);
  82. }
  83. },
  84. });
  85. useEffect(() => {
  86. if (maskRef.current) {
  87. maskRef.current.scrollLeft =
  88. (maskRef.current.scrollWidth - maskRef.current.clientWidth) / 2;
  89. }
  90. }, [groups]);
  91. return (
  92. <div className={styles["new-chat"]}>
  93. <div className={styles["mask-header"]}>
  94. <IconButton
  95. icon={<LeftIcon />}
  96. text={Locale.NewChat.Return}
  97. onClick={() => navigate(Path.Home)}
  98. ></IconButton>
  99. {!state?.fromHome && (
  100. <IconButton
  101. text={Locale.NewChat.NotShow}
  102. onClick={async () => {
  103. if (await showConfirm(Locale.NewChat.ConfirmNoShow)) {
  104. startChat();
  105. config.update(
  106. (config) => (config.dontShowMaskSplashScreen = true),
  107. );
  108. }
  109. }}
  110. ></IconButton>
  111. )}
  112. </div>
  113. <div className={styles["mask-cards"]}>
  114. <div className={styles["mask-card"]}>
  115. <EmojiAvatar avatar="1f606" size={24} />
  116. </div>
  117. <div className={styles["mask-card"]}>
  118. <EmojiAvatar avatar="1f916" size={24} />
  119. </div>
  120. <div className={styles["mask-card"]}>
  121. <EmojiAvatar avatar="1f479" size={24} />
  122. </div>
  123. </div>
  124. <div className={styles["title"]}>{Locale.NewChat.Title}</div>
  125. <div className={styles["sub-title"]}>{Locale.NewChat.SubTitle}</div>
  126. <div className={styles["actions"]}>
  127. <IconButton
  128. text={Locale.NewChat.More}
  129. onClick={() => navigate(Path.Masks)}
  130. icon={<EyeIcon />}
  131. bordered
  132. shadow
  133. />
  134. <IconButton
  135. text={Locale.NewChat.Skip}
  136. onClick={() => startChat()}
  137. icon={<LightningIcon />}
  138. type="primary"
  139. shadow
  140. className={styles["skip"]}
  141. />
  142. </div>
  143. <div className={styles["masks"]} ref={maskRef}>
  144. {groups.map((masks, i) => (
  145. <div key={i} className={styles["mask-row"]}>
  146. {masks.map((mask, index) => (
  147. <MaskItem
  148. key={index}
  149. mask={mask}
  150. onClick={() => startChat(mask)}
  151. />
  152. ))}
  153. </div>
  154. ))}
  155. </div>
  156. </div>
  157. );
  158. }