new-chat.tsx 5.4 KB

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