new-chat.tsx 5.5 KB

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