new-chat.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. computeGroup();
  59. window.addEventListener("resize", computeGroup);
  60. return () => window.removeEventListener("resize", computeGroup);
  61. // eslint-disable-next-line react-hooks/exhaustive-deps
  62. }, []);
  63. return groups;
  64. }
  65. export function NewChat() {
  66. const chatStore = useChatStore();
  67. const maskStore = useMaskStore();
  68. const masks = maskStore.getAll();
  69. const groups = useMaskGroup(masks);
  70. const navigate = useNavigate();
  71. const config = useAppConfig();
  72. const maskRef = useRef<HTMLDivElement>(null);
  73. const { state } = useLocation();
  74. const startChat = (mask?: Mask) => {
  75. chatStore.newSession(mask);
  76. setTimeout(() => navigate(Path.Chat), 1);
  77. };
  78. useCommand({
  79. mask: (id) => {
  80. try {
  81. const mask = maskStore.get(parseInt(id));
  82. startChat(mask ?? undefined);
  83. } catch {
  84. console.error("[New Chat] failed to create chat from mask id=", id);
  85. }
  86. },
  87. });
  88. useEffect(() => {
  89. if (maskRef.current) {
  90. maskRef.current.scrollLeft =
  91. (maskRef.current.scrollWidth - maskRef.current.clientWidth) / 2;
  92. }
  93. }, [groups]);
  94. return (
  95. <div className={styles["new-chat"]}>
  96. <div className={styles["mask-header"]}>
  97. <IconButton
  98. icon={<LeftIcon />}
  99. text={Locale.NewChat.Return}
  100. onClick={() => navigate(Path.Home)}
  101. ></IconButton>
  102. {!state?.fromHome && (
  103. <IconButton
  104. text={Locale.NewChat.NotShow}
  105. onClick={() => {
  106. if (confirm(Locale.NewChat.ConfirmNoShow)) {
  107. startChat();
  108. config.update(
  109. (config) => (config.dontShowMaskSplashScreen = true),
  110. );
  111. }
  112. }}
  113. ></IconButton>
  114. )}
  115. </div>
  116. <div className={styles["mask-cards"]}>
  117. <div className={styles["mask-card"]}>
  118. <EmojiAvatar avatar="1f606" size={24} />
  119. </div>
  120. <div className={styles["mask-card"]}>
  121. <EmojiAvatar avatar="1f916" size={24} />
  122. </div>
  123. <div className={styles["mask-card"]}>
  124. <EmojiAvatar avatar="1f479" size={24} />
  125. </div>
  126. </div>
  127. <div className={styles["title"]}>{Locale.NewChat.Title}</div>
  128. <div className={styles["sub-title"]}>{Locale.NewChat.SubTitle}</div>
  129. <div className={styles["actions"]}>
  130. <IconButton
  131. text={Locale.NewChat.More}
  132. onClick={() => navigate(Path.Masks)}
  133. icon={<EyeIcon />}
  134. bordered
  135. shadow
  136. />
  137. <IconButton
  138. text={Locale.NewChat.Skip}
  139. onClick={() => startChat()}
  140. icon={<LightningIcon />}
  141. type="primary"
  142. shadow
  143. className={styles["skip"]}
  144. />
  145. </div>
  146. <div className={styles["masks"]} ref={maskRef}>
  147. {groups.map((masks, i) => (
  148. <div key={i} className={styles["mask-row"]}>
  149. {masks.map((mask, index) => (
  150. <MaskItem
  151. key={index}
  152. mask={mask}
  153. onClick={() => startChat(mask)}
  154. />
  155. ))}
  156. </div>
  157. ))}
  158. </div>
  159. </div>
  160. );
  161. }